From e2518f359ee2d4c28bafe4ed6c4e2211e6ad6547 Mon Sep 17 00:00:00 2001 From: Alex Kavanagh Date: Sun, 16 Oct 2022 20:27:42 +0100 Subject: [PATCH] In nova_cc_utils.py:resolve_hosts_for: .append -> .add The method was refactored (in part) to use sets to enforce uniqueness of the hosts. Unfortunately, a list method (.append()) slipped through that should have been converted to .add(). This fixes that error. Change-Id: I248430cd1a9156efab745fe110a39441b503b3a5 Closes-Bug: #1992789 (cherry picked from commit 007f9e33b0f111bcd22aebfa785bb5066832e802) --- hooks/nova_cc_utils.py | 2 +- unit_tests/test_nova_cc_utils.py | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/hooks/nova_cc_utils.py b/hooks/nova_cc_utils.py index a6d4cdb6..fffe7139 100644 --- a/hooks/nova_cc_utils.py +++ b/hooks/nova_cc_utils.py @@ -1381,7 +1381,7 @@ def resolve_hosts_for(private_address, hostname): hosts.add(hostname) if not ch_utils.is_ip(private_address): - hosts.append(private_address.lower()) + hosts.add(private_address.lower()) hosts.add(ch_utils.get_host_ip(private_address)) short = private_address.split('.')[0] if ch_ip.ns_query(short): diff --git a/unit_tests/test_nova_cc_utils.py b/unit_tests/test_nova_cc_utils.py index abc106e7..17e1057e 100644 --- a/unit_tests/test_nova_cc_utils.py +++ b/unit_tests/test_nova_cc_utils.py @@ -862,6 +862,28 @@ class NovaCCUtilsTests(CharmTestCase): utils.ssh_compute_remove(removed_key) self.assertEqual(_written, keys_removed) + @patch.object(utils.ch_utils, 'get_host_ip') + @patch.object(utils.ch_ip, 'ns_query') + def test_resolve_hosts_for_bug_1992789(self, _ns_query, _get_host_ip): + # regression test for bug: 1992789; when the function was refactored to + # use a set instead of a list to ensure uniqueness of hosts, a method + # was missed from being converted from .append() to .add() + private_address = "myhostname" # trigger behaviour of bug + hostname = "myhostname2" + db_key = "hostset-{}".format(private_address) + + # note that this is patched in class setup to :memory: sqlite db + db = charmhelpers.core.unitdata.kv() + db.unset(db_key) + + # ensure caching is disabled + self.test_config.set('cache-known-hosts', False) + _get_host_ip.return_value = "10.0.0.10" + _ns_query.return_value = False + hosts = utils.resolve_hosts_for(private_address, hostname) + self.assertEqual(sorted(hosts), + ["10.0.0.10", "myhostname", "myhostname2"]) + def test_determine_endpoints_base(self): self.relation_ids.return_value = [] self.os_release.return_value = 'diablo'