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
10 class testResourceModel(SimpleTestTopology
):
12 Test the general resource model API and functionality.
15 def testBaseResourceModelApi(self
):
17 Tast bare API without real resource madel.
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})
26 self
.assertTrue("test" in r
._flavors
)
27 self
.assertTrue(r
._flavors
.get("test").get("testmetric") == 42)
29 def testAddRmToDc(self
):
31 Test is allocate/free is called when a RM is added to a DC.
35 self
.createNet(nswitches
=0, ndatacenter
=1, nhosts
=2, ndockers
=0)
37 self
.net
.addLink(self
.dc
[0], self
.h
[0])
38 self
.net
.addLink(self
.h
[1], self
.dc
[0])
40 r
= BaseResourceModel()
41 self
.dc
[0].assignResourceModel(r
)
42 # start Mininet network
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)
52 # check if alloc was called during startCompute
53 self
.assertTrue(len(r
._allocated
_compute
_instances
) == 0)
54 self
.dc
[0].startCompute("tc1")
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
66 def createDummyContainerObject(name
, flavor
):
68 class DummyContainer(object):
74 self
.memswap_limit
= -1
76 def updateCpuLimit(self
, cpu_period
, cpu_quota
):
77 self
.cpu_period
= cpu_period
78 self
.cpu_quota
= cpu_quota
80 def updateMemoryLimit(self
, mem_limit
):
81 self
.mem_limit
= mem_limit
85 d
.flavor_name
= flavor
91 class testUpbSimpleCloudDcRM(SimpleTestTopology
):
93 Test the UpbSimpleCloudDc resource model.
96 def testAllocationComputations(self
):
98 Test the allocation procedures and correct calculations.
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
)
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
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
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
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
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
137 def testAllocationCpuLimit(self
):
139 Test CPU allocation limit
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
)
152 # test over provisioning exeption
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
)
166 self
.assertTrue(exception
)
168 def testAllocationMemLimit(self
):
170 Test MEM allocation limit
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
)
183 # test over provisioning exeption
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
)
195 self
.assertTrue(exception
)
199 Test the free procedure.
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)
213 self
.assertTrue(rm
.dc_alloc_cu
== 0)
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
):
219 Start a real container and check if limitations are really passed down to Conteinernet.
223 self
.createNet(nswitches
=0, ndatacenter
=1, nhosts
=2, ndockers
=0)
225 self
.net
.addLink(self
.dc
[0], self
.h
[0])
226 self
.net
.addLink(self
.h
[1], self
.dc
[0])
228 r
= UpbSimpleCloudDcRM(max_cu
=100, max_mu
=100)
229 self
.dc
[0].assignResourceModel(r
)
230 # start Mininet network
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)
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")
244 self
.assertTrue(len(r
._allocated
_compute
_instances
) == 1)
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)
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
259 class testUpbOverprovisioningCloudDcRM(SimpleTestTopology
):
261 Test the UpbOverprovisioningCloudDc resource model.
264 def testAllocationComputations(self
):
266 Test the allocation procedures and correct calculations.
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
)
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)
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)
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)
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)
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)
311 class testUpbDummyRM(SimpleTestTopology
):
313 Test the UpbDummyRM resource model.
316 def testAllocationComputations(self
):
318 Test the allocation procedures and correct calculations.
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
)
331 c1
= createDummyContainerObject("c1", flavor
="small")
332 rm
.allocate(c1
) # calculate allocation
333 self
.assertEqual(len(rm
._allocated
_compute
_instances
), 1)
335 c2
= createDummyContainerObject("c2", flavor
="small")
336 rm
.allocate(c2
) # calculate allocation
337 self
.assertEqual(len(rm
._allocated
_compute
_instances
), 2)