Added memory model to UPB simple resource model
[osm/vim-emu.git] / src / emuvim / test / test_resourcemodel_api.py
1 import time
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
6
7
8 class testResourceModel(SimpleTestTopology):
9 """
10 Test the general resource model API and functionality.
11 """
12
13 def testBaseResourceModelApi(self):
14 """
15 Tast bare API without real resource madel.
16 :return:
17 """
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})
23 r.addFlavour(f)
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"))
29
30 def testAddRmToDc(self):
31 """
32 Test is allocate/free is called when a RM is added to a DC.
33 :return:
34 """
35 # create network
36 self.createNet(nswitches=0, ndatacenter=1, nhosts=2, ndockers=0)
37 # setup links
38 self.net.addLink(self.dc[0], self.h[0])
39 self.net.addLink(self.h[1], self.dc[0])
40 # add resource model
41 r = BaseResourceModel()
42 self.dc[0].assignResourceModel(r)
43 # start Mininet network
44 self.startNet()
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)
52
53 # check if alloc was called during startCompute
54 self.assertTrue(len(r.allocated_compute_instances) == 0)
55 self.dc[0].startCompute("tc1")
56 time.sleep(1)
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
64 self.stopNet()
65
66
67 class testUpbSimpleCloudDcRM(SimpleTestTopology):
68 """
69 Test the UpbSimpleCloudDc resource model.
70 """
71
72 def testAllocation(self):
73 """
74 Test the allocation procedures and correct calculations.
75 :return:
76 """
77 # config
78 E_CPU = 1.0
79 MAX_CU = 100
80 E_MEM = 512
81 MAX_MU = 2048
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)
86
87 res = rm.allocate("c1", "tiny") # calculate allocation
88 self.assertEqual(res[0], E_CPU / MAX_CU * 1) # 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
91
92 res = rm.allocate("c2", "small") # calculate allocation
93 self.assertEqual(res[0], E_CPU / MAX_CU * 4) # 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
96
97 res = rm.allocate("c3", "medium") # calculate allocation
98 self.assertEqual(res[0], E_CPU / MAX_CU * 8) # 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
101
102 res = rm.allocate("c4", "large") # calculate allocation
103 self.assertEqual(res[0], E_CPU / MAX_CU * 16) # 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
106
107 res = rm.allocate("c5", "xlarge") # calculate allocation
108 self.assertEqual(res[0], E_CPU / MAX_CU * 32) # 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
111
112 def testAllocationCpuLimit(self):
113 """
114 Test CPU allocation limit
115 :return:
116 """
117 # config
118 E_CPU = 1.0
119 MAX_CU = 100
120 E_MEM = 512
121 MAX_MU = 4096
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)
126
127 # test over provisioning exeption
128 exception = False
129 try:
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)
136 exception = True
137 self.assertTrue(exception)
138
139 def testAllocationMemLimit(self):
140 """
141 Test MEM allocation limit
142 :return:
143 """
144 # config
145 E_CPU = 1.0
146 MAX_CU = 500
147 E_MEM = 512
148 MAX_MU = 2048
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)
153
154 # test over provisioning exeption
155 exception = False
156 try:
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)
162 exception = True
163 self.assertTrue(exception)
164
165 def testFree(self):
166 """
167 Test the free procedure.
168 :return:
169 """
170 # config
171 E_CPU = 1.0
172 MAX_CU = 100
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 == 1)
179 rm.free("c1")
180 self.assertTrue(rm.dc_alloc_cu == 0)
181
182 def testInRealTopo(self):
183 """
184 Start a real container and check if limitations are really passed down to Dockernet.
185 :return:
186 """
187 # create network
188 self.createNet(nswitches=0, ndatacenter=1, nhosts=2, ndockers=0)
189 # setup links
190 self.net.addLink(self.dc[0], self.h[0])
191 self.net.addLink(self.h[1], self.dc[0])
192 # add resource model
193 r = UpbSimpleCloudDcRM(max_cu=100, max_mu=100)
194 self.dc[0].assignResourceModel(r)
195 # start Mininet network
196 self.startNet()
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)
204
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")
208 time.sleep(1)
209 self.assertTrue(len(r.allocated_compute_instances) == 1)
210
211 # check if there is a real limitation set for containers cgroup
212 self.assertEqual(tc1.cpu_period/tc1.cpu_quota, 100)
213
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
220 self.stopNet()
221
222
223