test_gpg.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # -*- coding: utf-8 -*-
  2. # Import Python Libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Testing libs
  5. from tests.support.mixins import LoaderModuleMockMixin
  6. from tests.support.unit import skipIf, TestCase
  7. from tests.support.mock import (
  8. NO_MOCK,
  9. NO_MOCK_REASON,
  10. MagicMock,
  11. patch
  12. )
  13. # Import Salt libs
  14. import salt.renderers.gpg as gpg
  15. from salt.exceptions import SaltRenderError
  16. @skipIf(NO_MOCK, NO_MOCK_REASON)
  17. class GPGTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. unit test GPG renderer
  20. '''
  21. def setup_loader_modules(self):
  22. return {gpg: {}}
  23. def test__get_gpg_exec(self):
  24. '''
  25. test _get_gpg_exec
  26. '''
  27. gpg_exec = '/bin/gpg'
  28. with patch('salt.utils.path.which', MagicMock(return_value=gpg_exec)):
  29. self.assertEqual(gpg._get_gpg_exec(), gpg_exec)
  30. with patch('salt.utils.path.which', MagicMock(return_value=False)):
  31. self.assertRaises(SaltRenderError, gpg._get_gpg_exec)
  32. def test__decrypt_ciphertext(self):
  33. '''
  34. test _decrypt_ciphertext
  35. '''
  36. key_dir = '/etc/salt/gpgkeys'
  37. secret = 'Use more salt.'
  38. crypted = '!@#$%^&*()_+'
  39. class GPGDecrypt(object):
  40. def communicate(self, *args, **kwargs):
  41. return [secret, None]
  42. class GPGNotDecrypt(object):
  43. def communicate(self, *args, **kwargs):
  44. return [None, 'decrypt error']
  45. with patch('salt.renderers.gpg._get_key_dir', MagicMock(return_value=key_dir)), \
  46. patch('salt.utils.path.which', MagicMock()):
  47. with patch('salt.renderers.gpg.Popen', MagicMock(return_value=GPGDecrypt())):
  48. self.assertEqual(gpg._decrypt_ciphertext(crypted), secret)
  49. with patch('salt.renderers.gpg.Popen', MagicMock(return_value=GPGNotDecrypt())):
  50. self.assertEqual(gpg._decrypt_ciphertext(crypted), crypted)
  51. def test__decrypt_object(self):
  52. '''
  53. test _decrypt_object
  54. '''
  55. secret = 'Use more salt.'
  56. crypted = '-----BEGIN PGP MESSAGE-----!@#$%^&*()_+'
  57. secret_map = {'secret': secret}
  58. crypted_map = {'secret': crypted}
  59. secret_list = [secret]
  60. crypted_list = [crypted]
  61. with patch('salt.renderers.gpg._decrypt_ciphertext', MagicMock(return_value=secret)):
  62. self.assertEqual(gpg._decrypt_object(secret), secret)
  63. self.assertEqual(gpg._decrypt_object(crypted), secret)
  64. self.assertEqual(gpg._decrypt_object(crypted_map), secret_map)
  65. self.assertEqual(gpg._decrypt_object(crypted_list), secret_list)
  66. self.assertEqual(gpg._decrypt_object(None), None)
  67. def test_render(self):
  68. '''
  69. test render
  70. '''
  71. key_dir = '/etc/salt/gpgkeys'
  72. secret = 'Use more salt.'
  73. crypted = '-----BEGIN PGP MESSAGE-----!@#$%^&*()_+'
  74. with patch('salt.renderers.gpg._get_gpg_exec', MagicMock(return_value=True)):
  75. with patch('salt.renderers.gpg._get_key_dir', MagicMock(return_value=key_dir)):
  76. with patch('salt.renderers.gpg._decrypt_object', MagicMock(return_value=secret)):
  77. self.assertEqual(gpg.render(crypted), secret)