3 from emuvim
.test
.base
import SimpleTestTopology
4 from emuvim
.dcemulator
.resourcemodel
import BaseResourceModel
, ResourceFlavor
, NotEnoughResourcesAvailable
, ResourceModelRegistrar
5 from emuvim
.dcemulator
.resourcemodel
.upb
.simple
import UpbSimpleCloudDcRM
, UpbOverprovisioningCloudDcRM
, UpbDummyRM
9 class testResourceModel(SimpleTestTopology
):
11 Test the general resource model API and functionality.
14 def testBaseResourceModelApi(self
):
16 Tast bare API without real resource madel.
19 r
= BaseResourceModel()
20 # check if default flavors are there
21 self
.assertTrue(len(r
._flavors
) == 5)
22 # check addFlavor functionality
23 f
= ResourceFlavor("test", {"testmetric": 42})
25 self
.assertTrue("test" in r
._flavors
)
26 self
.assertTrue(r
._flavors
.get("test").get("testmetric") == 42)
28 def testAddRmToDc(self
):
30 Test is allocate/free is called when a RM is added to a DC.
34 self
.createNet(nswitches
=0, ndatacenter
=1, nhosts
=2, ndockers
=0)
36 self
.net
.addLink(self
.dc
[0], self
.h
[0])
37 self
.net
.addLink(self
.h
[1], self
.dc
[0])
39 r
= BaseResourceModel()
40 self
.dc
[0].assignResourceModel(r
)
41 # start Mininet network
43 # check number of running nodes
44 self
.assertTrue(len(self
.getDockernetContainers()) == 0)
45 self
.assertTrue(len(self
.net
.hosts
) == 2)
46 self
.assertTrue(len(self
.net
.switches
) == 1)
47 # check resource model and resource model registrar
48 self
.assertTrue(self
.dc
[0]._resource
_model
is not None)
49 self
.assertTrue(len(self
.net
.rm_registrar
.resource_models
) == 1)
51 # check if alloc was called during startCompute
52 self
.assertTrue(len(r
._allocated
_compute
_instances
) == 0)
53 self
.dc
[0].startCompute("tc1")
55 self
.assertTrue(len(r
._allocated
_compute
_instances
) == 1)
56 # check if free was called during stopCompute
57 self
.dc
[0].stopCompute("tc1")
58 self
.assertTrue(len(r
._allocated
_compute
_instances
) == 0)
59 # check connectivity by using ping
60 self
.assertTrue(self
.net
.ping([self
.h
[0], self
.h
[1]]) <= 0.0)
61 # stop Mininet network
65 def createDummyContainerObject(name
, flavor
):
67 class DummyContainer(object):
73 self
.memswap_limit
= -1
75 def updateCpuLimit(self
, cpu_period
, cpu_quota
):
76 self
.cpu_period
= cpu_period
77 self
.cpu_quota
= cpu_quota
79 def updateMemoryLimit(self
, mem_limit
):
80 self
.mem_limit
= mem_limit
84 d
.flavor_name
= flavor
90 class testUpbSimpleCloudDcRM(SimpleTestTopology
):
92 Test the UpbSimpleCloudDc resource model.
95 def testAllocationComputations(self
):
97 Test the allocation procedures and correct calculations.
105 # create dummy resource model environment
106 reg
= ResourceModelRegistrar(dc_emulation_max_cpu
=E_CPU
, dc_emulation_max_mem
=E_MEM
)
107 rm
= UpbSimpleCloudDcRM(max_cu
=MAX_CU
, max_mu
=MAX_MU
)
108 reg
.register("test_dc", rm
)
110 c1
= createDummyContainerObject("c1", flavor
="tiny")
111 rm
.allocate(c1
) # calculate allocation
112 self
.assertEqual(float(c1
.cpu_quota
) / c1
.cpu_period
, E_CPU
/ MAX_CU
* 0.5) # validate compute result
113 self
.assertEqual(float(c1
.mem_limit
/1024/1024), float(E_MEM
) / MAX_MU
* 32) # validate memory result
115 c2
= createDummyContainerObject("c2", flavor
="small")
116 rm
.allocate(c2
) # calculate allocation
117 self
.assertEqual(float(c2
.cpu_quota
) / c2
.cpu_period
, E_CPU
/ MAX_CU
* 1) # validate compute result
118 self
.assertEqual(float(c2
.mem_limit
/1024/1024), float(E_MEM
) / MAX_MU
* 128) # validate memory result
120 c3
= createDummyContainerObject("c3", flavor
="medium")
121 rm
.allocate(c3
) # calculate allocation
122 self
.assertEqual(float(c3
.cpu_quota
) / c3
.cpu_period
, E_CPU
/ MAX_CU
* 4) # validate compute result
123 self
.assertEqual(float(c3
.mem_limit
/1024/1024), float(E_MEM
) / MAX_MU
* 256) # validate memory result
125 c4
= createDummyContainerObject("c4", flavor
="large")
126 rm
.allocate(c4
) # calculate allocation
127 self
.assertEqual(float(c4
.cpu_quota
) / c4
.cpu_period
, E_CPU
/ MAX_CU
* 8) # validate compute result
128 self
.assertEqual(float(c4
.mem_limit
/1024/1024), float(E_MEM
) / MAX_MU
* 512) # validate memory result
130 c5
= createDummyContainerObject("c5", flavor
="xlarge")
131 rm
.allocate(c5
) # calculate allocation
132 self
.assertEqual(float(c5
.cpu_quota
) / c5
.cpu_period
, E_CPU
/ MAX_CU
* 16) # validate compute result
133 self
.assertEqual(float(c5
.mem_limit
/1024/1024), float(E_MEM
) / MAX_MU
* 1024) # validate memory result
136 def testAllocationCpuLimit(self
):
138 Test CPU allocation limit
146 # create dummy resource model environment
147 reg
= ResourceModelRegistrar(dc_emulation_max_cpu
=E_CPU
, dc_emulation_max_mem
=E_MEM
)
148 rm
= UpbSimpleCloudDcRM(max_cu
=MAX_CU
, max_mu
=MAX_MU
)
149 reg
.register("test_dc", rm
)
151 # test over provisioning exeption
154 c6
= createDummyContainerObject("c6", flavor
="xlarge")
155 c7
= createDummyContainerObject("c7", flavor
="xlarge")
156 c8
= createDummyContainerObject("c8", flavor
="xlarge")
157 c9
= createDummyContainerObject("c9", flavor
="xlarge")
158 rm
.allocate(c6
) # calculate allocation
159 rm
.allocate(c7
) # calculate allocation
160 rm
.allocate(c8
) # calculate allocation
161 rm
.allocate(c9
) # calculate allocation
162 except NotEnoughResourcesAvailable
as e
:
163 self
.assertIn("Not enough compute", e
.message
)
165 self
.assertTrue(exception
)
167 def testAllocationMemLimit(self
):
169 Test MEM allocation limit
177 # create dummy resource model environment
178 reg
= ResourceModelRegistrar(dc_emulation_max_cpu
=E_CPU
, dc_emulation_max_mem
=E_MEM
)
179 rm
= UpbSimpleCloudDcRM(max_cu
=MAX_CU
, max_mu
=MAX_MU
)
180 reg
.register("test_dc", rm
)
182 # test over provisioning exeption
185 c6
= createDummyContainerObject("c6", flavor
="xlarge")
186 c7
= createDummyContainerObject("c7", flavor
="xlarge")
187 c8
= createDummyContainerObject("c8", flavor
="xlarge")
188 rm
.allocate(c6
) # calculate allocation
189 rm
.allocate(c7
) # calculate allocation
190 rm
.allocate(c8
) # calculate allocation
191 except NotEnoughResourcesAvailable
as e
:
192 self
.assertIn("Not enough memory", e
.message
)
194 self
.assertTrue(exception
)
198 Test the free procedure.
204 # create dummy resource model environment
205 reg
= ResourceModelRegistrar(dc_emulation_max_cpu
=1.0, dc_emulation_max_mem
=512)
206 rm
= UpbSimpleCloudDcRM(max_cu
=100, max_mu
=100)
207 reg
.register("test_dc", rm
)
208 c1
= createDummyContainerObject("c6", flavor
="tiny")
209 rm
.allocate(c1
) # calculate allocation
210 self
.assertTrue(rm
.dc_alloc_cu
== 0.5)
212 self
.assertTrue(rm
.dc_alloc_cu
== 0)
214 def testInRealTopo(self
):
216 Start a real container and check if limitations are really passed down to Dockernet.
219 # ATTENTION: This test should only be executed if emu runs not inside a Docker container,
220 # because it manipulates cgroups.
221 if os
.environ
.get("SON_EMU_IN_DOCKER") is not None:
224 self
.createNet(nswitches
=0, ndatacenter
=1, nhosts
=2, ndockers
=0)
226 self
.net
.addLink(self
.dc
[0], self
.h
[0])
227 self
.net
.addLink(self
.h
[1], self
.dc
[0])
229 r
= UpbSimpleCloudDcRM(max_cu
=100, max_mu
=100)
230 self
.dc
[0].assignResourceModel(r
)
231 # start Mininet network
233 # check number of running nodes
234 self
.assertTrue(len(self
.getDockernetContainers()) == 0)
235 self
.assertTrue(len(self
.net
.hosts
) == 2)
236 self
.assertTrue(len(self
.net
.switches
) == 1)
237 # check resource model and resource model registrar
238 self
.assertTrue(self
.dc
[0]._resource
_model
is not None)
239 self
.assertTrue(len(self
.net
.rm_registrar
.resource_models
) == 1)
241 # check if alloc was called during startCompute
242 self
.assertTrue(len(r
._allocated
_compute
_instances
) == 0)
243 tc1
= self
.dc
[0].startCompute("tc1", flavor_name
="tiny")
245 self
.assertTrue(len(r
._allocated
_compute
_instances
) == 1)
247 # check if there is a real limitation set for containers cgroup
248 # deactivated for now, seems not to work in docker-in-docker setup used in CI
249 self
.assertEqual(float(tc1
.cpu_quota
)/tc1
.cpu_period
, 0.005)
251 # check if free was called during stopCompute
252 self
.dc
[0].stopCompute("tc1")
253 self
.assertTrue(len(r
._allocated
_compute
_instances
) == 0)
254 # check connectivity by using ping
255 self
.assertTrue(self
.net
.ping([self
.h
[0], self
.h
[1]]) <= 0.0)
256 # stop Mininet network
260 class testUpbOverprovisioningCloudDcRM(SimpleTestTopology
):
262 Test the UpbOverprovisioningCloudDc resource model.
265 def testAllocationComputations(self
):
267 Test the allocation procedures and correct calculations.
275 # create dummy resource model environment
276 reg
= ResourceModelRegistrar(dc_emulation_max_cpu
=E_CPU
, dc_emulation_max_mem
=E_MEM
)
277 rm
= UpbOverprovisioningCloudDcRM(max_cu
=MAX_CU
, max_mu
=MAX_MU
)
278 reg
.register("test_dc", rm
)
280 c1
= createDummyContainerObject("c1", flavor
="small")
281 rm
.allocate(c1
) # calculate allocation
282 self
.assertAlmostEqual(float(c1
.cpu_quota
) / c1
.cpu_period
, E_CPU
/ MAX_CU
* 1.0, places
=5)
283 self
.assertAlmostEqual(float(c1
.mem_limit
/1024/1024), float(E_MEM
) / MAX_MU
* 128)
284 self
.assertAlmostEqual(rm
.cpu_op_factor
, 1.0)
286 c2
= createDummyContainerObject("c2", flavor
="small")
287 rm
.allocate(c2
) # calculate allocation
288 self
.assertAlmostEqual(float(c2
.cpu_quota
) / c2
.cpu_period
, E_CPU
/ MAX_CU
* 1.0, places
=5)
289 self
.assertAlmostEqual(float(c2
.mem_limit
/1024/1024), float(E_MEM
) / MAX_MU
* 128)
290 self
.assertAlmostEqual(rm
.cpu_op_factor
, 1.0)
292 c3
= createDummyContainerObject("c3", flavor
="small")
293 rm
.allocate(c3
) # calculate allocation
294 self
.assertAlmostEqual(float(c3
.cpu_quota
) / c3
.cpu_period
, E_CPU
/ MAX_CU
* 1.0, places
=5)
295 self
.assertAlmostEqual(float(c3
.mem_limit
/1024/1024), float(E_MEM
) / MAX_MU
* 128)
296 self
.assertAlmostEqual(rm
.cpu_op_factor
, 1.0)
298 # from this container onwards, we should go to over provisioning mode:
299 c4
= createDummyContainerObject("c4", flavor
="small")
300 rm
.allocate(c4
) # calculate allocation
301 self
.assertAlmostEqual(float(c4
.cpu_quota
) / c4
.cpu_period
, E_CPU
/ MAX_CU
* (float(3) / 4), places
=5)
302 self
.assertAlmostEqual(float(c4
.mem_limit
/1024/1024), float(E_MEM
) / MAX_MU
* 128, places
=5)
303 self
.assertAlmostEqual(rm
.cpu_op_factor
, 0.75)
305 c5
= createDummyContainerObject("c5", flavor
="small")
306 rm
.allocate(c5
) # calculate allocation
307 self
.assertAlmostEqual(float(c5
.cpu_quota
) / c5
.cpu_period
, E_CPU
/ MAX_CU
* (float(3) / 5), places
=5)
308 self
.assertAlmostEqual(float(c5
.mem_limit
/1024/1024), float(E_MEM
) / MAX_MU
* 128)
309 self
.assertAlmostEqual(rm
.cpu_op_factor
, 0.6)
312 class testUpbDummyRM(SimpleTestTopology
):
314 Test the UpbDummyRM resource model.
317 def testAllocationComputations(self
):
319 Test the allocation procedures and correct calculations.
327 # create dummy resource model environment
328 reg
= ResourceModelRegistrar(dc_emulation_max_cpu
=E_CPU
, dc_emulation_max_mem
=E_MEM
)
329 rm
= UpbDummyRM(max_cu
=MAX_CU
, max_mu
=MAX_MU
)
330 reg
.register("test_dc", rm
)
332 c1
= createDummyContainerObject("c1", flavor
="small")
333 rm
.allocate(c1
) # calculate allocation
334 self
.assertEqual(len(rm
._allocated
_compute
_instances
), 1)
336 c2
= createDummyContainerObject("c2", flavor
="small")
337 rm
.allocate(c2
) # calculate allocation
338 self
.assertEqual(len(rm
._allocated
_compute
_instances
), 2)