diff options
author | Matt Clay <matt@mystile.com> | 2023-10-12 02:31:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-12 02:31:04 +0200 |
commit | 9295bb5b5d12b7d1ac9f24c7adc1e96d5e5893b5 (patch) | |
tree | 54ec45a491bf10231382c04bfb4b9ffaea21fa14 /test/units/module_utils | |
parent | ansible-test - Update base/default containers (#81957) (diff) | |
download | ansible-9295bb5b5d12b7d1ac9f24c7adc1e96d5e5893b5.tar.xz ansible-9295bb5b5d12b7d1ac9f24c7adc1e96d5e5893b5.zip |
Unit test cleanup (#81958)
Remove obsolete Python 2.7 and 3.6 support code and fix up existing tests.
Diffstat (limited to 'test/units/module_utils')
-rw-r--r-- | test/units/module_utils/basic/test_exit_json.py | 4 | ||||
-rw-r--r-- | test/units/module_utils/basic/test_get_available_hash_algorithms.py | 31 | ||||
-rw-r--r-- | test/units/module_utils/basic/test_imports.py | 27 |
3 files changed, 1 insertions, 61 deletions
diff --git a/test/units/module_utils/basic/test_exit_json.py b/test/units/module_utils/basic/test_exit_json.py index 803f20e897..ef44cecfa0 100644 --- a/test/units/module_utils/basic/test_exit_json.py +++ b/test/units/module_utils/basic/test_exit_json.py @@ -100,9 +100,7 @@ class TestAnsibleModuleExitJson: with pytest.raises(TypeError) as ctx: am.fail_json() - if sys.version_info < (3,): - error_msg = "fail_json() takes exactly 2 arguments (1 given)" - elif sys.version_info >= (3, 10): + if sys.version_info >= (3, 10): error_msg = "AnsibleModule.fail_json() missing 1 required positional argument: 'msg'" else: error_msg = "fail_json() missing 1 required positional argument: 'msg'" diff --git a/test/units/module_utils/basic/test_get_available_hash_algorithms.py b/test/units/module_utils/basic/test_get_available_hash_algorithms.py index d22efc8f74..e7fb76b2b5 100644 --- a/test/units/module_utils/basic/test_get_available_hash_algorithms.py +++ b/test/units/module_utils/basic/test_get_available_hash_algorithms.py @@ -2,16 +2,9 @@ from __future__ import annotations - -import hashlib -import sys - -import pytest - from ansible.module_utils.basic import _get_available_hash_algorithms -@pytest.mark.skipif(sys.version_info < (2, 7, 9), reason="requires Python 2.7.9 or later") def test_unavailable_algorithm(mocker): """Simulate an available algorithm that isn't.""" expected_algorithms = {'sha256', 'sha512'} # guaranteed to be available @@ -23,7 +16,6 @@ def test_unavailable_algorithm(mocker): assert sorted(expected_algorithms) == sorted(available_algorithms) -@pytest.mark.skipif(sys.version_info < (2, 7, 9), reason="requires Python 2.7.9 or later") def test_fips_mode(mocker): """Simulate running in FIPS mode on Python 2.7.9 or later.""" expected_algorithms = {'sha256', 'sha512'} # guaranteed to be available @@ -34,26 +26,3 @@ def test_fips_mode(mocker): available_algorithms = _get_available_hash_algorithms() assert sorted(expected_algorithms) == sorted(available_algorithms) - - -@pytest.mark.skipif(sys.version_info < (2, 7, 9) or sys.version_info[:2] != (2, 7), reason="requires Python 2.7 (2.7.9 or later)") -def test_legacy_python(mocker): - """Simulate behavior on Python 2.7.x earlier than Python 2.7.9.""" - expected_algorithms = {'sha256', 'sha512'} # guaranteed to be available - - # This attribute is exclusive to Python 2.7. - # Since `hashlib.algorithms_available` is used on Python 2.7.9 and later, only Python 2.7.0 through 2.7.8 utilize this attribute. - mocker.patch('hashlib.algorithms', expected_algorithms) - - saved_algorithms = hashlib.algorithms_available - - # Make sure that this attribute is unavailable, to simulate running on Python 2.7.0 through 2.7.8. - # It will be restored immediately after performing the test. - del hashlib.algorithms_available - - try: - available_algorithms = _get_available_hash_algorithms() - finally: - hashlib.algorithms_available = saved_algorithms - - assert sorted(expected_algorithms) == sorted(available_algorithms) diff --git a/test/units/module_utils/basic/test_imports.py b/test/units/module_utils/basic/test_imports.py index 93b357bb36..0d4f0027b8 100644 --- a/test/units/module_utils/basic/test_imports.py +++ b/test/units/module_utils/basic/test_imports.py @@ -10,7 +10,6 @@ import sys from units.mock.procenv import ModuleTestCase -from units.compat import unittest from units.compat.mock import patch from ansible.module_utils.six.moves import builtins @@ -67,32 +66,6 @@ class TestImports(ModuleTestCase): # from ansible.module_utils import basic @patch.object(builtins, '__import__') - @unittest.skipIf(sys.version_info[0] >= 3, "literal_eval is available in every version of Python3") - def test_module_utils_basic_import_literal_eval(self, mock_import): - def _mock_import(name, *args, **kwargs): - try: - fromlist = kwargs.get('fromlist', args[2]) - except IndexError: - fromlist = [] - if name == 'ast' and 'literal_eval' in fromlist: - raise ImportError - return realimport(name, *args, **kwargs) - - mock_import.side_effect = _mock_import - self.clear_modules(['ast', 'ansible.module_utils.basic']) - mod = builtins.__import__('ansible.module_utils.basic') - self.assertEqual(mod.module_utils.basic.literal_eval("'1'"), "1") - self.assertEqual(mod.module_utils.basic.literal_eval("1"), 1) - self.assertEqual(mod.module_utils.basic.literal_eval("-1"), -1) - self.assertEqual(mod.module_utils.basic.literal_eval("(1,2,3)"), (1, 2, 3)) - self.assertEqual(mod.module_utils.basic.literal_eval("[1]"), [1]) - self.assertEqual(mod.module_utils.basic.literal_eval("True"), True) - self.assertEqual(mod.module_utils.basic.literal_eval("False"), False) - self.assertEqual(mod.module_utils.basic.literal_eval("None"), None) - # self.assertEqual(mod.module_utils.basic.literal_eval('{"a": 1}'), dict(a=1)) - self.assertRaises(ValueError, mod.module_utils.basic.literal_eval, "asdfasdfasdf") - - @patch.object(builtins, '__import__') def test_module_utils_basic_import_systemd_journal(self, mock_import): def _mock_import(name, *args, **kwargs): try: |