test_etcd_util.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing Libs
  8. from tests.support.unit import TestCase, skipIf
  9. from tests.support.mock import (
  10. MagicMock,
  11. patch,
  12. )
  13. # Import Salt Libs
  14. import salt.utils.etcd_util as etcd_util
  15. try:
  16. from urllib3.exceptions import ReadTimeoutError, MaxRetryError
  17. HAS_URLLIB3 = True
  18. except ImportError:
  19. HAS_URLLIB3 = False
  20. try:
  21. import etcd
  22. HAS_ETCD = True
  23. except ImportError:
  24. HAS_ETCD = False
  25. @skipIf(HAS_URLLIB3 is False, 'urllib3 module must be installed.')
  26. @skipIf(HAS_ETCD is False, 'python-etcd module must be installed.')
  27. class EtcdUtilTestCase(TestCase):
  28. '''
  29. Test cases for salt.utils.etcd_util
  30. '''
  31. # 'get_' function tests: 1
  32. def test_read(self):
  33. '''
  34. Test to make sure we interact with etcd correctly
  35. '''
  36. with patch('etcd.Client', autospec=True) as mock:
  37. etcd_client = mock.return_value
  38. etcd_return = MagicMock(value='salt')
  39. etcd_client.read.return_value = etcd_return
  40. client = etcd_util.EtcdClient({})
  41. self.assertEqual(client.read('/salt'), etcd_return)
  42. etcd_client.read.assert_called_with('/salt', recursive=False, wait=False, timeout=None)
  43. client.read('salt', True, True, 10, 5)
  44. etcd_client.read.assert_called_with('salt', recursive=True, wait=True, timeout=10, waitIndex=5)
  45. etcd_client.read.side_effect = etcd.EtcdKeyNotFound
  46. self.assertRaises(etcd.EtcdKeyNotFound, client.read, 'salt')
  47. etcd_client.read.side_effect = etcd.EtcdConnectionFailed
  48. self.assertRaises(etcd.EtcdConnectionFailed, client.read, 'salt')
  49. etcd_client.read.side_effect = etcd.EtcdValueError
  50. self.assertRaises(etcd.EtcdValueError, client.read, 'salt')
  51. etcd_client.read.side_effect = ValueError
  52. self.assertRaises(ValueError, client.read, 'salt')
  53. etcd_client.read.side_effect = ReadTimeoutError(None, None, None)
  54. self.assertRaises(etcd.EtcdConnectionFailed, client.read, 'salt')
  55. etcd_client.read.side_effect = MaxRetryError(None, None)
  56. self.assertRaises(etcd.EtcdConnectionFailed, client.read, 'salt')
  57. def test_get(self):
  58. '''
  59. Test if it get a value from etcd, by direct path
  60. '''
  61. with patch('etcd.Client') as mock:
  62. client = etcd_util.EtcdClient({})
  63. with patch.object(client, 'read', autospec=True) as mock:
  64. mock.return_value = MagicMock(value='stack')
  65. self.assertEqual(client.get('salt'), 'stack')
  66. mock.assert_called_with('salt', recursive=False)
  67. self.assertEqual(client.get('salt', recurse=True), 'stack')
  68. mock.assert_called_with('salt', recursive=True)
  69. # iter(list(Exception)) works correctly with both mock<1.1 and mock>=1.1
  70. mock.side_effect = iter([etcd.EtcdKeyNotFound()])
  71. self.assertEqual(client.get('not-found'), None)
  72. mock.side_effect = iter([etcd.EtcdConnectionFailed()])
  73. self.assertEqual(client.get('watching'), None)
  74. # python 2.6 test
  75. mock.side_effect = ValueError
  76. self.assertEqual(client.get('not-found'), None)
  77. mock.side_effect = Exception
  78. self.assertRaises(Exception, client.get, 'some-error')
  79. def test_tree(self):
  80. '''
  81. Test recursive gets
  82. '''
  83. with patch('etcd.Client') as mock:
  84. client = etcd_util.EtcdClient({})
  85. with patch.object(client, 'read', autospec=True) as mock:
  86. c1, c2 = MagicMock(), MagicMock()
  87. c1.__iter__.return_value = [
  88. MagicMock(key='/x/a', value='1'),
  89. MagicMock(key='/x/b', value='2'),
  90. MagicMock(key='/x/c', dir=True)]
  91. c2.__iter__.return_value = [
  92. MagicMock(key='/x/c/d', value='3')
  93. ]
  94. mock.side_effect = iter([
  95. MagicMock(children=c1),
  96. MagicMock(children=c2)
  97. ])
  98. self.assertDictEqual(client.tree('/x'), {'a': '1', 'b': '2', 'c': {'d': '3'}})
  99. mock.assert_any_call('/x')
  100. mock.assert_any_call('/x/c')
  101. # iter(list(Exception)) works correctly with both mock<1.1 and mock>=1.1
  102. mock.side_effect = iter([etcd.EtcdKeyNotFound()])
  103. self.assertEqual(client.tree('not-found'), None)
  104. mock.side_effect = ValueError
  105. self.assertEqual(client.tree('/x'), None)
  106. mock.side_effect = Exception
  107. self.assertRaises(Exception, client.tree, 'some-error')
  108. def test_ls(self):
  109. with patch('etcd.Client') as mock:
  110. client = etcd_util.EtcdClient({})
  111. with patch.object(client, 'read', autospec=True) as mock:
  112. c1 = MagicMock()
  113. c1.__iter__.return_value = [
  114. MagicMock(key='/x/a', value='1'),
  115. MagicMock(key='/x/b', value='2'),
  116. MagicMock(key='/x/c', dir=True)]
  117. mock.return_value = MagicMock(children=c1)
  118. self.assertEqual(client.ls('/x'), {'/x': {'/x/a': '1', '/x/b': '2', '/x/c/': {}}})
  119. mock.assert_called_with('/x')
  120. # iter(list(Exception)) works correctly with both mock<1.1 and mock>=1.1
  121. mock.side_effect = iter([etcd.EtcdKeyNotFound()])
  122. self.assertEqual(client.ls('/not-found'), {})
  123. mock.side_effect = Exception
  124. self.assertRaises(Exception, client.tree, 'some-error')
  125. def test_write(self):
  126. with patch('etcd.Client', autospec=True) as mock:
  127. client = etcd_util.EtcdClient({})
  128. etcd_client = mock.return_value
  129. etcd_client.write.return_value = MagicMock(value='salt')
  130. self.assertEqual(client.write('/some-key', 'salt'), 'salt')
  131. etcd_client.write.assert_called_with('/some-key', 'salt', ttl=None, dir=False)
  132. self.assertEqual(client.write('/some-key', 'salt', ttl=5), 'salt')
  133. etcd_client.write.assert_called_with('/some-key', 'salt', ttl=5, dir=False)
  134. etcd_client.write.return_value = MagicMock(dir=True)
  135. self.assertEqual(client.write('/some-dir', 'salt', ttl=0, directory=True), True)
  136. etcd_client.write.assert_called_with('/some-dir', None, ttl=0, dir=True)
  137. etcd_client.write.side_effect = etcd.EtcdRootReadOnly()
  138. self.assertEqual(client.write('/', 'some-val'), None)
  139. etcd_client.write.side_effect = etcd.EtcdNotFile()
  140. self.assertEqual(client.write('/some-key', 'some-val'), None)
  141. etcd_client.write.side_effect = etcd.EtcdNotDir()
  142. self.assertEqual(client.write('/some-dir', 'some-val'), None)
  143. etcd_client.write.side_effect = MaxRetryError(None, None)
  144. self.assertEqual(client.write('/some-key', 'some-val'), None)
  145. etcd_client.write.side_effect = ValueError
  146. self.assertEqual(client.write('/some-key', 'some-val'), None)
  147. etcd_client.write.side_effect = Exception
  148. self.assertRaises(Exception, client.set, 'some-key', 'some-val')
  149. def test_flatten(self):
  150. with patch('etcd.Client', autospec=True) as mock:
  151. client = etcd_util.EtcdClient({})
  152. some_data = {
  153. '/x/y/a': '1',
  154. 'x': {
  155. 'y': {
  156. 'b': '2'
  157. }
  158. },
  159. 'm/j/': '3',
  160. 'z': '4',
  161. 'd': {},
  162. }
  163. result_path = {
  164. '/test/x/y/a': '1',
  165. '/test/x/y/b': '2',
  166. '/test/m/j': '3',
  167. '/test/z': '4',
  168. '/test/d': {},
  169. }
  170. result_nopath = {
  171. '/x/y/a': '1',
  172. '/x/y/b': '2',
  173. '/m/j': '3',
  174. '/z': '4',
  175. '/d': {},
  176. }
  177. result_root = {
  178. '/x/y/a': '1',
  179. '/x/y/b': '2',
  180. '/m/j': '3',
  181. '/z': '4',
  182. '/d': {},
  183. }
  184. self.assertEqual(client._flatten(some_data, path='/test'), result_path)
  185. self.assertEqual(client._flatten(some_data, path='/'), result_root)
  186. self.assertEqual(client._flatten(some_data), result_nopath)
  187. def test_update(self):
  188. with patch('etcd.Client', autospec=True) as mock:
  189. client = etcd_util.EtcdClient({})
  190. some_data = {
  191. '/x/y/a': '1',
  192. 'x': {
  193. 'y': {
  194. 'b': '3'
  195. }
  196. },
  197. 'm/j/': '3',
  198. 'z': '4',
  199. 'd': {},
  200. }
  201. result = {
  202. '/test/x/y/a': '1',
  203. '/test/x/y/b': '2',
  204. '/test/m/j': '3',
  205. '/test/z': '4',
  206. '/test/d': True,
  207. }
  208. flatten_result = {
  209. '/test/x/y/a': '1',
  210. '/test/x/y/b': '2',
  211. '/test/m/j': '3',
  212. '/test/z': '4',
  213. '/test/d': {}
  214. }
  215. client._flatten = MagicMock(return_value=flatten_result)
  216. self.assertEqual(client.update('/some/key', path='/blah'), None)
  217. with patch.object(client, 'write', autospec=True) as write_mock:
  218. def write_return(key, val, ttl=None, directory=None):
  219. return result.get(key, None)
  220. write_mock.side_effect = write_return
  221. self.assertDictEqual(client.update(some_data, path='/test'), result)
  222. client._flatten.assert_called_with(some_data, '/test')
  223. self.assertEqual(write_mock.call_count, 5)
  224. def test_rm(self):
  225. with patch('etcd.Client', autospec=True) as mock:
  226. etcd_client = mock.return_value
  227. client = etcd_util.EtcdClient({})
  228. etcd_client.delete.return_value = True
  229. self.assertEqual(client.rm('/some-key'), True)
  230. etcd_client.delete.assert_called_with('/some-key', recursive=False)
  231. self.assertEqual(client.rm('/some-dir', recurse=True), True)
  232. etcd_client.delete.assert_called_with('/some-dir', recursive=True)
  233. etcd_client.delete.side_effect = etcd.EtcdNotFile()
  234. self.assertEqual(client.rm('/some-dir'), None)
  235. etcd_client.delete.side_effect = etcd.EtcdDirNotEmpty()
  236. self.assertEqual(client.rm('/some-key'), None)
  237. etcd_client.delete.side_effect = etcd.EtcdRootReadOnly()
  238. self.assertEqual(client.rm('/'), None)
  239. etcd_client.delete.side_effect = ValueError
  240. self.assertEqual(client.rm('/some-dir'), None)
  241. etcd_client.delete.side_effect = Exception
  242. self.assertRaises(Exception, client.rm, 'some-dir')
  243. def test_watch(self):
  244. with patch('etcd.Client', autospec=True) as client_mock:
  245. client = etcd_util.EtcdClient({})
  246. with patch.object(client, 'read', autospec=True) as mock:
  247. mock.return_value = MagicMock(value='stack', key='/some-key', modifiedIndex=1, dir=False)
  248. self.assertDictEqual(client.watch('/some-key'),
  249. {'value': 'stack', 'key': '/some-key', 'mIndex': 1, 'changed': True, 'dir': False})
  250. mock.assert_called_with('/some-key', wait=True, recursive=False, timeout=0, waitIndex=None)
  251. mock.side_effect = iter([etcd_util.EtcdUtilWatchTimeout, mock.return_value])
  252. self.assertDictEqual(client.watch('/some-key'),
  253. {'value': 'stack', 'changed': False, 'mIndex': 1, 'key': '/some-key', 'dir': False})
  254. mock.side_effect = iter([etcd_util.EtcdUtilWatchTimeout, etcd.EtcdKeyNotFound])
  255. self.assertEqual(client.watch('/some-key'),
  256. {'value': None, 'changed': False, 'mIndex': 0, 'key': '/some-key', 'dir': False})
  257. mock.side_effect = iter([etcd_util.EtcdUtilWatchTimeout, ValueError])
  258. self.assertEqual(client.watch('/some-key'), {})
  259. mock.side_effect = None
  260. mock.return_value = MagicMock(value='stack', key='/some-key', modifiedIndex=1, dir=True)
  261. self.assertDictEqual(client.watch('/some-dir', recurse=True, timeout=5, index=10),
  262. {'value': 'stack', 'key': '/some-key', 'mIndex': 1, 'changed': True, 'dir': True})
  263. mock.assert_called_with('/some-dir', wait=True, recursive=True, timeout=5, waitIndex=10)
  264. # iter(list(Exception)) works correctly with both mock<1.1 and mock>=1.1
  265. mock.side_effect = iter([MaxRetryError(None, None)])
  266. self.assertEqual(client.watch('/some-key'), {})
  267. mock.side_effect = iter([etcd.EtcdConnectionFailed()])
  268. self.assertEqual(client.watch('/some-key'), {})
  269. mock.side_effect = None
  270. mock.return_value = None
  271. self.assertEqual(client.watch('/some-key'), {})