Merge pull request #65 from mpeuster/master
[osm/vim-emu.git] / src / emuvim / test / test_resourcemodel_api.py
index 2ec31a0..3412ec4 100644 (file)
@@ -1,13 +1,24 @@
+import time
 from emuvim.test.base import SimpleTestTopology
-from emuvim.dcemulator.resourcemodel import BaseResourceModel
+from emuvim.dcemulator.resourcemodel import BaseResourceModel, ResourceFlavor
+from emuvim.dcemulator.resourcemodel.upb.simple import UpbSimpleCloudDcRM
+from emuvim.dcemulator.resourcemodel import ResourceModelRegistrar
 
 
 class testResourceModel(SimpleTestTopology):
 
     def testBaseResourceModelApi(self):
-        pass
-        # TODO test add flavor etc.
-        # TODO test aaloc / free
+        r = BaseResourceModel()
+        # check if default flavors are there
+        assert(len(r._flavors) == 5)
+        # check addFlavor functionality
+        f = ResourceFlavor("test", {"testmetric": 42})
+        r.addFlavour(f)
+        assert("test" in r._flavors)
+        assert(r._flavors.get("test").get("testmetric") == 42)
+        # test if allocate and free runs through
+        assert(len(r.allocate("testc", "tiny")) == 3)  # expected: 3tuple
+        assert(r.free("testc"))
 
     def testAddRmToDc(self):
         # create network
@@ -26,10 +37,84 @@ class testResourceModel(SimpleTestTopology):
         assert(len(self.net.switches) == 1)
         # check resource model and resource model registrar
         assert(self.dc[0]._resource_model is not None)
-        assert(self.net.rm_registrar.num_models == 1)
-        # TODO test if alloc was called on start
-        # TODO if free was called on stop
+        assert(len(self.net.rm_registrar.resource_models) == 1)
+
+        # check if alloc was called during startCompute
+        assert(len(r.allocated_compute_instances) == 0)
+        self.dc[0].startCompute("tc1")
+        time.sleep(1)
+        assert(len(r.allocated_compute_instances) == 1)
+        # check if free was called during stopCompute
+        self.dc[0].stopCompute("tc1")
+        assert(len(r.allocated_compute_instances) == 0)
         # check connectivity by using ping
         assert(self.net.ping([self.h[0], self.h[1]]) <= 0.0)
         # stop Mininet network
         self.stopNet()
+
+
+class testUpbSimpleCloudDcRM(SimpleTestTopology):
+    """
+    Test the UpbSimpleCloudDc resource model.
+    """
+    def testAllocation(self):
+        # config
+        E_CPU = 1.0
+        MAX_CU = 100
+        # create dummy resource model environment
+        reg = ResourceModelRegistrar(dc_emulation_max_cpu=1.0)
+        rm = UpbSimpleCloudDcRM(max_cu=100, max_mu=100)
+        reg.register("test_dc", rm)
+
+        res = rm.allocate("c1", "tiny")  # calculate allocation
+        assert(res[0] == E_CPU / MAX_CU * 1)   # validate compute result
+        assert(res[1] < 0)   # validate memory result
+        assert(res[2] < 0)   # validate disk result
+
+        res = rm.allocate("c2", "small")  # calculate allocation
+        assert(res[0] == E_CPU / MAX_CU * 4)   # validate compute result
+        assert(res[1] < 0)   # validate memory result
+        assert(res[2] < 0)   # validate disk result
+
+        res = rm.allocate("c3", "medium")  # calculate allocation
+        assert(res[0] == E_CPU / MAX_CU * 8)   # validate compute result
+        assert(res[1] < 0)   # validate memory result
+        assert(res[2] < 0)   # validate disk result
+
+        res = rm.allocate("c4", "large")  # calculate allocation
+        assert(res[0] == E_CPU / MAX_CU * 16)   # validate compute result
+        assert(res[1] < 0)   # validate memory result
+        assert(res[2] < 0)   # validate disk result
+
+        res = rm.allocate("c5", "xlarge")  # calculate allocation
+        assert(res[0] == E_CPU / MAX_CU * 32)   # validate compute result
+        assert(res[1] < 0)   # validate memory result
+        assert(res[2] < 0)   # validate disk result
+
+        # test over provisioning exeption
+        exception = False
+        try:
+            rm.allocate("c6", "xlarge")  # calculate allocation
+            rm.allocate("c7", "xlarge")  # calculate allocation
+            rm.allocate("c8", "xlarge")  # calculate allocation
+            rm.allocate("c9", "xlarge")  # calculate allocation
+        except Exception as e:
+            assert("Not enough compute" in e.message)
+            exception = True
+        assert(exception)
+
+    def testFree(self):
+        # config
+        E_CPU = 1.0
+        MAX_CU = 100
+        # create dummy resource model environment
+        reg = ResourceModelRegistrar(dc_emulation_max_cpu=1.0)
+        rm = UpbSimpleCloudDcRM(max_cu=100, max_mu=100)
+        reg.register("test_dc", rm)
+        rm.allocate("c1", "tiny")  # calculate allocation
+        assert(rm.dc_alloc_cu == 1)
+        rm.free("c1")
+        assert(rm.dc_alloc_cu == 0)
+
+
+