def test_set_cache(self): ''' Tests to ensure the cache is written correctly ''' @cache.context_cache def _test_set_cache(): ''' This will inherit globals from the test module itself. Normally these are injected by the salt loader [salt.loader] ''' pass _test_set_cache() target_cache_file = os.path.join(__opts__['cachedir'], 'context', '{0}.p'.format(__name__)) self.assertTrue(os.path.isfile(target_cache_file), 'Context cache did not write cache file') # Test manual de-serialize with salt.utils.files.fopen(target_cache_file, 'rb') as fp_: target_cache_data = salt.utils.data.decode( salt.payload.Serial(__opts__).load(fp_)) self.assertDictEqual(__context__, target_cache_data) # Test cache de-serialize cc = cache.ContextCache(__opts__, __name__) retrieved_cache = cc.get_cache_context() self.assertDictEqual(retrieved_cache, __context__)
def test_smoke_context(minion_config): """ Smoke test the context cache """ context_cache = cache.ContextCache(minion_config, "cache_test") data = {"a": "b"} context_cache.cache_context(data.copy()) ret = context_cache.get_cache_context() assert ret == data
def test_smoke_context(self): ''' Smoke test the context cache ''' if os.path.exists(os.path.join(tempfile.gettempdir(), 'context')): self.skipTest('Context dir already exists') else: opts = salt.config.DEFAULT_MINION_OPTS.copy() opts['cachedir'] = tempfile.gettempdir() context_cache = cache.ContextCache(opts, 'cache_test') context_cache.cache_context({'a': 'b'}) ret = context_cache.get_cache_context() self.assertDictEqual({'a': 'b'}, ret)
def test_smoke_context(self): """ Smoke test the context cache """ if os.path.exists(os.path.join(tempfile.gettempdir(), "context")): self.skipTest("Context dir already exists") else: opts = salt.config.DEFAULT_MINION_OPTS.copy() opts['cachedir'] = tempfile.gettempdir() context_cache = cache.ContextCache(opts, 'cache_test') context_cache.cache_context({"a": "b"}) ret = context_cache.get_cache_context() self.assertDictEqual({"a": "b"}, ret)
def test_set_cache(minion_config, cache_mods_path, cache_mod_name, cache_dir): """ Tests to ensure the cache is written correctly """ context = {"c": "d"} loader = salt.loader.LazyLoader( [str(cache_mods_path)], tag="rawmodule", virtual_enable=False, opts=minion_config, pack={ "__context__": context, "__opts__": minion_config }, ) cache_test_func = loader["cache_mod.test_context_module"] # Call the function to trigger the context cache assert cache_test_func()["called"] == 0 assert cache_test_func()["called"] == 1 assert cache_test_func()["called"] == 2 cache_file_name = "salt.loaded.ext.rawmodule.{}.p".format(cache_mod_name) cached_file = cache_dir / "context" / cache_file_name assert cached_file.exists() # Test manual de-serialize target_cache_data = salt.utils.data.decode( salt.payload.Serial(minion_config).loads(cached_file.read_bytes())) assert target_cache_data == dict(context, called=1) # Test cache de-serialize cc = cache.ContextCache( minion_config, "salt.loaded.ext.rawmodule.{}".format(cache_mod_name)) retrieved_cache = cc.get_cache_context() assert retrieved_cache == dict(context, called=1)