2014-03-06 02:12:20 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
tests.unit.utils.cache_test
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Test the salt cache objects
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import Salt Testing libs
|
|
|
|
from salttesting import TestCase
|
|
|
|
from salttesting.helpers import ensure_in_syspath
|
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
|
|
|
# Import salt libs
|
|
|
|
from salt.utils import cache
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
class CacheDictTestCase(TestCase):
|
|
|
|
|
|
|
|
def test_sanity(self):
|
|
|
|
'''
|
|
|
|
Make sure you can instantiate etc.
|
|
|
|
'''
|
|
|
|
cd = cache.CacheDict(5)
|
2014-07-15 17:56:50 +00:00
|
|
|
self.assertIsInstance(cd, cache.CacheDict)
|
2014-03-06 02:12:20 +00:00
|
|
|
|
|
|
|
# do some tests to make sure it looks like a dict
|
2014-07-15 17:56:50 +00:00
|
|
|
self.assertNotIn('foo', cd)
|
2014-03-06 02:12:20 +00:00
|
|
|
cd['foo'] = 'bar'
|
2014-07-15 17:56:50 +00:00
|
|
|
self.assertEqual(cd['foo'], 'bar')
|
2014-03-06 02:12:20 +00:00
|
|
|
del cd['foo']
|
2014-07-15 17:56:50 +00:00
|
|
|
self.assertNotIn('foo', cd)
|
2014-03-06 02:12:20 +00:00
|
|
|
|
|
|
|
def test_ttl(self):
|
|
|
|
cd = cache.CacheDict(0.1)
|
|
|
|
cd['foo'] = 'bar'
|
2014-07-15 17:56:50 +00:00
|
|
|
self.assertIn('foo', cd)
|
|
|
|
self.assertEqual(cd['foo'], 'bar')
|
2014-07-22 21:16:52 +00:00
|
|
|
time.sleep(0.2)
|
2014-07-15 17:56:50 +00:00
|
|
|
self.assertNotIn('foo', cd)
|
|
|
|
|
2014-03-06 02:12:20 +00:00
|
|
|
# make sure that a get would get a regular old key error
|
|
|
|
self.assertRaises(KeyError, cd.__getitem__, 'foo')
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(CacheDictTestCase, needs_daemon=False)
|