diff --git a/test/units/cli/test_galaxy.py b/test/units/cli/test_galaxy.py index f0be9ebad87..8f7e891e7d0 100644 --- a/test/units/cli/test_galaxy.py +++ b/test/units/cli/test_galaxy.py @@ -81,9 +81,8 @@ class TestGalaxy(unittest.TestCase): # creating a temp file with installation requirements cls.role_req = './delete_me_requirements.yml' - fd = open(cls.role_req, "w") - fd.write("- 'src': '%s'\n 'name': '%s'\n 'path': '%s'" % (cls.role_tar, cls.role_name, cls.role_path)) - fd.close() + with open(cls.role_req, "w") as fd: + fd.write("- 'src': '%s'\n 'name': '%s'\n 'path': '%s'" % (cls.role_tar, cls.role_name, cls.role_path)) @classmethod def makeTar(cls, output_file, source_dir): diff --git a/test/units/module_utils/facts/hardware/linux_data.py b/test/units/module_utils/facts/hardware/linux_data.py index b33ee7d83df..f92f14ebf5d 100644 --- a/test/units/module_utils/facts/hardware/linux_data.py +++ b/test/units/module_utils/facts/hardware/linux_data.py @@ -18,6 +18,12 @@ __metaclass__ = type import os + +def read_lines(path): + with open(path) as file: + return file.readlines() + + LSBLK_OUTPUT = b""" /dev/sda /dev/sda1 32caaec3-ef40-4691-a3b6-438c3f9bc1c0 @@ -368,7 +374,7 @@ CPU_INFO_TEST_SCENARIOS = [ 'architecture': 'armv61', 'nproc_out': 1, 'sched_getaffinity': set([0]), - 'cpuinfo': open(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/armv6-rev7-1cpu-cpuinfo')).readlines(), + 'cpuinfo': read_lines(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/armv6-rev7-1cpu-cpuinfo')), 'expected_result': { 'processor': ['0', 'ARMv6-compatible processor rev 7 (v6l)'], 'processor_cores': 1, @@ -381,7 +387,7 @@ CPU_INFO_TEST_SCENARIOS = [ 'architecture': 'armv71', 'nproc_out': 4, 'sched_getaffinity': set([0, 1, 2, 3]), - 'cpuinfo': open(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/armv7-rev4-4cpu-cpuinfo')).readlines(), + 'cpuinfo': read_lines(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/armv7-rev4-4cpu-cpuinfo')), 'expected_result': { 'processor': [ '0', 'ARMv7 Processor rev 4 (v7l)', @@ -399,7 +405,7 @@ CPU_INFO_TEST_SCENARIOS = [ 'architecture': 'aarch64', 'nproc_out': 4, 'sched_getaffinity': set([0, 1, 2, 3]), - 'cpuinfo': open(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/aarch64-4cpu-cpuinfo')).readlines(), + 'cpuinfo': read_lines(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/aarch64-4cpu-cpuinfo')), 'expected_result': { 'processor': [ '0', 'AArch64 Processor rev 4 (aarch64)', @@ -417,7 +423,7 @@ CPU_INFO_TEST_SCENARIOS = [ 'architecture': 'x86_64', 'nproc_out': 4, 'sched_getaffinity': set([0, 1, 2, 3]), - 'cpuinfo': open(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/x86_64-4cpu-cpuinfo')).readlines(), + 'cpuinfo': read_lines(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/x86_64-4cpu-cpuinfo')), 'expected_result': { 'processor': [ '0', 'AuthenticAMD', 'Dual-Core AMD Opteron(tm) Processor 2216', @@ -435,7 +441,7 @@ CPU_INFO_TEST_SCENARIOS = [ 'architecture': 'x86_64', 'nproc_out': 4, 'sched_getaffinity': set([0, 1, 2, 3]), - 'cpuinfo': open(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/x86_64-8cpu-cpuinfo')).readlines(), + 'cpuinfo': read_lines(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/x86_64-8cpu-cpuinfo')), 'expected_result': { 'processor': [ '0', 'GenuineIntel', 'Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz', @@ -457,7 +463,7 @@ CPU_INFO_TEST_SCENARIOS = [ 'architecture': 'arm64', 'nproc_out': 4, 'sched_getaffinity': set([0, 1, 2, 3]), - 'cpuinfo': open(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/arm64-4cpu-cpuinfo')).readlines(), + 'cpuinfo': read_lines(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/arm64-4cpu-cpuinfo')), 'expected_result': { 'processor': ['0', '1', '2', '3'], 'processor_cores': 1, @@ -470,7 +476,7 @@ CPU_INFO_TEST_SCENARIOS = [ 'architecture': 'armv71', 'nproc_out': 8, 'sched_getaffinity': set([0, 1, 2, 3, 4, 5, 6, 7]), - 'cpuinfo': open(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/armv7-rev3-8cpu-cpuinfo')).readlines(), + 'cpuinfo': read_lines(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/armv7-rev3-8cpu-cpuinfo')), 'expected_result': { 'processor': [ '0', 'ARMv7 Processor rev 3 (v7l)', @@ -492,7 +498,7 @@ CPU_INFO_TEST_SCENARIOS = [ 'architecture': 'x86_64', 'nproc_out': 2, 'sched_getaffinity': set([0, 1]), - 'cpuinfo': open(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/x86_64-2cpu-cpuinfo')).readlines(), + 'cpuinfo': read_lines(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/x86_64-2cpu-cpuinfo')), 'expected_result': { 'processor': [ '0', 'GenuineIntel', 'Intel(R) Xeon(R) CPU E5-2680 v2 @ 2.80GHz', @@ -505,7 +511,7 @@ CPU_INFO_TEST_SCENARIOS = [ 'processor_vcpus': 2}, }, { - 'cpuinfo': open(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/ppc64-power7-rhel7-8cpu-cpuinfo')).readlines(), + 'cpuinfo': read_lines(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/ppc64-power7-rhel7-8cpu-cpuinfo')), 'architecture': 'ppc64', 'nproc_out': 8, 'sched_getaffinity': set([0, 1, 2, 3, 4, 5, 6, 7]), @@ -528,7 +534,7 @@ CPU_INFO_TEST_SCENARIOS = [ }, }, { - 'cpuinfo': open(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/ppc64le-power8-24cpu-cpuinfo')).readlines(), + 'cpuinfo': read_lines(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/ppc64le-power8-24cpu-cpuinfo')), 'architecture': 'ppc64le', 'nproc_out': 24, 'sched_getaffinity': set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]), @@ -567,7 +573,7 @@ CPU_INFO_TEST_SCENARIOS = [ }, }, { - 'cpuinfo': open(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/s390x-z13-2cpu-cpuinfo')).readlines(), + 'cpuinfo': read_lines(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/s390x-z13-2cpu-cpuinfo')), 'architecture': 's390x', 'nproc_out': 2, 'sched_getaffinity': set([0, 1]), @@ -583,7 +589,7 @@ CPU_INFO_TEST_SCENARIOS = [ }, }, { - 'cpuinfo': open(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/s390x-z14-64cpu-cpuinfo')).readlines(), + 'cpuinfo': read_lines(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/s390x-z14-64cpu-cpuinfo')), 'architecture': 's390x', 'nproc_out': 64, 'sched_getaffinity': set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, @@ -601,7 +607,7 @@ CPU_INFO_TEST_SCENARIOS = [ }, }, { - 'cpuinfo': open(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/sparc-t5-debian-ldom-24vcpu')).readlines(), + 'cpuinfo': read_lines(os.path.join(os.path.dirname(__file__), '../fixtures/cpuinfo/sparc-t5-debian-ldom-24vcpu')), 'architecture': 'sparc64', 'nproc_out': 24, 'sched_getaffinity': set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]), diff --git a/test/units/module_utils/facts/system/distribution/test_parse_distribution_file_ClearLinux.py b/test/units/module_utils/facts/system/distribution/test_parse_distribution_file_ClearLinux.py index c0957566cef..6667ada7b00 100644 --- a/test/units/module_utils/facts/system/distribution/test_parse_distribution_file_ClearLinux.py +++ b/test/units/module_utils/facts/system/distribution/test_parse_distribution_file_ClearLinux.py @@ -21,7 +21,8 @@ def test_input(): def test_parse_distribution_file_clear_linux(mock_module, test_input): - test_input['data'] = open(os.path.join(os.path.dirname(__file__), '../../fixtures/distribution_files/ClearLinux')).read() + with open(os.path.join(os.path.dirname(__file__), '../../fixtures/distribution_files/ClearLinux')) as file: + test_input['data'] = file.read() result = ( True, @@ -43,7 +44,8 @@ def test_parse_distribution_file_clear_linux_no_match(mock_module, distro_file, Test against data from Linux Mint and CoreOS to ensure we do not get a reported match from parse_distribution_file_ClearLinux() """ - test_input['data'] = open(os.path.join(os.path.dirname(__file__), '../../fixtures/distribution_files', distro_file)).read() + with open(os.path.join(os.path.dirname(__file__), '../../fixtures/distribution_files', distro_file)) as file: + test_input['data'] = file.read() result = (False, {}) diff --git a/test/units/module_utils/facts/system/distribution/test_parse_distribution_file_Slackware.py b/test/units/module_utils/facts/system/distribution/test_parse_distribution_file_Slackware.py index 53fd4ea1e91..efb937e0767 100644 --- a/test/units/module_utils/facts/system/distribution/test_parse_distribution_file_Slackware.py +++ b/test/units/module_utils/facts/system/distribution/test_parse_distribution_file_Slackware.py @@ -19,9 +19,12 @@ from ansible.module_utils.facts.system.distribution import DistributionFiles ) ) def test_parse_distribution_file_slackware(mock_module, distro_file, expected_version): + with open(os.path.join(os.path.dirname(__file__), '../../fixtures/distribution_files', distro_file)) as file: + data = file.read() + test_input = { 'name': 'Slackware', - 'data': open(os.path.join(os.path.dirname(__file__), '../../fixtures/distribution_files', distro_file)).read(), + 'data': data, 'path': '/etc/os-release', 'collected_facts': None, } diff --git a/test/units/parsing/vault/test_vault_editor.py b/test/units/parsing/vault/test_vault_editor.py index 77509f08aa4..37791839bbb 100644 --- a/test/units/parsing/vault/test_vault_editor.py +++ b/test/units/parsing/vault/test_vault_editor.py @@ -90,10 +90,9 @@ class TestVaultEditor(unittest.TestCase): def _create_file(self, test_dir, name, content=None, symlink=False): file_path = os.path.join(test_dir, name) - opened_file = open(file_path, 'wb') - if content: - opened_file.write(content) - opened_file.close() + with open(file_path, 'wb') as opened_file: + if content: + opened_file.write(content) return file_path def _vault_editor(self, vault_secrets=None): @@ -174,10 +173,9 @@ class TestVaultEditor(unittest.TestCase): tmp_path = editor_args[-1] # simulate the tmp file being editted - tmp_file = open(tmp_path, 'wb') - if new_src_contents: - tmp_file.write(new_src_contents) - tmp_file.close() + with open(tmp_path, 'wb') as tmp_file: + if new_src_contents: + tmp_file.write(new_src_contents) def _faux_command(self, tmp_path): pass @@ -198,13 +196,13 @@ class TestVaultEditor(unittest.TestCase): ve._edit_file_helper(src_file_path, self.vault_secret, existing_data=src_file_contents) - new_target_file = open(src_file_path, 'rb') - new_target_file_contents = new_target_file.read() - self.assertEqual(src_file_contents, new_target_file_contents) + with open(src_file_path, 'rb') as new_target_file: + new_target_file_contents = new_target_file.read() + self.assertEqual(src_file_contents, new_target_file_contents) def _assert_file_is_encrypted(self, vault_editor, src_file_path, src_contents): - new_src_file = open(src_file_path, 'rb') - new_src_file_contents = new_src_file.read() + with open(src_file_path, 'rb') as new_src_file: + new_src_file_contents = new_src_file.read() # TODO: assert that it is encrypted self.assertTrue(vault.is_encrypted(new_src_file_contents)) @@ -339,8 +337,8 @@ class TestVaultEditor(unittest.TestCase): ve.encrypt_file(src_file_path, self.vault_secret) ve.edit_file(src_file_path) - new_src_file = open(src_file_path, 'rb') - new_src_file_contents = new_src_file.read() + with open(src_file_path, 'rb') as new_src_file: + new_src_file_contents = new_src_file.read() self.assertTrue(b'$ANSIBLE_VAULT;1.1;AES256' in new_src_file_contents) @@ -367,8 +365,8 @@ class TestVaultEditor(unittest.TestCase): vault_id='vault_secrets') ve.edit_file(src_file_path) - new_src_file = open(src_file_path, 'rb') - new_src_file_contents = new_src_file.read() + with open(src_file_path, 'rb') as new_src_file: + new_src_file_contents = new_src_file.read() self.assertTrue(b'$ANSIBLE_VAULT;1.2;AES256;vault_secrets' in new_src_file_contents) @@ -399,8 +397,8 @@ class TestVaultEditor(unittest.TestCase): ve.edit_file(src_file_link_path) - new_src_file = open(src_file_path, 'rb') - new_src_file_contents = new_src_file.read() + with open(src_file_path, 'rb') as new_src_file: + new_src_file_contents = new_src_file.read() src_file_plaintext = ve.vault.decrypt(new_src_file_contents) @@ -485,9 +483,8 @@ class TestVaultEditor(unittest.TestCase): error_hit = True # verify decrypted content - f = open(v11_file.name, "rb") - fdata = to_text(f.read()) - f.close() + with open(v11_file.name, "rb") as f: + fdata = to_text(f.read()) os.unlink(v11_file.name)