diff options
author | Gorka Eguileor <geguileo@redhat.com> | 2019-10-18 13:39:14 +0200 |
---|---|---|
committer | Gorka Eguileor <geguileo@redhat.com> | 2019-12-20 13:35:20 +0100 |
commit | e2c71e9937fe1b7faf06bc1435f0018a386eb4ac (patch) | |
tree | ab2526a787e40f3d0cb4c0d25cc760dd902b56b1 /oslo_concurrency/tests/unit/test_lockutils.py | |
parent | e49eca232f272f011bdbfdf2770c45cb7c90bd20 (diff) | |
download | oslo-concurrency-e2c71e9937fe1b7faf06bc1435f0018a386eb4ac.tar.gz |
Fix remove_lock test
Current external lock removal unit tests are not really testing the
removal as they don't bother to check that we are calling os.remove
This patch fixes this situation by mocking the appropriate methods and
checking that they are being called (or not) as expected.
Change-Id: I25efc1638106431ee36e760c0c213a1a675ec908
Diffstat (limited to 'oslo_concurrency/tests/unit/test_lockutils.py')
-rw-r--r-- | oslo_concurrency/tests/unit/test_lockutils.py | 51 |
1 files changed, 29 insertions, 22 deletions
diff --git a/oslo_concurrency/tests/unit/test_lockutils.py b/oslo_concurrency/tests/unit/test_lockutils.py index b124963..710b9ae 100644 --- a/oslo_concurrency/tests/unit/test_lockutils.py +++ b/oslo_concurrency/tests/unit/test_lockutils.py @@ -22,6 +22,7 @@ import tempfile import threading import time +import mock from oslo_config import cfg from oslotest import base as test_base import six @@ -302,28 +303,34 @@ class LockTestCase(test_base.BaseTestCase): with lockutils.lock("test") as sem2: self.assertEqual(sem, sem2) - def _test_remove_lock_external_file(self, lock_dir, use_external=False): - lock_name = 'mylock' - lock_pfix = 'mypfix-remove-lock-test-' - - if use_external: - lock_path = lock_dir - else: - lock_path = None - - lockutils.remove_external_lock_file(lock_name, lock_pfix, lock_path) - - for ent in os.listdir(lock_dir): - self.assertRaises(OSError, ent.startswith, lock_pfix) - - def test_remove_lock_external_file(self): - lock_dir = tempfile.mkdtemp() - self.config(lock_path=lock_dir, group='oslo_concurrency') - self._test_remove_lock_external_file(lock_dir) - - def test_remove_lock_external_file_lock_path(self): - self._test_remove_lock_external_file(tempfile.mkdtemp(), - use_external=True) + @mock.patch('logging.Logger.info') + @mock.patch('os.remove') + @mock.patch('oslo_concurrency.lockutils._get_lock_path') + def test_remove_lock_external_file_exists(self, path_mock, remove_mock, + log_mock): + lockutils.remove_external_lock_file(mock.sentinel.name, + mock.sentinel.prefix, + mock.sentinel.lock_path) + + path_mock.assert_called_once_with(mock.sentinel.name, + mock.sentinel.prefix, + mock.sentinel.lock_path) + remove_mock.assert_called_once_with(path_mock.return_value) + log_mock.assert_not_called() + + @mock.patch('logging.Logger.info') + @mock.patch('os.remove', side_effect=OSError) + @mock.patch('oslo_concurrency.lockutils._get_lock_path') + def test_remove_lock_external_file_doesnt_exists(self, path_mock, + remove_mock, log_mock): + lockutils.remove_external_lock_file(mock.sentinel.name, + mock.sentinel.prefix, + mock.sentinel.lock_path) + path_mock.assert_called_once_with(mock.sentinel.name, + mock.sentinel.prefix, + mock.sentinel.lock_path) + remove_mock.assert_called_once_with(path_mock.return_value) + log_mock.assert_called() def test_no_slash_in_b64(self): # base64(sha1(foobar)) has a slash in it |