diff options
Diffstat (limited to 'test/units/plugins/lookup/test_password.py')
-rw-r--r-- | test/units/plugins/lookup/test_password.py | 30 |
1 files changed, 11 insertions, 19 deletions
diff --git a/test/units/plugins/lookup/test_password.py b/test/units/plugins/lookup/test_password.py index 318bc10b..685f2ce7 100644 --- a/test/units/plugins/lookup/test_password.py +++ b/test/units/plugins/lookup/test_password.py @@ -23,7 +23,7 @@ __metaclass__ = type try: import passlib from passlib.handlers import pbkdf2 -except ImportError: +except ImportError: # pragma: nocover passlib = None pbkdf2 = None @@ -36,7 +36,7 @@ from unittest.mock import mock_open, patch from ansible.errors import AnsibleError from ansible.module_utils.six import text_type from ansible.module_utils.six.moves import builtins -from ansible.module_utils._text import to_bytes +from ansible.module_utils.common.text.converters import to_bytes from ansible.plugins.loader import PluginLoader, lookup_loader from ansible.plugins.lookup import password @@ -416,8 +416,6 @@ class BaseTestLookupModule(unittest.TestCase): password.os.open = lambda path, flag: None self.os_close = password.os.close password.os.close = lambda fd: None - self.os_remove = password.os.remove - password.os.remove = lambda path: None self.makedirs_safe = password.makedirs_safe password.makedirs_safe = lambda path, mode: None @@ -425,7 +423,6 @@ class BaseTestLookupModule(unittest.TestCase): password.os.path.exists = self.os_path_exists password.os.open = self.os_open password.os.close = self.os_close - password.os.remove = self.os_remove password.makedirs_safe = self.makedirs_safe @@ -467,23 +464,17 @@ class TestLookupModuleWithoutPasslib(BaseTestLookupModule): def test_lock_been_held(self, mock_sleep): # pretend the lock file is here password.os.path.exists = lambda x: True - try: + with pytest.raises(AnsibleError): with patch.object(builtins, 'open', mock_open(read_data=b'hunter42 salt=87654321\n')) as m: # should timeout here - results = self.password_lookup.run([u'/path/to/somewhere chars=anything'], None) - self.fail("Lookup didn't timeout when lock already been held") - except AnsibleError: - pass + self.password_lookup.run([u'/path/to/somewhere chars=anything'], None) def test_lock_not_been_held(self): # pretend now there is password file but no lock password.os.path.exists = lambda x: x == to_bytes('/path/to/somewhere') - try: - with patch.object(builtins, 'open', mock_open(read_data=b'hunter42 salt=87654321\n')) as m: - # should not timeout here - results = self.password_lookup.run([u'/path/to/somewhere chars=anything'], None) - except AnsibleError: - self.fail('Lookup timeouts when lock is free') + with patch.object(builtins, 'open', mock_open(read_data=b'hunter42 salt=87654321\n')) as m: + # should not timeout here + results = self.password_lookup.run([u'/path/to/somewhere chars=anything'], None) for result in results: self.assertEqual(result, u'hunter42') @@ -531,10 +522,8 @@ class TestLookupModuleWithPasslib(BaseTestLookupModule): self.assertEqual(int(str_parts[2]), crypt_parts['rounds']) self.assertIsInstance(result, text_type) - @patch.object(PluginLoader, '_get_paths') @patch('ansible.plugins.lookup.password._write_password_file') - def test_password_already_created_encrypt(self, mock_get_paths, mock_write_file): - mock_get_paths.return_value = ['/path/one', '/path/two', '/path/three'] + def test_password_already_created_encrypt(self, mock_write_file): password.os.path.exists = lambda x: x == to_bytes('/path/to/somewhere') with patch.object(builtins, 'open', mock_open(read_data=b'hunter42 salt=87654321\n')) as m: @@ -542,6 +531,9 @@ class TestLookupModuleWithPasslib(BaseTestLookupModule): for result in results: self.assertEqual(result, u'$pbkdf2-sha256$20000$ODc2NTQzMjE$Uikde0cv0BKaRaAXMrUQB.zvG4GmnjClwjghwIRf2gU') + # Assert the password file is not rewritten + mock_write_file.assert_not_called() + @pytest.mark.skipif(passlib is None, reason='passlib must be installed to run these tests') class TestLookupModuleWithPasslibWrappedAlgo(BaseTestLookupModule): |