Merge pull request #127 from hadik3r/master
[osm/vim-emu.git] / src / emuvim / test / unittests / test_restapi.py
1 """
2 Test suite to automatically test emulator REST API endpoints.
3 """
4
5 import time
6 import unittest
7 from emuvim.test.api_base import SimpleTestTopology
8 import subprocess
9 from emuvim.dcemulator.node import EmulatorCompute
10 import ast
11
12 class testRestApi( SimpleTestTopology ):
13 """
14 Tests to check the REST API endpoints of the emulator.
15 """
16
17 def testRestApi(self):
18
19 # create network
20 self.createNet(nswitches=0, ndatacenter=2, nhosts=2, ndockers=0)
21
22 # setup links
23 self.net.addLink(self.dc[0], self.h[0])
24 self.net.addLink(self.h[1], self.dc[1])
25 self.net.addLink(self.dc[0], self.dc[1])
26
27 # start api
28 self.startApi()
29
30 # start Mininet network
31 self.startNet()
32
33 print('->>>>>>> son-emu-cli compute start -d datacenter0 -n vnf1 ->>>>>>>>>>>>>>>')
34 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
35 subprocess.call("son-emu-cli compute start -d datacenter0 -n vnf1", shell=True)
36 print('->>>>>>> son-emu-cli compute start -d datacenter0 -n vnf2 ->>>>>>>>>>>>>>>')
37 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
38 subprocess.call("son-emu-cli compute start -d datacenter0 -n vnf2", shell=True)
39 print('->>>>>>> son-emu-cli compute start -d datacenter0 -n vnf3 ->>>>>>>>>>>>>>>')
40 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
41 subprocess.call("son-emu-cli compute start -d datacenter1 -n vnf3", shell=True)
42 subprocess.call("son-emu-cli compute list", shell=True)
43 print('->>>>>>> checking running nodes, compute list, and connectivity >>>>>>>>>>')
44
45 # check number of running nodes
46 self.assertTrue(len(self.getContainernetContainers()) == 3)
47 self.assertTrue(len(self.net.hosts) == 5)
48 self.assertTrue(len(self.net.switches) == 2)
49
50 # check compute list result
51 self.assertTrue(len(self.dc[0].listCompute()) == 2)
52 self.assertTrue(len(self.dc[1].listCompute()) == 1)
53 self.assertTrue(isinstance(self.dc[0].listCompute()[0], EmulatorCompute))
54 self.assertTrue(isinstance(self.dc[0].listCompute()[1], EmulatorCompute))
55 self.assertTrue(isinstance(self.dc[1].listCompute()[0], EmulatorCompute))
56 self.assertTrue(self.dc[0].listCompute()[1].name == "vnf1")
57 self.assertTrue(self.dc[0].listCompute()[0].name == "vnf2")
58 self.assertTrue(self.dc[1].listCompute()[0].name == "vnf3")
59
60 # check connectivity by using ping
61 self.assertTrue(self.net.ping([self.dc[0].listCompute()[1], self.dc[0].listCompute()[0]]) <= 0.0)
62 self.assertTrue(self.net.ping([self.dc[0].listCompute()[0], self.dc[1].listCompute()[0]]) <= 0.0)
63 self.assertTrue(self.net.ping([self.dc[1].listCompute()[0], self.dc[0].listCompute()[1]]) <= 0.0)
64
65 print('network add vnf1 vnf2->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
66 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
67 subprocess.call("son-emu-cli network add -src vnf1 -dst vnf2 -b -c 10", shell=True)
68 print('network remove vnf1 vnf2->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
69 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
70 subprocess.call("son-emu-cli network remove -src vnf1 -dst vnf2 -b", shell=True)
71
72 print('>>>>> checking --> son-emu-cli compute stop -d datacenter0 -n vnf2 ->>>>>>')
73 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
74 subprocess.call("son-emu-cli compute stop -d datacenter0 -n vnf2", shell=True)
75
76 # check number of running nodes
77 self.assertTrue(len(self.getContainernetContainers()) == 2)
78 self.assertTrue(len(self.net.hosts) == 4)
79 self.assertTrue(len(self.net.switches) == 2)
80 # check compute list result
81 self.assertTrue(len(self.dc[0].listCompute()) == 1)
82 self.assertTrue(len(self.dc[1].listCompute()) == 1)
83
84 print('>>>>> checking --> son-emu-cli compute list ->>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
85 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
86 subprocess.call("son-emu-cli compute list", shell=True)
87 output = subprocess.check_output("son-emu-cli compute list", shell=True)
88
89 # check datacenter list result
90 self.assertTrue("datacenter0" in output)
91
92 print('>>>>> checking --> son-emu-cli compute status -d datacenter0 -n vnf1 ->>>>')
93 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
94 subprocess.call("son-emu-cli compute status -d datacenter0 -n vnf1", shell=True)
95 output = subprocess.check_output("son-emu-cli compute status -d datacenter0 -n vnf1", shell=True)
96 output= ast.literal_eval(output)
97
98 # check compute status result
99 self.assertTrue(output["name"] == "vnf1")
100 self.assertTrue(output["state"]["Running"])
101
102 print('>>>>> checking --> son-emu-cli datacenter list ->>>>>>>>>>>>>>>>>>>>>>>>>>')
103 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
104 subprocess.call("son-emu-cli datacenter list", shell=True)
105 output = subprocess.check_output("son-emu-cli datacenter list", shell=True)
106
107 # check datacenter list result
108
109 self.assertTrue("datacenter0" in output)
110
111 print('->>>>> checking --> son-emu-cli datacenter status -d datacenter0 ->>>>>>>>')
112 print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
113 subprocess.call("son-emu-cli datacenter status -d datacenter0", shell=True)
114 output = subprocess.check_output("son-emu-cli datacenter status -d datacenter0", shell=True)
115
116 # check datacenter status result
117 self.assertTrue("datacenter0" in output)
118
119 self.stopNet()
120
121
122 if __name__ == '__main__':
123 unittest.main()