Fix Python 3.12 unittest compatibility

Python 3.12 has removed long-deprecated unittest features [1],
therefore some of the existing unit test fail when run on Python
3.12. This patch replaces the 'assertEquals' with 'assertTrue' or
'assertFalse', depending on the usage.

This patch also fixes the 'test_amqp_changed' and 'test_amqp_departed'
test methods which improperly called 'called_with' which is no longer
a valid assertion method [2]. And in fact on Python < 3.12 the
'test_amqp_changed' and 'test_amqp_departed' pass their assertions,
when they actually should fail due to the out-of-sync with
'amqp-relation-changed' and 'amqp-relation-departed' hook code.
In this case the 'called_with' is replaced with 'assert_called_once'.

[1] https://docs.python.org/3.12/whatsnew/3.12.html#id3
[2] https://github.com/python/cpython/issues/100690

Change-Id: I90b400f6bafe8f03f04f991d9fe58635d19b8b2e
Signed-off-by: Marcin Wilk <marcin.wilk@canonical.com>
This commit is contained in:
Marcin Wilk 2024-09-19 10:38:01 +02:00
parent 0a004b25b1
commit 63465640fe
2 changed files with 13 additions and 12 deletions

View File

@ -309,8 +309,9 @@ class NeutronOVSHooksTests(CharmTestCase):
def test_amqp_changed(self): def test_amqp_changed(self):
self.CONFIGS.complete_contexts.return_value = ['amqp'] self.CONFIGS.complete_contexts.return_value = ['amqp']
self._call_hook('amqp-relation-changed') self._call_hook('amqp-relation-changed')
self.assertTrue(self.CONFIGS.write.called_with(NEUTRON_CONF)) self.CONFIGS.write_all.assert_called_once()
def test_amqp_departed(self): def test_amqp_departed(self):
self.CONFIGS.complete_contexts.return_value = ['amqp']
self._call_hook('amqp-relation-departed') self._call_hook('amqp-relation-departed')
self.assertTrue(self.CONFIGS.write.called_with(NEUTRON_CONF)) self.CONFIGS.write_all.assert_called_once()

View File

@ -1261,43 +1261,43 @@ class TestNeutronOVSUtils(CharmTestCase):
def test_use_dvr(self, _is_container, _NeutronAPIContext): def test_use_dvr(self, _is_container, _NeutronAPIContext):
_is_container.return_value = False _is_container.return_value = False
_NeutronAPIContext()().get.return_value = True _NeutronAPIContext()().get.return_value = True
self.assertEquals(nutils.use_dvr(), True) self.assertTrue(nutils.use_dvr())
_is_container.return_value = True _is_container.return_value = True
self.assertEquals(nutils.use_dvr(), False) self.assertFalse(nutils.use_dvr())
@patch.object(nutils.context, 'NeutronAPIContext') @patch.object(nutils.context, 'NeutronAPIContext')
@patch.object(nutils, 'is_container') @patch.object(nutils, 'is_container')
def test_use_l3ha(self, _is_container, _NeutronAPIContext): def test_use_l3ha(self, _is_container, _NeutronAPIContext):
_is_container.return_value = False _is_container.return_value = False
_NeutronAPIContext()().get.return_value = True _NeutronAPIContext()().get.return_value = True
self.assertEquals(nutils.use_l3ha(), True) self.assertTrue(nutils.use_l3ha())
_is_container.return_value = True _is_container.return_value = True
self.assertEquals(nutils.use_l3ha(), False) self.assertFalse(nutils.use_l3ha())
@patch.object(nutils.context, 'NeutronAPIContext') @patch.object(nutils.context, 'NeutronAPIContext')
@patch.object(nutils, 'is_container') @patch.object(nutils, 'is_container')
def test_enable_nova_metadata(self, _is_container, _NeutronAPIContext): def test_enable_nova_metadata(self, _is_container, _NeutronAPIContext):
_is_container.return_value = False _is_container.return_value = False
_NeutronAPIContext()().get.return_value = True _NeutronAPIContext()().get.return_value = True
self.assertEquals(nutils.enable_nova_metadata(), True) self.assertTrue(nutils.enable_nova_metadata())
_is_container.return_value = True _is_container.return_value = True
self.assertEquals(nutils.enable_nova_metadata(), False) self.assertFalse(nutils.enable_nova_metadata())
@patch.object(nutils, 'config') @patch.object(nutils, 'config')
@patch.object(nutils, 'is_container') @patch.object(nutils, 'is_container')
def test_enable_local_dhcp(self, _is_container, _config): def test_enable_local_dhcp(self, _is_container, _config):
_is_container.return_value = False _is_container.return_value = False
_config.return_value = True _config.return_value = True
self.assertEquals(nutils.enable_local_dhcp(), True) self.assertTrue(nutils.enable_local_dhcp())
_is_container.return_value = True _is_container.return_value = True
self.assertEquals(nutils.enable_local_dhcp(), False) self.assertFalse(nutils.enable_local_dhcp())
@patch.object(nutils, 'kv') @patch.object(nutils, 'kv')
def test_use_fqdn_hint(self, _kv): def test_use_fqdn_hint(self, _kv):
_kv().get.return_value = False _kv().get.return_value = False
self.assertEquals(nutils.use_fqdn_hint(), False) self.assertFalse(nutils.use_fqdn_hint())
_kv().get.return_value = True _kv().get.return_value = True
self.assertEquals(nutils.use_fqdn_hint(), True) self.assertTrue(nutils.use_fqdn_hint())
def test_use_hw_offload_rocky(self): def test_use_hw_offload_rocky(self):
self.os_release.return_value = 'rocky' self.os_release.return_value = 'rocky'