test_vault.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 Testing libs
  8. from tests.support.unit import TestCase
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.mock import (
  11. MagicMock,
  12. call,
  13. patch)
  14. # Import Salt libs
  15. import salt.sdb.vault as vault
  16. class TestVaultSDB(LoaderModuleMockMixin, TestCase):
  17. '''
  18. Test case for the vault SDB module
  19. '''
  20. def setup_loader_modules(self):
  21. return {
  22. vault: {
  23. '__opts__': {
  24. 'vault': {
  25. 'url': "http://127.0.0.1",
  26. "auth": {
  27. 'token': 'test',
  28. 'method': 'token'
  29. }
  30. }
  31. }
  32. }
  33. }
  34. def test_set(self):
  35. '''
  36. Test salt.sdb.vault.set function
  37. '''
  38. mock_vault = MagicMock()
  39. mock_vault.return_value.status_code = 200
  40. with patch.dict(vault.__utils__, {'vault.make_request': mock_vault}):
  41. vault.set_('sdb://myvault/path/to/foo/bar', 'super awesome')
  42. assert mock_vault.call_args_list == [call('POST',
  43. 'v1/sdb://myvault/path/to/foo',
  44. None, json={'bar': 'super awesome'})]
  45. def test_set_question_mark(self):
  46. '''
  47. Test salt.sdb.vault.set_ while using the old
  48. deprecated solution with a question mark.
  49. '''
  50. mock_vault = MagicMock()
  51. mock_vault.return_value.status_code = 200
  52. with patch.dict(vault.__utils__, {'vault.make_request': mock_vault}):
  53. vault.set_('sdb://myvault/path/to/foo?bar', 'super awesome')
  54. assert mock_vault.call_args_list == [call('POST',
  55. 'v1/sdb://myvault/path/to/foo',
  56. None, json={'bar': 'super awesome'})]
  57. def test_get(self):
  58. '''
  59. Test salt.sdb.vault.get function
  60. '''
  61. mock_vault = MagicMock()
  62. mock_vault.return_value.status_code = 200
  63. mock_vault.content.return_value = [{'data': {'bar', 'test'}}]
  64. with patch.dict(vault.__utils__, {'vault.make_request': mock_vault}):
  65. vault.get('sdb://myvault/path/to/foo/bar')
  66. assert mock_vault.call_args_list == [call('GET',
  67. 'v1/sdb://myvault/path/to/foo',
  68. None)]
  69. def test_get_question_mark(self):
  70. '''
  71. Test salt.sdb.vault.get while using the old
  72. deprecated solution with a question mark.
  73. '''
  74. mock_vault = MagicMock()
  75. mock_vault.return_value.status_code = 200
  76. mock_vault.content.return_value = [{'data': {'bar', 'test'}}]
  77. with patch.dict(vault.__utils__, {'vault.make_request': mock_vault}):
  78. vault.get('sdb://myvault/path/to/foo?bar')
  79. assert mock_vault.call_args_list == [call('GET',
  80. 'v1/sdb://myvault/path/to/foo',
  81. None)]