Coverage for manila/tests/scheduler/weighers/test_goodness.py: 100%
67 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 (c) 2014 Hewlett-Packard Development Company, L.P.
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 Goodness Weigher.
17"""
19from manila.scheduler.weighers import goodness
20from manila import test
21from manila.tests.scheduler import fakes
24class GoodnessWeigherTestCase(test.TestCase):
26 def test_goodness_weigher_with_no_goodness_function(self):
27 weigher = goodness.GoodnessWeigher()
28 host_state = fakes.FakeHostState('host1', {
29 'host': 'host.example.com',
30 'capabilities': {
31 'foo': '50'
32 }
33 })
35 weight_properties = {}
36 weight = weigher._weigh_object(host_state, weight_properties)
37 self.assertEqual(0, weight)
39 def test_goodness_weigher_passing_host(self):
40 weigher = goodness.GoodnessWeigher()
41 host_state = fakes.FakeHostState('host1', {
42 'host': 'host.example.com',
43 'capabilities': {
44 'goodness_function': '100'
45 }
46 })
47 host_state_2 = fakes.FakeHostState('host2', {
48 'host': 'host2.example.com',
49 'capabilities': {
50 'goodness_function': '0'
51 }
52 })
53 host_state_3 = fakes.FakeHostState('host3', {
54 'host': 'host3.example.com',
55 'capabilities': {
56 'goodness_function': '100 / 2'
57 }
58 })
60 weight_properties = {}
61 weight = weigher._weigh_object(host_state, weight_properties)
62 self.assertEqual(100, weight)
63 weight = weigher._weigh_object(host_state_2, weight_properties)
64 self.assertEqual(0, weight)
65 weight = weigher._weigh_object(host_state_3, weight_properties)
66 self.assertEqual(50, weight)
68 def test_goodness_weigher_capabilities_substitution(self):
69 weigher = goodness.GoodnessWeigher()
70 host_state = fakes.FakeHostState('host1', {
71 'host': 'host.example.com',
72 'capabilities': {
73 'foo': 50,
74 'goodness_function': '10 + capabilities.foo'
75 }
76 })
78 weight_properties = {}
79 weight = weigher._weigh_object(host_state, weight_properties)
80 self.assertEqual(60, weight)
82 def test_goodness_weigher_extra_specs_substitution(self):
83 weigher = goodness.GoodnessWeigher()
84 host_state = fakes.FakeHostState('host1', {
85 'host': 'host.example.com',
86 'capabilities': {
87 'goodness_function': '10 + extra.foo'
88 }
89 })
91 weight_properties = {
92 'share_type': {
93 'extra_specs': {
94 'foo': 50
95 }
96 }
97 }
98 weight = weigher._weigh_object(host_state, weight_properties)
99 self.assertEqual(60, weight)
101 def test_goodness_weigher_share_substitution(self):
102 weigher = goodness.GoodnessWeigher()
103 host_state = fakes.FakeHostState('host1', {
104 'host': 'host.example.com',
105 'capabilities': {
106 'goodness_function': '10 + share.foo'
107 }
108 })
110 weight_properties = {
111 'request_spec': {
112 'resource_properties': {
113 'foo': 50
114 }
115 }
116 }
117 weight = weigher._weigh_object(host_state, weight_properties)
118 self.assertEqual(60, weight)
120 def test_goodness_weigher_stats_substitution(self):
121 weigher = goodness.GoodnessWeigher()
122 host_state = fakes.FakeHostState('host1', {
123 'host': 'host.example.com',
124 'capabilities': {
125 'goodness_function': 'stats.free_capacity_gb > 20'
126 },
127 'free_capacity_gb': 50
128 })
130 weight_properties = {}
131 weight = weigher._weigh_object(host_state, weight_properties)
132 self.assertEqual(100, weight)
134 def test_goodness_weigher_invalid_substitution(self):
135 weigher = goodness.GoodnessWeigher()
136 host_state = fakes.FakeHostState('host1', {
137 'host': 'host.example.com',
138 'capabilities': {
139 'goodness_function': '10 + stats.my_val'
140 },
141 'foo': 50
142 })
144 weight_properties = {}
145 weight = weigher._weigh_object(host_state, weight_properties)
146 self.assertEqual(0, weight)
148 def test_goodness_weigher_host_rating_out_of_bounds(self):
149 weigher = goodness.GoodnessWeigher()
150 host_state = fakes.FakeHostState('host1', {
151 'host': 'host.example.com',
152 'capabilities': {
153 'goodness_function': '-10'
154 }
155 })
156 host_state_2 = fakes.FakeHostState('host2', {
157 'host': 'host2.example.com',
158 'capabilities': {
159 'goodness_function': '200'
160 }
161 })
163 weight_properties = {}
164 weight = weigher._weigh_object(host_state, weight_properties)
165 self.assertEqual(0, weight)
166 weight = weigher._weigh_object(host_state_2, weight_properties)
167 self.assertEqual(0, weight)
169 def test_goodness_weigher_invalid_goodness_function(self):
170 weigher = goodness.GoodnessWeigher()
171 host_state = fakes.FakeHostState('host1', {
172 'host': 'host.example.com',
173 'capabilities': {
174 'goodness_function': '50 / 0'
175 }
176 })
178 weight_properties = {}
179 weight = weigher._weigh_object(host_state, weight_properties)
180 self.assertEqual(0, weight)