test_cache.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # -*- coding: utf-8 -*-
  2. '''
  3. unit tests for salt.cache
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing libs
  8. # import integration
  9. from tests.support.unit import skipIf, TestCase
  10. from tests.support.mock import (
  11. NO_MOCK,
  12. NO_MOCK_REASON,
  13. patch,
  14. )
  15. # Import Salt libs
  16. import salt.payload
  17. import salt.cache
  18. class CacheFunctionsTest(TestCase):
  19. '''
  20. Validate the cache package functions.
  21. '''
  22. def setUp(self):
  23. self.opts = {'cache': 'localfs',
  24. 'memcache_expire_seconds': 0,
  25. 'memcache_max_items': 0,
  26. 'memcache_full_cleanup': False,
  27. 'memcache_debug': False}
  28. def test_factory_cache(self):
  29. ret = salt.cache.factory(self.opts)
  30. self.assertIsInstance(ret, salt.cache.Cache)
  31. def test_factory_memcache(self):
  32. self.opts['memcache_expire_seconds'] = 10
  33. ret = salt.cache.factory(self.opts)
  34. self.assertIsInstance(ret, salt.cache.MemCache)
  35. @skipIf(NO_MOCK, NO_MOCK_REASON)
  36. class MemCacheTest(TestCase):
  37. '''
  38. Validate Cache class methods
  39. '''
  40. @patch('salt.payload.Serial')
  41. def setUp(self, serial_mock): # pylint: disable=W0221
  42. salt.cache.MemCache.data = {}
  43. self.opts = {'cache': 'fake_driver',
  44. 'memcache_expire_seconds': 10,
  45. 'memcache_max_items': 3,
  46. 'memcache_full_cleanup': False,
  47. 'memcache_debug': False}
  48. self.cache = salt.cache.factory(self.opts)
  49. @patch('salt.cache.Cache.fetch', return_value='fake_data')
  50. @patch('salt.loader.cache', return_value={})
  51. def test_fetch(self, loader_mock, cache_fetch_mock):
  52. # Fetch value, it will be kept in cache.
  53. with patch('time.time', return_value=0):
  54. ret = self.cache.fetch('bank', 'key')
  55. self.assertEqual(ret, 'fake_data')
  56. self.assertDictEqual(salt.cache.MemCache.data, {
  57. 'fake_driver': {
  58. ('bank', 'key'): [0, 'fake_data'],
  59. }})
  60. cache_fetch_mock.assert_called_once_with('bank', 'key')
  61. cache_fetch_mock.reset_mock()
  62. # Fetch again, cached value is used, time updated.
  63. with patch('time.time', return_value=1):
  64. ret = self.cache.fetch('bank', 'key')
  65. self.assertEqual(ret, 'fake_data')
  66. self.assertDictEqual(salt.cache.MemCache.data, {
  67. 'fake_driver': {
  68. ('bank', 'key'): [1, 'fake_data'],
  69. }})
  70. cache_fetch_mock.assert_not_called()
  71. # Fetch after expire
  72. with patch('time.time', return_value=12):
  73. ret = self.cache.fetch('bank', 'key')
  74. self.assertEqual(ret, 'fake_data')
  75. self.assertDictEqual(salt.cache.MemCache.data, {
  76. 'fake_driver': {
  77. ('bank', 'key'): [12, 'fake_data'],
  78. }})
  79. cache_fetch_mock.assert_called_once_with('bank', 'key')
  80. cache_fetch_mock.reset_mock()
  81. @patch('salt.cache.Cache.store')
  82. @patch('salt.loader.cache', return_value={})
  83. def test_store(self, loader_mock, cache_store_mock):
  84. # Fetch value, it will be kept in cache.
  85. with patch('time.time', return_value=0):
  86. self.cache.store('bank', 'key', 'fake_data')
  87. self.assertDictEqual(salt.cache.MemCache.data, {
  88. 'fake_driver': {
  89. ('bank', 'key'): [0, 'fake_data'],
  90. }})
  91. cache_store_mock.assert_called_once_with('bank', 'key', 'fake_data')
  92. cache_store_mock.reset_mock()
  93. # Store another value.
  94. with patch('time.time', return_value=1):
  95. self.cache.store('bank', 'key2', 'fake_data2')
  96. self.assertDictEqual(salt.cache.MemCache.data, {
  97. 'fake_driver': {
  98. ('bank', 'key'): [0, 'fake_data'],
  99. ('bank', 'key2'): [1, 'fake_data2'],
  100. }})
  101. cache_store_mock.assert_called_once_with('bank', 'key2', 'fake_data2')
  102. @patch('salt.cache.Cache.store')
  103. @patch('salt.cache.Cache.flush')
  104. @patch('salt.loader.cache', return_value={})
  105. def test_flush(self, loader_mock, cache_flush_mock, cache_store_mock):
  106. # Flush non-existing bank
  107. self.cache.flush('bank')
  108. self.assertDictEqual(salt.cache.MemCache.data, {'fake_driver': {}})
  109. cache_flush_mock.assert_called_once_with('bank', None)
  110. cache_flush_mock.reset_mock()
  111. # Flush non-existing key
  112. self.cache.flush('bank', 'key')
  113. self.assertDictEqual(salt.cache.MemCache.data, {'fake_driver': {}})
  114. cache_flush_mock.assert_called_once_with('bank', 'key')
  115. cache_flush_mock.reset_mock()
  116. # Flush existing key
  117. with patch('time.time', return_value=0):
  118. self.cache.store('bank', 'key', 'fake_data')
  119. self.assertEqual(salt.cache.MemCache.data['fake_driver'][('bank', 'key')],
  120. [0, 'fake_data'])
  121. self.assertDictEqual(salt.cache.MemCache.data, {
  122. 'fake_driver': {
  123. ('bank', 'key'): [0, 'fake_data'],
  124. }})
  125. self.cache.flush('bank', 'key')
  126. self.assertDictEqual(salt.cache.MemCache.data, {'fake_driver': {}})
  127. cache_flush_mock.assert_called_once_with('bank', 'key')
  128. cache_flush_mock.reset_mock()
  129. @patch('salt.cache.Cache.store')
  130. @patch('salt.loader.cache', return_value={})
  131. def test_max_items(self, loader_mock, cache_store_mock):
  132. # Put MAX=3 values
  133. with patch('time.time', return_value=0):
  134. self.cache.store('bank1', 'key1', 'fake_data11')
  135. with patch('time.time', return_value=1):
  136. self.cache.store('bank1', 'key2', 'fake_data12')
  137. with patch('time.time', return_value=2):
  138. self.cache.store('bank2', 'key1', 'fake_data21')
  139. self.assertDictEqual(salt.cache.MemCache.data['fake_driver'], {
  140. ('bank1', 'key1'): [0, 'fake_data11'],
  141. ('bank1', 'key2'): [1, 'fake_data12'],
  142. ('bank2', 'key1'): [2, 'fake_data21'],
  143. })
  144. # Put one more and check the oldest was removed
  145. with patch('time.time', return_value=3):
  146. self.cache.store('bank2', 'key2', 'fake_data22')
  147. self.assertDictEqual(salt.cache.MemCache.data['fake_driver'], {
  148. ('bank1', 'key2'): [1, 'fake_data12'],
  149. ('bank2', 'key1'): [2, 'fake_data21'],
  150. ('bank2', 'key2'): [3, 'fake_data22'],
  151. })
  152. @patch('salt.cache.Cache.store')
  153. @patch('salt.loader.cache', return_value={})
  154. def test_full_cleanup(self, loader_mock, cache_store_mock):
  155. # Enable full cleanup
  156. self.cache.cleanup = True
  157. # Put MAX=3 values
  158. with patch('time.time', return_value=0):
  159. self.cache.store('bank1', 'key1', 'fake_data11')
  160. with patch('time.time', return_value=1):
  161. self.cache.store('bank1', 'key2', 'fake_data12')
  162. with patch('time.time', return_value=2):
  163. self.cache.store('bank2', 'key1', 'fake_data21')
  164. self.assertDictEqual(salt.cache.MemCache.data['fake_driver'], {
  165. ('bank1', 'key1'): [0, 'fake_data11'],
  166. ('bank1', 'key2'): [1, 'fake_data12'],
  167. ('bank2', 'key1'): [2, 'fake_data21'],
  168. })
  169. # Put one more and check all expired was removed
  170. with patch('time.time', return_value=12):
  171. self.cache.store('bank2', 'key2', 'fake_data22')
  172. self.assertDictEqual(salt.cache.MemCache.data['fake_driver'], {
  173. ('bank2', 'key1'): [2, 'fake_data21'],
  174. ('bank2', 'key2'): [12, 'fake_data22'],
  175. })
  176. @patch('salt.cache.Cache.fetch', return_value='fake_data')
  177. @patch('salt.loader.cache', return_value={})
  178. def test_fetch_debug(self, loader_mock, cache_fetch_mock):
  179. # Recreate cache with debug enabled
  180. self.opts['memcache_debug'] = True
  181. self.cache = salt.cache.factory(self.opts)
  182. # Fetch 2 values (no cache hit)
  183. with patch('time.time', return_value=0):
  184. ret = self.cache.fetch('bank', 'key1')
  185. with patch('time.time', return_value=1):
  186. ret = self.cache.fetch('bank', 'key2')
  187. # Fetch 3 times (cache hit)
  188. with patch('time.time', return_value=2):
  189. ret = self.cache.fetch('bank', 'key2')
  190. with patch('time.time', return_value=3):
  191. ret = self.cache.fetch('bank', 'key1')
  192. with patch('time.time', return_value=4):
  193. ret = self.cache.fetch('bank', 'key1')
  194. # Fetch an expired value (no cache hit)
  195. with patch('time.time', return_value=13):
  196. ret = self.cache.fetch('bank', 'key2')
  197. # Check debug data
  198. self.assertEqual(self.cache.call, 6)
  199. self.assertEqual(self.cache.hit, 3)