test_cache.py 8.6 KB

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