test_acme.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Herbert Buurman <herbert.buurman@ogd.nl>
  4. '''
  5. # Import future libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import MagicMock, patch
  11. # Import Salt Module
  12. import salt.states.acme as acme
  13. class AcmeTestCase(TestCase, LoaderModuleMockMixin):
  14. '''
  15. Test cases for salt.modules.acme
  16. '''
  17. def setup_loader_modules(self):
  18. return {acme: {'__opts__': {'test': False}}}
  19. def test_cert_no_changes_t(self):
  20. '''
  21. Test cert state with no needed changes. (test=True)
  22. '''
  23. # With test=True
  24. with patch.dict(acme.__salt__, { # pylint: disable=no-member
  25. 'acme.has': MagicMock(return_value=True),
  26. 'acme.needs_renewal': MagicMock(return_value=False),
  27. }), \
  28. patch.dict(acme.__opts__, {'test': True}): # pylint: disable=no-member
  29. self.assertEqual(acme.cert('test'), {
  30. 'name': 'test',
  31. 'result': True,
  32. 'comment': ['Certificate test exists and does not need renewal.'],
  33. 'changes': {},
  34. })
  35. def test_cert_no_changes(self):
  36. '''
  37. Test cert state with no needed changes. (test=False)
  38. '''
  39. # With test=False
  40. with patch.dict(acme.__salt__, { # pylint: disable=no-member
  41. 'acme.has': MagicMock(return_value=True),
  42. 'acme.needs_renewal': MagicMock(return_value=False),
  43. }):
  44. self.assertEqual(acme.cert('test'), {
  45. 'name': 'test',
  46. 'result': True,
  47. 'comment': ['Certificate test exists and does not need renewal.'],
  48. 'changes': {},
  49. })
  50. def test_cert_fresh_certificate_t(self):
  51. '''
  52. Test cert state fetching a new certificate. (test=True)
  53. '''
  54. # With test=True
  55. with patch.dict(acme.__salt__, { # pylint: disable=no-member
  56. 'acme.has': MagicMock(return_value=False),
  57. # 'acme.cert': MagicMock(return_value={'result': True, 'comment': 'Mockery'}),
  58. 'acme.info': MagicMock(return_value={'foo': 'bar'}),
  59. }), \
  60. patch.dict(acme.__opts__, {'test': True}): # pylint: disable=no-member
  61. self.assertEqual(acme.cert('test'), {
  62. 'name': 'test',
  63. 'result': None,
  64. 'comment': ['Certificate test would have been obtained.'],
  65. 'changes': {'old': 'current certificate', 'new': 'new certificate'},
  66. })
  67. def test_cert_fresh_certificate(self):
  68. '''
  69. Test cert state fetching a new certificate. (test=False)
  70. '''
  71. # With test=False
  72. with patch.dict(acme.__salt__, { # pylint: disable=no-member
  73. 'acme.has': MagicMock(return_value=False),
  74. 'acme.cert': MagicMock(return_value={'result': True, 'comment': 'Mockery'}),
  75. 'acme.info': MagicMock(return_value={'foo': 'bar'}),
  76. }):
  77. self.assertEqual(acme.cert('test'), {
  78. 'name': 'test',
  79. 'result': True,
  80. 'comment': ['Mockery'],
  81. 'changes': {'new': {'foo': 'bar'}},
  82. })
  83. def test_cert_renew_certificate_t(self):
  84. '''
  85. Test cert state renewing a certificate. (test=True)
  86. '''
  87. with patch.dict(acme.__salt__, { # pylint: disable=no-member
  88. 'acme.has': MagicMock(return_value=True),
  89. 'acme.needs_renewal': MagicMock(return_value=True),
  90. 'acme.info': MagicMock(side_effect=[{'name': 'old cert'}, {'name': 'new cert'}]),
  91. 'acme.cert': MagicMock(return_value={'result': True, 'comment': 'Mockery'}),
  92. }), \
  93. patch.dict(acme.__opts__, {'test': True}): # pylint: disable=no-member
  94. self.assertEqual(acme.cert('test'), {
  95. 'name': 'test',
  96. 'result': None,
  97. 'comment': ['Certificate test would have been renewed.'],
  98. 'changes': {'old': 'current certificate', 'new': 'new certificate'},
  99. })
  100. def test_cert_renew_certificate(self):
  101. '''
  102. Test cert state renewing a certificate. (test=False)
  103. '''
  104. with patch.dict(acme.__salt__, { # pylint: disable=no-member
  105. 'acme.has': MagicMock(return_value=True),
  106. 'acme.needs_renewal': MagicMock(return_value=True),
  107. 'acme.info': MagicMock(side_effect=[{'name': 'old cert'}, {'name': 'new cert'}]),
  108. 'acme.cert': MagicMock(return_value={'result': True, 'comment': 'Mockery'}),
  109. }):
  110. self.assertEqual(acme.cert('test'), {
  111. 'name': 'test',
  112. 'result': True,
  113. 'comment': ['Mockery'],
  114. 'changes': {'old': {'name': 'old cert'}, 'new': {'name': 'new cert'}},
  115. })