Coverage for manila/tests/scheduler/weighers/test_pool.py: 100%
53 statements
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
1# Copyright 2015 Mirantis Inc.
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15"""
16Tests For Pool Weigher.
17"""
19from unittest import mock
21from oslo_config import cfg
22from oslo_utils import timeutils
24from manila import context
25from manila.db import api as db_api
26from manila.scheduler.weighers import base_host
27from manila.scheduler.weighers import pool
28from manila.share import utils
29from manila import test
30from manila.tests.scheduler import fakes
32CONF = cfg.CONF
35class PoolWeigherTestCase(test.TestCase):
36 def setUp(self):
37 super(PoolWeigherTestCase, self).setUp()
38 self.host_manager = fakes.FakeHostManager()
39 self.weight_handler = base_host.HostWeightHandler(
40 'manila.scheduler.weighers')
41 share_servers = [
42 {'id': 'fake_server_id0'},
43 {'id': 'fake_server_id1'},
44 {'id': 'fake_server_id2'},
45 {'id': 'fake_server_id3'},
46 {'id': 'fake_server_id4'},
47 ]
48 services = [
49 dict(id=1, host='host1@AAA', topic='share', disabled=False,
50 availability_zone='zone1', updated_at=timeutils.utcnow()),
51 dict(id=2, host='host2@BBB', topic='share', disabled=False,
52 availability_zone='zone1', updated_at=timeutils.utcnow()),
53 dict(id=3, host='host3@CCC', topic='share', disabled=False,
54 availability_zone='zone2', updated_at=timeutils.utcnow()),
55 dict(id=4, host='host@DDD', topic='share', disabled=False,
56 availability_zone='zone3', updated_at=timeutils.utcnow()),
57 dict(id=5, host='host5@EEE', topic='share', disabled=False,
58 availability_zone='zone3', updated_at=timeutils.utcnow()),
59 ]
60 self.host_manager.service_states = (
61 fakes.SHARE_SERVICE_STATES_WITH_POOLS)
62 self.mock_object(db_api, 'share_server_get_all_by_host',
63 mock.Mock(return_value=share_servers))
64 self.mock_object(db_api.IMPL, 'service_get_all_by_topic',
65 mock.Mock(return_value=services))
67 def _get_weighed_host(self, hosts, weight_properties=None):
68 if weight_properties is None:
69 weight_properties = {
70 'server_pools_mapping': {
71 'fake_server_id2': [{'pool_name': 'pool2'}, ],
72 },
73 }
74 return self.weight_handler.get_weighed_objects(
75 [pool.PoolWeigher],
76 hosts,
77 weight_properties)[0]
79 def _get_all_hosts(self):
80 ctxt = context.get_admin_context()
81 host_states = self.host_manager.get_all_host_states_share(ctxt)
82 db_api.IMPL.service_get_all_by_topic.assert_called_once_with(
83 ctxt, CONF.share_topic, consider_disabled=False)
84 return host_states
86 def test_no_server_pool_mapping(self):
87 weight_properties = {
88 'server_pools_mapping': {},
89 }
90 weighed_host = self._get_weighed_host(self._get_all_hosts(),
91 weight_properties)
92 self.assertEqual(0.0, weighed_host.weight)
94 def test_choose_pool_with_existing_share_server(self):
95 # host1: weight = 0*(1.0)
96 # host2: weight = 1*(1.0)
97 # host3: weight = 0*(1.0)
98 # host4: weight = 0*(1.0)
99 # host5: weight = 0*(1.0)
101 # so, host2 should win:
103 weighed_host = self._get_weighed_host(self._get_all_hosts())
104 self.assertEqual(1.0, weighed_host.weight)
105 self.assertEqual(
106 'host2@BBB', utils.extract_host(weighed_host.obj.host))
108 def test_pool_weight_multiplier_positive(self):
109 self.flags(pool_weight_multiplier=2.0)
111 # host1: weight = 0*(2.0)
112 # host2: weight = 1*(2.0)
113 # host3: weight = 0*(2.0)
114 # host4: weight = 0*(2.0)
115 # host5: weight = 0*(2.0)
117 # so, host2 should win:
119 weighed_host = self._get_weighed_host(self._get_all_hosts())
120 self.assertEqual(2.0, weighed_host.weight)
121 self.assertEqual(
122 'host2@BBB', utils.extract_host(weighed_host.obj.host))
124 def test_pool_weight_multiplier_negative(self):
125 self.flags(pool_weight_multiplier=-1.0)
126 weight_properties = {
127 'server_pools_mapping': {
128 'fake_server_id0': [{'pool_name': 'pool1'}],
129 'fake_server_id2': [{'pool_name': 'pool3'}],
130 'fake_server_id3': [
131 {'pool_name': 'pool4a'},
132 {'pool_name': 'pool4b'},
133 ],
134 'fake_server_id4': [
135 {'pool_name': 'pool5a'},
136 {'pool_name': 'pool5b'},
137 ],
138 },
139 }
141 # host1: weight = 1*(-1.0)
142 # host2: weight = 0*(-1.0)
143 # host3: weight = 1*(-1.0)
144 # host4: weight = 1*(-1.0)
145 # host5: weight = 1*(-1.0)
147 # so, host2 should win:
148 weighed_host = self._get_weighed_host(self._get_all_hosts(),
149 weight_properties)
150 self.assertEqual(0.0, weighed_host.weight)
151 self.assertEqual(
152 'host2@BBB', utils.extract_host(weighed_host.obj.host))
154 def test_pool_weigher_all_pools_with_share_servers(self):
155 weight_properties = {
156 'server_pools_mapping': {
157 'fake_server_id0': [{'pool_name': 'pool1'}],
158 'fake_server_id1': [{'pool_name': 'pool2'}],
159 'fake_server_id2': [{'pool_name': 'pool3'}],
160 'fake_server_id3': [
161 {'pool_name': 'pool4a'},
162 {'pool_name': 'pool4b'},
163 ],
164 'fake_server_id4': [
165 {'pool_name': 'pool5a'},
166 {'pool_name': 'pool5b'},
167 ],
168 },
169 }
171 # host1: weight = 1*(1.0)
172 # host2: weight = 1*(1.0)
173 # host3: weight = 1*(1.0)
174 # host4: weight = 1*(1.0)
175 # host5: weight = 1*(1.0)
177 # But after normalization all weighers will be 0
179 weighed_host = self._get_weighed_host(self._get_all_hosts(),
180 weight_properties)
181 self.assertEqual(0.0, weighed_host.weight)