blob: e63cb2420ec91eb16ad18e01a0876d99f7906a92 [file] [log] [blame]
peusterm72f09882018-05-15 17:10:27 +02001# Copyright (c) 2015 SONATA-NFV and Paderborn University
2# ALL RIGHTS RESERVED.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16# Neither the name of the SONATA-NFV, Paderborn University
17# nor the names of its contributors may be used to endorse or promote
18# products derived from this software without specific prior written
19# permission.
20#
21# This work has been performed in the framework of the SONATA project,
22# funded by the European Commission under Grant number 671517 through
23# the Horizon 2020 and 5G-PPP programmes. The authors would like to
24# acknowledge the contributions of their colleagues of the SONATA
25# partner consortium (www.sonata-nfv.eu).
hadik3r237d3f52016-06-27 17:57:49 +020026import unittest
27from emuvim.test.api_base import SimpleTestTopology
28import subprocess
hadik3r895477d2016-06-29 14:38:05 +020029from emuvim.dcemulator.node import EmulatorCompute
30import ast
hadik3r237d3f52016-06-27 17:57:49 +020031
hadik3ra9dd9012016-08-09 10:51:13 +020032
33class testRestApi(SimpleTestTopology):
hadik3r237d3f52016-06-27 17:57:49 +020034 """
35 Tests to check the REST API endpoints of the emulator.
36 """
37
38 def testRestApi(self):
hadik3r237d3f52016-06-27 17:57:49 +020039 # create network
40 self.createNet(nswitches=0, ndatacenter=2, nhosts=2, ndockers=0)
hadik3r895477d2016-06-29 14:38:05 +020041
hadik3r237d3f52016-06-27 17:57:49 +020042 # setup links
43 self.net.addLink(self.dc[0], self.h[0])
44 self.net.addLink(self.h[1], self.dc[1])
45 self.net.addLink(self.dc[0], self.dc[1])
hadik3r895477d2016-06-29 14:38:05 +020046
hadik3r237d3f52016-06-27 17:57:49 +020047 # start api
48 self.startApi()
hadik3r895477d2016-06-29 14:38:05 +020049
hadik3r237d3f52016-06-27 17:57:49 +020050 # start Mininet network
51 self.startNet()
hadik3r895477d2016-06-29 14:38:05 +020052
peustermdd559512017-09-21 16:29:34 +020053 print('->>>>>>> vim-emu compute start -d datacenter0 -n vnf1 ->>>>>>>>>>>>>>>')
hadik3r237d3f52016-06-27 17:57:49 +020054 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
peusterm72f09882018-05-15 17:10:27 +020055 subprocess.call(
56 "vim-emu compute start -d datacenter0 -n vnf1", shell=True)
peustermdd559512017-09-21 16:29:34 +020057 print('->>>>>>> vim-emu compute start -d datacenter0 -n vnf2 ->>>>>>>>>>>>>>>')
hadik3r237d3f52016-06-27 17:57:49 +020058 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
peusterm72f09882018-05-15 17:10:27 +020059 subprocess.call(
60 "vim-emu compute start -d datacenter0 -n vnf2", shell=True)
peustermdd559512017-09-21 16:29:34 +020061 print('->>>>>>> vim-emu compute start -d datacenter0 -n vnf3 ->>>>>>>>>>>>>>>')
hadik3r237d3f52016-06-27 17:57:49 +020062 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
peusterm72f09882018-05-15 17:10:27 +020063 subprocess.call(
64 "vim-emu compute start -d datacenter1 -n vnf3", shell=True)
peustermdd559512017-09-21 16:29:34 +020065 subprocess.call("vim-emu compute list", shell=True)
hadik3r895477d2016-06-29 14:38:05 +020066 print('->>>>>>> checking running nodes, compute list, and connectivity >>>>>>>>>>')
67
68 # check number of running nodes
Rafael Schellenbergaa8823c2019-12-06 15:03:56 +010069 self.assertEqual(len(self.getContainernetContainers()), 3)
70 self.assertEqual(len(self.net.hosts), 5)
71 self.assertEqual(len(self.net.switches), 2)
hadik3r895477d2016-06-29 14:38:05 +020072
73 # check compute list result
Rafael Schellenbergaa8823c2019-12-06 15:03:56 +010074 self.assertEqual(len(self.dc[0].listCompute()), 2)
75 self.assertEqual(len(self.dc[1].listCompute()), 1)
peusterm72f09882018-05-15 17:10:27 +020076 self.assertTrue(isinstance(
77 self.dc[0].listCompute()[0], EmulatorCompute))
78 self.assertTrue(isinstance(
79 self.dc[0].listCompute()[1], EmulatorCompute))
80 self.assertTrue(isinstance(
81 self.dc[1].listCompute()[0], EmulatorCompute))
Rafael Schellenbergaa8823c2019-12-06 15:03:56 +010082 print("dc1: ", self.dc[0].listCompute())
83 print("dc2: ", self.dc[1].listCompute())
84 self.assertIn("vnf1", list(map(lambda x: x.name, self.dc[0].listCompute())))
85 self.assertIn("vnf2", list(map(lambda x: x.name, self.dc[0].listCompute())))
86 self.assertIn("vnf3", list(map(lambda x: x.name, self.dc[1].listCompute())))
hadik3r895477d2016-06-29 14:38:05 +020087
88 # check connectivity by using ping
peusterm72f09882018-05-15 17:10:27 +020089 self.assertTrue(self.net.ping(
90 [self.dc[0].listCompute()[1], self.dc[0].listCompute()[0]]) <= 0.0)
91 self.assertTrue(self.net.ping(
92 [self.dc[0].listCompute()[0], self.dc[1].listCompute()[0]]) <= 0.0)
93 self.assertTrue(self.net.ping(
94 [self.dc[1].listCompute()[0], self.dc[0].listCompute()[1]]) <= 0.0)
stevenvanrossem73efd192016-06-29 01:44:07 +020095
96 print('network add vnf1 vnf2->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
97 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
peusterm72f09882018-05-15 17:10:27 +020098 output = subprocess.check_output(
99 "vim-emu network add -src vnf1 -dst vnf2 -b -c 10", shell=True)
Rafael Schellenbergaa8823c2019-12-06 15:03:56 +0100100 print("output: ", output)
101 print("type: ", type(output))
102 self.assertIn("add-flow", output.decode())
103 self.assertIn("success", output.decode())
stevenvanrossemae588012017-06-06 10:33:19 +0200104
stevenvanrossem73efd192016-06-29 01:44:07 +0200105 print('network remove vnf1 vnf2->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
106 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
peusterm72f09882018-05-15 17:10:27 +0200107 output = subprocess.check_output(
108 "vim-emu network remove -src vnf1 -dst vnf2 -b", shell=True)
Rafael Schellenbergaa8823c2019-12-06 15:03:56 +0100109 self.assertIn("del-flows", output.decode())
110 self.assertIn("success", output.decode())
stevenvanrossem73efd192016-06-29 01:44:07 +0200111
peustermdd559512017-09-21 16:29:34 +0200112 print('>>>>> checking --> vim-emu compute stop -d datacenter0 -n vnf2 ->>>>>>')
hadik3r237d3f52016-06-27 17:57:49 +0200113 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
peusterm72f09882018-05-15 17:10:27 +0200114 output = subprocess.check_output(
115 "vim-emu compute stop -d datacenter0 -n vnf2", shell=True)
hadik3r895477d2016-06-29 14:38:05 +0200116
117 # check number of running nodes
Rafael Schellenbergaa8823c2019-12-06 15:03:56 +0100118 self.assertEqual(len(self.getContainernetContainers()), 2)
119 self.assertEqual(len(self.net.hosts), 4)
120 self.assertEqual(len(self.net.switches), 2)
hadik3r895477d2016-06-29 14:38:05 +0200121 # check compute list result
Rafael Schellenbergaa8823c2019-12-06 15:03:56 +0100122 self.assertEqual(len(self.dc[0].listCompute()), 1)
123 self.assertEqual(len(self.dc[1].listCompute()), 1)
hadik3r895477d2016-06-29 14:38:05 +0200124
peustermdd559512017-09-21 16:29:34 +0200125 print('>>>>> checking --> vim-emu compute list ->>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
hadik3r237d3f52016-06-27 17:57:49 +0200126 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
peustermdd559512017-09-21 16:29:34 +0200127 output = subprocess.check_output("vim-emu compute list", shell=True)
hadik3r895477d2016-06-29 14:38:05 +0200128
129 # check datacenter list result
Rafael Schellenbergaa8823c2019-12-06 15:03:56 +0100130 self.assertIn("datacenter0", output.decode())
hadik3r895477d2016-06-29 14:38:05 +0200131
peustermdd559512017-09-21 16:29:34 +0200132 print('>>>>> checking --> vim-emu compute status -d datacenter0 -n vnf1 ->>>>')
hadik3r237d3f52016-06-27 17:57:49 +0200133 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
peusterm72f09882018-05-15 17:10:27 +0200134 output = subprocess.check_output(
135 "vim-emu compute status -d datacenter0 -n vnf1", shell=True)
Rafael Schellenbergaa8823c2019-12-06 15:03:56 +0100136 output = ast.literal_eval(output.decode())
hadik3r895477d2016-06-29 14:38:05 +0200137
138 # check compute status result
Rafael Schellenbergaa8823c2019-12-06 15:03:56 +0100139 self.assertEqual(output["name"], "vnf1")
hadik3r895477d2016-06-29 14:38:05 +0200140 self.assertTrue(output["state"]["Running"])
141
peustermdd559512017-09-21 16:29:34 +0200142 print('>>>>> checking --> vim-emu datacenter list ->>>>>>>>>>>>>>>>>>>>>>>>>>')
hadik3r237d3f52016-06-27 17:57:49 +0200143 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
peustermdd559512017-09-21 16:29:34 +0200144 output = subprocess.check_output("vim-emu datacenter list", shell=True)
hadik3r895477d2016-06-29 14:38:05 +0200145 # check datacenter list result
Rafael Schellenbergaa8823c2019-12-06 15:03:56 +0100146 self.assertIn("datacenter0", output.decode())
hadik3r895477d2016-06-29 14:38:05 +0200147
peustermdd559512017-09-21 16:29:34 +0200148 print('->>>>> checking --> vim-emu datacenter status -d datacenter0 ->>>>>>>>')
hadik3r237d3f52016-06-27 17:57:49 +0200149 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
peusterm72f09882018-05-15 17:10:27 +0200150 output = subprocess.check_output(
151 "vim-emu datacenter status -d datacenter0", shell=True)
hadik3r895477d2016-06-29 14:38:05 +0200152 # check datacenter status result
Rafael Schellenbergaa8823c2019-12-06 15:03:56 +0100153 self.assertIn("datacenter0", output.decode())
peusterm25074142017-08-18 10:10:27 +0200154 self.stopApi()
hadik3r237d3f52016-06-27 17:57:49 +0200155 self.stopNet()
156
157
158if __name__ == '__main__':
159 unittest.main()