test_boto_vpc.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import random
  5. import string
  6. import os.path
  7. import sys
  8. # Import Salt Testing libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import skipIf, TestCase
  11. from tests.support.mock import patch
  12. from tests.support.runtests import RUNTIME_VARS
  13. # Import Salt libs
  14. import salt.config
  15. import salt.utils.botomod as botomod
  16. from salt.ext import six
  17. from salt.utils.versions import LooseVersion
  18. import salt.states.boto_vpc as boto_vpc
  19. # pylint: disable=import-error,unused-import
  20. from tests.unit.modules.test_boto_vpc import BotoVpcTestCaseMixin
  21. # Import 3rd-party libs
  22. from salt.ext.six.moves import range # pylint: disable=import-error,redefined-builtin
  23. try:
  24. import boto
  25. boto.ENDPOINTS_PATH = os.path.join(RUNTIME_VARS.TESTS_DIR, 'unit/files/endpoints.json')
  26. import boto3
  27. from boto.exception import BotoServerError
  28. HAS_BOTO = True
  29. except ImportError:
  30. HAS_BOTO = False
  31. try:
  32. from moto import mock_ec2_deprecated
  33. HAS_MOTO = True
  34. except ImportError:
  35. HAS_MOTO = False
  36. def mock_ec2_deprecated(self):
  37. '''
  38. if the mock_ec2_deprecated function is not available due to import failure
  39. this replaces the decorated function with stub_function.
  40. Allows boto_vpc unit tests to use the @mock_ec2_deprecated decorator
  41. without a "NameError: name 'mock_ec2_deprecated' is not defined" error.
  42. '''
  43. def stub_function(self):
  44. pass
  45. return stub_function
  46. # pylint: enable=import-error,unused-import
  47. # the boto_vpc module relies on the connect_to_region() method
  48. # which was added in boto 2.8.0
  49. # https://github.com/boto/boto/commit/33ac26b416fbb48a60602542b4ce15dcc7029f12
  50. required_boto_version = '2.8.0'
  51. region = 'us-east-1'
  52. access_key = 'GKTADJGHEIQSXMKKRBJ08H'
  53. secret_key = 'askdjghsdfjkghWupUjasdflkdfklgjsdfjajkghs'
  54. conn_parameters = {'region': region, 'key': access_key, 'keyid': secret_key, 'profile': {}}
  55. cidr_block = '10.0.0.0/24'
  56. subnet_id = 'subnet-123456'
  57. dhcp_options_parameters = {'domain_name': 'example.com', 'domain_name_servers': ['1.2.3.4'], 'ntp_servers': ['5.6.7.8'],
  58. 'netbios_name_servers': ['10.0.0.1'], 'netbios_node_type': 2}
  59. network_acl_entry_parameters = ('fake', 100, -1, 'allow', cidr_block)
  60. dhcp_options_parameters.update(conn_parameters)
  61. def _has_required_boto():
  62. '''
  63. Returns True/False boolean depending on if Boto is installed and correct
  64. version.
  65. '''
  66. if not HAS_BOTO:
  67. return False
  68. elif LooseVersion(boto.__version__) < LooseVersion(required_boto_version):
  69. return False
  70. else:
  71. return True
  72. class BotoVpcStateTestCaseBase(TestCase, LoaderModuleMockMixin):
  73. def setup_loader_modules(self):
  74. ctx = {}
  75. utils = salt.loader.utils(
  76. self.opts,
  77. whitelist=['boto', 'boto3', 'args', 'systemd', 'path', 'platform', 'reg'],
  78. context=ctx)
  79. serializers = salt.loader.serializers(self.opts)
  80. self.funcs = salt.loader.minion_mods(self.opts, context=ctx, utils=utils, whitelist=['boto_vpc', 'config'])
  81. self.salt_states = salt.loader.states(opts=self.opts, functions=self.funcs, utils=utils, whitelist=['boto_vpc'],
  82. serializers=serializers)
  83. return {
  84. boto_vpc: {
  85. '__opts__': self.opts,
  86. '__salt__': self.funcs,
  87. '__utils__': utils,
  88. '__states__': self.salt_states,
  89. '__serializers__': serializers,
  90. },
  91. botomod: {}
  92. }
  93. @classmethod
  94. def setUpClass(cls):
  95. cls.opts = salt.config.DEFAULT_MINION_OPTS.copy()
  96. cls.opts['grains'] = salt.loader.grains(cls.opts)
  97. @classmethod
  98. def tearDownClass(cls):
  99. del cls.opts
  100. def setUp(self):
  101. self.addCleanup(delattr, self, 'funcs')
  102. self.addCleanup(delattr, self, 'salt_states')
  103. # connections keep getting cached from prior tests, can't find the
  104. # correct context object to clear it. So randomize the cache key, to prevent any
  105. # cache hits
  106. conn_parameters['key'] = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(50))
  107. @skipIf(HAS_BOTO is False, 'The boto module must be installed.')
  108. @skipIf(HAS_MOTO is False, 'The moto module must be installed.')
  109. @skipIf(_has_required_boto() is False, 'The boto module must be greater than'
  110. ' or equal to version {0}'
  111. .format(required_boto_version))
  112. class BotoVpcTestCase(BotoVpcStateTestCaseBase, BotoVpcTestCaseMixin):
  113. '''
  114. TestCase for salt.states.boto_vpc state.module
  115. '''
  116. @skipIf(sys.version_info > (3, 6), 'Disabled for 3.7+ pending https://github.com/spulec/moto/issues/1706.')
  117. @mock_ec2_deprecated
  118. def test_present_when_vpc_does_not_exist(self):
  119. '''
  120. Tests present on a VPC that does not exist.
  121. '''
  122. with patch.dict(botomod.__salt__, self.funcs):
  123. vpc_present_result = self.salt_states['boto_vpc.present']('test', cidr_block)
  124. self.assertTrue(vpc_present_result['result'])
  125. self.assertEqual(vpc_present_result['changes']['new']['vpc']['state'], 'available')
  126. @skipIf(sys.version_info > (3, 6), 'Disabled for 3.7+ pending https://github.com/spulec/moto/issues/1706.')
  127. @mock_ec2_deprecated
  128. def test_present_when_vpc_exists(self):
  129. vpc = self._create_vpc(name='test')
  130. vpc_present_result = self.salt_states['boto_vpc.present']('test', cidr_block)
  131. self.assertTrue(vpc_present_result['result'])
  132. self.assertEqual(vpc_present_result['changes'], {})
  133. @mock_ec2_deprecated
  134. @skipIf(True, 'Disabled pending https://github.com/spulec/moto/issues/493')
  135. def test_present_with_failure(self):
  136. with patch('moto.ec2.models.VPCBackend.create_vpc', side_effect=BotoServerError(400, 'Mocked error')):
  137. vpc_present_result = self.salt_states['boto_vpc.present']('test', cidr_block)
  138. self.assertFalse(vpc_present_result['result'])
  139. self.assertTrue('Mocked error' in vpc_present_result['comment'])
  140. @skipIf(sys.version_info > (3, 6), 'Disabled for 3.7+ pending https://github.com/spulec/moto/issues/1706.')
  141. @mock_ec2_deprecated
  142. def test_absent_when_vpc_does_not_exist(self):
  143. '''
  144. Tests absent on a VPC that does not exist.
  145. '''
  146. with patch.dict(botomod.__salt__, self.funcs):
  147. vpc_absent_result = self.salt_states['boto_vpc.absent']('test')
  148. self.assertTrue(vpc_absent_result['result'])
  149. self.assertEqual(vpc_absent_result['changes'], {})
  150. @skipIf(sys.version_info > (3, 6), 'Disabled for 3.7+ pending https://github.com/spulec/moto/issues/1706.')
  151. @mock_ec2_deprecated
  152. def test_absent_when_vpc_exists(self):
  153. vpc = self._create_vpc(name='test')
  154. with patch.dict(botomod.__salt__, self.funcs):
  155. vpc_absent_result = self.salt_states['boto_vpc.absent']('test')
  156. self.assertTrue(vpc_absent_result['result'])
  157. self.assertEqual(vpc_absent_result['changes']['new']['vpc'], None)
  158. @mock_ec2_deprecated
  159. @skipIf(True, 'Disabled pending https://github.com/spulec/moto/issues/493')
  160. def test_absent_with_failure(self):
  161. vpc = self._create_vpc(name='test')
  162. with patch('moto.ec2.models.VPCBackend.delete_vpc', side_effect=BotoServerError(400, 'Mocked error')):
  163. vpc_absent_result = self.salt_states['boto_vpc.absent']('test')
  164. self.assertFalse(vpc_absent_result['result'])
  165. self.assertTrue('Mocked error' in vpc_absent_result['comment'])
  166. class BotoVpcResourceTestCaseMixin(BotoVpcTestCaseMixin):
  167. resource_type = None
  168. backend_create = None
  169. backend_delete = None
  170. extra_kwargs = {}
  171. def _create_resource(self, vpc_id=None, name=None):
  172. _create = getattr(self, '_create_' + self.resource_type)
  173. _create(vpc_id=vpc_id, name=name, **self.extra_kwargs)
  174. @skipIf(sys.version_info > (3, 6), 'Disabled for 3.7+ pending https://github.com/spulec/moto/issues/1706.')
  175. @mock_ec2_deprecated
  176. def test_present_when_resource_does_not_exist(self):
  177. '''
  178. Tests present on a resource that does not exist.
  179. '''
  180. vpc = self._create_vpc(name='test')
  181. with patch.dict(botomod.__salt__, self.funcs):
  182. resource_present_result = self.salt_states['boto_vpc.{0}_present'.format(self.resource_type)](
  183. name='test', vpc_name='test', **self.extra_kwargs)
  184. self.assertTrue(resource_present_result['result'])
  185. exists = self.funcs['boto_vpc.resource_exists'](self.resource_type, 'test').get('exists')
  186. self.assertTrue(exists)
  187. @skipIf(sys.version_info > (3, 6), 'Disabled for 3.7+ pending https://github.com/spulec/moto/issues/1706.')
  188. @mock_ec2_deprecated
  189. def test_present_when_resource_exists(self):
  190. vpc = self._create_vpc(name='test')
  191. self._create_resource(vpc_id=vpc.id, name='test')
  192. with patch.dict(botomod.__salt__, self.funcs):
  193. resource_present_result = self.salt_states['boto_vpc.{0}_present'.format(self.resource_type)](
  194. name='test', vpc_name='test', **self.extra_kwargs)
  195. self.assertTrue(resource_present_result['result'])
  196. self.assertEqual(resource_present_result['changes'], {})
  197. @mock_ec2_deprecated
  198. @skipIf(True, 'Disabled pending https://github.com/spulec/moto/issues/493')
  199. def test_present_with_failure(self):
  200. vpc = self._create_vpc(name='test')
  201. with patch('moto.ec2.models.{0}'.format(self.backend_create), side_effect=BotoServerError(400, 'Mocked error')):
  202. resource_present_result = self.salt_states['boto_vpc.{0}_present'.format(self.resource_type)](
  203. name='test', vpc_name='test', **self.extra_kwargs)
  204. self.assertFalse(resource_present_result['result'])
  205. self.assertTrue('Mocked error' in resource_present_result['comment'])
  206. @skipIf(sys.version_info > (3, 6), 'Disabled for 3.7+ pending https://github.com/spulec/moto/issues/1706.')
  207. @mock_ec2_deprecated
  208. def test_absent_when_resource_does_not_exist(self):
  209. '''
  210. Tests absent on a resource that does not exist.
  211. '''
  212. with patch.dict(botomod.__salt__, self.funcs):
  213. resource_absent_result = self.salt_states['boto_vpc.{0}_absent'.format(self.resource_type)]('test')
  214. self.assertTrue(resource_absent_result['result'])
  215. self.assertEqual(resource_absent_result['changes'], {})
  216. @skipIf(sys.version_info > (3, 6), 'Disabled for 3.7+ pending https://github.com/spulec/moto/issues/1706.')
  217. @mock_ec2_deprecated
  218. def test_absent_when_resource_exists(self):
  219. vpc = self._create_vpc(name='test')
  220. self._create_resource(vpc_id=vpc.id, name='test')
  221. with patch.dict(botomod.__salt__, self.funcs):
  222. resource_absent_result = self.salt_states['boto_vpc.{0}_absent'.format(self.resource_type)]('test')
  223. self.assertTrue(resource_absent_result['result'])
  224. self.assertEqual(resource_absent_result['changes']['new'][self.resource_type], None)
  225. exists = self.funcs['boto_vpc.resource_exists'](self.resource_type, 'test').get('exists')
  226. self.assertFalse(exists)
  227. @mock_ec2_deprecated
  228. @skipIf(True, 'Disabled pending https://github.com/spulec/moto/issues/493')
  229. def test_absent_with_failure(self):
  230. vpc = self._create_vpc(name='test')
  231. self._create_resource(vpc_id=vpc.id, name='test')
  232. with patch('moto.ec2.models.{0}'.format(self.backend_delete), side_effect=BotoServerError(400, 'Mocked error')):
  233. resource_absent_result = self.salt_states['boto_vpc.{0}_absent'.format(self.resource_type)]('test')
  234. self.assertFalse(resource_absent_result['result'])
  235. self.assertTrue('Mocked error' in resource_absent_result['comment'])
  236. @skipIf(HAS_BOTO is False, 'The boto module must be installed.')
  237. @skipIf(HAS_MOTO is False, 'The moto module must be installed.')
  238. @skipIf(_has_required_boto() is False, 'The boto module must be greater than'
  239. ' or equal to version {0}'
  240. .format(required_boto_version))
  241. class BotoVpcSubnetsTestCase(BotoVpcStateTestCaseBase, BotoVpcResourceTestCaseMixin):
  242. resource_type = 'subnet'
  243. backend_create = 'SubnetBackend.create_subnet'
  244. backend_delete = 'SubnetBackend.delete_subnet'
  245. extra_kwargs = {'cidr_block': cidr_block}
  246. @skipIf(HAS_BOTO is False, 'The boto module must be installed.')
  247. @skipIf(HAS_MOTO is False, 'The moto module must be installed.')
  248. @skipIf(_has_required_boto() is False, 'The boto module must be greater than'
  249. ' or equal to version {0}'
  250. .format(required_boto_version))
  251. class BotoVpcInternetGatewayTestCase(BotoVpcStateTestCaseBase, BotoVpcResourceTestCaseMixin):
  252. resource_type = 'internet_gateway'
  253. backend_create = 'InternetGatewayBackend.create_internet_gateway'
  254. backend_delete = 'InternetGatewayBackend.delete_internet_gateway'
  255. @skipIf(six.PY3, 'Disabled for Python 3 due to upstream bugs: '
  256. 'https://github.com/spulec/moto/issues/548 and '
  257. 'https://github.com/gabrielfalcao/HTTPretty/issues/325')
  258. @skipIf(HAS_BOTO is False, 'The boto module must be installed.')
  259. @skipIf(HAS_MOTO is False, 'The moto module must be installed.')
  260. @skipIf(_has_required_boto() is False, 'The boto module must be greater than'
  261. ' or equal to version {0}'
  262. .format(required_boto_version))
  263. class BotoVpcRouteTableTestCase(BotoVpcStateTestCaseBase, BotoVpcResourceTestCaseMixin):
  264. resource_type = 'route_table'
  265. backend_create = 'RouteTableBackend.create_route_table'
  266. backend_delete = 'RouteTableBackend.delete_route_table'
  267. @mock_ec2_deprecated
  268. def test_present_with_subnets(self):
  269. vpc = self._create_vpc(name='test')
  270. subnet1 = self._create_subnet(vpc_id=vpc.id, cidr_block='10.0.0.0/25', name='test1')
  271. subnet2 = self._create_subnet(vpc_id=vpc.id, cidr_block='10.0.0.128/25', name='test2')
  272. route_table_present_result = self.salt_states['boto_vpc.route_table_present'](
  273. name='test', vpc_name='test', subnet_names=['test1'], subnet_ids=[subnet2.id])
  274. associations = route_table_present_result['changes']['new']['subnets_associations']
  275. assoc_subnets = [x['subnet_id'] for x in associations]
  276. self.assertEqual(set(assoc_subnets), set([subnet1.id, subnet2.id]))
  277. route_table_present_result = self.salt_states['boto_vpc.route_table_present'](
  278. name='test', vpc_name='test', subnet_ids=[subnet2.id])
  279. changes = route_table_present_result['changes']
  280. old_subnets = [x['subnet_id'] for x in changes['old']['subnets_associations']]
  281. self.assertEqual(set(assoc_subnets), set(old_subnets))
  282. new_subnets = changes['new']['subnets_associations']
  283. self.assertEqual(new_subnets[0]['subnet_id'], subnet2.id)
  284. @mock_ec2_deprecated
  285. def test_present_with_routes(self):
  286. vpc = self._create_vpc(name='test')
  287. igw = self._create_internet_gateway(name='test', vpc_id=vpc.id)
  288. with patch.dict(botomod.__salt__, self.funcs):
  289. route_table_present_result = self.salt_states['boto_vpc.route_table_present'](
  290. name='test', vpc_name='test', routes=[{'destination_cidr_block': '0.0.0.0/0',
  291. 'gateway_id': igw.id},
  292. {'destination_cidr_block': '10.0.0.0/24',
  293. 'gateway_id': 'local'}])
  294. routes = [x['gateway_id'] for x in route_table_present_result['changes']['new']['routes']]
  295. self.assertEqual(set(routes), set(['local', igw.id]))
  296. route_table_present_result = self.salt_states['boto_vpc.route_table_present'](
  297. name='test', vpc_name='test', routes=[{'destination_cidr_block': '10.0.0.0/24',
  298. 'gateway_id': 'local'}])
  299. changes = route_table_present_result['changes']
  300. old_routes = [x['gateway_id'] for x in changes['old']['routes']]
  301. self.assertEqual(set(routes), set(old_routes))
  302. self.assertEqual(changes['new']['routes'][0]['gateway_id'], 'local')