Fix NoneType error when calling MetricsWeigher
Given that dict comprehensions don't allow to have None as a list, we had a problem with the Ironic gate-ironic-inspector-dsvm job because host_state.metrics was set to None by default. Adding a unittest to make sure we don't regress. Closes-Bug: #1499271 Change-Id: I6e3104bbd9b9865ec2dcc724c2f56da6d0b2b687
This commit is contained in:
parent
2dae8b4c87
commit
ccea5d6b0a
|
@ -86,7 +86,7 @@ class MetricsWeigher(weights.BaseHostWeigher):
|
||||||
|
|
||||||
# NOTE(sbauza): Keying a dict of Metrics per metric name given that we
|
# NOTE(sbauza): Keying a dict of Metrics per metric name given that we
|
||||||
# have a MonitorMetricList object
|
# have a MonitorMetricList object
|
||||||
metrics_dict = {m.name: m for m in host_state.metrics}
|
metrics_dict = {m.name: m for m in host_state.metrics or []}
|
||||||
for (name, ratio) in self.setting:
|
for (name, ratio) in self.setting:
|
||||||
try:
|
try:
|
||||||
value += metrics_dict[name].value * ratio
|
value += metrics_dict[name].value * ratio
|
||||||
|
|
|
@ -74,6 +74,15 @@ class MetricsWeigherTestCase(test.NoDBTestCase):
|
||||||
self.assertEqual(expected_weight, weighed_host.weight)
|
self.assertEqual(expected_weight, weighed_host.weight)
|
||||||
self.assertEqual(expected_host, weighed_host.obj.host)
|
self.assertEqual(expected_host, weighed_host.obj.host)
|
||||||
|
|
||||||
|
def test_single_resource_no_metrics(self):
|
||||||
|
setting = [idle + '=1']
|
||||||
|
hostinfo_list = [fakes.FakeHostState('host1', 'node1',
|
||||||
|
{'metrics': None}),
|
||||||
|
fakes.FakeHostState('host2', 'node2',
|
||||||
|
{'metrics': None})]
|
||||||
|
self.assertRaises(exception.ComputeHostMetricNotFound,
|
||||||
|
self._get_weighed_host, hostinfo_list, setting)
|
||||||
|
|
||||||
def test_single_resource(self):
|
def test_single_resource(self):
|
||||||
# host1: idle=512
|
# host1: idle=512
|
||||||
# host2: idle=1024
|
# host2: idle=1024
|
||||||
|
|
Loading…
Reference in New Issue