merge master and fix SDN chaining unit test
[osm/vim-emu.git] / src / emuvim / test / unittests / test_resourcemodel.py
1 import time
2 import os
3 import unittest
4 from emuvim.test.base import SimpleTestTopology
5 from emuvim.dcemulator.resourcemodel import BaseResourceModel, ResourceFlavor, NotEnoughResourcesAvailable, ResourceModelRegistrar
6 from emuvim.dcemulator.resourcemodel.upb.simple import UpbSimpleCloudDcRM, UpbOverprovisioningCloudDcRM, UpbDummyRM
7
8
9
10 class testResourceModel(SimpleTestTopology):
11 """
12 Test the general resource model API and functionality.
13 """
14
15 def testBaseResourceModelApi(self):
16 """
17 Tast bare API without real resource madel.
18 :return:
19 """
20 r = BaseResourceModel()
21 # check if default flavors are there
22 self.assertTrue(len(r._flavors) == 5)
23 # check addFlavor functionality
24 f = ResourceFlavor("test", {"testmetric": 42})
25 r.addFlavour(f)
26 self.assertTrue("test" in r._flavors)
27 self.assertTrue(r._flavors.get("test").get("testmetric") == 42)
28
29 def testAddRmToDc(self):
30 """
31 Test is allocate/free is called when a RM is added to a DC.
32 :return:
33 """
34 # create network
35 self.createNet(nswitches=0, ndatacenter=1, nhosts=2, ndockers=0)
36 # setup links
37 self.net.addLink(self.dc[0], self.h[0])
38 self.net.addLink(self.h[1], self.dc[0])
39 # add resource model
40 r = BaseResourceModel()
41 self.dc[0].assignResourceModel(r)
42 # start Mininet network
43 self.startNet()
44 # check number of running nodes
45 self.assertTrue(len(self.getContainernetContainers()) == 0)
46 self.assertTrue(len(self.net.hosts) == 2)
47 self.assertTrue(len(self.net.switches) == 1)
48 # check resource model and resource model registrar
49 self.assertTrue(self.dc[0]._resource_model is not None)
50 self.assertTrue(len(self.net.rm_registrar.resource_models) == 1)
51
52 # check if alloc was called during startCompute
53 self.assertTrue(len(r._allocated_compute_instances) == 0)
54 self.dc[0].startCompute("tc1")
55 time.sleep(1)
56 self.assertTrue(len(r._allocated_compute_instances) == 1)
57 # check if free was called during stopCompute
58 self.dc[0].stopCompute("tc1")
59 self.assertTrue(len(r._allocated_compute_instances) == 0)
60 # check connectivity by using ping
61 self.assertTrue(self.net.ping([self.h[0], self.h[1]]) <= 0.0)
62 # stop Mininet network
63 self.stopNet()
64
65
66 def createDummyContainerObject(name, flavor):
67
68 class DummyContainer(object):
69
70 def __init__(self):
71 self.cpu_period = -1
72 self.cpu_quota = -1
73 self.mem_limit = -1
74 self.memswap_limit = -1
75
76 def updateCpuLimit(self, cpu_period, cpu_quota):
77 self.cpu_period = cpu_period
78 self.cpu_quota = cpu_quota
79
80 def updateMemoryLimit(self, mem_limit):
81 self.mem_limit = mem_limit
82
83 d = DummyContainer()
84 d.name = name
85 d.flavor_name = flavor
86 return d
87
88
89
90
91 class testUpbSimpleCloudDcRM(SimpleTestTopology):
92 """
93 Test the UpbSimpleCloudDc resource model.
94 """
95
96 def testAllocationComputations(self):
97 """
98 Test the allocation procedures and correct calculations.
99 :return:
100 """
101 # config
102 E_CPU = 1.0
103 MAX_CU = 100
104 E_MEM = 512
105 MAX_MU = 2048
106 # create dummy resource model environment
107 reg = ResourceModelRegistrar(dc_emulation_max_cpu=E_CPU, dc_emulation_max_mem=E_MEM)
108 rm = UpbSimpleCloudDcRM(max_cu=MAX_CU, max_mu=MAX_MU)
109 reg.register("test_dc", rm)
110
111 c1 = createDummyContainerObject("c1", flavor="tiny")
112 rm.allocate(c1) # calculate allocation
113 self.assertEqual(float(c1.cpu_quota) / c1.cpu_period, E_CPU / MAX_CU * 0.5) # validate compute result
114 self.assertEqual(float(c1.mem_limit/1024/1024), float(E_MEM) / MAX_MU * 32) # validate memory result
115
116 c2 = createDummyContainerObject("c2", flavor="small")
117 rm.allocate(c2) # calculate allocation
118 self.assertEqual(float(c2.cpu_quota) / c2.cpu_period, E_CPU / MAX_CU * 1) # validate compute result
119 self.assertEqual(float(c2.mem_limit/1024/1024), float(E_MEM) / MAX_MU * 128) # validate memory result
120
121 c3 = createDummyContainerObject("c3", flavor="medium")
122 rm.allocate(c3) # calculate allocation
123 self.assertEqual(float(c3.cpu_quota) / c3.cpu_period, E_CPU / MAX_CU * 4) # validate compute result
124 self.assertEqual(float(c3.mem_limit/1024/1024), float(E_MEM) / MAX_MU * 256) # validate memory result
125
126 c4 = createDummyContainerObject("c4", flavor="large")
127 rm.allocate(c4) # calculate allocation
128 self.assertEqual(float(c4.cpu_quota) / c4.cpu_period, E_CPU / MAX_CU * 8) # validate compute result
129 self.assertEqual(float(c4.mem_limit/1024/1024), float(E_MEM) / MAX_MU * 512) # validate memory result
130
131 c5 = createDummyContainerObject("c5", flavor="xlarge")
132 rm.allocate(c5) # calculate allocation
133 self.assertEqual(float(c5.cpu_quota) / c5.cpu_period, E_CPU / MAX_CU * 16) # validate compute result
134 self.assertEqual(float(c5.mem_limit/1024/1024), float(E_MEM) / MAX_MU * 1024) # validate memory result
135
136
137 def testAllocationCpuLimit(self):
138 """
139 Test CPU allocation limit
140 :return:
141 """
142 # config
143 E_CPU = 1.0
144 MAX_CU = 40
145 E_MEM = 512
146 MAX_MU = 4096
147 # create dummy resource model environment
148 reg = ResourceModelRegistrar(dc_emulation_max_cpu=E_CPU, dc_emulation_max_mem=E_MEM)
149 rm = UpbSimpleCloudDcRM(max_cu=MAX_CU, max_mu=MAX_MU)
150 reg.register("test_dc", rm)
151
152 # test over provisioning exeption
153 exception = False
154 try:
155 c6 = createDummyContainerObject("c6", flavor="xlarge")
156 c7 = createDummyContainerObject("c7", flavor="xlarge")
157 c8 = createDummyContainerObject("c8", flavor="xlarge")
158 c9 = createDummyContainerObject("c9", flavor="xlarge")
159 rm.allocate(c6) # calculate allocation
160 rm.allocate(c7) # calculate allocation
161 rm.allocate(c8) # calculate allocation
162 rm.allocate(c9) # calculate allocation
163 except NotEnoughResourcesAvailable as e:
164 self.assertIn("Not enough compute", e.message)
165 exception = True
166 self.assertTrue(exception)
167
168 def testAllocationMemLimit(self):
169 """
170 Test MEM allocation limit
171 :return:
172 """
173 # config
174 E_CPU = 1.0
175 MAX_CU = 500
176 E_MEM = 512
177 MAX_MU = 2048
178 # create dummy resource model environment
179 reg = ResourceModelRegistrar(dc_emulation_max_cpu=E_CPU, dc_emulation_max_mem=E_MEM)
180 rm = UpbSimpleCloudDcRM(max_cu=MAX_CU, max_mu=MAX_MU)
181 reg.register("test_dc", rm)
182
183 # test over provisioning exeption
184 exception = False
185 try:
186 c6 = createDummyContainerObject("c6", flavor="xlarge")
187 c7 = createDummyContainerObject("c7", flavor="xlarge")
188 c8 = createDummyContainerObject("c8", flavor="xlarge")
189 rm.allocate(c6) # calculate allocation
190 rm.allocate(c7) # calculate allocation
191 rm.allocate(c8) # calculate allocation
192 except NotEnoughResourcesAvailable as e:
193 self.assertIn("Not enough memory", e.message)
194 exception = True
195 self.assertTrue(exception)
196
197 def testFree(self):
198 """
199 Test the free procedure.
200 :return:
201 """
202 # config
203 E_CPU = 1.0
204 MAX_CU = 100
205 # create dummy resource model environment
206 reg = ResourceModelRegistrar(dc_emulation_max_cpu=1.0, dc_emulation_max_mem=512)
207 rm = UpbSimpleCloudDcRM(max_cu=100, max_mu=100)
208 reg.register("test_dc", rm)
209 c1 = createDummyContainerObject("c6", flavor="tiny")
210 rm.allocate(c1) # calculate allocation
211 self.assertTrue(rm.dc_alloc_cu == 0.5)
212 rm.free(c1)
213 self.assertTrue(rm.dc_alloc_cu == 0)
214
215 @unittest.skipIf(os.environ.get("SON_EMU_IN_DOCKER") is not None,
216 "skipping test when running inside Docker container")
217 def testInRealTopo(self):
218 """
219 Start a real container and check if limitations are really passed down to Conteinernet.
220 :return:
221 """
222 # create network
223 self.createNet(nswitches=0, ndatacenter=1, nhosts=2, ndockers=0)
224 # setup links
225 self.net.addLink(self.dc[0], self.h[0])
226 self.net.addLink(self.h[1], self.dc[0])
227 # add resource model
228 r = UpbSimpleCloudDcRM(max_cu=100, max_mu=100)
229 self.dc[0].assignResourceModel(r)
230 # start Mininet network
231 self.startNet()
232 # check number of running nodes
233 self.assertTrue(len(self.getContainernetContainers()) == 0)
234 self.assertTrue(len(self.net.hosts) == 2)
235 self.assertTrue(len(self.net.switches) == 1)
236 # check resource model and resource model registrar
237 self.assertTrue(self.dc[0]._resource_model is not None)
238 self.assertTrue(len(self.net.rm_registrar.resource_models) == 1)
239
240 # check if alloc was called during startCompute
241 self.assertTrue(len(r._allocated_compute_instances) == 0)
242 tc1 = self.dc[0].startCompute("tc1", flavor_name="tiny")
243 time.sleep(1)
244 self.assertTrue(len(r._allocated_compute_instances) == 1)
245
246 # check if there is a real limitation set for containers cgroup
247 # deactivated for now, seems not to work in docker-in-docker setup used in CI
248 self.assertEqual(float(tc1.cpu_quota)/tc1.cpu_period, 0.005)
249
250 # check if free was called during stopCompute
251 self.dc[0].stopCompute("tc1")
252 self.assertTrue(len(r._allocated_compute_instances) == 0)
253 # check connectivity by using ping
254 self.assertTrue(self.net.ping([self.h[0], self.h[1]]) <= 0.0)
255 # stop Mininet network
256 self.stopNet()
257
258
259 class testUpbOverprovisioningCloudDcRM(SimpleTestTopology):
260 """
261 Test the UpbOverprovisioningCloudDc resource model.
262 """
263
264 def testAllocationComputations(self):
265 """
266 Test the allocation procedures and correct calculations.
267 :return:
268 """
269 # config
270 E_CPU = 1.0
271 MAX_CU = 3
272 E_MEM = 512
273 MAX_MU = 2048
274 # create dummy resource model environment
275 reg = ResourceModelRegistrar(dc_emulation_max_cpu=E_CPU, dc_emulation_max_mem=E_MEM)
276 rm = UpbOverprovisioningCloudDcRM(max_cu=MAX_CU, max_mu=MAX_MU)
277 reg.register("test_dc", rm)
278
279 c1 = createDummyContainerObject("c1", flavor="small")
280 rm.allocate(c1) # calculate allocation
281 self.assertAlmostEqual(float(c1.cpu_quota) / c1.cpu_period, E_CPU / MAX_CU * 1.0, places=5)
282 self.assertAlmostEqual(float(c1.mem_limit/1024/1024), float(E_MEM) / MAX_MU * 128)
283 self.assertAlmostEqual(rm.cpu_op_factor, 1.0)
284
285 c2 = createDummyContainerObject("c2", flavor="small")
286 rm.allocate(c2) # calculate allocation
287 self.assertAlmostEqual(float(c2.cpu_quota) / c2.cpu_period, E_CPU / MAX_CU * 1.0, places=5)
288 self.assertAlmostEqual(float(c2.mem_limit/1024/1024), float(E_MEM) / MAX_MU * 128)
289 self.assertAlmostEqual(rm.cpu_op_factor, 1.0)
290
291 c3 = createDummyContainerObject("c3", flavor="small")
292 rm.allocate(c3) # calculate allocation
293 self.assertAlmostEqual(float(c3.cpu_quota) / c3.cpu_period, E_CPU / MAX_CU * 1.0, places=5)
294 self.assertAlmostEqual(float(c3.mem_limit/1024/1024), float(E_MEM) / MAX_MU * 128)
295 self.assertAlmostEqual(rm.cpu_op_factor, 1.0)
296
297 # from this container onwards, we should go to over provisioning mode:
298 c4 = createDummyContainerObject("c4", flavor="small")
299 rm.allocate(c4) # calculate allocation
300 self.assertAlmostEqual(float(c4.cpu_quota) / c4.cpu_period, E_CPU / MAX_CU * (float(3) / 4), places=5)
301 self.assertAlmostEqual(float(c4.mem_limit/1024/1024), float(E_MEM) / MAX_MU * 128, places=5)
302 self.assertAlmostEqual(rm.cpu_op_factor, 0.75)
303
304 c5 = createDummyContainerObject("c5", flavor="small")
305 rm.allocate(c5) # calculate allocation
306 self.assertAlmostEqual(float(c5.cpu_quota) / c5.cpu_period, E_CPU / MAX_CU * (float(3) / 5), places=5)
307 self.assertAlmostEqual(float(c5.mem_limit/1024/1024), float(E_MEM) / MAX_MU * 128)
308 self.assertAlmostEqual(rm.cpu_op_factor, 0.6)
309
310
311 class testUpbDummyRM(SimpleTestTopology):
312 """
313 Test the UpbDummyRM resource model.
314 """
315
316 def testAllocationComputations(self):
317 """
318 Test the allocation procedures and correct calculations.
319 :return:
320 """
321 # config
322 E_CPU = 1.0
323 MAX_CU = 3
324 E_MEM = 512
325 MAX_MU = 2048
326 # create dummy resource model environment
327 reg = ResourceModelRegistrar(dc_emulation_max_cpu=E_CPU, dc_emulation_max_mem=E_MEM)
328 rm = UpbDummyRM(max_cu=MAX_CU, max_mu=MAX_MU)
329 reg.register("test_dc", rm)
330
331 c1 = createDummyContainerObject("c1", flavor="small")
332 rm.allocate(c1) # calculate allocation
333 self.assertEqual(len(rm._allocated_compute_instances), 1)
334
335 c2 = createDummyContainerObject("c2", flavor="small")
336 rm.allocate(c2) # calculate allocation
337 self.assertEqual(len(rm._allocated_compute_instances), 2)
338