diff options
Diffstat (limited to 'test/units/parsing/vault/test_vault_editor.py')
-rw-r--r-- | test/units/parsing/vault/test_vault_editor.py | 79 |
1 files changed, 23 insertions, 56 deletions
diff --git a/test/units/parsing/vault/test_vault_editor.py b/test/units/parsing/vault/test_vault_editor.py index 77509f08..28561c6a 100644 --- a/test/units/parsing/vault/test_vault_editor.py +++ b/test/units/parsing/vault/test_vault_editor.py @@ -33,8 +33,7 @@ from ansible import errors from ansible.parsing import vault from ansible.parsing.vault import VaultLib, VaultEditor, match_encrypt_secret -from ansible.module_utils.six import PY3 -from ansible.module_utils._text import to_bytes, to_text +from ansible.module_utils.common.text.converters import to_bytes, to_text from units.mock.vault_helper import TextVaultSecret @@ -88,12 +87,10 @@ class TestVaultEditor(unittest.TestCase): suffix = '_ansible_unit_test_%s_' % (self.__class__.__name__) return tempfile.mkdtemp(suffix=suffix) - def _create_file(self, test_dir, name, content=None, symlink=False): + def _create_file(self, test_dir, name, content, symlink=False): file_path = os.path.join(test_dir, name) - opened_file = open(file_path, 'wb') - if content: + with open(file_path, 'wb') as opened_file: opened_file.write(content) - opened_file.close() return file_path def _vault_editor(self, vault_secrets=None): @@ -118,11 +115,8 @@ class TestVaultEditor(unittest.TestCase): def test_stdin_binary(self): stdin_data = '\0' - if PY3: - fake_stream = StringIO(stdin_data) - fake_stream.buffer = BytesIO(to_bytes(stdin_data)) - else: - fake_stream = BytesIO(to_bytes(stdin_data)) + fake_stream = StringIO(stdin_data) + fake_stream.buffer = BytesIO(to_bytes(stdin_data)) with patch('sys.stdin', fake_stream): ve = self._vault_editor() @@ -167,17 +161,15 @@ class TestVaultEditor(unittest.TestCase): self.assertNotEqual(src_file_contents, b_ciphertext, 'b_ciphertext should be encrypted and not equal to src_contents') - def _faux_editor(self, editor_args, new_src_contents=None): + def _faux_editor(self, editor_args, new_src_contents): if editor_args[0] == 'shred': return tmp_path = editor_args[-1] # simulate the tmp file being editted - tmp_file = open(tmp_path, 'wb') - if new_src_contents: + with open(tmp_path, 'wb') as tmp_file: tmp_file.write(new_src_contents) - tmp_file.close() def _faux_command(self, tmp_path): pass @@ -198,13 +190,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 +331,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 +359,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 +391,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) @@ -418,13 +410,6 @@ class TestVaultEditor(unittest.TestCase): src_file_path = self._create_file(self._test_dir, 'src_file', content=src_contents) - new_src_contents = to_bytes("The info is different now.") - - def faux_editor(editor_args): - self._faux_editor(editor_args, new_src_contents) - - mock_sp_call.side_effect = faux_editor - ve = self._vault_editor() self.assertRaisesRegex(errors.AnsibleError, 'input is not vault encrypted data', @@ -478,20 +463,14 @@ class TestVaultEditor(unittest.TestCase): ve = self._vault_editor(self._secrets("ansible")) # make sure the password functions for the cipher - error_hit = False - try: - ve.decrypt_file(v11_file.name) - except errors.AnsibleError: - error_hit = True + ve.decrypt_file(v11_file.name) # 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) - assert error_hit is False, "error decrypting 1.1 file" assert fdata.strip() == "foo", "incorrect decryption of 1.1 file: %s" % fdata.strip() def test_real_path_dash(self): @@ -501,21 +480,9 @@ class TestVaultEditor(unittest.TestCase): res = ve._real_path(filename) self.assertEqual(res, '-') - def test_real_path_dev_null(self): + def test_real_path_not_dash(self): filename = '/dev/null' ve = self._vault_editor() res = ve._real_path(filename) - self.assertEqual(res, '/dev/null') - - def test_real_path_symlink(self): - self._test_dir = os.path.realpath(self._create_test_dir()) - file_path = self._create_file(self._test_dir, 'test_file', content=b'this is a test file') - file_link_path = os.path.join(self._test_dir, 'a_link_to_test_file') - - os.symlink(file_path, file_link_path) - - ve = self._vault_editor() - - res = ve._real_path(file_link_path) - self.assertEqual(res, file_path) + self.assertNotEqual(res, '-') |