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