summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/inventory_cache/plugins/inventory/exercise_cache.py
blob: cca2aa0fd9f0f2151c2b3dcd87546919c25d14d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# Copyright (c) 2022 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

DOCUMENTATION = '''
    inventory: exercise_cache
    short_description: run tests against the specified cache plugin
    description:
      - This plugin doesn't modify inventory.
      - Load a cache plugin and test the inventory cache interface is dict-like.
      - Most inventory cache write methods only apply to the in-memory cache.
      - The 'flush' and 'set_cache' methods should be used to apply changes to the backing cache plugin.
      - The inventory cache read methods prefer the in-memory cache, and fall back to reading from the cache plugin.
    extends_documentation_fragment:
      - inventory_cache
    options:
      plugin:
        required: true
        description: name of the plugin (exercise_cache)
    cache_timeout:
        ini: []
        env: []
        cli: []
        default: 0  # never expire
'''

from ansible.errors import AnsibleError
from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable
from ansible.utils.display import Display

from time import sleep


display = Display()


class InventoryModule(BaseInventoryPlugin, Cacheable):

    NAME = 'exercise_cache'

    test_cache_methods = [
        'test_plugin_name',
        'test_update_cache_if_changed',
        'test_set_cache',
        'test_load_whole_cache',
        'test_iter',
        'test_len',
        'test_get_missing_key',
        'test_get_expired_key',
        'test_initial_get',
        'test_get',
        'test_items',
        'test_keys',
        'test_values',
        'test_pop',
        'test_del',
        'test_set',
        'test_update',
        'test_flush',
    ]

    def verify_file(self, path):
        if not path.endswith(('exercise_cache.yml', 'exercise_cache.yaml',)):
            return False
        return super(InventoryModule, self).verify_file(path)

    def parse(self, inventory, loader, path, cache=None):
        super(InventoryModule, self).parse(inventory, loader, path)
        self._read_config_data(path)

        try:
            self.exercise_test_cache()
        except AnsibleError:
            raise
        except Exception as e:
            raise AnsibleError("Failed to run cache tests: {0}".format(e)) from e

    def exercise_test_cache(self):
        failed = []
        for test_name in self.test_cache_methods:
            try:
                getattr(self, test_name)()
            except AssertionError:
                failed.append(test_name)
            finally:
                self.cache.flush()
                self.cache.update_cache_if_changed()

        if failed:
            raise AnsibleError(f"Cache tests failed: {', '.join(failed)}")

    def test_equal(self, a, b):
        try:
            assert a == b
        except AssertionError:
            display.warning(f"Assertion {a} == {b} failed")
            raise

    def test_plugin_name(self):
        self.test_equal(self.cache._plugin_name, self.get_option('cache_plugin'))

    def test_update_cache_if_changed(self):
        self.cache._retrieved = {}
        self.cache._cache = {'foo': 'bar'}

        self.cache.update_cache_if_changed()

        self.test_equal(self.cache._retrieved, {'foo': 'bar'})
        self.test_equal(self.cache._cache, {'foo': 'bar'})

    def test_set_cache(self):
        cache_key1 = 'key1'
        cache1 = {'hosts': {'h1': {'foo': 'bar'}}}
        cache_key2 = 'key2'
        cache2 = {'hosts': {'h2': {}}}

        self.cache._cache = {cache_key1: cache1, cache_key2: cache2}
        self.cache.set_cache()

        self.test_equal(self.cache._plugin.contains(cache_key1), True)
        self.test_equal(self.cache._plugin.get(cache_key1), cache1)
        self.test_equal(self.cache._plugin.contains(cache_key2), True)
        self.test_equal(self.cache._plugin.get(cache_key2), cache2)

    def test_load_whole_cache(self):
        cache_data = {
            'key1': {'hosts': {'h1': {'foo': 'bar'}}},
            'key2': {'hosts': {'h2': {}}},
        }
        self.cache._cache = cache_data
        self.cache.set_cache()
        self.cache._cache = {}

        self.cache.load_whole_cache()
        self.test_equal(self.cache._cache, cache_data)

    def test_iter(self):
        cache_data = {
            'key1': {'hosts': {'h1': {'foo': 'bar'}}},
            'key2': {'hosts': {'h2': {}}},
        }
        self.cache._cache = cache_data
        self.test_equal(sorted(list(self.cache)), ['key1', 'key2'])

    def test_len(self):
        cache_data = {
            'key1': {'hosts': {'h1': {'foo': 'bar'}}},
            'key2': {'hosts': {'h2': {}}},
        }
        self.cache._cache = cache_data
        self.test_equal(len(self.cache), 2)

    def test_get_missing_key(self):
        # cache should behave like a dictionary
        # a missing key with __getitem__ should raise a KeyError
        try:
            self.cache['keyerror']
        except KeyError:
            pass
        else:
            assert False

        # get should return the default instead
        self.test_equal(self.cache.get('missing'), None)
        self.test_equal(self.cache.get('missing', 'default'), 'default')

    def _setup_expired(self):
        self.cache._cache = {'expired': True}
        self.cache.set_cache()

        # empty the in-memory info to test loading the key
        # keys that expire mid-use do not cause errors
        self.cache._cache = {}
        self.cache._retrieved = {}
        self.cache._plugin._cache = {}

        self.cache._plugin.set_option('timeout', 1)
        self.cache._plugin._timeout = 1
        sleep(2)

    def _cleanup_expired(self):
        # Set cache timeout back to never
        self.cache._plugin.set_option('timeout', 0)
        self.cache._plugin._timeout = 0

    def test_get_expired_key(self):
        if not hasattr(self.cache._plugin, '_timeout'):
            # DB-backed caches do not have a standard timeout interface
            return

        self._setup_expired()
        try:
            self.cache['expired']
        except KeyError:
            pass
        else:
            assert False
        finally:
            self._cleanup_expired()

        self._setup_expired()
        try:
            self.test_equal(self.cache.get('expired'), None)
            self.test_equal(self.cache.get('expired', 'default'), 'default')
        finally:
            self._cleanup_expired()

    def test_initial_get(self):
        # test cache behaves like a dictionary

        # set the cache to test getting a key that exists
        k1 = {'hosts': {'h1': {'foo': 'bar'}}}
        k2 = {'hosts': {'h2': {}}}
        self.cache._cache = {'key1': k1, 'key2': k2}
        self.cache.set_cache()

        # empty the in-memory info to test loading the key from the plugin
        self.cache._cache = {}
        self.cache._retrieved = {}
        self.cache._plugin._cache = {}

        self.test_equal(self.cache['key1'], k1)

        # empty the in-memory info to test loading the key from the plugin
        self.cache._cache = {}
        self.cache._retrieved = {}
        self.cache._plugin._cache = {}

        self.test_equal(self.cache.get('key1'), k1)

    def test_get(self):
        # test cache behaves like a dictionary

        # set the cache to test getting a key that exists
        k1 = {'hosts': {'h1': {'foo': 'bar'}}}
        k2 = {'hosts': {'h2': {}}}
        self.cache._cache = {'key1': k1, 'key2': k2}
        self.cache.set_cache()

        self.test_equal(self.cache['key1'], k1)
        self.test_equal(self.cache.get('key1'), k1)

    def test_items(self):
        self.test_equal(self.cache.items(), {}.items())

        test_items = {'hosts': {'host1': {'foo': 'bar'}}}
        self.cache._cache = test_items
        self.test_equal(self.cache.items(), test_items.items())

    def test_keys(self):
        self.test_equal(self.cache.keys(), {}.keys())

        test_items = {'hosts': {'host1': {'foo': 'bar'}}}
        self.cache._cache = test_items
        self.test_equal(self.cache.keys(), test_items.keys())

    def test_values(self):
        self.test_equal(list(self.cache.values()), list({}.values()))

        test_items = {'hosts': {'host1': {'foo': 'bar'}}}
        self.cache._cache = test_items
        self.test_equal(list(self.cache.values()), list(test_items.values()))

    def test_pop(self):
        try:
            self.cache.pop('missing')
        except KeyError:
            pass
        else:
            assert False

        self.test_equal(self.cache.pop('missing', 'default'), 'default')

        self.cache._cache = {'cache_key': 'cache'}
        self.test_equal(self.cache.pop('cache_key'), 'cache')

        # test backing plugin cache isn't modified
        cache_key1 = 'key1'
        cache1 = {'hosts': {'h1': {'foo': 'bar'}}}
        cache_key2 = 'key2'
        cache2 = {'hosts': {'h2': {}}}

        self.cache._cache = {cache_key1: cache1, cache_key2: cache2}
        self.cache.set_cache()

        self.test_equal(self.cache.pop('key1'), cache1)
        self.test_equal(self.cache._cache, {cache_key2: cache2})
        self.test_equal(self.cache._plugin._cache, {cache_key1: cache1, cache_key2: cache2})

    def test_del(self):
        try:
            del self.cache['missing']
        except KeyError:
            pass
        else:
            assert False

        cache_key1 = 'key1'
        cache1 = {'hosts': {'h1': {'foo': 'bar'}}}
        cache_key2 = 'key2'
        cache2 = {'hosts': {'h2': {}}}

        self.cache._cache = {cache_key1: cache1, cache_key2: cache2}
        self.cache.set_cache()

        del self.cache['key1']

        self.test_equal(self.cache._cache, {cache_key2: cache2})
        self.test_equal(self.cache._plugin._cache, {cache_key1: cache1, cache_key2: cache2})

    def test_set(self):
        cache_key = 'key1'
        hosts = {'hosts': {'h1': {'foo': 'bar'}}}
        self.cache[cache_key] = hosts

        self.test_equal(self.cache._cache, {cache_key: hosts})
        self.test_equal(self.cache._plugin._cache, {})

    def test_update(self):
        cache_key1 = 'key1'
        cache1 = {'hosts': {'h1': {'foo': 'bar'}}}
        cache_key2 = 'key2'
        cache2 = {'hosts': {'h2': {}}}

        self.cache._cache = {cache_key1: cache1}
        self.cache.update({cache_key2: cache2})
        self.test_equal(self.cache._cache, {cache_key1: cache1, cache_key2: cache2})

    def test_flush(self):
        cache_key1 = 'key1'
        cache1 = {'hosts': {'h1': {'foo': 'bar'}}}
        cache_key2 = 'key2'
        cache2 = {'hosts': {'h2': {}}}

        self.cache._cache = {cache_key1: cache1, cache_key2: cache2}
        self.cache.set_cache()

        # Unlike the dict write methods, cache.flush() flushes the backing plugin
        self.cache.flush()

        self.test_equal(self.cache._cache, {})
        self.test_equal(self.cache._plugin._cache, {})