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, 19 insertions, 11 deletions
diff --git a/test/units/plugins/lookup/test_password.py b/test/units/plugins/lookup/test_password.py index 685f2ce7..318bc10b 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: # pragma: nocover +except ImportError: 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.common.text.converters import to_bytes +from ansible.module_utils._text import to_bytes from ansible.plugins.loader import PluginLoader, lookup_loader from ansible.plugins.lookup import password @@ -416,6 +416,8 @@ 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 @@ -423,6 +425,7 @@ 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 @@ -464,17 +467,23 @@ class TestLookupModuleWithoutPasslib(BaseTestLookupModule): def test_lock_been_held(self, mock_sleep): # pretend the lock file is here password.os.path.exists = lambda x: True - with pytest.raises(AnsibleError): + try: with patch.object(builtins, 'open', mock_open(read_data=b'hunter42 salt=87654321\n')) as m: # should timeout here - self.password_lookup.run([u'/path/to/somewhere chars=anything'], None) + 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 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') - 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) + 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') for result in results: self.assertEqual(result, u'hunter42') @@ -522,8 +531,10 @@ 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_write_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'] 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: @@ -531,9 +542,6 @@ 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): |