diff --git a/tests/unit/utils/cache_mods/cache_mod.py b/tests/unit/utils/cache_mods/cache_mod.py new file mode 100644 index 0000000000..40032a5832 --- /dev/null +++ b/tests/unit/utils/cache_mods/cache_mod.py @@ -0,0 +1,18 @@ +import salt.utils.cache + +''' +This is a module used in +unit.utils.cache to test +the context wrapper functions. +''' + +def __virtual__(): + return True + +@salt.utils.cache.context_cache +def test_context_module(): + if 'called' in __context__: + __context__['called'] += 1 + else: + __context__['called'] = 0 + return __context__ diff --git a/tests/unit/utils/cache_test.py b/tests/unit/utils/cache_test.py index a20c0c7df1..5341f373a2 100644 --- a/tests/unit/utils/cache_test.py +++ b/tests/unit/utils/cache_test.py @@ -8,7 +8,10 @@ # Import python libs from __future__ import absolute_import +import os import time +import tempfile +import shutil # Import Salt Testing libs from salttesting import TestCase @@ -16,6 +19,8 @@ from salttesting.helpers import ensure_in_syspath ensure_in_syspath('../../') # Import salt libs +import salt.config +import salt.loader from salt.utils import cache @@ -46,6 +51,52 @@ class CacheDictTestCase(TestCase): # make sure that a get would get a regular old key error self.assertRaises(KeyError, cd.__getitem__, 'foo') +class CacheContextTestCase(TestCase): + + def setUp(self): + context_dir = os.path.join(tempfile.gettempdir(), 'context') + if os.path.exists(context_dir): + shutil.rmtree(os.path.join(tempfile.gettempdir(), 'context')) + + 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 + 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_context_wrapper(self): + ''' + Test to ensure that a module which decorates itself + with a context cache can store and retreive its contextual + data + ''' + opts = salt.config.DEFAULT_MINION_OPTS + opts['cachedir'] = tempfile.gettempdir() + + ll_ = salt.loader.LazyLoader( + [os.path.join(os.path.dirname(os.path.realpath(__file__)), 'cache_mods')], + tag='rawmodule', + virtual_enable=False, + opts=opts) + + cache_test_func = ll_['cache_mod.test_context_module'] + + self.assertEqual(cache_test_func()['called'], 0) + self.assertEqual(cache_test_func()['called'], 1) + + if __name__ == '__main__': from integration import run_tests