1
0

test_boto_elb.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. # -*- coding: utf-8 -*-
  2. # import Python Libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import logging
  5. from copy import deepcopy
  6. import pkg_resources
  7. import os.path
  8. import sys
  9. # Import test support libs
  10. from tests.support.runtime import RUNTIME_VARS
  11. # import Python Third Party Libs
  12. # pylint: disable=import-error
  13. try:
  14. import boto
  15. boto.ENDPOINTS_PATH = os.path.join(RUNTIME_VARS.TESTS_DIR, 'unit/files/endpoints.json')
  16. import boto.ec2.elb
  17. HAS_BOTO = True
  18. except ImportError:
  19. HAS_BOTO = False
  20. try:
  21. from moto import mock_ec2_deprecated, mock_elb_deprecated
  22. HAS_MOTO = True
  23. except ImportError:
  24. HAS_MOTO = False
  25. def mock_ec2_deprecated(self):
  26. '''
  27. if the mock_ec2_deprecated function is not available due to import failure
  28. this replaces the decorated function with stub_function.
  29. Allows boto_elb unit tests to use the @mock_ec2_deprecated decorator
  30. without a "NameError: name 'mock_ec2_deprecated' is not defined" error.
  31. '''
  32. def stub_function(self):
  33. pass
  34. return stub_function
  35. def mock_elb_deprecated(self):
  36. '''
  37. if the mock_elb_deprecated function is not available due to import failure
  38. this replaces the decorated function with stub_function.
  39. Allows boto_elb unit tests to use the @mock_elb_deprecated decorator
  40. without a "NameError: name 'mock_elb_deprecated' is not defined" error.
  41. '''
  42. def stub_function(self):
  43. pass
  44. return stub_function
  45. # pylint: enable=import-error
  46. # Import Salt Libs
  47. import salt.config
  48. from salt.ext import six
  49. import salt.loader
  50. import salt.modules.boto_elb as boto_elb
  51. import salt.utils.versions
  52. # Import Salt Testing Libs
  53. from tests.support.mixins import LoaderModuleMockMixin
  54. from tests.support.unit import skipIf, TestCase
  55. log = logging.getLogger(__name__)
  56. region = 'us-east-1'
  57. access_key = 'GKTADJGHEIQSXMKKRBJ08H'
  58. secret_key = 'askdjghsdfjkghWupUjasdflkdfklgjsdfjajkghs'
  59. conn_parameters = {'region': region, 'key': access_key, 'keyid': secret_key,
  60. 'profile': {}}
  61. boto_conn_parameters = {'aws_access_key_id': access_key,
  62. 'aws_secret_access_key': secret_key}
  63. instance_parameters = {'instance_type': 't1.micro'}
  64. required_moto = '0.3.7'
  65. required_moto_py3 = '1.0.1'
  66. def _has_required_moto():
  67. '''
  68. Returns True or False depending on if ``moto`` is installed and at the correct version,
  69. depending on what version of Python is running these tests.
  70. '''
  71. if not HAS_MOTO:
  72. return False
  73. else:
  74. moto_version = salt.utils.versions.LooseVersion(pkg_resources.get_distribution('moto').version)
  75. if moto_version < salt.utils.versions.LooseVersion(required_moto):
  76. return False
  77. elif six.PY3 and moto_version < salt.utils.versions.LooseVersion(required_moto_py3):
  78. return False
  79. return True
  80. @skipIf(HAS_BOTO is False, 'The boto module must be installed.')
  81. @skipIf(HAS_MOTO is False, 'The moto module must be installed.')
  82. @skipIf(_has_required_moto() is False, 'The moto module must be >= to {0} for '
  83. 'PY2 or {1} for PY3.'.format(required_moto, required_moto_py3))
  84. class BotoElbTestCase(TestCase, LoaderModuleMockMixin):
  85. '''
  86. TestCase for salt.modules.boto_elb module
  87. '''
  88. def setup_loader_modules(self):
  89. opts = salt.config.DEFAULT_MASTER_OPTS
  90. utils = salt.loader.utils(
  91. opts,
  92. whitelist=['boto', 'args', 'systemd', 'path', 'platform'])
  93. funcs = salt.loader.minion_mods(opts, utils=utils)
  94. return {
  95. boto_elb: {
  96. '__opts__': opts,
  97. '__utils__': utils,
  98. '__salt__': funcs
  99. }
  100. }
  101. def setUp(self):
  102. TestCase.setUp(self)
  103. # __virtual__ must be caller in order for _get_conn to be injected
  104. boto_elb.__virtual__()
  105. @mock_ec2_deprecated
  106. @mock_elb_deprecated
  107. @skipIf(sys.version_info > (3, 6), 'Disabled for 3.7+ pending https://github.com/spulec/moto/issues/1706.')
  108. def test_register_instances_valid_id_result_true(self):
  109. '''
  110. tests that given a valid instance id and valid ELB that
  111. register_instances returns True.
  112. '''
  113. conn_ec2 = boto.ec2.connect_to_region(region, **boto_conn_parameters)
  114. conn_elb = boto.ec2.elb.connect_to_region(region,
  115. **boto_conn_parameters)
  116. zones = [zone.name for zone in conn_ec2.get_all_zones()]
  117. elb_name = 'TestRegisterInstancesValidIdResult'
  118. conn_elb.create_load_balancer(elb_name, zones, [(80, 80, 'http')])
  119. reservations = conn_ec2.run_instances('ami-08389d60')
  120. register_result = boto_elb.register_instances(elb_name,
  121. reservations.instances[0].id,
  122. **conn_parameters)
  123. self.assertEqual(True, register_result)
  124. @mock_ec2_deprecated
  125. @mock_elb_deprecated
  126. @skipIf(sys.version_info > (3, 6), 'Disabled for 3.7+ pending https://github.com/spulec/moto/issues/1706.')
  127. def test_register_instances_valid_id_string(self):
  128. '''
  129. tests that given a string containing a instance id and valid ELB that
  130. register_instances adds the given instance to an ELB
  131. '''
  132. conn_ec2 = boto.ec2.connect_to_region(region, **boto_conn_parameters)
  133. conn_elb = boto.ec2.elb.connect_to_region(region,
  134. **boto_conn_parameters)
  135. zones = [zone.name for zone in conn_ec2.get_all_zones()]
  136. elb_name = 'TestRegisterInstancesValidIdResult'
  137. conn_elb.create_load_balancer(elb_name, zones, [(80, 80, 'http')])
  138. reservations = conn_ec2.run_instances('ami-08389d60')
  139. boto_elb.register_instances(elb_name, reservations.instances[0].id,
  140. **conn_parameters)
  141. load_balancer_refreshed = conn_elb.get_all_load_balancers(elb_name)[0]
  142. registered_instance_ids = [instance.id for instance in
  143. load_balancer_refreshed.instances]
  144. log.debug(load_balancer_refreshed.instances)
  145. self.assertEqual([reservations.instances[0].id], registered_instance_ids)
  146. @mock_ec2_deprecated
  147. @mock_elb_deprecated
  148. @skipIf(sys.version_info > (3, 6), 'Disabled for 3.7+ pending https://github.com/spulec/moto/issues/1706.')
  149. def test_deregister_instances_valid_id_result_true(self):
  150. '''
  151. tests that given an valid id the boto_elb deregister_instances method
  152. removes exactly one of a number of ELB registered instances
  153. '''
  154. conn_ec2 = boto.ec2.connect_to_region(region, **boto_conn_parameters)
  155. conn_elb = boto.ec2.elb.connect_to_region(region,
  156. **boto_conn_parameters)
  157. zones = [zone.name for zone in conn_ec2.get_all_zones()]
  158. elb_name = 'TestDeregisterInstancesValidIdResult'
  159. load_balancer = conn_elb.create_load_balancer(elb_name, zones,
  160. [(80, 80, 'http')])
  161. reservations = conn_ec2.run_instances('ami-08389d60')
  162. load_balancer.register_instances(reservations.instances[0].id)
  163. deregister_result = boto_elb.deregister_instances(elb_name,
  164. reservations.instances[0].id,
  165. **conn_parameters)
  166. self.assertEqual(True, deregister_result)
  167. @mock_ec2_deprecated
  168. @mock_elb_deprecated
  169. @skipIf(sys.version_info > (3, 6), 'Disabled for 3.7+ pending https://github.com/spulec/moto/issues/1706.')
  170. def test_deregister_instances_valid_id_string(self):
  171. '''
  172. tests that given an valid id the boto_elb deregister_instances method
  173. removes exactly one of a number of ELB registered instances
  174. '''
  175. conn_ec2 = boto.ec2.connect_to_region(region, **boto_conn_parameters)
  176. conn_elb = boto.ec2.elb.connect_to_region(region,
  177. **boto_conn_parameters)
  178. zones = [zone.name for zone in conn_ec2.get_all_zones()]
  179. elb_name = 'TestDeregisterInstancesValidIdString'
  180. load_balancer = conn_elb.create_load_balancer(elb_name, zones,
  181. [(80, 80, 'http')])
  182. reservations = conn_ec2.run_instances('ami-08389d60', min_count=2)
  183. all_instance_ids = [instance.id for instance in reservations.instances]
  184. load_balancer.register_instances(all_instance_ids)
  185. boto_elb.deregister_instances(elb_name, reservations.instances[0].id,
  186. **conn_parameters)
  187. load_balancer_refreshed = conn_elb.get_all_load_balancers(elb_name)[0]
  188. expected_instances = deepcopy(all_instance_ids)
  189. expected_instances.remove(reservations.instances[0].id)
  190. actual_instances = [instance.id for instance in
  191. load_balancer_refreshed.instances]
  192. self.assertEqual(actual_instances, expected_instances)
  193. @mock_ec2_deprecated
  194. @mock_elb_deprecated
  195. @skipIf(sys.version_info > (3, 6), 'Disabled for 3.7+ pending https://github.com/spulec/moto/issues/1706.')
  196. def test_deregister_instances_valid_id_list(self):
  197. '''
  198. tests that given an valid ids in the form of a list that the boto_elb
  199. deregister_instances all members of the given list
  200. '''
  201. conn_ec2 = boto.ec2.connect_to_region(region, **boto_conn_parameters)
  202. conn_elb = boto.ec2.elb.connect_to_region(region,
  203. **boto_conn_parameters)
  204. zones = [zone.name for zone in conn_ec2.get_all_zones()]
  205. elb_name = 'TestDeregisterInstancesValidIdList'
  206. load_balancer = conn_elb.create_load_balancer(elb_name, zones,
  207. [(80, 80, 'http')])
  208. reservations = conn_ec2.run_instances('ami-08389d60', min_count=3)
  209. all_instance_ids = [instance.id for instance in reservations.instances]
  210. load_balancer.register_instances(all_instance_ids)
  211. # reservations.instances[:-1] refers to all instances except list
  212. # instance
  213. deregister_instances = [instance.id for instance in
  214. reservations.instances[:-1]]
  215. expected_instances = [reservations.instances[-1].id]
  216. boto_elb.deregister_instances(elb_name, deregister_instances,
  217. **conn_parameters)
  218. load_balancer_refreshed = conn_elb.get_all_load_balancers(elb_name)[0]
  219. actual_instances = [instance.id for instance in
  220. load_balancer_refreshed.instances]
  221. self.assertEqual(actual_instances, expected_instances)