1
0

test_cache.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # -*- coding: utf-8 -*-
  2. '''
  3. tests.unit.utils.cache_test
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. Test the salt cache objects
  6. '''
  7. # Import python libs
  8. from __future__ import absolute_import, print_function, unicode_literals
  9. import os
  10. import time
  11. import tempfile
  12. import shutil
  13. # Import Salt Testing libs
  14. from tests.support.unit import TestCase
  15. # Import salt libs
  16. import salt.config
  17. import salt.loader
  18. import salt.payload
  19. import salt.utils.data
  20. import salt.utils.files
  21. import salt.utils.cache as cache
  22. class CacheDictTestCase(TestCase):
  23. def test_sanity(self):
  24. '''
  25. Make sure you can instantiate etc.
  26. '''
  27. cd = cache.CacheDict(5)
  28. self.assertIsInstance(cd, cache.CacheDict)
  29. # do some tests to make sure it looks like a dict
  30. self.assertNotIn('foo', cd)
  31. cd['foo'] = 'bar'
  32. self.assertEqual(cd['foo'], 'bar')
  33. del cd['foo']
  34. self.assertNotIn('foo', cd)
  35. def test_ttl(self):
  36. cd = cache.CacheDict(0.1)
  37. cd['foo'] = 'bar'
  38. self.assertIn('foo', cd)
  39. self.assertEqual(cd['foo'], 'bar')
  40. time.sleep(0.2)
  41. self.assertNotIn('foo', cd)
  42. # make sure that a get would get a regular old key error
  43. self.assertRaises(KeyError, cd.__getitem__, 'foo')
  44. class CacheContextTestCase(TestCase):
  45. def setUp(self):
  46. context_dir = os.path.join(tempfile.gettempdir(), 'context')
  47. if os.path.exists(context_dir):
  48. shutil.rmtree(os.path.join(tempfile.gettempdir(), 'context'))
  49. def test_smoke_context(self):
  50. '''
  51. Smoke test the context cache
  52. '''
  53. if os.path.exists(os.path.join(tempfile.gettempdir(), 'context')):
  54. self.skipTest('Context dir already exists')
  55. else:
  56. opts = salt.config.DEFAULT_MINION_OPTS
  57. opts['cachedir'] = tempfile.gettempdir()
  58. context_cache = cache.ContextCache(opts, 'cache_test')
  59. context_cache.cache_context({'a': 'b'})
  60. ret = context_cache.get_cache_context()
  61. self.assertDictEqual({'a': 'b'}, ret)
  62. def test_context_wrapper(self):
  63. '''
  64. Test to ensure that a module which decorates itself
  65. with a context cache can store and retrieve its contextual
  66. data
  67. '''
  68. opts = salt.config.DEFAULT_MINION_OPTS
  69. opts['cachedir'] = tempfile.gettempdir()
  70. ll_ = salt.loader.LazyLoader(
  71. [os.path.join(os.path.dirname(os.path.realpath(__file__)), 'cache_mods')],
  72. tag='rawmodule',
  73. virtual_enable=False,
  74. opts=opts)
  75. cache_test_func = ll_['cache_mod.test_context_module']
  76. self.assertEqual(cache_test_func()['called'], 0)
  77. self.assertEqual(cache_test_func()['called'], 1)
  78. __context__ = {'a': 'b'}
  79. __opts__ = {'cachedir': '/tmp'}
  80. class ContextCacheTest(TestCase):
  81. '''
  82. Test case for salt.utils.cache.ContextCache
  83. '''
  84. def setUp(self):
  85. '''
  86. Clear the cache before every test
  87. '''
  88. context_dir = os.path.join(__opts__['cachedir'], 'context')
  89. if os.path.isdir(context_dir):
  90. shutil.rmtree(context_dir)
  91. def test_set_cache(self):
  92. '''
  93. Tests to ensure the cache is written correctly
  94. '''
  95. @cache.context_cache
  96. def _test_set_cache():
  97. '''
  98. This will inherit globals from the test module itself.
  99. Normally these are injected by the salt loader [salt.loader]
  100. '''
  101. pass
  102. _test_set_cache()
  103. target_cache_file = os.path.join(__opts__['cachedir'], 'context', '{0}.p'.format(__name__))
  104. self.assertTrue(os.path.isfile(target_cache_file), 'Context cache did not write cache file')
  105. # Test manual de-serialize
  106. with salt.utils.files.fopen(target_cache_file, 'rb') as fp_:
  107. target_cache_data = salt.utils.data.decode(salt.payload.Serial(__opts__).load(fp_))
  108. self.assertDictEqual(__context__, target_cache_data)
  109. # Test cache de-serialize
  110. cc = cache.ContextCache(__opts__, __name__)
  111. retrieved_cache = cc.get_cache_context()
  112. self.assertDictEqual(retrieved_cache, __context__)
  113. def test_refill_cache(self):
  114. '''
  115. Tests to ensure that the context cache can rehydrate a wrapped function
  116. '''
  117. # First populate the cache
  118. @cache.context_cache
  119. def _test_set_cache():
  120. pass
  121. _test_set_cache()
  122. # Then try to rehydate a func
  123. @cache.context_cache
  124. def _test_refill_cache(comparison_context):
  125. self.assertEqual(__context__, comparison_context)
  126. global __context__
  127. __context__ = {}
  128. _test_refill_cache({'a': 'b'}) # Compare to the context before it was emptied
  129. class CacheDiskTestCase(TestCase):
  130. def test_everything(self):
  131. '''
  132. Make sure you can instantiate, add, update, remove, expire
  133. '''
  134. try:
  135. tmpdir = tempfile.mkdtemp()
  136. path = os.path.join(tmpdir, 'CacheDisk_test')
  137. # test instantiation
  138. cd = cache.CacheDisk(0.1, path)
  139. self.assertIsInstance(cd, cache.CacheDisk)
  140. # test to make sure it looks like a dict
  141. self.assertNotIn('foo', cd)
  142. cd['foo'] = 'bar'
  143. self.assertIn('foo', cd)
  144. self.assertEqual(cd['foo'], 'bar')
  145. del cd['foo']
  146. self.assertNotIn('foo', cd)
  147. # test persistence
  148. cd['foo'] = 'bar'
  149. cd2 = cache.CacheDisk(0.1, path)
  150. self.assertIn('foo', cd2)
  151. self.assertEqual(cd2['foo'], 'bar')
  152. # test ttl
  153. time.sleep(0.2)
  154. self.assertNotIn('foo', cd)
  155. self.assertNotIn('foo', cd2)
  156. finally:
  157. shutil.rmtree(tmpdir, ignore_errors=True)