e63cb2420ec91eb16ad18e01a0876d99f7906a92
[osm/vim-emu.git] / src / emuvim / test / unittests / test_restapi.py
1 # 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).
26 import unittest
27 from emuvim.test.api_base import SimpleTestTopology
28 import subprocess
29 from emuvim.dcemulator.node import EmulatorCompute
30 import ast
31
32
33 class testRestApi(SimpleTestTopology):
34 """
35 Tests to check the REST API endpoints of the emulator.
36 """
37
38 def testRestApi(self):
39 # create network
40 self.createNet(nswitches=0, ndatacenter=2, nhosts=2, ndockers=0)
41
42 # 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])
46
47 # start api
48 self.startApi()
49
50 # start Mininet network
51 self.startNet()
52
53 print('->>>>>>> vim-emu compute start -d datacenter0 -n vnf1 ->>>>>>>>>>>>>>>')
54 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
55 subprocess.call(
56 "vim-emu compute start -d datacenter0 -n vnf1", shell=True)
57 print('->>>>>>> vim-emu compute start -d datacenter0 -n vnf2 ->>>>>>>>>>>>>>>')
58 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
59 subprocess.call(
60 "vim-emu compute start -d datacenter0 -n vnf2", shell=True)
61 print('->>>>>>> vim-emu compute start -d datacenter0 -n vnf3 ->>>>>>>>>>>>>>>')
62 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
63 subprocess.call(
64 "vim-emu compute start -d datacenter1 -n vnf3", shell=True)
65 subprocess.call("vim-emu compute list", shell=True)
66 print('->>>>>>> checking running nodes, compute list, and connectivity >>>>>>>>>>')
67
68 # check number of running nodes
69 self.assertEqual(len(self.getContainernetContainers()), 3)
70 self.assertEqual(len(self.net.hosts), 5)
71 self.assertEqual(len(self.net.switches), 2)
72
73 # check compute list result
74 self.assertEqual(len(self.dc[0].listCompute()), 2)
75 self.assertEqual(len(self.dc[1].listCompute()), 1)
76 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))
82 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())))
87
88 # check connectivity by using ping
89 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)
95
96 print('network add vnf1 vnf2->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
97 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
98 output = subprocess.check_output(
99 "vim-emu network add -src vnf1 -dst vnf2 -b -c 10", shell=True)
100 print("output: ", output)
101 print("type: ", type(output))
102 self.assertIn("add-flow", output.decode())
103 self.assertIn("success", output.decode())
104
105 print('network remove vnf1 vnf2->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
106 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
107 output = subprocess.check_output(
108 "vim-emu network remove -src vnf1 -dst vnf2 -b", shell=True)
109 self.assertIn("del-flows", output.decode())
110 self.assertIn("success", output.decode())
111
112 print('>>>>> checking --> vim-emu compute stop -d datacenter0 -n vnf2 ->>>>>>')
113 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
114 output = subprocess.check_output(
115 "vim-emu compute stop -d datacenter0 -n vnf2", shell=True)
116
117 # check number of running nodes
118 self.assertEqual(len(self.getContainernetContainers()), 2)
119 self.assertEqual(len(self.net.hosts), 4)
120 self.assertEqual(len(self.net.switches), 2)
121 # check compute list result
122 self.assertEqual(len(self.dc[0].listCompute()), 1)
123 self.assertEqual(len(self.dc[1].listCompute()), 1)
124
125 print('>>>>> checking --> vim-emu compute list ->>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
126 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
127 output = subprocess.check_output("vim-emu compute list", shell=True)
128
129 # check datacenter list result
130 self.assertIn("datacenter0", output.decode())
131
132 print('>>>>> checking --> vim-emu compute status -d datacenter0 -n vnf1 ->>>>')
133 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
134 output = subprocess.check_output(
135 "vim-emu compute status -d datacenter0 -n vnf1", shell=True)
136 output = ast.literal_eval(output.decode())
137
138 # check compute status result
139 self.assertEqual(output["name"], "vnf1")
140 self.assertTrue(output["state"]["Running"])
141
142 print('>>>>> checking --> vim-emu datacenter list ->>>>>>>>>>>>>>>>>>>>>>>>>>')
143 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
144 output = subprocess.check_output("vim-emu datacenter list", shell=True)
145 # check datacenter list result
146 self.assertIn("datacenter0", output.decode())
147
148 print('->>>>> checking --> vim-emu datacenter status -d datacenter0 ->>>>>>>>')
149 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
150 output = subprocess.check_output(
151 "vim-emu datacenter status -d datacenter0", shell=True)
152 # check datacenter status result
153 self.assertIn("datacenter0", output.decode())
154 self.stopApi()
155 self.stopNet()
156
157
158 if __name__ == '__main__':
159 unittest.main()