Added some delays to test to see if this causes the problem in the CI.
[osm/vim-emu.git] / src / emuvim / test / test_emulator.py
1 """
2 Test suite to automatically test emulator functionalities.
3 Directly interacts with the emulator through the Mininet-like
4 Python API.
5
6 Does not test API endpoints. This is done in separated test suites.
7 """
8
9 import time
10 import unittest
11 from emuvim.dcemulator.node import EmulatorCompute
12 from emuvim.test.base import SimpleTestTopology
13
14
15 #@unittest.skip("disabled topology tests for development")
16 class testEmulatorTopology( SimpleTestTopology ):
17 """
18 Tests to check the topology API of the emulator.
19 """
20
21 def testSingleDatacenter(self):
22 """
23 Create a single data center and add check if its switch is up
24 by using manually added hosts. Tests especially the
25 data center specific addLink method.
26 """
27 # create network
28 self.createNet(nswitches=0, ndatacenter=1, nhosts=2, ndockers=0)
29 # setup links
30 self.net.addLink(self.dc[0], self.h[0])
31 self.net.addLink(self.h[1], self.dc[0])
32 # start Mininet network
33 self.startNet()
34 # check number of running nodes
35 assert(len(self.getDockernetContainers()) == 0)
36 assert(len(self.net.hosts) == 2)
37 assert(len(self.net.switches) == 1)
38 # check connectivity by using ping
39 assert(self.net.ping([self.h[0], self.h[1]]) <= 0.0)
40 # stop Mininet network
41 self.stopNet()
42
43 def testMultipleDatacenterDirect(self):
44 """
45 Create a two data centers and interconnect them.
46 """
47 # create network
48 self.createNet(nswitches=0, ndatacenter=2, nhosts=2, ndockers=0)
49 # setup links
50 self.net.addLink(self.dc[0], self.h[0])
51 self.net.addLink(self.h[1], self.dc[1])
52 self.net.addLink(self.dc[0], self.dc[1])
53 # start Mininet network
54 self.startNet()
55 # check number of running nodes
56 assert(len(self.getDockernetContainers()) == 0)
57 assert(len(self.net.hosts) == 2)
58 assert(len(self.net.switches) == 2)
59 time.sleep(5)
60 # check connectivity by using ping
61 assert(self.net.ping([self.h[0], self.h[1]]) <= 0.0)
62 # stop Mininet network
63 self.stopNet()
64
65 def testMultipleDatacenterWithIntermediateSwitches(self):
66 """
67 Create a two data centers and interconnect them with additional
68 switches between them.
69 """
70 # create network
71 self.createNet(
72 nswitches=3, ndatacenter=2, nhosts=2, ndockers=0,
73 autolinkswitches=True)
74 # setup links
75 self.net.addLink(self.dc[0], self.h[0])
76 self.net.addLink(self.h[1], self.dc[1])
77 self.net.addLink(self.dc[0], self.s[0])
78 self.net.addLink(self.s[2], self.dc[1])
79 # start Mininet network
80 self.startNet()
81 # check number of running nodes
82 assert(len(self.getDockernetContainers()) == 0)
83 assert(len(self.net.hosts) == 2)
84 assert(len(self.net.switches) == 5)
85 time.sleep(5)
86 # check connectivity by using ping
87 assert(self.net.ping([self.h[0], self.h[1]]) <= 0.0)
88 # stop Mininet network
89 self.stopNet()
90
91
92 #@unittest.skip("disabled compute tests for development")
93 class testEmulatorCompute( SimpleTestTopology ):
94 """
95 Tests to check the emulator's API to add and remove
96 compute resources at runtime.
97 """
98
99 def testAddSingleComputeSingleDC(self):
100 """
101 Adds a single compute instance to
102 a single DC and checks its connectivity with a
103 manually added host.
104 """
105 # create network
106 self.createNet(nswitches=0, ndatacenter=1, nhosts=1, ndockers=0)
107 # setup links
108 self.net.addLink(self.dc[0], self.h[0])
109 # start Mininet network
110 self.startNet()
111 # add compute resources
112 vnf1 = self.dc[0].startCompute("vnf1")
113 # check number of running nodes
114 assert(len(self.getDockernetContainers()) == 1)
115 assert(len(self.net.hosts) == 2)
116 assert(len(self.net.switches) == 1)
117 # check compute list result
118 assert(len(self.dc[0].listCompute()) == 1)
119 assert(isinstance(self.dc[0].listCompute()[0], EmulatorCompute))
120 assert(self.dc[0].listCompute()[0].name == "vnf1")
121 # check connectivity by using ping
122 assert(self.net.ping([self.h[0], vnf1]) <= 0.0)
123 # stop Mininet network
124 self.stopNet()
125
126 def testRemoveSingleComputeSingleDC(self):
127 """
128 Test stop method for compute instances.
129 Check that the instance is really removed.
130 """
131 # create network
132 self.createNet(nswitches=0, ndatacenter=1, nhosts=1, ndockers=0)
133 # setup links
134 self.net.addLink(self.dc[0], self.h[0])
135 # start Mininet network
136 self.startNet()
137 # add compute resources
138 vnf1 = self.dc[0].startCompute("vnf1")
139 # check number of running nodes
140 assert(len(self.getDockernetContainers()) == 1)
141 assert(len(self.net.hosts) == 2)
142 assert(len(self.net.switches) == 1)
143 # check compute list result
144 assert(len(self.dc[0].listCompute()) == 1)
145 # check connectivity by using ping
146 assert(self.net.ping([self.h[0], vnf1]) <= 0.0)
147 # remove compute resources
148 self.dc[0].stopCompute("vnf1")
149 # check number of running nodes
150 assert(len(self.getDockernetContainers()) == 0)
151 assert(len(self.net.hosts) == 1)
152 assert(len(self.net.switches) == 1)
153 # check compute list result
154 assert(len(self.dc[0].listCompute()) == 0)
155 # stop Mininet network
156 self.stopNet()
157
158 def testGetStatusSingleComputeSingleDC(self):
159 """
160 Check if the getStatus functionality of EmulatorCompute
161 objects works well.
162 """
163 # create network
164 self.createNet(nswitches=0, ndatacenter=1, nhosts=1, ndockers=0)
165 # setup links
166 self.net.addLink(self.dc[0], self.h[0])
167 # start Mininet network
168 self.startNet()
169 # add compute resources
170 vnf1 = self.dc[0].startCompute("vnf1")
171 # check number of running nodes
172 assert(len(self.getDockernetContainers()) == 1)
173 assert(len(self.net.hosts) == 2)
174 assert(len(self.net.switches) == 1)
175 # check compute list result
176 assert(len(self.dc[0].listCompute()) == 1)
177 assert(isinstance(self.dc[0].listCompute()[0], EmulatorCompute))
178 assert(self.dc[0].listCompute()[0].name == "vnf1")
179 # check connectivity by using ping
180 assert(self.net.ping([self.h[0], vnf1]) <= 0.0)
181 # check get status
182 s = self.dc[0].containers.get("vnf1").getStatus()
183 assert(s["name"] == "vnf1")
184 assert(s["state"]["Running"])
185 # stop Mininet network
186 self.stopNet()
187
188 def testConnectivityMultiDC(self):
189 """
190 Test if compute instances started in different data centers
191 are able to talk to each other.
192 """
193 # create network
194 self.createNet(
195 nswitches=3, ndatacenter=2, nhosts=0, ndockers=0,
196 autolinkswitches=True)
197 # setup links
198 self.net.addLink(self.dc[0], self.s[0])
199 self.net.addLink(self.dc[1], self.s[2])
200 # start Mininet network
201 self.startNet()
202 # add compute resources
203 vnf1 = self.dc[0].startCompute("vnf1")
204 vnf2 = self.dc[1].startCompute("vnf2")
205 # check number of running nodes
206 assert(len(self.getDockernetContainers()) == 2)
207 assert(len(self.net.hosts) == 2)
208 assert(len(self.net.switches) == 5)
209 # check compute list result
210 assert(len(self.dc[0].listCompute()) == 1)
211 assert(len(self.dc[1].listCompute()) == 1)
212 # check connectivity by using ping
213 assert(self.net.ping([vnf1, vnf2]) <= 0.0)
214 # stop Mininet network
215 self.stopNet()
216
217 def testInterleavedAddRemoveMultiDC(self):
218 """
219 Test multiple, interleaved add and remove operations and ensure
220 that always all expected compute instances are reachable.
221 """
222 # create network
223 self.createNet(
224 nswitches=3, ndatacenter=2, nhosts=0, ndockers=0,
225 autolinkswitches=True)
226 # setup links
227 self.net.addLink(self.dc[0], self.s[0])
228 self.net.addLink(self.dc[1], self.s[2])
229 # start Mininet network
230 self.startNet()
231 # add compute resources
232 vnf1 = self.dc[0].startCompute("vnf1")
233 vnf2 = self.dc[1].startCompute("vnf2")
234 # check number of running nodes
235 assert(len(self.getDockernetContainers()) == 2)
236 assert(len(self.net.hosts) == 2)
237 assert(len(self.net.switches) == 5)
238 # check compute list result
239 assert(len(self.dc[0].listCompute()) == 1)
240 assert(len(self.dc[1].listCompute()) == 1)
241 # check connectivity by using ping
242 assert(self.net.ping([vnf1, vnf2]) <= 0.0)
243 # remove compute resources
244 self.dc[0].stopCompute("vnf1")
245 # check number of running nodes
246 assert(len(self.getDockernetContainers()) == 1)
247 assert(len(self.net.hosts) == 1)
248 assert(len(self.net.switches) == 5)
249 # check compute list result
250 assert(len(self.dc[0].listCompute()) == 0)
251 assert(len(self.dc[1].listCompute()) == 1)
252 # add compute resources
253 vnf3 = self.dc[0].startCompute("vnf3")
254 vnf4 = self.dc[0].startCompute("vnf4")
255 # check compute list result
256 assert(len(self.dc[0].listCompute()) == 2)
257 assert(len(self.dc[1].listCompute()) == 1)
258 assert(self.net.ping([vnf3, vnf2]) <= 0.0)
259 assert(self.net.ping([vnf4, vnf2]) <= 0.0)
260 # remove compute resources
261 self.dc[0].stopCompute("vnf3")
262 self.dc[0].stopCompute("vnf4")
263 self.dc[1].stopCompute("vnf2")
264 # check compute list result
265 assert(len(self.dc[0].listCompute()) == 0)
266 assert(len(self.dc[1].listCompute()) == 0)
267 # stop Mininet network
268 self.stopNet()
269
270 if __name__ == '__main__':
271 unittest.main()