fix unittest
[osm/vim-emu.git] / src / emuvim / test / unittests / test_sonata_dummy_gatekeeper.py
1 """
2 Copyright (c) 2015 SONATA-NFV
3 ALL RIGHTS RESERVED.
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16
17 Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
18 nor the names of its contributors may be used to endorse or promote
19 products derived from this software without specific prior written
20 permission.
21
22 This work has been performed in the framework of the SONATA project,
23 funded by the European Commission under Grant number 671517 through
24 the Horizon 2020 and 5G-PPP programmes. The authors would like to
25 acknowledge the contributions of their colleagues of the SONATA
26 partner consortium (www.sonata-nfv.eu).
27 """
28
29 import time
30 import requests
31 import json
32 import os
33 import unittest
34 from emuvim.test.base import SimpleTestTopology
35 from emuvim.api.sonata import SonataDummyGatekeeperEndpoint
36 from emuvim.api.sonata.dummygatekeeper import initialize_GK, parse_interface
37 import mininet.clean
38 from ipaddress import ip_network
39
40 PACKAGE_PATH = "misc/sonata-demo-service.son"
41
42
43 class testSonataDummyGatekeeper(SimpleTestTopology):
44
45 # @unittest.skip("disabled")
46 def test_GK_Api_start_service(self):
47 # create network
48 self.createNet(nswitches=0, ndatacenter=2, nhosts=2, ndockers=0, enable_learning=True)
49 # setup links
50 self.net.addLink(self.dc[0], self.h[0])
51 self.net.addLink(self.dc[0], self.dc[1])
52 self.net.addLink(self.h[1], self.dc[1])
53 # connect dummy GK to data centers
54 sdkg1 = SonataDummyGatekeeperEndpoint("0.0.0.0", 5000)
55 sdkg1.connectDatacenter(self.dc[0])
56 sdkg1.connectDatacenter(self.dc[1])
57 # run the dummy gatekeeper (in another thread, don't block)
58 sdkg1.start()
59 # start Mininet network
60 self.startNet()
61 time.sleep(1)
62
63 print "starting tests"
64 # board package
65 files = {"package": open(PACKAGE_PATH, "rb")}
66 r = requests.post("http://127.0.0.1:5000/packages", files=files)
67 self.assertEqual(r.status_code, 201)
68 self.assertTrue(json.loads(r.text).get("service_uuid") is not None)
69
70 # instantiate service
71 self.service_uuid = json.loads(r.text).get("service_uuid")
72 r2 = requests.post("http://127.0.0.1:5000/instantiations", data=json.dumps({"service_uuid": self.service_uuid}))
73 self.assertEqual(r2.status_code, 201)
74
75 # give the emulator some time to instantiate everything
76 time.sleep(2)
77
78 # check get request APIs
79 r3 = requests.get("http://127.0.0.1:5000/packages")
80 self.assertEqual(len(json.loads(r3.text).get("service_uuid_list")), 1)
81 r4 = requests.get("http://127.0.0.1:5000/instantiations")
82 self.assertEqual(len(json.loads(r4.text).get("service_instantiations_list")), 1)
83
84 # check number of running nodes
85 self.assertTrue(len(self.getContainernetContainers()) == 3)
86 self.assertTrue(len(self.net.hosts) == 5)
87 self.assertTrue(len(self.net.switches) == 2)
88 # check compute list result
89 self.assertEqual(len(self.dc[0].listCompute()), 2)
90 # check connectivity by using ping
91 ELAN_list=[]
92
93 # check E-Line connection, by checking the IP addresses
94 for link in self.net.deployed_elines:
95 vnf_src, intf_src, vnf_sap_docker_name = parse_interface(link['connection_points_reference'][0])
96 print vnf_src, intf_src
97 src = self.net.getNodeByName(vnf_src)
98 if not src:
99 continue
100 network_list = src.getNetworkStatus()
101 src_ip = [intf['ip'] for intf in network_list if intf['intf_name'] == intf_src][0]
102 src_mask = [intf['netmask'] for intf in network_list if intf['intf_name'] == intf_src][0]
103
104 vnf_dst, intf_dst, vnf_sap_docker_name = parse_interface(link['connection_points_reference'][1])
105 dst = self.net.getNodeByName(vnf_dst)
106 if not dst:
107 continue
108 network_list = dst.getNetworkStatus()
109 dst_ip = [intf['ip'] for intf in network_list if intf['intf_name'] == intf_dst][0]
110 dst_mask = [intf['netmask'] for intf in network_list if intf['intf_name'] == intf_dst][0]
111
112 print "src = {0}:{1} ip={2} ".format(vnf_src, intf_src, src_ip, src_mask)
113 print "dst = {0}:{1} ip={2} ".format(vnf_dst, intf_dst, dst_ip, dst_mask)
114
115 # check if the E-Line IP's are in the same subnet
116 ret = ip_network(u'{0}'.format(src_ip, src_mask), strict=False)\
117 .compare_networks(ip_network(u'{0}'.format(dst_ip, dst_mask),strict=False))
118 self.assertTrue(ret == 0)
119
120
121 for vnf in self.dc[0].listCompute():
122 # check E LAN connection
123 network_list = vnf.getNetworkStatus()
124 mgmt_ip = [intf['ip'] for intf in network_list if intf['intf_name'] == 'mgmt']
125 self.assertTrue(len(mgmt_ip) > 0)
126 ip_address = mgmt_ip[0]
127 ELAN_list.append(ip_address)
128 print ip_address
129
130 # check ELAN connection by ping over the mgmt network (needs to be configured as ELAN in the test service)
131 for vnf in self.dc[0].listCompute():
132 network_list = vnf.getNetworkStatus()
133 mgmt_ip = [intf['ip'] for intf in network_list if intf['intf_name'] == 'mgmt']
134 self.assertTrue(len(mgmt_ip) > 0)
135 ip_address = mgmt_ip[0]
136 print ELAN_list
137 print ip_address
138 test_ip_list = list(ELAN_list)
139 test_ip_list.remove(ip_address)
140 for ip in test_ip_list:
141 p = self.net.ping([vnf],manualdestip=ip)
142 print p
143 self.assertTrue(p <= 0.0)
144
145 # stop Mininet network
146 self.stopNet()
147 initialize_GK()
148
149 #@unittest.skip("disabled")
150 def test_GK_Api_stop_service(self):
151 # create network
152 self.createNet(ndatacenter=2, nhosts=2)
153 # setup links
154 self.net.addLink(self.dc[0], self.h[0])
155 self.net.addLink(self.dc[0], self.dc[1])
156 self.net.addLink(self.h[1], self.dc[1])
157 # connect dummy GK to data centers
158 sdkg1 = SonataDummyGatekeeperEndpoint("0.0.0.0", 5000)
159 sdkg1.connectDatacenter(self.dc[0])
160 sdkg1.connectDatacenter(self.dc[1])
161 # run the dummy gatekeeper (in another thread, don't block)
162 sdkg1.start()
163 # start Mininet network
164 self.startNet()
165 time.sleep(1)
166
167 print "starting tests"
168 # board package
169 files = {"package": open(PACKAGE_PATH, "rb")}
170 r = requests.post("http://127.0.0.1:5000/packages", files=files)
171 self.assertEqual(r.status_code, 201)
172 self.assertTrue(json.loads(r.text).get("service_uuid") is not None)
173
174 # instantiate service
175 self.service_uuid = json.loads(r.text).get("service_uuid")
176 r2 = requests.post("http://127.0.0.1:5000/instantiations", data=json.dumps({"service_uuid": self.service_uuid}))
177 self.assertEqual(r2.status_code, 201)
178
179 # give the emulator some time to instantiate everything
180 time.sleep(2)
181
182 # check get request APIs
183 r3 = requests.get("http://127.0.0.1:5000/packages")
184 self.assertEqual(len(json.loads(r3.text).get("service_uuid_list")), 1)
185 r4 = requests.get("http://127.0.0.1:5000/instantiations")
186 self.assertEqual(len(json.loads(r4.text).get("service_instantiations_list")), 1)
187
188 # check number of running nodes
189 self.assertTrue(len(self.getContainernetContainers()) == 3)
190 self.assertTrue(len(self.net.hosts) == 5)
191 self.assertTrue(len(self.net.switches) == 2)
192 # check compute list result
193 self.assertEqual(len(self.dc[0].listCompute()), 2)
194
195 # stop the service
196 service_instance_uuid = json.loads(r2.text).get("service_instance_uuid")
197 self.assertTrue(service_instance_uuid is not None)
198 requests.delete("http://127.0.0.1:5000/instantiations", data=json.dumps({"service_uuid": self.service_uuid, "service_instance_uuid":service_instance_uuid}))
199
200 r5 = requests.get("http://127.0.0.1:5000/instantiations")
201 self.assertTrue(len(json.loads(r5.text).get("service_instantiations_list")), 0) # note that there was 1 instance before
202
203 # stop Mininet network
204 self.stopNet()
205 initialize_GK()
206
207
208 #@unittest.skip("disabled")
209 def test_GK_stress_service(self):
210 # create network
211 self.createNet(ndatacenter=2, nhosts=2)
212 # connect dummy GK to data centers
213 sdkg1 = SonataDummyGatekeeperEndpoint("0.0.0.0", 5000)
214 sdkg1.connectDatacenter(self.dc[0])
215 sdkg1.connectDatacenter(self.dc[1])
216 # run the dummy gatekeeper (in another thread, don't block)
217 sdkg1.start()
218 # start Mininet network
219 self.startNet()
220 time.sleep(1)
221
222 print "starting tests"
223 # board package
224 files = {"package": open("misc/sonata-stress-service.son", "rb")}
225 r = requests.post("http://127.0.0.1:5000/packages", files=files)
226 self.assertEqual(r.status_code, 201)
227 self.assertTrue(json.loads(r.text).get("service_uuid") is not None)
228
229 # instantiate service
230 self.service_uuid = json.loads(r.text).get("service_uuid")
231 r2 = requests.post("http://127.0.0.1:5000/instantiations", data=json.dumps({"service_uuid": self.service_uuid}))
232 self.assertEqual(r2.status_code, 201)
233
234 # give the emulator some time to instantiate everything
235 time.sleep(2)
236
237 # check get request APIs
238 r3 = requests.get("http://127.0.0.1:5000/packages")
239 self.assertEqual(len(json.loads(r3.text).get("service_uuid_list")), 1)
240 r4 = requests.get("http://127.0.0.1:5000/instantiations")
241 self.assertEqual(len(json.loads(r4.text).get("service_instantiations_list")), 1)
242
243 # stop the service
244 service_instance_uuid = json.loads(r2.text).get("service_instance_uuid")
245 self.assertTrue(service_instance_uuid is not None)
246 requests.delete("http://127.0.0.1:5000/instantiations", data=json.dumps({"service_uuid": self.service_uuid, "service_instance_uuid":service_instance_uuid}))
247
248 r5 = requests.get("http://127.0.0.1:5000/instantiations")
249 self.assertTrue(len(json.loads(r5.text).get("service_instantiations_list")), 0) # note that there was 1 instance before
250
251 # stop Mininet network
252 self.stopNet()
253 initialize_GK()
254
255