Merge "Fix cache dir flooding when running from /tmp"
This commit is contained in:
commit
d7cfadbb7d
@ -0,0 +1,7 @@
|
||||
---
|
||||
features:
|
||||
- |
|
||||
Add possibility to skip caching endpoints to the filesystem when '.disable' file is present in the cache directory.
|
||||
fixes:
|
||||
- |
|
||||
When python interpreter invokimg the module is located under /tmp (the case when i.e. Ansible module uses stevedore) do not try to write cache to the file system.
|
@ -135,6 +135,14 @@ class Cache:
|
||||
cache_dir = _get_cache_dir()
|
||||
self._dir = cache_dir
|
||||
self._internal = {}
|
||||
self._disable_caching = False
|
||||
|
||||
# Caching can be disabled by either placing .disable file into the
|
||||
# target directory or when python executable is under /tmp (this is the
|
||||
# case when executed from ansible)
|
||||
if any([os.path.isfile(os.path.join(self._dir, '.disable')),
|
||||
sys.executable[0:4] == '/tmp']):
|
||||
self._disable_caching = True
|
||||
|
||||
def _get_data_for_path(self, path):
|
||||
if path is None:
|
||||
@ -153,14 +161,15 @@ class Cache:
|
||||
except (IOError, json.JSONDecodeError):
|
||||
data = _build_cacheable_data(path)
|
||||
data['path_values'] = path_values
|
||||
try:
|
||||
log.debug('writing to %s', filename)
|
||||
os.makedirs(self._dir, exist_ok=True)
|
||||
with open(filename, 'w') as f:
|
||||
json.dump(data, f)
|
||||
except (IOError, OSError):
|
||||
# Could not create cache dir or write file.
|
||||
pass
|
||||
if not self._disable_caching:
|
||||
try:
|
||||
log.debug('writing to %s', filename)
|
||||
os.makedirs(self._dir, exist_ok=True)
|
||||
with open(filename, 'w') as f:
|
||||
json.dump(data, f)
|
||||
except (IOError, OSError):
|
||||
# Could not create cache dir or write file.
|
||||
pass
|
||||
|
||||
self._internal[internal_key] = data
|
||||
return data
|
||||
|
56
stevedore/tests/test_cache.py
Normal file
56
stevedore/tests/test_cache.py
Normal file
@ -0,0 +1,56 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
"""Tests for stevedore._cache
|
||||
"""
|
||||
import sys
|
||||
|
||||
from unittest import mock
|
||||
|
||||
from stevedore import _cache
|
||||
from stevedore.tests import utils
|
||||
|
||||
|
||||
class TestCache(utils.TestCase):
|
||||
|
||||
def test_disable_caching_executable(self):
|
||||
"""Test caching is disabled if python interpreter is located under /tmp
|
||||
directory (Ansible)
|
||||
"""
|
||||
with mock.patch.object(sys, 'executable', '/tmp/fake'):
|
||||
sot = _cache.Cache()
|
||||
self.assertTrue(sot._disable_caching)
|
||||
|
||||
def test_disable_caching_file(self):
|
||||
"""Test caching is disabled if .disable file is present in target
|
||||
dir
|
||||
"""
|
||||
cache_dir = _cache._get_cache_dir()
|
||||
|
||||
with mock.patch('os.path.isfile') as mock_path:
|
||||
mock_path.return_value = True
|
||||
sot = _cache.Cache()
|
||||
mock_path.assert_called_with('%s/.disable' % cache_dir)
|
||||
self.assertTrue(sot._disable_caching)
|
||||
|
||||
mock_path.return_value = False
|
||||
sot = _cache.Cache()
|
||||
self.assertFalse(sot._disable_caching)
|
||||
|
||||
@mock.patch('os.makedirs')
|
||||
@mock.patch('builtins.open')
|
||||
def test__get_data_for_path_no_write(self, mock_open, mock_mkdir):
|
||||
sot = _cache.Cache()
|
||||
sot._disable_caching = True
|
||||
mock_open.side_effect = IOError
|
||||
sot._get_data_for_path('fake')
|
||||
mock_mkdir.assert_not_called()
|
Loading…
Reference in New Issue
Block a user