From 63465640fef8d545e8038f1960c17da0201bc03d Mon Sep 17 00:00:00 2001
From: Marcin Wilk <marcin.wilk@canonical.com>
Date: Thu, 19 Sep 2024 10:38:01 +0200
Subject: [PATCH] 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>
---
 unit_tests/test_neutron_ovs_hooks.py |  5 +++--
 unit_tests/test_neutron_ovs_utils.py | 20 ++++++++++----------
 2 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/unit_tests/test_neutron_ovs_hooks.py b/unit_tests/test_neutron_ovs_hooks.py
index 99110bbf..359dd36b 100644
--- a/unit_tests/test_neutron_ovs_hooks.py
+++ b/unit_tests/test_neutron_ovs_hooks.py
@@ -309,8 +309,9 @@ class NeutronOVSHooksTests(CharmTestCase):
     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()
diff --git a/unit_tests/test_neutron_ovs_utils.py b/unit_tests/test_neutron_ovs_utils.py
index 120b505b..1222583f 100644
--- a/unit_tests/test_neutron_ovs_utils.py
+++ b/unit_tests/test_neutron_ovs_utils.py
@@ -1261,43 +1261,43 @@ class TestNeutronOVSUtils(CharmTestCase):
     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'