Further tests for resource model api
authorpeusterm <manuel.peuster@uni-paderborn.de>
Mon, 14 Mar 2016 10:18:37 +0000 (11:18 +0100)
committerpeusterm <manuel.peuster@uni-paderborn.de>
Mon, 14 Mar 2016 10:18:37 +0000 (11:18 +0100)
src/emuvim/dcemulator/resourcemodel/__init__.py
src/emuvim/test/test_resourcemodel_api.py

index 6432361..52a35e5 100644 (file)
@@ -8,6 +8,9 @@ LOG.setLevel(logging.DEBUG)
 
 
 class ResourceModelRegistrar(object):
+    """
+    Global registry to keep track of all existing resource models.
+    """
 
     def __init__(self, dc_emulation_max_cpu):
         self.e_cpu = dc_emulation_max_cpu
@@ -16,16 +19,18 @@ class ResourceModelRegistrar(object):
         LOG.info("Resource model registrar created with dc_emulation_max_cpu=%r" % dc_emulation_max_cpu)
 
     def register(self, dc, rm):
+        """
+        Register a new resource model.
+        :param dc: Data center to which it is assigned.
+        :param rm: The resource model object.
+        :return: None
+        """
         if dc in self._resource_models:
             raise Exception("There is already an resource model assigned to this DC.")
         self._resource_models[dc] = rm
         rm.registrar = self
         LOG.info("Registrar: Added resource model: %r" % rm)
 
-    @property
-    def num_models(self):
-        return len(self._resource_models)
-
     @property
     def resource_models(self):
         return list(self._resource_models.itervalues())
@@ -52,16 +57,19 @@ class BaseResourceModel(object):
     """
 
     def __init__(self):
-        self._flavors=dict()
+        self._flavors = dict()
         self._initDefaultFlavors()
         self.registrar = None  # pointer to registrar
+        self.allocated_compute_instances = dict()
         LOG.info("Resource model %r initialized" % self)
 
     def __repr__(self):
         return self.__class__.__name__
 
     def _initDefaultFlavors(self):
-        # initialize some default flavours (inspired by OpenStack)
+        """
+        initialize some default flavours (naming/sizes inspired by OpenStack)
+        """
         self.addFlavour(ResourceFlavor(
             "tiny",  {"compute": 1, "memory": 32, "disk": 1}))
         self.addFlavour(ResourceFlavor(
@@ -91,6 +99,7 @@ class BaseResourceModel(object):
         :return: 3-tuple: (CPU-fraction, Mem-limit, Disk-limit)
         """
         LOG.warning("Allocating in BaseResourceModel: %r with flavor: %r" % (name, flavor_name))
+        self.allocated_compute_instances[name] = flavor_name
         return -1.0, -1.0, -1.0  # return invalid values to indicate that this RM is a dummy
 
     def free(self, name):
@@ -100,4 +109,5 @@ class BaseResourceModel(object):
         :return: True/False
         """
         LOG.warning("Free in BaseResourceModel: %r" % name)
+        del self.allocated_compute_instances[name]
         return True
index 2ec31a0..f31f3e4 100644 (file)
@@ -1,13 +1,22 @@
+import time
 from emuvim.test.base import SimpleTestTopology
-from emuvim.dcemulator.resourcemodel import BaseResourceModel
+from emuvim.dcemulator.resourcemodel import BaseResourceModel, ResourceFlavor
 
 
 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
@@ -27,8 +36,15 @@ class testResourceModel(SimpleTestTopology):
         # 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
+
+        # 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