From c9b558b0ff253c4fc7665c8dfd95ae58d713bc74 Mon Sep 17 00:00:00 2001 From: Alex Kavanagh Date: Fri, 14 Jun 2019 15:11:04 +0100 Subject: [PATCH] Ensure that get_host_ip() is mocked out in test The test test_setup_ufw() didn't mock out get_host_ip() which meant that the test leaked and used an actual IP hostname lookup. This failed when ubuntu.com's IP address changed. This patch mocks it out properly. Change-Id: Ida7aaa7d085cc0316aa496d0a0e24333c5828cee Closes-Bug: #1832829 --- unit_tests/test_swift_storage_utils.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/unit_tests/test_swift_storage_utils.py b/unit_tests/test_swift_storage_utils.py index 4bff0c3..309e8ab 100644 --- a/unit_tests/test_swift_storage_utils.py +++ b/unit_tests/test_swift_storage_utils.py @@ -616,9 +616,10 @@ class SwiftStorageUtilsTests(CharmTestCase): swift_utils.revoke_access(addr, port) self.ufw.revoke_access.assert_called_with(addr, port=port, proto='tcp') + @patch.object(swift_utils, 'get_host_ip') @patch.object(swift_utils, 'RsyncContext') @patch.object(swift_utils, 'grant_access') - def test_setup_ufw(self, mock_grant_access, mock_rsync): + def test_setup_ufw(self, mock_grant_access, mock_rsync, mock_get_host_ip): peer_addr_1 = '10.1.1.1' peer_addr_2 = '10.1.1.2' client_addrs = ['10.3.3.1', '10.3.3.2','10.3.3.3', 'ubuntu.com'] @@ -637,7 +638,6 @@ class SwiftStorageUtilsTests(CharmTestCase): context_call.return_value = {'allowed_hosts': '{} {}' ''.format(peer_addr_1, peer_addr_2)} mock_rsync.return_value = context_call - swift_utils.setup_ufw() calls = [] for addr in [peer_addr_1, peer_addr_2] + client_addrs: for port in ports: @@ -645,4 +645,14 @@ class SwiftStorageUtilsTests(CharmTestCase): calls.append(call('91.189.94.40', port)) else: calls.append(call(addr, port)) + + def _get_host_ip(ip): + if ip == 'ubuntu.com': + return '91.189.94.40' + else: + return ip + + mock_get_host_ip.side_effect = _get_host_ip + + swift_utils.setup_ufw() mock_grant_access.assert_has_calls(calls)