From 94cedb68404a94eed2b051e91fde6307eedf3654 Mon Sep 17 00:00:00 2001 From: Alex Kavanagh Date: Tue, 24 Oct 2023 15:52:30 +0100 Subject: [PATCH] Improve platform mocking Patch out charmhelpers.osplatform.get_platform() and charmhelpers.core.host.lsb_release() globally in the unit tests to insulate the unit tests from the platform that the unit tests are being run on. Change-Id: I2cc91999ccad52c8e42797a3d5ffbf65f9cb36d2 --- unit_tests/__init__.py | 12 ++++++++++++ unit_tests/test_neutron_api_context.py | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/unit_tests/__init__.py b/unit_tests/__init__.py index 2bd02ad2..71a2a600 100644 --- a/unit_tests/__init__.py +++ b/unit_tests/__init__.py @@ -23,3 +23,15 @@ sys.modules['apt_pkg'] = mock.MagicMock() sys.path.append('actions/') sys.path.append('hooks/') sys.path.append('unit_tests') + +# Patch out lsb_release() and get_platform() as unit tests should be fully +# insulated from the underlying platform. Unit tests assume that the system is +# ubuntu jammy. +mock.patch( + 'charmhelpers.osplatform.get_platform', return_value='ubuntu' +).start() +mock.patch( + 'charmhelpers.core.host.lsb_release', + return_value={ + 'DISTRIB_CODENAME': 'jammy' + }).start() diff --git a/unit_tests/test_neutron_api_context.py b/unit_tests/test_neutron_api_context.py index 104a1a93..92749e10 100644 --- a/unit_tests/test_neutron_api_context.py +++ b/unit_tests/test_neutron_api_context.py @@ -385,7 +385,7 @@ class HAProxyContextTest(CharmTestCase): with patch('builtins.__import__'): self.assertTrue('units' not in hap_ctxt()) - @patch.object(charmhelpers.contrib.network.ip, 'is_ipv6_disabled') + @patch.object(charmhelpers.contrib.openstack.context, 'is_ipv6_disabled') @patch.object(charmhelpers.contrib.openstack.context, 'get_relation_ip') @patch.object(charmhelpers.contrib.openstack.context, 'mkdir') @patch.object( @@ -445,7 +445,7 @@ class HAProxyContextTest(CharmTestCase): 'default_backend': '10.10.10.11', 'service_ports': service_ports, 'neutron_bind_port': 9686, - 'ipv6_enabled': True, + 'ipv6_enabled': False, 'https': False, 'backend_options': backend_options, }