Merge "Adding tests on cache handler"

This commit is contained in:
Zuul 2021-04-16 12:21:11 +00:00 committed by Gerrit Code Review
commit 06a6fc8dd0
1 changed files with 28 additions and 0 deletions

View File

@ -16,8 +16,10 @@
"""Test the cache handler module"""
import os
from unittest import mock
import fixtures
import oslo_config
from oslotest import base as test_base
from oslo_policy import _cache_handler as _ch
@ -64,3 +66,29 @@ class CacheHandlerTest(test_base.BaseTestCase):
reloaded, data = _ch.read_cached_file(file_cache, path)
self.assertTrue(reloaded)
@mock.patch.object(_ch, 'LOG')
def test_reloading_cache_with_permission_denied(self, mock_log):
file_cache = {}
path = os.path.join(self.tmpdir.path, 'tmpfile')
with open(path, 'w+') as fp:
fp.write('test')
os.chmod(path, 000)
self.assertRaises(
oslo_config.cfg.ConfigFilesPermissionDeniedError,
_ch.read_cached_file, file_cache, path)
mock_log.error.assert_called_once()
@mock.patch.object(_ch, 'LOG')
def test_reloading_on_removed_file(self, mock_log):
file_cache = {}
# don't actually create the file
path = os.path.join(self.tmpdir.path, 'tmpfile')
reloaded, data = _ch.read_cached_file(file_cache, path)
self.assertEqual({}, data)
self.assertTrue(reloaded)
mock_log.error.assert_called_once()