Adding tests on cache handler

New tests are:
- Permission denied
- File not found

Change-Id: I6d3343319ca6519fd9d215f2e8d3fa38516e9f4d
This commit is contained in:
Hervé Beraud 2020-08-18 19:07:48 +02:00 committed by Stephen Finucane
parent 9919f1317c
commit d7323a088c
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()