Refactoring: Made complete codebase PEP8 compatible.
[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.assertTrue(len(self.getContainernetContainers()) == 3)
70 self.assertTrue(len(self.net.hosts) == 5)
71 self.assertTrue(len(self.net.switches) == 2)
72
73 # check compute list result
74 self.assertTrue(len(self.dc[0].listCompute()) == 2)
75 self.assertTrue(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 self.assertTrue(self.dc[0].listCompute()[1].name == "vnf1")
83 self.assertTrue(self.dc[0].listCompute()[0].name == "vnf2")
84 self.assertTrue(self.dc[1].listCompute()[0].name == "vnf3")
85
86 # check connectivity by using ping
87 self.assertTrue(self.net.ping(
88 [self.dc[0].listCompute()[1], self.dc[0].listCompute()[0]]) <= 0.0)
89 self.assertTrue(self.net.ping(
90 [self.dc[0].listCompute()[0], self.dc[1].listCompute()[0]]) <= 0.0)
91 self.assertTrue(self.net.ping(
92 [self.dc[1].listCompute()[0], self.dc[0].listCompute()[1]]) <= 0.0)
93
94 print('network add vnf1 vnf2->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
95 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
96 output = subprocess.check_output(
97 "vim-emu network add -src vnf1 -dst vnf2 -b -c 10", shell=True)
98 self.assertTrue("add-flow" in output)
99 self.assertTrue("success" in output)
100
101 print('network remove vnf1 vnf2->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
102 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
103 output = subprocess.check_output(
104 "vim-emu network remove -src vnf1 -dst vnf2 -b", shell=True)
105 self.assertTrue("del-flows" in output)
106 self.assertTrue("success" in output)
107
108 print('>>>>> checking --> vim-emu compute stop -d datacenter0 -n vnf2 ->>>>>>')
109 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
110 output = subprocess.check_output(
111 "vim-emu compute stop -d datacenter0 -n vnf2", shell=True)
112
113 # check number of running nodes
114 self.assertTrue(len(self.getContainernetContainers()) == 2)
115 self.assertTrue(len(self.net.hosts) == 4)
116 self.assertTrue(len(self.net.switches) == 2)
117 # check compute list result
118 self.assertTrue(len(self.dc[0].listCompute()) == 1)
119 self.assertTrue(len(self.dc[1].listCompute()) == 1)
120
121 print('>>>>> checking --> vim-emu compute list ->>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
122 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
123 output = subprocess.check_output("vim-emu compute list", shell=True)
124
125 # check datacenter list result
126 self.assertTrue("datacenter0" in output)
127
128 print('>>>>> checking --> vim-emu compute status -d datacenter0 -n vnf1 ->>>>')
129 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
130 output = subprocess.check_output(
131 "vim-emu compute status -d datacenter0 -n vnf1", shell=True)
132 output = ast.literal_eval(output)
133
134 # check compute status result
135 self.assertTrue(output["name"] == "vnf1")
136 self.assertTrue(output["state"]["Running"])
137
138 print('>>>>> checking --> vim-emu datacenter list ->>>>>>>>>>>>>>>>>>>>>>>>>>')
139 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
140 output = subprocess.check_output("vim-emu datacenter list", shell=True)
141 # check datacenter list result
142 self.assertTrue("datacenter0" in output)
143
144 print('->>>>> checking --> vim-emu datacenter status -d datacenter0 ->>>>>>>>')
145 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
146 output = subprocess.check_output(
147 "vim-emu datacenter status -d datacenter0", shell=True)
148 # check datacenter status result
149 self.assertTrue("datacenter0" in output)
150 self.stopApi()
151 self.stopNet()
152
153
154 if __name__ == '__main__':
155 unittest.main()