cpu resource control via rest api + unittest including ELAN test
[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
37 import mininet.clean
38
39 PACKAGE_PATH = "misc/sonata-demo-service.son"
40
41
42 class testSonataDummyGatekeeper(SimpleTestTopology):
43
44 # @unittest.skip("disabled")
45 def test_GK_Api_start_service(self):
46 # create network
47 self.createNet(nswitches=0, ndatacenter=2, nhosts=2, ndockers=0, enable_learning=True)
48 # setup links
49 self.net.addLink(self.dc[0], self.h[0])
50 self.net.addLink(self.dc[0], self.dc[1])
51 self.net.addLink(self.h[1], self.dc[1])
52 # connect dummy GK to data centers
53 sdkg1 = SonataDummyGatekeeperEndpoint("0.0.0.0", 5000)
54 sdkg1.connectDatacenter(self.dc[0])
55 sdkg1.connectDatacenter(self.dc[1])
56 # run the dummy gatekeeper (in another thread, don't block)
57 sdkg1.start()
58 # start Mininet network
59 self.startNet()
60 time.sleep(1)
61
62 print "starting tests"
63 # board package
64 files = {"package": open(PACKAGE_PATH, "rb")}
65 r = requests.post("http://127.0.0.1:5000/packages", files=files)
66 self.assertEqual(r.status_code, 201)
67 self.assertTrue(json.loads(r.text).get("service_uuid") is not None)
68
69 # instantiate service
70 self.service_uuid = json.loads(r.text).get("service_uuid")
71 r2 = requests.post("http://127.0.0.1:5000/instantiations", data=json.dumps({"service_uuid": self.service_uuid}))
72 self.assertEqual(r2.status_code, 201)
73
74 # give the emulator some time to instantiate everything
75 time.sleep(2)
76
77 # check get request APIs
78 r3 = requests.get("http://127.0.0.1:5000/packages")
79 self.assertEqual(len(json.loads(r3.text).get("service_uuid_list")), 1)
80 r4 = requests.get("http://127.0.0.1:5000/instantiations")
81 self.assertEqual(len(json.loads(r4.text).get("service_instantiations_list")), 1)
82
83 # check number of running nodes
84 self.assertTrue(len(self.getContainernetContainers()) == 3)
85 self.assertTrue(len(self.net.hosts) == 5)
86 self.assertTrue(len(self.net.switches) == 2)
87 # check compute list result
88 self.assertEqual(len(self.dc[0].listCompute()), 2)
89 # check connectivity by using ping
90 ELAN_list=[]
91 for i in [0]:
92 for vnf in self.dc[i].listCompute():
93 # check connection
94 p = self.net.ping([self.h[i], vnf])
95 print p
96 self.assertTrue(p <= 0.0)
97
98 # check E LAN connection
99 network_list = vnf.getNetworkStatus()
100 mgmt_ip = [intf['ip'] for intf in network_list if intf['intf_name'] == 'mgmt']
101 self.assertTrue(len(mgmt_ip) > 0)
102 ip_address = mgmt_ip[0]
103 ELAN_list.append(ip_address)
104 print ip_address
105
106 # check ELAN connection by ping over the mgmt network (needs to be configured as ELAN in the test service)
107 for vnf in self.dc[0].listCompute():
108 network_list = vnf.getNetworkStatus()
109 mgmt_ip = [intf['ip'] for intf in network_list if intf['intf_name'] == 'mgmt']
110 self.assertTrue(len(mgmt_ip) > 0)
111 ip_address = mgmt_ip[0]
112 print ELAN_list
113 print ip_address
114 test_ip_list = list(ELAN_list)
115 test_ip_list.remove(ip_address)
116 for ip in test_ip_list:
117 p = self.net.ping([vnf],manualdestip=ip)
118 print p
119 self.assertTrue(p <= 0.0)
120
121 # stop Mininet network
122 self.stopNet()
123 initialize_GK()
124
125
126 def test_GK_Api_stop_service(self):
127 # create network
128 self.createNet(ndatacenter=2, nhosts=2)
129 # setup links
130 self.net.addLink(self.dc[0], self.h[0])
131 self.net.addLink(self.dc[0], self.dc[1])
132 self.net.addLink(self.h[1], self.dc[1])
133 # connect dummy GK to data centers
134 sdkg1 = SonataDummyGatekeeperEndpoint("0.0.0.0", 5000)
135 sdkg1.connectDatacenter(self.dc[0])
136 sdkg1.connectDatacenter(self.dc[1])
137 # run the dummy gatekeeper (in another thread, don't block)
138 sdkg1.start()
139 # start Mininet network
140 self.startNet()
141 time.sleep(1)
142
143 print "starting tests"
144 # board package
145 files = {"package": open(PACKAGE_PATH, "rb")}
146 r = requests.post("http://127.0.0.1:5000/packages", files=files)
147 self.assertEqual(r.status_code, 201)
148 self.assertTrue(json.loads(r.text).get("service_uuid") is not None)
149
150 # instantiate service
151 self.service_uuid = json.loads(r.text).get("service_uuid")
152 r2 = requests.post("http://127.0.0.1:5000/instantiations", data=json.dumps({"service_uuid": self.service_uuid}))
153 self.assertEqual(r2.status_code, 201)
154
155 # give the emulator some time to instantiate everything
156 time.sleep(2)
157
158 # check get request APIs
159 r3 = requests.get("http://127.0.0.1:5000/packages")
160 self.assertEqual(len(json.loads(r3.text).get("service_uuid_list")), 1)
161 r4 = requests.get("http://127.0.0.1:5000/instantiations")
162 self.assertEqual(len(json.loads(r4.text).get("service_instantiations_list")), 1)
163
164 # check number of running nodes
165 self.assertTrue(len(self.getContainernetContainers()) == 3)
166 self.assertTrue(len(self.net.hosts) == 5)
167 self.assertTrue(len(self.net.switches) == 2)
168 # check compute list result
169 self.assertEqual(len(self.dc[0].listCompute()), 2)
170
171 # stop the service
172 service_instance_uuid = json.loads(r2.text).get("service_instance_uuid")
173 self.assertTrue(service_instance_uuid is not None)
174 requests.delete("http://127.0.0.1:5000/instantiations", data=json.dumps({"service_uuid": self.service_uuid, "service_instance_uuid":service_instance_uuid}))
175
176 r5 = requests.get("http://127.0.0.1:5000/instantiations")
177 self.assertTrue(len(json.loads(r5.text).get("service_instantiations_list")), 0) # note that there was 1 instance before
178
179 # stop Mininet network
180 self.stopNet()
181 initialize_GK()