From 6c9713b36c076204a7b29781dd3c3a006c6beb52 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Wed, 4 Oct 2023 23:23:11 +0200 Subject: [PATCH] Fix Python 3.12 unit tests (#7348) * Re-enable Python 3.12 unit tests. * Stop using deprecated alias. * Stop using long deprecated subset comparison function. * Avoid another alias. * Fix name, add Python 2 compatibility. * Properly make backwards compatible. --- .azure-pipelines/azure-pipelines.yml | 2 +- .../plugins/callback/test_loganalytics.py | 7 +++++- .../module_utils/hwc/test_hwc_utils.py | 25 +++++++++++++------ tests/unit/plugins/modules/test_dnsimple.py | 4 +-- .../modules/test_jenkins_build_info.py | 6 ++--- tests/unit/plugins/modules/test_pagerduty.py | 24 +++++++++--------- .../plugins/modules/test_pagerduty_alert.py | 6 ++--- tests/unit/plugins/modules/test_pkgin.py | 16 ++++++------ tests/unit/plugins/modules/test_slack.py | 6 ++--- 9 files changed, 55 insertions(+), 41 deletions(-) diff --git a/.azure-pipelines/azure-pipelines.yml b/.azure-pipelines/azure-pipelines.yml index d0fbf0f2c7..071d5b81d1 100644 --- a/.azure-pipelines/azure-pipelines.yml +++ b/.azure-pipelines/azure-pipelines.yml @@ -127,7 +127,7 @@ stages: - test: 3.9 - test: '3.10' - test: '3.11' - # - test: '3.12' TODO + - test: '3.12' - stage: Units_2_16 displayName: Units 2.16 dependsOn: [] diff --git a/tests/unit/plugins/callback/test_loganalytics.py b/tests/unit/plugins/callback/test_loganalytics.py index f9fef3c5d6..17932ed5fa 100644 --- a/tests/unit/plugins/callback/test_loganalytics.py +++ b/tests/unit/plugins/callback/test_loganalytics.py @@ -12,6 +12,7 @@ from ansible_collections.community.general.plugins.callback.loganalytics import from datetime import datetime import json +import sys class TestAzureLogAnalytics(unittest.TestCase): @@ -27,6 +28,10 @@ class TestAzureLogAnalytics(unittest.TestCase): self.mock_host = Mock('MockHost') self.mock_host.name = 'myhost' + # Add backward compatibility + if sys.version_info < (3, 2): + self.assertRegex = self.assertRegexpMatches + @patch('ansible_collections.community.general.plugins.callback.loganalytics.datetime') @patch('ansible_collections.community.general.plugins.callback.loganalytics.open_url') def test_overall(self, open_url_mock, mock_datetime): @@ -62,5 +67,5 @@ class TestAzureLogAnalytics(unittest.TestCase): args, kwargs = open_url_mock.call_args headers = kwargs['headers'] - self.assertRegexpMatches(headers['Authorization'], r'^SharedKey 01234567-0123-0123-0123-01234567890a:.*=$') + self.assertRegex(headers['Authorization'], r'^SharedKey 01234567-0123-0123-0123-01234567890a:.*=$') self.assertEqual(headers['Log-Type'], 'ansible_playbook') diff --git a/tests/unit/plugins/module_utils/hwc/test_hwc_utils.py b/tests/unit/plugins/module_utils/hwc/test_hwc_utils.py index 1344496b18..9b0be0bb48 100644 --- a/tests/unit/plugins/module_utils/hwc/test_hwc_utils.py +++ b/tests/unit/plugins/module_utils/hwc/test_hwc_utils.py @@ -6,11 +6,20 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type +import sys + from ansible_collections.community.general.tests.unit.compat import unittest from ansible_collections.community.general.plugins.module_utils.hwc_utils import (HwcModuleException, navigate_value) class HwcUtilsTestCase(unittest.TestCase): + def setUp(self): + super(HwcUtilsTestCase, self).setUp() + + # Add backward compatibility + if sys.version_info < (3, 0): + self.assertRaisesRegex = self.assertRaisesRegexp + def test_navigate_value(self): value = { 'foo': { @@ -29,12 +38,12 @@ class HwcUtilsTestCase(unittest.TestCase): {"foo.quiet.trees": 1}), 1) - self.assertRaisesRegexp(HwcModuleException, - r".* key\(q\) is not exist in dict", - navigate_value, value, ["foo", "q", "tree"]) + self.assertRaisesRegex(HwcModuleException, + r".* key\(q\) is not exist in dict", + navigate_value, value, ["foo", "q", "tree"]) - self.assertRaisesRegexp(HwcModuleException, - r".* the index is out of list", - navigate_value, value, - ["foo", "quiet", "trees"], - {"foo.quiet.trees": 2}) + self.assertRaisesRegex(HwcModuleException, + r".* the index is out of list", + navigate_value, value, + ["foo", "quiet", "trees"], + {"foo.quiet.trees": 2}) diff --git a/tests/unit/plugins/modules/test_dnsimple.py b/tests/unit/plugins/modules/test_dnsimple.py index 95a78818d2..d5578252dc 100644 --- a/tests/unit/plugins/modules/test_dnsimple.py +++ b/tests/unit/plugins/modules/test_dnsimple.py @@ -45,7 +45,7 @@ class TestDNSimple(ModuleTestCase): def test_account_token(self, mock_whoami): mock_whoami.return_value.data.account = 42 ds = self.module.DNSimpleV2('fake', 'fake', True, self.module) - self.assertEquals(ds.account, 42) + self.assertEqual(ds.account, 42) @patch('dnsimple.service.Accounts.list_accounts') @patch('dnsimple.service.Identity.whoami') @@ -61,4 +61,4 @@ class TestDNSimple(ModuleTestCase): mock_accounts.return_value.data = [42] mock_whoami.return_value.data.account = None ds = self.module.DNSimpleV2('fake', 'fake', True, self.module) - self.assertEquals(ds.account, 42) + self.assertEqual(ds.account, 42) diff --git a/tests/unit/plugins/modules/test_jenkins_build_info.py b/tests/unit/plugins/modules/test_jenkins_build_info.py index 58a2d65b3d..b5d4126fe0 100644 --- a/tests/unit/plugins/modules/test_jenkins_build_info.py +++ b/tests/unit/plugins/modules/test_jenkins_build_info.py @@ -140,7 +140,7 @@ class TestJenkinsBuildInfo(unittest.TestCase): self.assertFalse(return_json.exception.args[0]['changed']) self.assertTrue(return_json.exception.args[0]['failed']) - self.assertEquals("ABSENT", return_json.exception.args[0]['build_info']['result']) + self.assertEqual("ABSENT", return_json.exception.args[0]['build_info']['result']) @patch('ansible_collections.community.general.plugins.modules.jenkins_build_info.test_dependencies') @patch('ansible_collections.community.general.plugins.modules.jenkins_build_info.JenkinsBuildInfo.get_jenkins_connection') @@ -157,7 +157,7 @@ class TestJenkinsBuildInfo(unittest.TestCase): jenkins_build_info.main() self.assertFalse(return_json.exception.args[0]['changed']) - self.assertEquals("SUCCESS", return_json.exception.args[0]['build_info']['result']) + self.assertEqual("SUCCESS", return_json.exception.args[0]['build_info']['result']) @patch('ansible_collections.community.general.plugins.modules.jenkins_build_info.test_dependencies') @patch('ansible_collections.community.general.plugins.modules.jenkins_build_info.JenkinsBuildInfo.get_jenkins_connection') @@ -177,4 +177,4 @@ class TestJenkinsBuildInfo(unittest.TestCase): self.assertFalse(return_json.exception.args[0]['changed']) self.assertTrue(return_json.exception.args[0]['failed']) - self.assertEquals("ABSENT", return_json.exception.args[0]['build_info']['result']) + self.assertEqual("ABSENT", return_json.exception.args[0]['build_info']['result']) diff --git a/tests/unit/plugins/modules/test_pagerduty.py b/tests/unit/plugins/modules/test_pagerduty.py index d363804bc7..5f12fda8db 100644 --- a/tests/unit/plugins/modules/test_pagerduty.py +++ b/tests/unit/plugins/modules/test_pagerduty.py @@ -20,9 +20,9 @@ class PagerDutyTest(unittest.TestCase): return object(), {'status': 200} def _assert_ongoing_window_with_v1_compatible_header(self, module, url, headers, data=None, method=None): - self.assertDictContainsSubset( - {'Accept': 'application/vnd.pagerduty+json;version=2'}, - headers, + self.assertEqual( + 'application/vnd.pagerduty+json;version=2', + headers.get('Accept'), 'Accept:application/vnd.pagerduty+json;version=2 HTTP header not found' ) return object(), {'status': 200} @@ -36,17 +36,17 @@ class PagerDutyTest(unittest.TestCase): return object(), {'status': 201} def _assert_create_a_maintenance_window_from_header(self, module, url, headers, data=None, method=None): - self.assertDictContainsSubset( - {'From': 'requester_id'}, - headers, + self.assertEqual( + 'requester_id', + headers.get('From'), 'From:requester_id HTTP header not found' ) return object(), {'status': 201} def _assert_create_window_with_v1_compatible_header(self, module, url, headers, data=None, method=None): - self.assertDictContainsSubset( - {'Accept': 'application/vnd.pagerduty+json;version=2'}, - headers, + self.assertEqual( + 'application/vnd.pagerduty+json;version=2', + headers.get('Accept'), 'Accept:application/vnd.pagerduty+json;version=2 HTTP header not found' ) return object(), {'status': 201} @@ -89,9 +89,9 @@ class PagerDutyTest(unittest.TestCase): return object(), {'status': 204} def _assert_absent_window_with_v1_compatible_header(self, module, url, headers, method=None): - self.assertDictContainsSubset( - {'Accept': 'application/vnd.pagerduty+json;version=2'}, - headers, + self.assertEqual( + 'application/vnd.pagerduty+json;version=2', + headers.get('Accept'), 'Accept:application/vnd.pagerduty+json;version=2 HTTP header not found' ) return object(), {'status': 204} diff --git a/tests/unit/plugins/modules/test_pagerduty_alert.py b/tests/unit/plugins/modules/test_pagerduty_alert.py index 958bd3fab7..7a1e951a24 100644 --- a/tests/unit/plugins/modules/test_pagerduty_alert.py +++ b/tests/unit/plugins/modules/test_pagerduty_alert.py @@ -22,9 +22,9 @@ class PagerDutyAlertsTest(unittest.TestCase): return Response(), {'status': 200} def _assert_compatibility_header(self, module, url, method, headers): - self.assertDictContainsSubset( - {'Accept': 'application/vnd.pagerduty+json;version=2'}, - headers, + self.assertEqual( + 'application/vnd.pagerduty+json;version=2', + headers.get('Accept'), 'Accept:application/vnd.pagerduty+json;version=2 HTTP header not found' ) return Response(), {'status': 200} diff --git a/tests/unit/plugins/modules/test_pkgin.py b/tests/unit/plugins/modules/test_pkgin.py index d73911e0c3..dea5a05b5c 100644 --- a/tests/unit/plugins/modules/test_pkgin.py +++ b/tests/unit/plugins/modules/test_pkgin.py @@ -30,7 +30,7 @@ class TestPkginQueryPackage(unittest.TestCase): command_result = pkgin.query_package(mock_module, package) # then - self.assertEquals(command_result, pkgin.PackageState.PRESENT) + self.assertEqual(command_result, pkgin.PackageState.PRESENT) @mock.patch('ansible_collections.community.general.plugins.modules.pkgin.AnsibleModule') def test_package_with_version_is_present(self, mock_module): @@ -46,7 +46,7 @@ class TestPkginQueryPackage(unittest.TestCase): command_result = pkgin.query_package(mock_module, package) # then - self.assertEquals(command_result, pkgin.PackageState.PRESENT) + self.assertEqual(command_result, pkgin.PackageState.PRESENT) @mock.patch('ansible_collections.community.general.plugins.modules.pkgin.AnsibleModule') def test_package_found_but_not_installed(self, mock_module): @@ -62,7 +62,7 @@ class TestPkginQueryPackage(unittest.TestCase): command_result = pkgin.query_package(mock_module, package) # then - self.assertEquals(command_result, pkgin.PackageState.NOT_INSTALLED) + self.assertEqual(command_result, pkgin.PackageState.NOT_INSTALLED) @mock.patch('ansible_collections.community.general.plugins.modules.pkgin.AnsibleModule') def test_package_found_outdated(self, mock_module): @@ -78,7 +78,7 @@ class TestPkginQueryPackage(unittest.TestCase): command_result = pkgin.query_package(mock_module, package) # then - self.assertEquals(command_result, pkgin.PackageState.OUTDATED) + self.assertEqual(command_result, pkgin.PackageState.OUTDATED) @mock.patch('ansible_collections.community.general.plugins.modules.pkgin.AnsibleModule') def test_package_with_version_found_outdated(self, mock_module): @@ -94,7 +94,7 @@ class TestPkginQueryPackage(unittest.TestCase): command_result = pkgin.query_package(mock_module, package) # then - self.assertEquals(command_result, pkgin.PackageState.OUTDATED) + self.assertEqual(command_result, pkgin.PackageState.OUTDATED) @mock.patch('ansible_collections.community.general.plugins.modules.pkgin.AnsibleModule') def test_package_not_found(self, mock_module): @@ -110,7 +110,7 @@ class TestPkginQueryPackage(unittest.TestCase): command_result = pkgin.query_package(mock_module, package) # then - self.assertEquals(command_result, pkgin.PackageState.NOT_FOUND) + self.assertEqual(command_result, pkgin.PackageState.NOT_FOUND) @mock.patch('ansible_collections.community.general.plugins.modules.pkgin.AnsibleModule') def test_with_parseable_flag_supported_package_is_present(self, mock_module): @@ -126,7 +126,7 @@ class TestPkginQueryPackage(unittest.TestCase): command_result = pkgin.query_package(mock_module, package) # then - self.assertEquals(command_result, pkgin.PackageState.PRESENT) + self.assertEqual(command_result, pkgin.PackageState.PRESENT) @mock.patch('ansible_collections.community.general.plugins.modules.pkgin.AnsibleModule') def test_with_parseable_flag_not_supported_package_is_present(self, mock_module): @@ -142,4 +142,4 @@ class TestPkginQueryPackage(unittest.TestCase): command_result = pkgin.query_package(mock_module, package) # then - self.assertEquals(command_result, pkgin.PackageState.PRESENT) + self.assertEqual(command_result, pkgin.PackageState.PRESENT) diff --git a/tests/unit/plugins/modules/test_slack.py b/tests/unit/plugins/modules/test_slack.py index ab4405baa7..52ac9b7f37 100644 --- a/tests/unit/plugins/modules/test_slack.py +++ b/tests/unit/plugins/modules/test_slack.py @@ -105,7 +105,7 @@ class TestSlackModule(ModuleTestCase): self.module.main() self.assertTrue(fetch_url_mock.call_count, 1) - self.assertEquals(fetch_url_mock.call_args[1]['url'], "https://slack.com/api/chat.postMessage") + self.assertEqual(fetch_url_mock.call_args[1]['url'], "https://slack.com/api/chat.postMessage") def test_edit_message(self): set_module_args({ @@ -125,9 +125,9 @@ class TestSlackModule(ModuleTestCase): self.module.main() self.assertTrue(fetch_url_mock.call_count, 2) - self.assertEquals(fetch_url_mock.call_args[1]['url'], "https://slack.com/api/chat.update") + self.assertEqual(fetch_url_mock.call_args[1]['url'], "https://slack.com/api/chat.update") call_data = json.loads(fetch_url_mock.call_args[1]['data']) - self.assertEquals(call_data['ts'], "12345") + self.assertEqual(call_data['ts'], "12345") def test_message_with_blocks(self): """tests sending a message with blocks"""