1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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.
This commit is contained in:
Felix Fontein 2023-10-04 23:23:11 +02:00 committed by GitHub
parent 160e00e5b9
commit 6c9713b36c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 55 additions and 41 deletions

View file

@ -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: []

View file

@ -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')

View file

@ -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})

View file

@ -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)

View file

@ -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'])

View file

@ -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}

View file

@ -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}

View file

@ -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)

View file

@ -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"""