test_boto_elasticsearch_domain.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import logging
  5. import random
  6. import string
  7. # Import Salt Testing libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import skipIf, TestCase
  10. from tests.support.mock import MagicMock, patch
  11. # Import Salt libs
  12. from salt.ext import six
  13. import salt.loader
  14. from salt.utils.versions import LooseVersion
  15. import salt.states.boto_elasticsearch_domain as boto_elasticsearch_domain
  16. # Import test suite libs
  17. # pylint: disable=import-error,no-name-in-module,unused-import
  18. from tests.unit.modules.test_boto_elasticsearch_domain import BotoElasticsearchDomainTestCaseMixin
  19. # Import 3rd-party libs
  20. from salt.ext.six.moves import range # pylint: disable=import-error,redefined-builtin
  21. try:
  22. import boto
  23. import boto3
  24. from botocore.exceptions import ClientError
  25. HAS_BOTO = True
  26. except ImportError:
  27. HAS_BOTO = False
  28. # pylint: enable=import-error,no-name-in-module,unused-import
  29. # the boto_elasticsearch_domain module relies on the connect_to_region() method
  30. # which was added in boto 2.8.0
  31. # https://github.com/boto/boto/commit/33ac26b416fbb48a60602542b4ce15dcc7029f12
  32. required_boto3_version = '1.2.1'
  33. log = logging.getLogger(__name__)
  34. def _has_required_boto():
  35. '''
  36. Returns True/False boolean depending on if Boto is installed and correct
  37. version.
  38. '''
  39. if not HAS_BOTO:
  40. return False
  41. elif LooseVersion(boto3.__version__) < LooseVersion(required_boto3_version):
  42. return False
  43. else:
  44. return True
  45. if _has_required_boto():
  46. region = 'us-east-1'
  47. access_key = 'GKTADJGHEIQSXMKKRBJ08H'
  48. secret_key = 'askdjghsdfjkghWupUjasdflkdfklgjsdfjajkghs'
  49. conn_parameters = {'region': region, 'key': access_key, 'keyid': secret_key, 'profile': {}}
  50. error_message = 'An error occurred (101) when calling the {0} operation: Test-defined error'
  51. not_found_error = ClientError({
  52. 'Error': {
  53. 'Code': 'ResourceNotFoundException',
  54. 'Message': "Test-defined error"
  55. }
  56. }, 'msg')
  57. error_content = {
  58. 'Error': {
  59. 'Code': 101,
  60. 'Message': "Test-defined error"
  61. }
  62. }
  63. domain_ret = dict(DomainName='testdomain',
  64. ElasticsearchClusterConfig={},
  65. EBSOptions={},
  66. AccessPolicies={},
  67. SnapshotOptions={},
  68. AdvancedOptions={},
  69. ElasticsearchVersion='1.5',
  70. )
  71. class BotoElasticsearchDomainStateTestCaseBase(TestCase, LoaderModuleMockMixin):
  72. conn = None
  73. def setup_loader_modules(self):
  74. ctx = {}
  75. utils = salt.loader.utils(
  76. self.opts,
  77. whitelist=['boto3', 'args', 'systemd', 'path', 'platform'],
  78. context=ctx)
  79. serializers = salt.loader.serializers(self.opts)
  80. self.funcs = funcs = salt.loader.minion_mods(self.opts, context=ctx, utils=utils, whitelist=['boto_elasticsearch_domain'])
  81. self.salt_states = salt.loader.states(opts=self.opts, functions=funcs, utils=utils, whitelist=['boto_elasticsearch_domain'],
  82. serializers=serializers)
  83. return {
  84. boto_elasticsearch_domain: {
  85. '__opts__': self.opts,
  86. '__salt__': funcs,
  87. '__utils__': utils,
  88. '__states__': self.salt_states,
  89. '__serializers__': serializers,
  90. }
  91. }
  92. @classmethod
  93. def setUpClass(cls):
  94. cls.opts = salt.config.DEFAULT_MINION_OPTS.copy()
  95. cls.opts['grains'] = salt.loader.grains(cls.opts)
  96. @classmethod
  97. def tearDownClass(cls):
  98. del cls.opts
  99. # Set up MagicMock to replace the boto3 session
  100. def setUp(self):
  101. self.addCleanup(delattr, self, 'funcs')
  102. self.addCleanup(delattr, self, 'salt_states')
  103. # Set up MagicMock to replace the boto3 session
  104. # connections keep getting cached from prior tests, can't find the
  105. # correct context object to clear it. So randomize the cache key, to prevent any
  106. # cache hits
  107. conn_parameters['key'] = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(50))
  108. self.patcher = patch('boto3.session.Session')
  109. self.addCleanup(self.patcher.stop)
  110. self.addCleanup(delattr, self, 'patcher')
  111. mock_session = self.patcher.start()
  112. session_instance = mock_session.return_value
  113. self.conn = MagicMock()
  114. self.addCleanup(delattr, self, 'conn')
  115. session_instance.client.return_value = self.conn
  116. @skipIf(HAS_BOTO is False, 'The boto module must be installed.')
  117. @skipIf(_has_required_boto() is False, 'The boto3 module must be greater than'
  118. ' or equal to version {0}'
  119. .format(required_boto3_version))
  120. class BotoElasticsearchDomainTestCase(BotoElasticsearchDomainStateTestCaseBase, BotoElasticsearchDomainTestCaseMixin):
  121. '''
  122. TestCase for salt.modules.boto_elasticsearch_domain state.module
  123. '''
  124. def test_present_when_domain_does_not_exist(self):
  125. '''
  126. Tests present on a domain that does not exist.
  127. '''
  128. self.conn.describe_elasticsearch_domain.side_effect = not_found_error
  129. self.conn.describe_elasticsearch_domain_config.return_value = {'DomainConfig': domain_ret}
  130. self.conn.create_elasticsearch_domain.return_value = {'DomainStatus': domain_ret}
  131. result = self.salt_states['boto_elasticsearch_domain.present'](
  132. 'domain present',
  133. **domain_ret)
  134. self.assertTrue(result['result'])
  135. self.assertEqual(result['changes']['new']['domain']['ElasticsearchClusterConfig'], None)
  136. def test_present_when_domain_exists(self):
  137. self.conn.describe_elasticsearch_domain.return_value = {'DomainStatus': domain_ret}
  138. cfg = {}
  139. for k, v in six.iteritems(domain_ret):
  140. cfg[k] = {'Options': v}
  141. cfg['AccessPolicies'] = {'Options': '{"a": "b"}'}
  142. self.conn.describe_elasticsearch_domain_config.return_value = {'DomainConfig': cfg}
  143. self.conn.update_elasticsearch_domain_config.return_value = {'DomainConfig': cfg}
  144. result = self.salt_states['boto_elasticsearch_domain.present'](
  145. 'domain present',
  146. **domain_ret)
  147. self.assertTrue(result['result'])
  148. self.assertEqual(result['changes'], {'new': {'AccessPolicies': {}}, 'old': {'AccessPolicies': {'a': 'b'}}})
  149. def test_present_with_failure(self):
  150. self.conn.describe_elasticsearch_domain.side_effect = not_found_error
  151. self.conn.describe_elasticsearch_domain_config.return_value = {'DomainConfig': domain_ret}
  152. self.conn.create_elasticsearch_domain.side_effect = ClientError(error_content, 'create_domain')
  153. result = self.salt_states['boto_elasticsearch_domain.present'](
  154. 'domain present',
  155. **domain_ret)
  156. self.assertFalse(result['result'])
  157. self.assertTrue('An error occurred' in result['comment'])
  158. def test_absent_when_domain_does_not_exist(self):
  159. '''
  160. Tests absent on a domain that does not exist.
  161. '''
  162. self.conn.describe_elasticsearch_domain.side_effect = not_found_error
  163. result = self.salt_states['boto_elasticsearch_domain.absent']('test', 'mydomain')
  164. self.assertTrue(result['result'])
  165. self.assertEqual(result['changes'], {})
  166. def test_absent_when_domain_exists(self):
  167. self.conn.describe_elasticsearch_domain.return_value = {'DomainStatus': domain_ret}
  168. self.conn.describe_elasticsearch_domain_config.return_value = {'DomainConfig': domain_ret}
  169. result = self.salt_states['boto_elasticsearch_domain.absent']('test', domain_ret['DomainName'])
  170. self.assertTrue(result['result'])
  171. self.assertEqual(result['changes']['new']['domain'], None)
  172. def test_absent_with_failure(self):
  173. self.conn.describe_elasticsearch_domain.return_value = {'DomainStatus': domain_ret}
  174. self.conn.describe_elasticsearch_domain_config.return_value = {'DomainConfig': domain_ret}
  175. self.conn.delete_elasticsearch_domain.side_effect = ClientError(error_content, 'delete_domain')
  176. result = self.salt_states['boto_elasticsearch_domain.absent']('test', domain_ret['DomainName'])
  177. self.assertFalse(result['result'])
  178. self.assertTrue('An error occurred' in result['comment'])