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: I25df688c9ec07168b825815b1e1c27ecf2673d2b
This commit is contained in:
Alex Kavanagh 2023-10-24 12:01:42 +01:00
parent f022edc81f
commit 3d237a44ec
2 changed files with 16 additions and 1 deletions

View File

@ -32,3 +32,15 @@ def _add_path(path):
_add_path(_root) _add_path(_root)
# 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()

View File

@ -141,6 +141,7 @@ class NovaComputeContextTests(CharmTestCase):
self.assertEqual(ctxt['nova_url'], 'http://10.0.1.1:8774/v2') self.assertEqual(ctxt['nova_url'], 'http://10.0.1.1:8774/v2')
self.assertFalse('neutron_url' in ctxt) self.assertFalse('neutron_url' in ctxt)
@mock.patch('charmhelpers.contrib.openstack.context.is_ipv6_disabled')
@mock.patch('charmhelpers.core.hookenv.relation_ids') @mock.patch('charmhelpers.core.hookenv.relation_ids')
@mock.patch('charmhelpers.contrib.openstack.context.config') @mock.patch('charmhelpers.contrib.openstack.context.config')
@mock.patch('charmhelpers.contrib.openstack.context.get_relation_ip') @mock.patch('charmhelpers.contrib.openstack.context.get_relation_ip')
@ -159,12 +160,14 @@ class NovaComputeContextTests(CharmTestCase):
mock_local_unit, mock_get_netmask_for_address, mock_local_unit, mock_get_netmask_for_address,
mock_get_address_in_network, mock_kv, mock_https, mock_get_address_in_network, mock_kv, mock_https,
mock_network_manager, mock_mkdir, mock_network_manager, mock_mkdir,
mock_get_relation_ip, mock_config, mock_rids): mock_get_relation_ip, mock_config, mock_rids,
mock_is_ipv6_disabled):
self.os_release.return_value = 'ocata' self.os_release.return_value = 'ocata'
mock_config.side_effect = self.test_config.get mock_config.side_effect = self.test_config.get
mock_https.return_value = False mock_https.return_value = False
mock_network_manager.return_value = 'neutron' mock_network_manager.return_value = 'neutron'
mock_rids.return_value = [] mock_rids.return_value = []
mock_is_ipv6_disabled.return_value = True
ctxt = context.HAProxyContext()() ctxt = context.HAProxyContext()()
self.assertEqual(ctxt['service_ports']['nova-api-os-compute'], self.assertEqual(ctxt['service_ports']['nova-api-os-compute'],
[8774, 8764]) [8774, 8764])