123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- """
- Test case for the vault SDB module
- """
- # Import python libs
- # Import Salt libs
- import salt.sdb.vault as vault
- from tests.support.mixins import LoaderModuleMockMixin
- from tests.support.mock import MagicMock, call, patch
- # Import Salt Testing libs
- from tests.support.unit import TestCase
- class TestVaultSDB(LoaderModuleMockMixin, TestCase):
- """
- Test case for the vault SDB module
- """
- def setup_loader_modules(self):
- return {
- vault: {
- "__opts__": {
- "vault": {
- "url": "http://127.0.0.1",
- "auth": {"token": "test", "method": "token"},
- }
- }
- }
- }
- def test_set(self):
- """
- Test salt.sdb.vault.set function
- """
- version = {"v2": False, "data": None, "metadata": None, "type": None}
- mock_version = MagicMock(return_value=version)
- mock_vault = MagicMock()
- mock_vault.return_value.status_code = 200
- with patch.dict(
- vault.__utils__, {"vault.make_request": mock_vault}
- ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
- vault.set_("sdb://myvault/path/to/foo/bar", "super awesome")
- self.assertEqual(
- mock_vault.call_args_list,
- [
- call(
- "POST",
- "v1/sdb://myvault/path/to/foo",
- json={"bar": "super awesome"},
- )
- ],
- )
- def test_set_v2(self):
- """
- Test salt.sdb.vault.set function with kv v2 backend
- """
- version = {
- "v2": True,
- "data": "path/data/to/foo",
- "metadata": "path/metadata/to/foo",
- "type": "kv",
- }
- mock_version = MagicMock(return_value=version)
- mock_vault = MagicMock()
- mock_vault.return_value.status_code = 200
- with patch.dict(
- vault.__utils__, {"vault.make_request": mock_vault}
- ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
- vault.set_("sdb://myvault/path/to/foo/bar", "super awesome")
- self.assertEqual(
- mock_vault.call_args_list,
- [
- call(
- "POST",
- "v1/path/data/to/foo",
- json={"data": {"bar": "super awesome"}},
- )
- ],
- )
- def test_set_question_mark(self):
- """
- Test salt.sdb.vault.set_ while using the old
- deprecated solution with a question mark.
- """
- version = {"v2": False, "data": None, "metadata": None, "type": None}
- mock_version = MagicMock(return_value=version)
- mock_vault = MagicMock()
- mock_vault.return_value.status_code = 200
- with patch.dict(
- vault.__utils__, {"vault.make_request": mock_vault}
- ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
- vault.set_("sdb://myvault/path/to/foo?bar", "super awesome")
- self.assertEqual(
- mock_vault.call_args_list,
- [
- call(
- "POST",
- "v1/sdb://myvault/path/to/foo",
- json={"bar": "super awesome"},
- )
- ],
- )
- def test_get(self):
- """
- Test salt.sdb.vault.get function
- """
- version = {"v2": False, "data": None, "metadata": None, "type": None}
- mock_version = MagicMock(return_value=version)
- mock_vault = MagicMock()
- mock_vault.return_value.status_code = 200
- mock_vault.return_value.json.return_value = {"data": {"bar": "test"}}
- with patch.dict(
- vault.__utils__, {"vault.make_request": mock_vault}
- ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
- self.assertEqual(vault.get("sdb://myvault/path/to/foo/bar"), "test")
- self.assertEqual(
- mock_vault.call_args_list, [call("GET", "v1/sdb://myvault/path/to/foo")],
- )
- def test_get_v2(self):
- """
- Test salt.sdb.vault.get function with kv v2 backend
- """
- version = {
- "v2": True,
- "data": "path/data/to/foo",
- "metadata": "path/metadata/to/foo",
- "type": "kv",
- }
- mock_version = MagicMock(return_value=version)
- mock_vault = MagicMock()
- mock_vault.return_value.status_code = 200
- mock_vault.return_value.json.return_value = {"data": {"data": {"bar": "test"}}}
- with patch.dict(
- vault.__utils__, {"vault.make_request": mock_vault}
- ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
- self.assertEqual(vault.get("sdb://myvault/path/to/foo/bar"), "test")
- self.assertEqual(
- mock_vault.call_args_list, [call("GET", "v1/path/data/to/foo")]
- )
- def test_get_question_mark(self):
- """
- Test salt.sdb.vault.get while using the old
- deprecated solution with a question mark.
- """
- version = {"v2": False, "data": None, "metadata": None, "type": None}
- mock_version = MagicMock(return_value=version)
- mock_vault = MagicMock()
- mock_vault.return_value.status_code = 200
- mock_vault.return_value.json.return_value = {"data": {"bar": "test"}}
- with patch.dict(
- vault.__utils__, {"vault.make_request": mock_vault}
- ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
- self.assertEqual(vault.get("sdb://myvault/path/to/foo?bar"), "test")
- self.assertEqual(
- mock_vault.call_args_list, [call("GET", "v1/sdb://myvault/path/to/foo")],
- )
- def test_get_missing(self):
- """
- Test salt.sdb.vault.get function returns None
- if vault does not have an entry
- """
- version = {"v2": False, "data": None, "metadata": None, "type": None}
- mock_version = MagicMock(return_value=version)
- mock_vault = MagicMock()
- mock_vault.return_value.status_code = 404
- with patch.dict(
- vault.__utils__, {"vault.make_request": mock_vault}
- ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
- self.assertIsNone(vault.get("sdb://myvault/path/to/foo/bar"))
- assert mock_vault.call_args_list == [
- call("GET", "v1/sdb://myvault/path/to/foo")
- ]
- def test_get_missing_key(self):
- """
- Test salt.sdb.vault.get function returns None
- if vault does not have the key but does have the entry
- """
- version = {"v2": False, "data": None, "metadata": None, "type": None}
- mock_version = MagicMock(return_value=version)
- mock_vault = MagicMock()
- mock_vault.return_value.status_code = 200
- mock_vault.return_value.json.return_value = {"data": {"bar": "test"}}
- with patch.dict(
- vault.__utils__, {"vault.make_request": mock_vault}
- ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
- self.assertIsNone(vault.get("sdb://myvault/path/to/foo/foo"))
- assert mock_vault.call_args_list == [
- call("GET", "v1/sdb://myvault/path/to/foo")
- ]
|