mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 08:58:59 +00:00
Cache tests
This commit is contained in:
parent
efef3948ad
commit
3e848ad126
18
tests/unit/utils/cache_mods/cache_mod.py
Normal file
18
tests/unit/utils/cache_mods/cache_mod.py
Normal file
@ -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__
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user