Cache tests

This commit is contained in:
Mike Place 2016-11-08 18:49:10 +13:00
parent efef3948ad
commit 3e848ad126
No known key found for this signature in database
GPG Key ID: 9136F4F13705CFD3
2 changed files with 69 additions and 0 deletions

View 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__

View File

@ -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