Debian Codegraph @

test/units/plugins/cache/test_cache.py

raw
1 # (c) 2012-2015, Michael DeHaan <[email protected]>
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import annotations
19
20
21 import shutil
22 import tempfile
23
24
25 import unittest
26 from ansible.errors import AnsibleError
27 from ansible.plugins.cache import CachePluginAdjudicator
28 from ansible.plugins.cache.memory import CacheModule as MemoryCache
29 from ansible.plugins.loader import cache_loader
30
31 import pytest
32
33
34 class TestCachePluginAdjudicator(unittest.TestCase):
35 def setUp(self):
36 # memory plugin cache
37 self.cache = CachePluginAdjudicator()
38 self.cache['cache_key'] = {'key1': 'value1', 'key2': 'value2'}
39 self.cache['cache_key_2'] = {'key': 'value'}
40
41 def test___setitem__(self):
42 self.cache['new_cache_key'] = {'new_key1': ['new_value1', 'new_value2']}
43 assert self.cache['new_cache_key'] == {'new_key1': ['new_value1', 'new_value2']}
44
45 def test_inner___setitem__(self):
46 self.cache['new_cache_key'] = {'new_key1': ['new_value1', 'new_value2']}
47 self.cache['new_cache_key']['new_key1'][0] = 'updated_value1'
48 assert self.cache['new_cache_key'] == {'new_key1': ['updated_value1', 'new_value2']}
49
50 def test___contains__(self):
51 assert 'cache_key' in self.cache
52 assert 'not_cache_key' not in self.cache
53
54 def test_get(self):
55 assert self.cache.get('cache_key') == {'key1': 'value1', 'key2': 'value2'}
56
57 def test_get_with_default(self):
58 assert self.cache.get('foo', 'bar') == 'bar'
59
60 def test_get_without_default(self):
61 assert self.cache.get('foo') is None
62
63 def test___getitem__(self):
64 with pytest.raises(KeyError):
65 self.cache['foo'] # pylint: disable=pointless-statement
66
67 def test_pop_with_default(self):
68 assert self.cache.pop('foo', 'bar') == 'bar'
69
70 def test_pop_without_default(self):
71 with pytest.raises(KeyError):
72 self.cache.pop('foo')
73
74 def test_pop(self):
75 v = self.cache.pop('cache_key_2')
76 assert v == {'key': 'value'}
77 assert 'cache_key_2' not in self.cache
78
79 def test_update(self):
80 self.cache.update({'cache_key': {'key2': 'updatedvalue'}})
81 assert self.cache['cache_key']['key2'] == 'updatedvalue'
82
83 def test_update_cache_if_changed(self):
84 # Changes are stored in the CachePluginAdjudicator and will be
85 # persisted to the plugin when calling update_cache_if_changed()
86 # The exception is flush which flushes the plugin immediately.
87 assert len(self.cache.keys()) == 2
88 assert len(self.cache._plugin.keys()) == 0
89 self.cache.update_cache_if_changed()
90 assert len(self.cache._plugin.keys()) == 2
91
92
93 class TestJsonFileCache(TestCachePluginAdjudicator):
94 cache_prefix = ''
95
96 def setUp(self):
97 self.cache_dir = tempfile.mkdtemp(prefix='ansible-plugins-cache-')
98 self.cache = self.get_cache(self.cache_prefix)
99 self.cache['cache_key'] = {'key1': 'value1', 'key2': 'value2'}
100 self.cache['cache_key_2'] = {'key': 'value'}
101
102 def get_cache(self, prefix):
103 return CachePluginAdjudicator(
104 plugin_name='jsonfile', _uri=self.cache_dir,
105 _prefix=prefix)
106
107 def test_keys(self):
108 # A cache without a prefix will consider all files in the cache
109 # directory as valid cache entries.
110 cache_writer = self.get_cache(self.cache_prefix)
111 cache_writer["no_prefix"] = dict(a=1)
112 cache_writer["special_test"] = dict(b=2)
113 cache_writer.update_cache_if_changed()
114
115 # The plugin does not know the CachePluginAdjudicator entries.
116 assert sorted(self.cache._plugin.keys()) == [
117 'no_prefix', 'special_test']
118
119 assert 'no_prefix' in self.cache
120 assert 'special_test' in self.cache
121 assert 'test' not in self.cache
122 assert self.cache['no_prefix'] == dict(a=1)
123 assert self.cache['special_test'] == dict(b=2)
124
125 def tearDown(self):
126 shutil.rmtree(self.cache_dir)
127
128
129 class TestJsonFileCachePrefix(TestJsonFileCache):
130 cache_prefix = 'special_'
131
132 def test_keys(self):
133 # For caches with a prefix only files that match the prefix are
134 # considered. The prefix is removed from the key name.
135 cache_writer = self.get_cache('')
136 cache_writer["no_prefix"] = dict(a=1)
137 cache_writer.update_cache_if_changed()
138
139 cache_writer = self.get_cache(self.cache_prefix)
140 cache_writer["test"] = dict(b=2)
141 cache_writer.update_cache_if_changed()
142
143 # The plugin does not know the CachePluginAdjudicator entries.
144 assert sorted(self.cache._plugin.keys()) == ['test']
145
146 assert 'no_prefix' not in self.cache
147 assert 'special_test' not in self.cache
148 assert 'test' in self.cache
149 assert self.cache['test'] == dict(b=2)
150
151
152 class TestCachePlugin(unittest.TestCase):
153 def setUp(self):
154 self.cache = cache_loader.get('memory')
155
156 @pytest.mark.usefixtures('collection_loader')
157 def test_plugin_load_failure(self):
158 # See https://github.com/ansible/ansible/issues/18751
159 # Note no fact_connection config set, so this will fail
160 with pytest.raises(AnsibleError, match="Unable to load the cache plugin.*json.*"):
161 cache_loader.get('json')
162
163 def test_update(self):
164 self.cache.set('cache_key', {'key2': 'updatedvalue'})
165 assert self.cache.get('cache_key')['key2'] == 'updatedvalue'
166
167
168 def test_memory_cachemodule_with_loader():
169 assert isinstance(cache_loader.get('memory'), MemoryCache)