class AllocatedCapacityWeigherTestCase(test.TestCase):
    def setUp(self):
        super(AllocatedCapacityWeigherTestCase, self).setUp()
        self.host_manager = fakes.FakeHostManager()
        self.weight_handler = HostWeightHandler("cinder.scheduler.weights")

    def _get_weighed_host(self, hosts, weight_properties=None):
        if weight_properties is None:
            weight_properties = {}
        return self.weight_handler.get_weighed_objects([ACW], hosts, weight_properties)[0]

    @mock.patch("cinder.db.sqlalchemy.api.service_get_all_by_topic")
    def _get_all_hosts(self, _mock_service_get_all_by_topic):
        ctxt = context.get_admin_context()
        fakes.mock_host_manager_db_calls(_mock_service_get_all_by_topic)
        host_states = self.host_manager.get_all_host_states(ctxt)
        _mock_service_get_all_by_topic.assert_called_once_with(ctxt, CONF.volume_topic)
        return host_states

    def test_default_of_spreading_first(self):
        hostinfo_list = self._get_all_hosts()

        # host1: allocated_capacity_gb=0, weight=0
        # host2: allocated_capacity_gb=1748, weight=-1748
        # host3: allocated_capacity_gb=256, weight=-256
        # host4: allocated_capacity_gb=1848, weight=-1848

        # so, host1 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 0)
        self.assertEqual(weighed_host.obj.host, "host1")

    def test_capacity_weight_multiplier1(self):
        self.flags(allocated_capacity_weight_multiplier=1.0)
        hostinfo_list = self._get_all_hosts()

        # host1: allocated_capacity_gb=0, weight=0
        # host2: allocated_capacity_gb=1748, weight=1748
        # host3: allocated_capacity_gb=256, weight=256
        # host4: allocated_capacity_gb=1848, weight=1848

        # so, host4 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 1848.0)
        self.assertEqual(weighed_host.obj.host, "host4")

    def test_capacity_weight_multiplier2(self):
        self.flags(allocated_capacity_weight_multiplier=-2.0)
        hostinfo_list = self._get_all_hosts()

        # host1: allocated_capacity_gb=0, weight=0
        # host2: allocated_capacity_gb=1748, weight=-3496
        # host3: allocated_capacity_gb=256, weight=-512
        # host4: allocated_capacity_gb=1848, weight=-3696

        # so, host1 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 0)
        self.assertEqual(weighed_host.obj.host, "host1")
Esempio n. 2
0
class VolumeNumberWeigherTestCase(test.TestCase):
    def setUp(self):
        super(VolumeNumberWeigherTestCase, self).setUp()
        self.context = context.get_admin_context()
        self.host_manager = fakes.FakeHostManager()
        self.weight_handler = HostWeightHandler('cinder.scheduler.weights')

    def _get_weighed_host(self, hosts, weight_properties=None):
        if weight_properties is None:
            weight_properties = {'context': self.context}
        return self.weight_handler.get_weighed_objects([VolumeNumberWeigher],
                                                       hosts,
                                                       weight_properties)[0]

    @mock.patch('cinder.db.sqlalchemy.api.service_get_all_by_topic')
    def _get_all_hosts(self, _mock_service_get_all_by_topic, disabled=False):
        ctxt = context.get_admin_context()
        fakes.mock_host_manager_db_calls(_mock_service_get_all_by_topic,
                                         disabled=disabled)
        host_states = self.host_manager.get_all_host_states(ctxt)
        _mock_service_get_all_by_topic.assert_called_once_with(
            ctxt, CONF.volume_topic, disabled=disabled)
        return host_states

    def test_volume_number_weight_multiplier1(self):
        self.flags(volume_number_multiplier=-1.0)
        hostinfo_list = self._get_all_hosts()

        # host1: 1 volume
        # host2: 2 volumes
        # host3: 3 volumes
        # host4: 4 volumes
        # host5: 5 volumes
        # so, host1 should win:
        with mock.patch.object(api, 'volume_data_get_for_host',
                               fake_volume_data_get_for_host):
            weighed_host = self._get_weighed_host(hostinfo_list)
            self.assertEqual(weighed_host.weight, -1.0)
            self.assertEqual(utils.extract_host(weighed_host.obj.host),
                             'host1')

    def test_volume_number_weight_multiplier2(self):
        self.flags(volume_number_multiplier=1.0)
        hostinfo_list = self._get_all_hosts()

        # host1: 1 volume
        # host2: 2 volumes
        # host3: 3 volumes
        # host4: 4 volumes
        # host5: 5 volumes
        # so, host5 should win:
        with mock.patch.object(api, 'volume_data_get_for_host',
                               fake_volume_data_get_for_host):
            weighed_host = self._get_weighed_host(hostinfo_list)
            self.assertEqual(weighed_host.weight, 5.0)
            self.assertEqual(utils.extract_host(weighed_host.obj.host),
                             'host5')
Esempio n. 3
0
 def setUp(self):
     super(CapacityWeigherTestCase, self).setUp()
     self.host_manager = fakes.FakeHostManager()
     self.weight_handler = HostWeightHandler('cinder.scheduler.weights')
Esempio n. 4
0
class CapacityWeigherTestCase(test.TestCase):
    def setUp(self):
        super(CapacityWeigherTestCase, self).setUp()
        self.host_manager = fakes.FakeHostManager()
        self.weight_handler = HostWeightHandler('cinder.scheduler.weights')

    def _get_weighed_host(self, hosts, weight_properties=None):
        if weight_properties is None:
            weight_properties = {'size': 1}
        return self.weight_handler.get_weighed_objects([CapacityWeigher],
                                                       hosts,
                                                       weight_properties)[0]

    @mock.patch('cinder.db.sqlalchemy.api.service_get_all_by_topic')
    def _get_all_hosts(self, _mock_service_get_all_by_topic, disabled=False):
        ctxt = context.get_admin_context()
        fakes.mock_host_manager_db_calls(_mock_service_get_all_by_topic,
                                         disabled=disabled)
        host_states = self.host_manager.get_all_host_states(ctxt)
        _mock_service_get_all_by_topic.assert_called_once_with(
            ctxt, CONF.volume_topic, disabled=disabled)
        return host_states

    # If thin_provisioning_support = False, use the following formula:
    # free = free_space - math.floor(total * reserved)
    # Otherwise, use the following formula:
    # free = (total * host_state.max_over_subscription_ratio
    #         - host_state.provisioned_capacity_gb
    #         - math.floor(total * reserved))
    def test_default_of_spreading_first(self):
        hostinfo_list = self._get_all_hosts()

        # host1: thin_provisioning_support = False
        #        free_capacity_gb=1024,
        #        free=1024-math.floor(1024*0.1)=922
        # host2: thin_provisioning_support = True
        #        free_capacity_gb=300,
        #        free=2048*1.5-1748-math.floor(2048*0.1)=1120
        # host3: thin_provisioning_support = False
        #        free_capacity_gb=512, free=256-512*0=256
        # host4: thin_provisioning_support = True
        #        free_capacity_gb=200,
        #        free=2048*1.0-2047-math.floor(2048*0.05)=-101
        # host5: free_capacity_gb=unknown free=-1

        # so, host2 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 1120.0)
        self.assertEqual(utils.extract_host(weighed_host.obj.host), 'host2')

    def test_capacity_weight_multiplier1(self):
        self.flags(capacity_weight_multiplier=-1.0)
        hostinfo_list = self._get_all_hosts()

        # host1: thin_provisioning_support = False
        #        free_capacity_gb=1024,
        #        free=-(1024-math.floor(1024*0.1))=-922
        # host2: thin_provisioning_support = True
        #        free_capacity_gb=300,
        #        free=-(2048*1.5-1748-math.floor(2048*0.1))=-1120
        # host3: thin_provisioning_support = False
        #        free_capacity_gb=512, free=-(256-512*0)=-256
        # host4: thin_provisioning_support = True
        #        free_capacity_gb=200,
        #        free=-(2048*1.0-2047-math.floor(2048*0.05))=101
        # host5: free_capacity_gb=unknown free=-float('inf')

        # so, host4 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 101.0)
        self.assertEqual(utils.extract_host(weighed_host.obj.host), 'host4')

    def test_capacity_weight_multiplier2(self):
        self.flags(capacity_weight_multiplier=2.0)
        hostinfo_list = self._get_all_hosts()

        # host1: thin_provisioning_support = False
        #        free_capacity_gb=1024,
        #        free=(1024-math.floor(1024*0.1))*2=1844
        # host2: thin_provisioning_support = True
        #        free_capacity_gb=300,
        #        free=(2048*1.5-1748-math.floor(2048*0.1))*2=2240
        # host3: thin_provisioning_support = False
        #        free_capacity_gb=512, free=(256-512*0)*2=512
        # host4: thin_provisioning_support = True
        #        free_capacity_gb=200,
        #        free=(2048*1.0-2047-math.floor(2048*0.05))*2=-202
        # host5: free_capacity_gb=unknown free=-2

        # so, host2 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 1120.0 * 2)
        self.assertEqual(utils.extract_host(weighed_host.obj.host), 'host2')
Esempio n. 5
0
 def setUp(self):
     super(CapacityWeigherTestCase, self).setUp()
     self.host_manager = fakes.FakeHostManager()
     self.weight_handler = HostWeightHandler('cinder.scheduler.weights')
Esempio n. 6
0
class CapacityWeigherTestCase(test.TestCase):
    def setUp(self):
        super(CapacityWeigherTestCase, self).setUp()
        self.host_manager = fakes.FakeHostManager()
        self.weight_handler = HostWeightHandler('cinder.scheduler.weights')

    def _get_weighed_host(self, hosts, weight_properties=None):
        if weight_properties is None:
            weight_properties = {}
        return self.weight_handler.get_weighed_objects([CapacityWeigher],
                                                       hosts,
                                                       weight_properties)[0]

    @mock.patch('cinder.db.sqlalchemy.api.service_get_all_by_topic')
    def _get_all_hosts(self, _mock_service_get_all_by_topic, disabled=False):
        ctxt = context.get_admin_context()
        fakes.mock_host_manager_db_calls(_mock_service_get_all_by_topic,
                                         disabled=disabled)
        host_states = self.host_manager.get_all_host_states(ctxt)
        _mock_service_get_all_by_topic.assert_called_once_with(
            ctxt, CONF.volume_topic, disabled=disabled)
        return host_states

    def test_default_of_spreading_first(self):
        hostinfo_list = self._get_all_hosts()

        # host1: free_capacity_gb=1024, free=1024*(1-0.1)
        # host2: free_capacity_gb=300, free=300*(1-0.1)
        # host3: free_capacity_gb=512, free=256
        # host4: free_capacity_gb=200, free=200*(1-0.05)
        # host5: free_capacity_gb=unknown free=-1

        # so, host1 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 921.0)
        self.assertEqual(
            utils.extract_host(weighed_host.obj.host), 'host1')

    def test_capacity_weight_multiplier1(self):
        self.flags(capacity_weight_multiplier=-1.0)
        hostinfo_list = self._get_all_hosts()

        # host1: free_capacity_gb=1024, free=-1024*(1-0.1)
        # host2: free_capacity_gb=300, free=-300*(1-0.1)
        # host3: free_capacity_gb=512, free=-256
        # host4: free_capacity_gb=200, free=-200*(1-0.05)
        # host5: free_capacity_gb=unknown free=-float('inf')

        # so, host4 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, -190.0)
        self.assertEqual(
            utils.extract_host(weighed_host.obj.host), 'host4')

    def test_capacity_weight_multiplier2(self):
        self.flags(capacity_weight_multiplier=2.0)
        hostinfo_list = self._get_all_hosts()

        # host1: free_capacity_gb=1024, free=1024*(1-0.1)*2
        # host2: free_capacity_gb=300, free=300*(1-0.1)*2
        # host3: free_capacity_gb=512, free=256*2
        # host4: free_capacity_gb=200, free=200*(1-0.05)*2
        # host5: free_capacity_gb=unknown free=-2

        # so, host1 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 921.0 * 2)
        self.assertEqual(
            utils.extract_host(weighed_host.obj.host), 'host1')
Esempio n. 7
0
class CapacityWeigherTestCase(test.TestCase):
    def setUp(self):
        super(CapacityWeigherTestCase, self).setUp()
        self.host_manager = fakes.FakeHostManager()
        self.weight_handler = HostWeightHandler('cinder.scheduler.weights')

    def _get_weighed_host(self, hosts, weight_properties=None):
        if weight_properties is None:
            weight_properties = {}
        return self.weight_handler.get_weighed_objects([CapacityWeigher],
                                                       hosts,
                                                       weight_properties)[0]

    @mock.patch('cinder.db.sqlalchemy.api.service_get_all_by_topic')
    def _get_all_hosts(self, _mock_service_get_all_by_topic):
        ctxt = context.get_admin_context()
        fakes.mock_host_manager_db_calls(_mock_service_get_all_by_topic)
        host_states = self.host_manager.get_all_host_states(ctxt)
        _mock_service_get_all_by_topic.assert_called_once_with(
            ctxt, CONF.volume_topic)
        return host_states

    def test_default_of_spreading_first(self):
        hostinfo_list = self._get_all_hosts()

        # host1: free_capacity_gb=1024, free=1024*(1-0.1)
        # host2: free_capacity_gb=300, free=300*(1-0.1)
        # host3: free_capacity_gb=512, free=512
        # host4: free_capacity_gb=200, free=200*(1-0.05)

        # so, host1 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 921.0)
        self.assertEqual(weighed_host.obj.host, 'host1')

    def test_capacity_weight_multiplier1(self):
        self.flags(capacity_weight_multiplier=-1.0)
        hostinfo_list = self._get_all_hosts()

        # host1: free_capacity_gb=1024, free=-1024*(1-0.1)
        # host2: free_capacity_gb=300, free=-300*(1-0.1)
        # host3: free_capacity_gb=512, free=-512
        # host4: free_capacity_gb=200, free=-200*(1-0.05)

        # so, host4 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, -190.0)
        self.assertEqual(weighed_host.obj.host, 'host4')

    def test_capacity_weight_multiplier2(self):
        self.flags(capacity_weight_multiplier=2.0)
        hostinfo_list = self._get_all_hosts()

        # host1: free_capacity_gb=1024, free=1024*(1-0.1)*2
        # host2: free_capacity_gb=300, free=300*(1-0.1)*2
        # host3: free_capacity_gb=512, free=512*2
        # host4: free_capacity_gb=200, free=200*(1-0.05)*2

        # so, host1 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 921.0 * 2)
        self.assertEqual(weighed_host.obj.host, 'host1')
Esempio n. 8
0
class CapacityWeigherTestCase(test.TestCase):
    def setUp(self):
        super(CapacityWeigherTestCase, self).setUp()
        self.host_manager = fakes.FakeHostManager()
        self.weight_handler = HostWeightHandler('cinder.scheduler.weights')
        self.weight_classes = self.weight_handler.get_all_classes()

    def _get_weighed_host(self, hosts, weight_properties=None):
        if weight_properties is None:
            weight_properties = {}
        return self.weight_handler.get_weighed_objects(self.weight_classes,
                                                       hosts,
                                                       weight_properties)[0]

    def _get_all_hosts(self):
        ctxt = context.get_admin_context()
        fakes.mox_host_manager_db_calls(self.mox, ctxt)
        self.mox.ReplayAll()
        host_states = self.host_manager.get_all_host_states(ctxt)
        self.mox.VerifyAll()
        self.mox.ResetAll()
        return host_states

    def test_default_of_spreading_first(self):
        hostinfo_list = self._get_all_hosts()

        # host1: free_capacity_gb=1024, free=1024*(1-0.1)
        # host2: free_capacity_gb=300, free=300*(1-0.1)
        # host3: free_capacity_gb=512, free=512
        # host4: free_capacity_gb=200, free=200*(1-0.05)

        # so, host1 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 921.0)
        self.assertEqual(weighed_host.obj.host, 'host1')

    def test_capacity_weight_multiplier1(self):
        self.flags(capacity_weight_multiplier=-1.0)
        hostinfo_list = self._get_all_hosts()

        # host1: free_capacity_gb=1024, free=-1024*(1-0.1)
        # host2: free_capacity_gb=300, free=-300*(1-0.1)
        # host3: free_capacity_gb=512, free=-512
        # host4: free_capacity_gb=200, free=-200*(1-0.05)

        # so, host4 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, -190.0)
        self.assertEqual(weighed_host.obj.host, 'host4')

    def test_capacity_weight_multiplier2(self):
        self.flags(capacity_weight_multiplier=2.0)
        hostinfo_list = self._get_all_hosts()

        # host1: free_capacity_gb=1024, free=1024*(1-0.1)*2
        # host2: free_capacity_gb=300, free=300*(1-0.1)*2
        # host3: free_capacity_gb=512, free=512*2
        # host4: free_capacity_gb=200, free=200*(1-0.05)*2

        # so, host1 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 921.0 * 2)
        self.assertEqual(weighed_host.obj.host, 'host1')
class AllocatedCapacityWeigherTestCase(test.TestCase):
    def setUp(self):
        super(AllocatedCapacityWeigherTestCase, self).setUp()
        self.host_manager = fakes.FakeHostManager()
        self.weight_handler = HostWeightHandler('cinder.scheduler.weights')

    def _get_weighed_host(self, hosts, weight_properties=None):
        if weight_properties is None:
            weight_properties = {}
        return self.weight_handler.get_weighed_objects([ACW], hosts,
                                                       weight_properties)[0]

    @mock.patch('cinder.db.sqlalchemy.api.service_get_all_by_topic')
    def _get_all_hosts(self, _mock_service_get_all_by_topic, disabled=False):
        ctxt = context.get_admin_context()
        fakes.mock_host_manager_db_calls(_mock_service_get_all_by_topic,
                                         disabled=disabled)
        host_states = self.host_manager.get_all_host_states(ctxt)
        _mock_service_get_all_by_topic.assert_called_once_with(
            ctxt, CONF.volume_topic, disabled=disabled)
        return host_states

    def test_default_of_spreading_first(self):
        hostinfo_list = self._get_all_hosts()

        # host1: allocated_capacity_gb=0, weight=0
        # host2: allocated_capacity_gb=1748, weight=-1748
        # host3: allocated_capacity_gb=256, weight=-256
        # host4: allocated_capacity_gb=1848, weight=-1848

        # so, host1 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 0)
        self.assertEqual(
            utils.extract_host(weighed_host.obj.host), 'host1')

    def test_capacity_weight_multiplier1(self):
        self.flags(allocated_capacity_weight_multiplier=1.0)
        hostinfo_list = self._get_all_hosts()

        # host1: allocated_capacity_gb=0, weight=0
        # host2: allocated_capacity_gb=1748, weight=1748
        # host3: allocated_capacity_gb=256, weight=256
        # host4: allocated_capacity_gb=1848, weight=1848

        # so, host4 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 1848.0)
        self.assertEqual(
            utils.extract_host(weighed_host.obj.host), 'host4')

    def test_capacity_weight_multiplier2(self):
        self.flags(allocated_capacity_weight_multiplier=-2.0)
        hostinfo_list = self._get_all_hosts()

        # host1: allocated_capacity_gb=0, weight=0
        # host2: allocated_capacity_gb=1748, weight=-3496
        # host3: allocated_capacity_gb=256, weight=-512
        # host4: allocated_capacity_gb=1848, weight=-3696

        # so, host1 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 0)
        self.assertEqual(
            utils.extract_host(weighed_host.obj.host), 'host1')
Esempio n. 10
0
 def setUp(self):
     super(VolumeNumberWeigherTestCase, self).setUp()
     self.context = context.get_admin_context()
     self.host_manager = fakes.FakeHostManager()
     self.weight_handler = HostWeightHandler('cinder.scheduler.weights')
class CapacityWeigherTestCase(test.TestCase):
    def setUp(self):
        super(CapacityWeigherTestCase, self).setUp()
        self.host_manager = fakes.FakeHostManager()
        self.weight_handler = HostWeightHandler('cinder.scheduler.weights')

    def _get_weighed_host(self, hosts, weight_properties=None):
        if weight_properties is None:
            weight_properties = {}
        return self.weight_handler.get_weighed_objects([CapacityWeigher],
                                                       hosts,
                                                       weight_properties)[0]

    def _get_all_hosts(self):
        ctxt = context.get_admin_context()
        fakes.mox_host_manager_db_calls(self.mox, ctxt)
        self.mox.ReplayAll()
        host_states = self.host_manager.get_all_host_states(ctxt)
        self.mox.VerifyAll()
        self.mox.ResetAll()
        return host_states

    def test_default_of_spreading_first(self):
        hostinfo_list = self._get_all_hosts()

        # host1: free_capacity_gb=1024, free=1024*(1-0.1)
        # host2: free_capacity_gb=300, free=300*(1-0.1)
        # host3: free_capacity_gb=512, free=512
        # host4: free_capacity_gb=200, free=200*(1-0.05)

        # so, host1 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 921.0)
        self.assertEqual(weighed_host.obj.host, 'host1')

    def test_capacity_weight_multiplier1(self):
        self.flags(capacity_weight_multiplier=-1.0)
        hostinfo_list = self._get_all_hosts()

        # host1: free_capacity_gb=1024, free=-1024*(1-0.1)
        # host2: free_capacity_gb=300, free=-300*(1-0.1)
        # host3: free_capacity_gb=512, free=-512
        # host4: free_capacity_gb=200, free=-200*(1-0.05)

        # so, host4 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, -190.0)
        self.assertEqual(weighed_host.obj.host, 'host4')

    def test_capacity_weight_multiplier2(self):
        self.flags(capacity_weight_multiplier=2.0)
        hostinfo_list = self._get_all_hosts()

        # host1: free_capacity_gb=1024, free=1024*(1-0.1)*2
        # host2: free_capacity_gb=300, free=300*(1-0.1)*2
        # host3: free_capacity_gb=512, free=512*2
        # host4: free_capacity_gb=200, free=200*(1-0.05)*2

        # so, host1 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 921.0 * 2)
        self.assertEqual(weighed_host.obj.host, 'host1')
Esempio n. 12
0
 def setUp(self):
     super(CapacityWeigherTestCase, self).setUp()
     self.host_manager = fakes.FakeHostManager()
     self.weight_handler = HostWeightHandler("cinder.scheduler.weights")
     self.weight_classes = self.weight_handler.get_all_classes()
Esempio n. 13
0
class CapacityWeigherTestCase(test.TestCase):
    def setUp(self):
        super(CapacityWeigherTestCase, self).setUp()
        self.host_manager = fakes.FakeHostManager()
        self.weight_handler = HostWeightHandler('cinder.scheduler.weights')

    def _get_weighed_host(self, hosts, weight_properties=None):
        if weight_properties is None:
            weight_properties = {'size': 1}
        return self.weight_handler.get_weighed_objects([CapacityWeigher],
                                                       hosts,
                                                       weight_properties)[0]

    @mock.patch('cinder.db.sqlalchemy.api.service_get_all_by_topic')
    def _get_all_hosts(self, _mock_service_get_all_by_topic, disabled=False):
        ctxt = context.get_admin_context()
        fakes.mock_host_manager_db_calls(_mock_service_get_all_by_topic,
                                         disabled=disabled)
        host_states = self.host_manager.get_all_host_states(ctxt)
        _mock_service_get_all_by_topic.assert_called_once_with(
            ctxt, CONF.volume_topic, disabled=disabled)
        return host_states

    # If thin_provisioning_support = False, use the following formula:
    # free = free_space - math.floor(total * reserved)
    # Otherwise, use the following formula:
    # free = (total * host_state.max_over_subscription_ratio
    #         - host_state.provisioned_capacity_gb
    #         - math.floor(total * reserved))
    def test_default_of_spreading_first(self):
        hostinfo_list = self._get_all_hosts()

        # host1: thin_provisioning_support = False
        #        free_capacity_gb=1024,
        #        free=1024-math.floor(1024*0.1)=922
        # host2: thin_provisioning_support = True
        #        free_capacity_gb=300,
        #        free=2048*1.5-1748-math.floor(2048*0.1)=1120
        # host3: thin_provisioning_support = False
        #        free_capacity_gb=512, free=256-512*0=256
        # host4: thin_provisioning_support = True
        #        free_capacity_gb=200,
        #        free=2048*1.0-2047-math.floor(2048*0.05)=-101
        # host5: free_capacity_gb=unknown free=-1

        # so, host2 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 1120.0)
        self.assertEqual(
            utils.extract_host(weighed_host.obj.host), 'host2')

    def test_capacity_weight_multiplier1(self):
        self.flags(capacity_weight_multiplier=-1.0)
        hostinfo_list = self._get_all_hosts()

        # host1: thin_provisioning_support = False
        #        free_capacity_gb=1024,
        #        free=-(1024-math.floor(1024*0.1))=-922
        # host2: thin_provisioning_support = True
        #        free_capacity_gb=300,
        #        free=-(2048*1.5-1748-math.floor(2048*0.1))=-1120
        # host3: thin_provisioning_support = False
        #        free_capacity_gb=512, free=-(256-512*0)=-256
        # host4: thin_provisioning_support = True
        #        free_capacity_gb=200,
        #        free=-(2048*1.0-2047-math.floor(2048*0.05))=101
        # host5: free_capacity_gb=unknown free=-float('inf')

        # so, host4 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 101.0)
        self.assertEqual(
            utils.extract_host(weighed_host.obj.host), 'host4')

    def test_capacity_weight_multiplier2(self):
        self.flags(capacity_weight_multiplier=2.0)
        hostinfo_list = self._get_all_hosts()

        # host1: thin_provisioning_support = False
        #        free_capacity_gb=1024,
        #        free=(1024-math.floor(1024*0.1))*2=1844
        # host2: thin_provisioning_support = True
        #        free_capacity_gb=300,
        #        free=(2048*1.5-1748-math.floor(2048*0.1))*2=2240
        # host3: thin_provisioning_support = False
        #        free_capacity_gb=512, free=(256-512*0)*2=512
        # host4: thin_provisioning_support = True
        #        free_capacity_gb=200,
        #        free=(2048*1.0-2047-math.floor(2048*0.05))*2=-202
        # host5: free_capacity_gb=unknown free=-2

        # so, host2 should win:
        weighed_host = self._get_weighed_host(hostinfo_list)
        self.assertEqual(weighed_host.weight, 1120.0 * 2)
        self.assertEqual(
            utils.extract_host(weighed_host.obj.host), 'host2')