From 2caf8f65e414732fb1ef1ac32a5884a48af6655d Mon Sep 17 00:00:00 2001 From: Rajesh Tailor Date: Fri, 18 Aug 2017 12:33:09 +0530 Subject: [PATCH] Make host_aggregate_map dictionary case-insensitive As of now, if hostname is set as "compute0.example.com" (in lower case) and user tries to add this host to host-aggregate but by-mistake types "COMPUTE0.example.com" (in capital case), then instead of throwing HostNotFound error, that host is successfully added to host-aggregate as "COMPUTE0.example.com". And after that if instance creation request with flavor matching metadata as host-aggregate metadata comes, this host is not filtered by scheduler, since there is no host with hostname COMPUTE0.example.com, as added in host-aggregate. Fixed the issue by lowercasing all hostnames in host_aggregate_map dictionary. Change-Id: Iee4b9bbf412adfdc6fdc62ea3429fb960d6ac2a2 Closes-Bug: 1709260 (cherry picked from commit 0dc0db932e3ad5ad911f2072015cb9854f6e4e23) --- nova/scheduler/host_manager.py | 6 +++--- nova/tests/unit/scheduler/test_host_manager.py | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/nova/scheduler/host_manager.py b/nova/scheduler/host_manager.py index 5a419a18b2f5..64cdece734ea 100644 --- a/nova/scheduler/host_manager.py +++ b/nova/scheduler/host_manager.py @@ -376,7 +376,7 @@ class HostManager(object): for agg in aggs: self.aggs_by_id[agg.id] = agg for host in agg.hosts: - self.host_aggregates_map[host].add(agg.id) + self.host_aggregates_map[host.lower()].add(agg.id) def update_aggregates(self, aggregates): """Updates internal HostManager information about aggregates.""" @@ -395,7 +395,7 @@ class HostManager(object): for host in self.host_aggregates_map: if (aggregate.id in self.host_aggregates_map[host] and host not in aggregate.hosts): - self.host_aggregates_map[host].remove(aggregate.id) + self.host_aggregates_map[host.lower()].remove(aggregate.id) def delete_aggregate(self, aggregate): """Deletes internal HostManager information about a specific aggregate. @@ -714,7 +714,7 @@ class HostManager(object): def _get_aggregates_info(self, host): return [self.aggs_by_id[agg_id] for agg_id in - self.host_aggregates_map[host]] + self.host_aggregates_map[host.lower()]] def _get_instances_by_host(self, context, host_name): try: diff --git a/nova/tests/unit/scheduler/test_host_manager.py b/nova/tests/unit/scheduler/test_host_manager.py index 9b1dfaa9466b..7d4fde61d2d2 100644 --- a/nova/tests/unit/scheduler/test_host_manager.py +++ b/nova/tests/unit/scheduler/test_host_manager.py @@ -173,6 +173,17 @@ class HostManagerTestCase(test.NoDBTestCase): self.assertEqual({'fake-host': set([1])}, self.host_manager.host_aggregates_map) + @mock.patch.object(host_manager.HostManager, '_init_instance_info') + @mock.patch.object(objects.AggregateList, 'get_all') + def test_init_aggregates_one_agg_with_hosts_upper_case(self, agg_get_all, + mock_init_info): + fake_agg = objects.Aggregate(id=1, hosts=['FAKE-host']) + agg_get_all.return_value = [fake_agg] + self.host_manager = host_manager.HostManager() + self.assertEqual({1: fake_agg}, self.host_manager.aggs_by_id) + self.assertEqual({'fake-host': set([1])}, + self.host_manager.host_aggregates_map) + def test_update_aggregates(self): fake_agg = objects.Aggregate(id=1, hosts=['fake-host']) self.host_manager.update_aggregates([fake_agg])