test_vault.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # -*- coding: utf-8 -*-
  2. """
  3. Test case for the vault SDB module
  4. """
  5. # Import python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt libs
  8. import salt.sdb.vault as vault
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.mock import MagicMock, call, patch
  11. # Import Salt Testing libs
  12. from tests.support.unit import TestCase
  13. class TestVaultSDB(LoaderModuleMockMixin, TestCase):
  14. """
  15. Test case for the vault SDB module
  16. """
  17. def setup_loader_modules(self):
  18. return {
  19. vault: {
  20. "__opts__": {
  21. "vault": {
  22. "url": "http://127.0.0.1",
  23. "auth": {"token": "test", "method": "token"},
  24. }
  25. }
  26. }
  27. }
  28. def test_set(self):
  29. """
  30. Test salt.sdb.vault.set function
  31. """
  32. version = {"v2": False, "data": None, "metadata": None, "type": None}
  33. mock_version = MagicMock(return_value=version)
  34. mock_vault = MagicMock()
  35. mock_vault.return_value.status_code = 200
  36. with patch.dict(
  37. vault.__utils__, {"vault.make_request": mock_vault}
  38. ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
  39. vault.set_("sdb://myvault/path/to/foo/bar", "super awesome")
  40. self.assertEqual(
  41. mock_vault.call_args_list,
  42. [
  43. call(
  44. "POST",
  45. "v1/sdb://myvault/path/to/foo",
  46. json={"bar": "super awesome"},
  47. )
  48. ],
  49. )
  50. def test_set_v2(self):
  51. """
  52. Test salt.sdb.vault.set function with kv v2 backend
  53. """
  54. version = {
  55. "v2": True,
  56. "data": "path/data/to/foo",
  57. "metadata": "path/metadata/to/foo",
  58. "type": "kv",
  59. }
  60. mock_version = MagicMock(return_value=version)
  61. mock_vault = MagicMock()
  62. mock_vault.return_value.status_code = 200
  63. with patch.dict(
  64. vault.__utils__, {"vault.make_request": mock_vault}
  65. ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
  66. vault.set_("sdb://myvault/path/to/foo/bar", "super awesome")
  67. self.assertEqual(
  68. mock_vault.call_args_list,
  69. [
  70. call(
  71. "POST",
  72. "v1/path/data/to/foo",
  73. json={"data": {"bar": "super awesome"}},
  74. )
  75. ],
  76. )
  77. def test_set_question_mark(self):
  78. """
  79. Test salt.sdb.vault.set_ while using the old
  80. deprecated solution with a question mark.
  81. """
  82. version = {"v2": False, "data": None, "metadata": None, "type": None}
  83. mock_version = MagicMock(return_value=version)
  84. mock_vault = MagicMock()
  85. mock_vault.return_value.status_code = 200
  86. with patch.dict(
  87. vault.__utils__, {"vault.make_request": mock_vault}
  88. ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
  89. vault.set_("sdb://myvault/path/to/foo?bar", "super awesome")
  90. self.assertEqual(
  91. mock_vault.call_args_list,
  92. [
  93. call(
  94. "POST",
  95. "v1/sdb://myvault/path/to/foo",
  96. json={"bar": "super awesome"},
  97. )
  98. ],
  99. )
  100. def test_get(self):
  101. """
  102. Test salt.sdb.vault.get function
  103. """
  104. version = {"v2": False, "data": None, "metadata": None, "type": None}
  105. mock_version = MagicMock(return_value=version)
  106. mock_vault = MagicMock()
  107. mock_vault.return_value.status_code = 200
  108. mock_vault.return_value.json.return_value = {"data": {"bar": "test"}}
  109. with patch.dict(
  110. vault.__utils__, {"vault.make_request": mock_vault}
  111. ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
  112. self.assertEqual(vault.get("sdb://myvault/path/to/foo/bar"), "test")
  113. self.assertEqual(
  114. mock_vault.call_args_list,
  115. [call("GET", "v1/sdb://myvault/path/to/foo", None)],
  116. )
  117. def test_get_v2(self):
  118. """
  119. Test salt.sdb.vault.get function with kv v2 backend
  120. """
  121. version = {
  122. "v2": True,
  123. "data": "path/data/to/foo",
  124. "metadata": "path/metadata/to/foo",
  125. "type": "kv",
  126. }
  127. mock_version = MagicMock(return_value=version)
  128. mock_vault = MagicMock()
  129. mock_vault.return_value.status_code = 200
  130. mock_vault.return_value.json.return_value = {"data": {"data": {"bar": "test"}}}
  131. with patch.dict(
  132. vault.__utils__, {"vault.make_request": mock_vault}
  133. ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
  134. self.assertEqual(vault.get("sdb://myvault/path/to/foo/bar"), "test")
  135. self.assertEqual(
  136. mock_vault.call_args_list, [call("GET", "v1/path/data/to/foo", None)]
  137. )
  138. def test_get_question_mark(self):
  139. """
  140. Test salt.sdb.vault.get while using the old
  141. deprecated solution with a question mark.
  142. """
  143. version = {"v2": False, "data": None, "metadata": None, "type": None}
  144. mock_version = MagicMock(return_value=version)
  145. mock_vault = MagicMock()
  146. mock_vault.return_value.status_code = 200
  147. mock_vault.return_value.json.return_value = {"data": {"bar": "test"}}
  148. with patch.dict(
  149. vault.__utils__, {"vault.make_request": mock_vault}
  150. ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
  151. self.assertEqual(vault.get("sdb://myvault/path/to/foo?bar"), "test")
  152. self.assertEqual(
  153. mock_vault.call_args_list,
  154. [call("GET", "v1/sdb://myvault/path/to/foo", None)],
  155. )
  156. def test_get_missing(self):
  157. """
  158. Test salt.sdb.vault.get function returns None
  159. if vault does not have an entry
  160. """
  161. version = {"v2": False, "data": None, "metadata": None, "type": None}
  162. mock_version = MagicMock(return_value=version)
  163. mock_vault = MagicMock()
  164. mock_vault.return_value.status_code = 404
  165. with patch.dict(
  166. vault.__utils__, {"vault.make_request": mock_vault}
  167. ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
  168. self.assertIsNone(vault.get("sdb://myvault/path/to/foo/bar"))
  169. assert mock_vault.call_args_list == [
  170. call("GET", "v1/sdb://myvault/path/to/foo", None)
  171. ]
  172. def test_get_missing_key(self):
  173. """
  174. Test salt.sdb.vault.get function returns None
  175. if vault does not have the key but does have the entry
  176. """
  177. version = {"v2": False, "data": None, "metadata": None, "type": None}
  178. mock_version = MagicMock(return_value=version)
  179. mock_vault = MagicMock()
  180. mock_vault.return_value.status_code = 200
  181. mock_vault.return_value.json.return_value = {"data": {"bar": "test"}}
  182. with patch.dict(
  183. vault.__utils__, {"vault.make_request": mock_vault}
  184. ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
  185. self.assertIsNone(vault.get("sdb://myvault/path/to/foo/foo"))
  186. assert mock_vault.call_args_list == [
  187. call("GET", "v1/sdb://myvault/path/to/foo", None)
  188. ]