summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.opendev.org>2020-11-05 22:10:11 +0000
committerGerrit Code Review <review@openstack.org>2020-11-05 22:10:11 +0000
commitd7cfadbb7d2ffff65b0c25411369a978f91eee25 (patch)
treec7772f72c6253a34e1031e8631233cb7d85d5ad3
parent9f4a7d1e26dc426244b5f049ce45b9035bd27c88 (diff)
parent3a46e291416f453c612fe27a31d0b57918aaf276 (diff)
downloadstevedore-d7cfadbb7d2ffff65b0c25411369a978f91eee25.tar.gz
Merge "Fix cache dir flooding when running from /tmp"
-rw-r--r--releasenotes/notes/add-skip-caching-aa13be0299cc8b8c.yaml7
-rw-r--r--stevedore/_cache.py25
-rw-r--r--stevedore/tests/test_cache.py56
3 files changed, 80 insertions, 8 deletions
diff --git a/releasenotes/notes/add-skip-caching-aa13be0299cc8b8c.yaml b/releasenotes/notes/add-skip-caching-aa13be0299cc8b8c.yaml
new file mode 100644
index 0000000..d18fc9d
--- /dev/null
+++ b/releasenotes/notes/add-skip-caching-aa13be0299cc8b8c.yaml
@@ -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.
diff --git a/stevedore/_cache.py b/stevedore/_cache.py
index cd0e1cd..15f0fff 100644
--- a/stevedore/_cache.py
+++ b/stevedore/_cache.py
@@ -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
diff --git a/stevedore/tests/test_cache.py b/stevedore/tests/test_cache.py
new file mode 100644
index 0000000..8bf49c8
--- /dev/null
+++ b/stevedore/tests/test_cache.py
@@ -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()