Skip to content

Commit

Permalink
Fix Python 3.12 unittest compatibility
Browse files Browse the repository at this point in the history
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] python/cpython#100690

Change-Id: I90b400f6bafe8f03f04f991d9fe58635d19b8b2e
Signed-off-by: Marcin Wilk <[email protected]>
  • Loading branch information
Marcin Wilk committed Sep 20, 2024
1 parent 0a004b2 commit 6346564
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
5 changes: 3 additions & 2 deletions unit_tests/test_neutron_ovs_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,9 @@ def test_amqp_joined(self):
def test_amqp_changed(self):
self.CONFIGS.complete_contexts.return_value = ['amqp']
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):
self.CONFIGS.complete_contexts.return_value = ['amqp']
self._call_hook('amqp-relation-departed')
self.assertTrue(self.CONFIGS.write.called_with(NEUTRON_CONF))
self.CONFIGS.write_all.assert_called_once()
20 changes: 10 additions & 10 deletions unit_tests/test_neutron_ovs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,43 +1261,43 @@ def test_enable_ovs_dpdk_vhostuser_client(
def test_use_dvr(self, _is_container, _NeutronAPIContext):
_is_container.return_value = False
_NeutronAPIContext()().get.return_value = True
self.assertEquals(nutils.use_dvr(), True)
self.assertTrue(nutils.use_dvr())
_is_container.return_value = True
self.assertEquals(nutils.use_dvr(), False)
self.assertFalse(nutils.use_dvr())

@patch.object(nutils.context, 'NeutronAPIContext')
@patch.object(nutils, 'is_container')
def test_use_l3ha(self, _is_container, _NeutronAPIContext):
_is_container.return_value = False
_NeutronAPIContext()().get.return_value = True
self.assertEquals(nutils.use_l3ha(), True)
self.assertTrue(nutils.use_l3ha())
_is_container.return_value = True
self.assertEquals(nutils.use_l3ha(), False)
self.assertFalse(nutils.use_l3ha())

@patch.object(nutils.context, 'NeutronAPIContext')
@patch.object(nutils, 'is_container')
def test_enable_nova_metadata(self, _is_container, _NeutronAPIContext):
_is_container.return_value = False
_NeutronAPIContext()().get.return_value = True
self.assertEquals(nutils.enable_nova_metadata(), True)
self.assertTrue(nutils.enable_nova_metadata())
_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, 'is_container')
def test_enable_local_dhcp(self, _is_container, _config):
_is_container.return_value = False
_config.return_value = True
self.assertEquals(nutils.enable_local_dhcp(), True)
self.assertTrue(nutils.enable_local_dhcp())
_is_container.return_value = True
self.assertEquals(nutils.enable_local_dhcp(), False)
self.assertFalse(nutils.enable_local_dhcp())

@patch.object(nutils, 'kv')
def test_use_fqdn_hint(self, _kv):
_kv().get.return_value = False
self.assertEquals(nutils.use_fqdn_hint(), False)
self.assertFalse(nutils.use_fqdn_hint())
_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):
self.os_release.return_value = 'rocky'
Expand Down

0 comments on commit 6346564

Please sign in to comment.