1
0

test_cache.py 5.9 KB

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