From: James Carey <jecarey@us.ibm.com>
Date: Thu, 6 Aug 2015 04:11:27 +0000 (+0000)
Subject: Add unit test cases for the capacity scheduler
X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=39217183edd2bb7ac2b1fb0ac57071174ced76cd;p=openstack-build%2Fcinder-build.git

Add unit test cases for the capacity scheduler

Add unit tests for the capacity weigher for cases where the host
has 'infinite' or 'unknown' capacities.

Note that while the new test cases follow the correct parameter
ordering for assertEqual, the other test cases will be fixed
in a separate patch.

Change-Id: I5ab20443563c253b561f328aedca4fd2f62b8d47
---

diff --git a/cinder/tests/unit/scheduler/test_capacity_weigher.py b/cinder/tests/unit/scheduler/test_capacity_weigher.py
index 9d10f3b14..11f496c20 100644
--- a/cinder/tests/unit/scheduler/test_capacity_weigher.py
+++ b/cinder/tests/unit/scheduler/test_capacity_weigher.py
@@ -36,13 +36,13 @@ class CapacityWeigherTestCase(test.TestCase):
         self.weight_handler = weights.HostWeightHandler(
             'cinder.scheduler.weights')
 
-    def _get_weighed_host(self, hosts, weight_properties=None):
+    def _get_weighed_hosts(self, hosts, weight_properties=None):
         if weight_properties is None:
             weight_properties = {'size': 1}
         return self.weight_handler.get_weighed_objects(
             [capacity.CapacityWeigher],
             hosts,
-            weight_properties)[0]
+            weight_properties)
 
     @mock.patch('cinder.db.sqlalchemy.api.service_get_all_by_topic')
     def _get_all_hosts(self, _mock_service_get_all_by_topic, disabled=False):
@@ -82,7 +82,7 @@ class CapacityWeigherTestCase(test.TestCase):
         #        Norm=0.0819000819001
 
         # so, host2 should win:
-        weighed_host = self._get_weighed_host(hostinfo_list)
+        weighed_host = self._get_weighed_hosts(hostinfo_list)[0]
         self.assertEqual(1.0, weighed_host.weight)
         self.assertEqual('host2', utils.extract_host(weighed_host.obj.host))
 
@@ -109,7 +109,7 @@ class CapacityWeigherTestCase(test.TestCase):
         #        Norm=-1.0
 
         # so, host4 should win:
-        weighed_host = self._get_weighed_host(hostinfo_list)
+        weighed_host = self._get_weighed_hosts(hostinfo_list)[0]
         self.assertEqual(0.0, weighed_host.weight)
         self.assertEqual('host4', utils.extract_host(weighed_host.obj.host))
 
@@ -136,6 +136,205 @@ class CapacityWeigherTestCase(test.TestCase):
         #        Norm=0.1638001638
 
         # so, host2 should win:
-        weighed_host = self._get_weighed_host(hostinfo_list)
+        weighed_host = self._get_weighed_hosts(hostinfo_list)[0]
         self.assertEqual(1.0 * 2, weighed_host.weight)
         self.assertEqual('host2', utils.extract_host(weighed_host.obj.host))
+
+    def test_capacity_weight_no_unknown_or_infinite(self):
+        self.flags(capacity_weight_multiplier=-1.0)
+        del self.host_manager.service_states['host5']
+        hostinfo_list = self._get_all_hosts()
+
+        # host1: thin_provisioning_support = False
+        #        free_capacity_gb=1024,
+        #        free=(1024-math.floor(1024*0.1))=-922
+        #        Norm=-0.837837837838
+        # host2: thin_provisioning_support = True
+        #        free_capacity_gb=300,
+        #        free=(2048*1.5-1748-math.floor(2048*0.1))=-1120
+        #        Norm=-1.0
+        # host3: thin_provisioning_support = False
+        #        free_capacity_gb=512, free=(256-512*0)=-256
+        #        Norm=-0.292383292383
+        # host4: thin_provisioning_support = True
+        #        free_capacity_gb=200,
+        #        free=(2048*1.0-2047-math.floor(2048*0.05))=101
+        #        Norm=0.0
+
+        # so, host4 should win:
+        weighed_hosts = self._get_weighed_hosts(hostinfo_list)
+        best_host = weighed_hosts[0]
+        self.assertEqual(0.0, best_host.weight)
+        self.assertEqual('host4', utils.extract_host(best_host.obj.host))
+        # and host2 is the worst:
+        worst_host = weighed_hosts[-1]
+        self.assertEqual(-1.0, worst_host.weight)
+        self.assertEqual('host2', utils.extract_host(worst_host.obj.host))
+
+    def test_capacity_weight_free_unknown(self):
+        self.flags(capacity_weight_multiplier=-1.0)
+        self.host_manager.service_states['host5'] = {
+            'total_capacity_gb': 3000,
+            'free_capacity_gb': 'unknown',
+            'allocated_capacity_gb': 1548,
+            'provisioned_capacity_gb': 1548,
+            'max_over_subscription_ratio': 1.0,
+            'thin_provisioning_support': True,
+            'thick_provisioning_support': False,
+            'reserved_percentage': 5,
+            'timestamp': None}
+        hostinfo_list = self._get_all_hosts()
+
+        # host1: thin_provisioning_support = False
+        #        free_capacity_gb=1024,
+        #        free=(1024-math.floor(1024*0.1))=-922
+        #        Norm= -0.00829542413701
+        # host2: thin_provisioning_support = True
+        #        free_capacity_gb=300,
+        #        free=(2048*1.5-1748-math.floor(2048*0.1))=-1120
+        #        Norm=-0.00990099009901
+        # host3: thin_provisioning_support = False
+        #        free_capacity_gb=512, free=(256-512*0)=-256
+        #        Norm=-0.002894884083
+        # host4: thin_provisioning_support = True
+        #        free_capacity_gb=200,
+        #        free=(2048*1.0-2047-math.floor(2048*0.05))=101
+        #        Norm=0.0
+        # host5: free_capacity_gb=unknown free=3000
+        #        Norm=-1.0
+
+        # so, host4 should win:
+        weighed_hosts = self._get_weighed_hosts(hostinfo_list)
+        best_host = weighed_hosts[0]
+        self.assertEqual(0.0, best_host.weight)
+        self.assertEqual('host4', utils.extract_host(best_host.obj.host))
+        # and host5 is the worst:
+        worst_host = weighed_hosts[-1]
+        self.assertEqual(-1.0, worst_host.weight)
+        self.assertEqual('host5', utils.extract_host(worst_host.obj.host))
+
+    def test_capacity_weight_cap_unknown(self):
+        self.flags(capacity_weight_multiplier=-1.0)
+        self.host_manager.service_states['host5'] = {
+            'total_capacity_gb': 'unknown',
+            'free_capacity_gb': 3000,
+            'allocated_capacity_gb': 1548,
+            'provisioned_capacity_gb': 1548,
+            'max_over_subscription_ratio': 1.0,
+            'thin_provisioning_support': True,
+            'thick_provisioning_support': False,
+            'reserved_percentage': 5,
+            'timestamp': None}
+        hostinfo_list = self._get_all_hosts()
+
+        # host1: thin_provisioning_support = False
+        #        free_capacity_gb=1024,
+        #        free=(1024-math.floor(1024*0.1))=-922
+        #        Norm= -0.00829542413701
+        # host2: thin_provisioning_support = True
+        #        free_capacity_gb=300,
+        #        free=(2048*1.5-1748-math.floor(2048*0.1))=-1120
+        #        Norm=-0.00990099009901
+        # host3: thin_provisioning_support = False
+        #        free_capacity_gb=512, free=(256-512*0)=-256
+        #        Norm=-0.002894884083
+        # host4: thin_provisioning_support = True
+        #        free_capacity_gb=200,
+        #        free=(2048*1.0-2047-math.floor(2048*0.05))=101
+        #        Norm=0.0
+        # host5: free_capacity_gb=3000 free=unknown
+        #        Norm=-1.0
+
+        # so, host4 should win:
+        weighed_hosts = self._get_weighed_hosts(hostinfo_list)
+        best_host = weighed_hosts[0]
+        self.assertEqual(0.0, best_host.weight)
+        self.assertEqual('host4', utils.extract_host(best_host.obj.host))
+        # and host5 is the worst:
+        worst_host = weighed_hosts[-1]
+        self.assertEqual(-1.0, worst_host.weight)
+        self.assertEqual('host5', utils.extract_host(worst_host.obj.host))
+
+    def test_capacity_weight_free_infinite(self):
+        self.flags(capacity_weight_multiplier=-1.0)
+        self.host_manager.service_states['host5'] = {
+            'total_capacity_gb': 3000,
+            'free_capacity_gb': 'infinite',
+            'allocated_capacity_gb': 1548,
+            'provisioned_capacity_gb': 1548,
+            'max_over_subscription_ratio': 1.0,
+            'thin_provisioning_support': True,
+            'thick_provisioning_support': False,
+            'reserved_percentage': 5,
+            'timestamp': None}
+        hostinfo_list = self._get_all_hosts()
+
+        # host1: thin_provisioning_support = False
+        #        free_capacity_gb=1024,
+        #        free=(1024-math.floor(1024*0.1))=-922
+        #        Norm= -0.00829542413701
+        # host2: thin_provisioning_support = True
+        #        free_capacity_gb=300,
+        #        free=(2048*1.5-1748-math.floor(2048*0.1))=-1120
+        #        Norm=-0.00990099009901
+        # host3: thin_provisioning_support = False
+        #        free_capacity_gb=512, free=(256-512*0)=-256
+        #        Norm=-0.002894884083
+        # host4: thin_provisioning_support = True
+        #        free_capacity_gb=200,
+        #        free=(2048*1.0-2047-math.floor(2048*0.05))=101
+        #        Norm=0.0
+        # host5: free_capacity_gb=infinite free=3000
+        #        Norm=-1.0
+
+        # so, host4 should win:
+        weighed_hosts = self._get_weighed_hosts(hostinfo_list)
+        best_host = weighed_hosts[0]
+        self.assertEqual(0.0, best_host.weight)
+        self.assertEqual('host4', utils.extract_host(best_host.obj.host))
+        # and host5 is the worst:
+        worst_host = weighed_hosts[-1]
+        self.assertEqual(-1.0, worst_host.weight)
+        self.assertEqual('host5', utils.extract_host(worst_host.obj.host))
+
+    def test_capacity_weight_cap_infinite(self):
+        self.flags(capacity_weight_multiplier=-1.0)
+        self.host_manager.service_states['host5'] = {
+            'total_capacity_gb': 'infinite',
+            'free_capacity_gb': 3000,
+            'allocated_capacity_gb': 1548,
+            'provisioned_capacity_gb': 1548,
+            'max_over_subscription_ratio': 1.0,
+            'thin_provisioning_support': True,
+            'thick_provisioning_support': False,
+            'reserved_percentage': 5,
+            'timestamp': None}
+        hostinfo_list = self._get_all_hosts()
+
+        # host1: thin_provisioning_support = False
+        #        free_capacity_gb=1024,
+        #        free=(1024-math.floor(1024*0.1))=-922
+        #        Norm= -0.00829542413701
+        # host2: thin_provisioning_support = True
+        #        free_capacity_gb=300,
+        #        free=(2048*1.5-1748-math.floor(2048*0.1))=-1120
+        #        Norm=-0.00990099009901
+        # host3: thin_provisioning_support = False
+        #        free_capacity_gb=512, free=(256-512*0)=-256
+        #        Norm=-0.002894884083
+        # host4: thin_provisioning_support = True
+        #        free_capacity_gb=200,
+        #        free=(2048*1.0-2047-math.floor(2048*0.05))=101
+        #        Norm=0.0
+        # host5: free_capacity_gb=3000 free=infinite
+        #        Norm=-1.0
+
+        # so, host4 should win:
+        weighed_hosts = self._get_weighed_hosts(hostinfo_list)
+        best_host = weighed_hosts[0]
+        self.assertEqual(0.0, best_host.weight)
+        self.assertEqual('host4', utils.extract_host(best_host.obj.host))
+        # and host5 is the worst:
+        worst_host = weighed_hosts[-1]
+        self.assertEqual(-1.0, worst_host.weight)
+        self.assertEqual('host5', utils.extract_host(worst_host.obj.host))