Merge pull request #235 from splietker/master
[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 time.sleep(3)
60 # start Mininet network
61 self.startNet()
62 time.sleep(1)
63
64 print "starting tests"
65 # board package
66 files = {"package": open(PACKAGE_PATH, "rb")}
67 r = requests.post("http://127.0.0.1:5000/packages", files=files)
68 self.assertEqual(r.status_code, 201)
69 self.assertTrue(json.loads(r.text).get("service_uuid") is not None)
70
71 # instantiate service
72 self.service_uuid = json.loads(r.text).get("service_uuid")
73 r2 = requests.post("http://127.0.0.1:5000/instantiations", data=json.dumps({"service_uuid": self.service_uuid}))
74 self.assertEqual(r2.status_code, 201)
75
76 # give the emulator some time to instantiate everything
77 time.sleep(2)
78
79 # check get request APIs
80 r3 = requests.get("http://127.0.0.1:5000/packages")
81 self.assertEqual(len(json.loads(r3.text).get("service_uuid_list")), 1)
82 r4 = requests.get("http://127.0.0.1:5000/instantiations")
83 self.assertEqual(len(json.loads(r4.text).get("service_instantiations_list")), 1)
84
85 # check number of running nodes
86 self.assertTrue(len(self.getContainernetContainers()) == 3)
87 self.assertTrue(len(self.net.hosts) == 5)
88 self.assertTrue(len(self.net.switches) == 2)
89 # check compute list result
90 self.assertEqual(len(self.dc[0].listCompute()), 2)
91 # check connectivity by using ping
92 ELAN_list=[]
93
94 # check E-Line connection, by checking the IP addresses
95 for link in self.net.deployed_elines:
96 vnf_src, intf_src, vnf_sap_docker_name = parse_interface(link['connection_points_reference'][0])
97 print vnf_src, intf_src
98 src = self.net.getNodeByName(vnf_src)
99 if not src:
100 continue
101 network_list = src.getNetworkStatus()
102 src_ip = [intf['ip'] for intf in network_list if intf['intf_name'] == intf_src][0]
103 src_mask = [intf['netmask'] for intf in network_list if intf['intf_name'] == intf_src][0]
104
105 vnf_dst, intf_dst, vnf_sap_docker_name = parse_interface(link['connection_points_reference'][1])
106 dst = self.net.getNodeByName(vnf_dst)
107 if not dst:
108 continue
109 network_list = dst.getNetworkStatus()
110 dst_ip = [intf['ip'] for intf in network_list if intf['intf_name'] == intf_dst][0]
111 dst_mask = [intf['netmask'] for intf in network_list if intf['intf_name'] == intf_dst][0]
112
113 print "src = {0}:{1} ip={2} ".format(vnf_src, intf_src, src_ip, src_mask)
114 print "dst = {0}:{1} ip={2} ".format(vnf_dst, intf_dst, dst_ip, dst_mask)
115
116 # check if the E-Line IP's are in the same subnet
117 ret = ip_network(u'{0}'.format(src_ip, src_mask), strict=False)\
118 .compare_networks(ip_network(u'{0}'.format(dst_ip, dst_mask),strict=False))
119 self.assertTrue(ret == 0)
120
121
122 for vnf in self.dc[0].listCompute():
123 # check E LAN connection
124 network_list = vnf.getNetworkStatus()
125 mgmt_ip = [intf['ip'] for intf in network_list if intf['intf_name'] == 'mgmt']
126 self.assertTrue(len(mgmt_ip) > 0)
127 ip_address = mgmt_ip[0]
128 ELAN_list.append(ip_address)
129 print ip_address
130
131 # check ELAN connection by ping over the mgmt network (needs to be configured as ELAN in the test service)
132 for vnf in self.dc[0].listCompute():
133 network_list = vnf.getNetworkStatus()
134 mgmt_ip = [intf['ip'] for intf in network_list if intf['intf_name'] == 'mgmt']
135 self.assertTrue(len(mgmt_ip) > 0)
136 ip_address = mgmt_ip[0]
137 print ELAN_list
138 print ip_address
139 test_ip_list = list(ELAN_list)
140 test_ip_list.remove(ip_address)
141 for ip in test_ip_list:
142 # only take ip address, without netmask
143 p = self.net.ping([vnf],manualdestip=ip.split('/')[0])
144 print p
145 self.assertTrue(p <= 0.0)
146
147 # stop Mininet network
148 self.stopNet()
149 initialize_GK()
150
151 #@unittest.skip("disabled")
152 def test_GK_Api_stop_service(self):
153 # create network
154 self.createNet(ndatacenter=2, nhosts=2)
155 # setup links
156 self.net.addLink(self.dc[0], self.h[0])
157 self.net.addLink(self.dc[0], self.dc[1])
158 self.net.addLink(self.h[1], self.dc[1])
159 # connect dummy GK to data centers
160 sdkg1 = SonataDummyGatekeeperEndpoint("0.0.0.0", 5000)
161 sdkg1.connectDatacenter(self.dc[0])
162 sdkg1.connectDatacenter(self.dc[1])
163 # run the dummy gatekeeper (in another thread, don't block)
164 sdkg1.start()
165 time.sleep(3)
166 # start Mininet network
167 self.startNet()
168 time.sleep(1)
169
170 print "starting tests"
171 # board package
172 files = {"package": open(PACKAGE_PATH, "rb")}
173 r = requests.post("http://127.0.0.1:5000/packages", files=files)
174 self.assertEqual(r.status_code, 201)
175 self.assertTrue(json.loads(r.text).get("service_uuid") is not None)
176
177 # instantiate service
178 self.service_uuid = json.loads(r.text).get("service_uuid")
179 r2 = requests.post("http://127.0.0.1:5000/instantiations", data=json.dumps({"service_uuid": self.service_uuid}))
180 self.assertEqual(r2.status_code, 201)
181
182 # give the emulator some time to instantiate everything
183 time.sleep(2)
184
185 # check get request APIs
186 r3 = requests.get("http://127.0.0.1:5000/packages")
187 self.assertEqual(len(json.loads(r3.text).get("service_uuid_list")), 1)
188 r4 = requests.get("http://127.0.0.1:5000/instantiations")
189 self.assertEqual(len(json.loads(r4.text).get("service_instantiations_list")), 1)
190
191 # check number of running nodes
192 self.assertTrue(len(self.getContainernetContainers()) == 3)
193 self.assertTrue(len(self.net.hosts) == 5)
194 self.assertTrue(len(self.net.switches) == 2)
195 # check compute list result
196 self.assertEqual(len(self.dc[0].listCompute()), 2)
197
198 # stop the service
199 service_instance_uuid = json.loads(r2.text).get("service_instance_uuid")
200 self.assertTrue(service_instance_uuid is not None)
201 requests.delete("http://127.0.0.1:5000/instantiations", data=json.dumps({"service_uuid": self.service_uuid, "service_instance_uuid":service_instance_uuid}))
202
203 r5 = requests.get("http://127.0.0.1:5000/instantiations")
204 self.assertTrue(len(json.loads(r5.text).get("service_instantiations_list")), 0) # note that there was 1 instance before
205
206 # stop Mininet network
207 self.stopNet()
208 initialize_GK()
209
210
211 #@unittest.skip("disabled")
212 def test_GK_stress_service(self):
213 # create network
214 self.createNet(ndatacenter=2, nhosts=2)
215 # connect dummy GK to data centers
216 sdkg1 = SonataDummyGatekeeperEndpoint("0.0.0.0", 5000)
217 sdkg1.connectDatacenter(self.dc[0])
218 sdkg1.connectDatacenter(self.dc[1])
219 # run the dummy gatekeeper (in another thread, don't block)
220 sdkg1.start()
221 time.sleep(3)
222 # start Mininet network
223 self.startNet()
224 time.sleep(1)
225
226 print "starting tests"
227 # board package
228 files = {"package": open("misc/sonata-stress-service.son", "rb")}
229 r = requests.post("http://127.0.0.1:5000/packages", files=files)
230 self.assertEqual(r.status_code, 201)
231 self.assertTrue(json.loads(r.text).get("service_uuid") is not None)
232
233 # instantiate service
234 self.service_uuid = json.loads(r.text).get("service_uuid")
235 r2 = requests.post("http://127.0.0.1:5000/instantiations", data=json.dumps({"service_uuid": self.service_uuid}))
236 self.assertEqual(r2.status_code, 201)
237
238 # give the emulator some time to instantiate everything
239 time.sleep(2)
240
241 # check get request APIs
242 r3 = requests.get("http://127.0.0.1:5000/packages")
243 self.assertEqual(len(json.loads(r3.text).get("service_uuid_list")), 1)
244 r4 = requests.get("http://127.0.0.1:5000/instantiations")
245 self.assertEqual(len(json.loads(r4.text).get("service_instantiations_list")), 1)
246
247 # stop the service
248 service_instance_uuid = json.loads(r2.text).get("service_instance_uuid")
249 self.assertTrue(service_instance_uuid is not None)
250 requests.delete("http://127.0.0.1:5000/instantiations", data=json.dumps({"service_uuid": self.service_uuid, "service_instance_uuid":service_instance_uuid}))
251
252 r5 = requests.get("http://127.0.0.1:5000/instantiations")
253 self.assertTrue(len(json.loads(r5.text).get("service_instantiations_list")), 0) # note that there was 1 instance before
254
255 # stop Mininet network
256 self.stopNet()
257 initialize_GK()
258
259