2 from emuvim
.test
.base
import SimpleTestTopology
3 from emuvim
.dcemulator
.resourcemodel
import BaseResourceModel
, ResourceFlavor
4 from emuvim
.dcemulator
.resourcemodel
.upb
.simple
import UpbSimpleCloudDcRM
5 from emuvim
.dcemulator
.resourcemodel
import ResourceModelRegistrar
8 class testResourceModel(SimpleTestTopology
):
10 Test the general resource model API and functionality.
13 def testBaseResourceModelApi(self
):
15 Tast bare API without real resource madel.
18 r
= BaseResourceModel()
19 # check if default flavors are there
20 self
.assertTrue(len(r
._flavors
) == 5)
21 # check addFlavor functionality
22 f
= ResourceFlavor("test", {"testmetric": 42})
24 self
.assertTrue("test" in r
._flavors
)
25 self
.assertTrue(r
._flavors
.get("test").get("testmetric") == 42)
26 # test if allocate and free runs through
27 self
.assertTrue(len(r
.allocate("testc", "tiny")) == 3) # expected: 3tuple
28 self
.assertTrue(r
.free("testc"))
30 def testAddRmToDc(self
):
32 Test is allocate/free is called when a RM is added to a DC.
36 self
.createNet(nswitches
=0, ndatacenter
=1, nhosts
=2, ndockers
=0)
38 self
.net
.addLink(self
.dc
[0], self
.h
[0])
39 self
.net
.addLink(self
.h
[1], self
.dc
[0])
41 r
= BaseResourceModel()
42 self
.dc
[0].assignResourceModel(r
)
43 # start Mininet network
45 # check number of running nodes
46 self
.assertTrue(len(self
.getDockernetContainers()) == 0)
47 self
.assertTrue(len(self
.net
.hosts
) == 2)
48 self
.assertTrue(len(self
.net
.switches
) == 1)
49 # check resource model and resource model registrar
50 self
.assertTrue(self
.dc
[0]._resource
_model
is not None)
51 self
.assertTrue(len(self
.net
.rm_registrar
.resource_models
) == 1)
53 # check if alloc was called during startCompute
54 self
.assertTrue(len(r
.allocated_compute_instances
) == 0)
55 self
.dc
[0].startCompute("tc1")
57 self
.assertTrue(len(r
.allocated_compute_instances
) == 1)
58 # check if free was called during stopCompute
59 self
.dc
[0].stopCompute("tc1")
60 self
.assertTrue(len(r
.allocated_compute_instances
) == 0)
61 # check connectivity by using ping
62 self
.assertTrue(self
.net
.ping([self
.h
[0], self
.h
[1]]) <= 0.0)
63 # stop Mininet network
67 class testUpbSimpleCloudDcRM(SimpleTestTopology
):
69 Test the UpbSimpleCloudDc resource model.
72 def testAllocation(self
):
74 Test the allocation procedures and correct calculations.
82 # create dummy resource model environment
83 reg
= ResourceModelRegistrar(dc_emulation_max_cpu
=E_CPU
, dc_emulation_max_mem
=E_MEM
)
84 rm
= UpbSimpleCloudDcRM(max_cu
=MAX_CU
, max_mu
=MAX_MU
)
85 reg
.register("test_dc", rm
)
87 res
= rm
.allocate("c1", "tiny") # calculate allocation
88 self
.assertEqual(res
[0], E_CPU
/ MAX_CU
* 0.5) # validate compute result
89 self
.assertEqual(res
[1], float(E_MEM
) / MAX_MU
* 32) # validate memory result
90 self
.assertTrue(res
[2] < 0) # validate disk result
92 res
= rm
.allocate("c2", "small") # calculate allocation
93 self
.assertEqual(res
[0], E_CPU
/ MAX_CU
* 1) # validate compute result
94 self
.assertEqual(res
[1], float(E_MEM
) / MAX_MU
* 128) # validate memory result
95 self
.assertTrue(res
[2] < 0) # validate disk result
97 res
= rm
.allocate("c3", "medium") # calculate allocation
98 self
.assertEqual(res
[0], E_CPU
/ MAX_CU
* 4) # validate compute result
99 self
.assertEqual(res
[1], float(E_MEM
) / MAX_MU
* 256) # validate memory result
100 self
.assertTrue(res
[2] < 0) # validate disk result
102 res
= rm
.allocate("c4", "large") # calculate allocation
103 self
.assertEqual(res
[0], E_CPU
/ MAX_CU
* 8) # validate compute result
104 self
.assertEqual(res
[1], float(E_MEM
) / MAX_MU
* 512) # validate memory result
105 self
.assertTrue(res
[2] < 0) # validate disk result
107 res
= rm
.allocate("c5", "xlarge") # calculate allocation
108 self
.assertEqual(res
[0], E_CPU
/ MAX_CU
* 16) # validate compute result
109 self
.assertEqual(res
[1], float(E_MEM
) / MAX_MU
* 1024) # validate memory result
110 self
.assertTrue(res
[2] < 0) # validate disk result
112 def testAllocationCpuLimit(self
):
114 Test CPU allocation limit
122 # create dummy resource model environment
123 reg
= ResourceModelRegistrar(dc_emulation_max_cpu
=E_CPU
, dc_emulation_max_mem
=E_MEM
)
124 rm
= UpbSimpleCloudDcRM(max_cu
=MAX_CU
, max_mu
=MAX_MU
)
125 reg
.register("test_dc", rm
)
127 # test over provisioning exeption
130 rm
.allocate("c6", "xlarge") # calculate allocation
131 rm
.allocate("c7", "xlarge") # calculate allocation
132 rm
.allocate("c8", "xlarge") # calculate allocation
133 rm
.allocate("c9", "xlarge") # calculate allocation
134 except Exception as e
:
135 self
.assertIn("Not enough compute", e
.message
)
137 self
.assertTrue(exception
)
139 def testAllocationMemLimit(self
):
141 Test MEM allocation limit
149 # create dummy resource model environment
150 reg
= ResourceModelRegistrar(dc_emulation_max_cpu
=E_CPU
, dc_emulation_max_mem
=E_MEM
)
151 rm
= UpbSimpleCloudDcRM(max_cu
=MAX_CU
, max_mu
=MAX_MU
)
152 reg
.register("test_dc", rm
)
154 # test over provisioning exeption
157 rm
.allocate("c6", "xlarge") # calculate allocation
158 rm
.allocate("c7", "xlarge") # calculate allocation
159 rm
.allocate("c8", "xlarge") # calculate allocation
160 except Exception as e
:
161 self
.assertIn("Not enough memory", e
.message
)
163 self
.assertTrue(exception
)
167 Test the free procedure.
173 # create dummy resource model environment
174 reg
= ResourceModelRegistrar(dc_emulation_max_cpu
=1.0, dc_emulation_max_mem
=512)
175 rm
= UpbSimpleCloudDcRM(max_cu
=100, max_mu
=100)
176 reg
.register("test_dc", rm
)
177 rm
.allocate("c1", "tiny") # calculate allocation
178 self
.assertTrue(rm
.dc_alloc_cu
== 0.5)
180 self
.assertTrue(rm
.dc_alloc_cu
== 0)
182 def testInRealTopo(self
):
184 Start a real container and check if limitations are really passed down to Dockernet.
188 self
.createNet(nswitches
=0, ndatacenter
=1, nhosts
=2, ndockers
=0)
190 self
.net
.addLink(self
.dc
[0], self
.h
[0])
191 self
.net
.addLink(self
.h
[1], self
.dc
[0])
193 r
= UpbSimpleCloudDcRM(max_cu
=100, max_mu
=100)
194 self
.dc
[0].assignResourceModel(r
)
195 # start Mininet network
197 # check number of running nodes
198 self
.assertTrue(len(self
.getDockernetContainers()) == 0)
199 self
.assertTrue(len(self
.net
.hosts
) == 2)
200 self
.assertTrue(len(self
.net
.switches
) == 1)
201 # check resource model and resource model registrar
202 self
.assertTrue(self
.dc
[0]._resource
_model
is not None)
203 self
.assertTrue(len(self
.net
.rm_registrar
.resource_models
) == 1)
205 # check if alloc was called during startCompute
206 self
.assertTrue(len(r
.allocated_compute_instances
) == 0)
207 tc1
= self
.dc
[0].startCompute("tc1", flavor_name
="tiny")
209 self
.assertTrue(len(r
.allocated_compute_instances
) == 1)
211 # check if there is a real limitation set for containers cgroup
212 self
.assertEqual(tc1
.cpu_period
/tc1
.cpu_quota
, 100)
214 # check if free was called during stopCompute
215 self
.dc
[0].stopCompute("tc1")
216 self
.assertTrue(len(r
.allocated_compute_instances
) == 0)
217 # check connectivity by using ping
218 self
.assertTrue(self
.net
.ping([self
.h
[0], self
.h
[1]]) <= 0.0)
219 # stop Mininet network