def setUp(self): region = "us-east-1" self.session = get_session(region=region) self.provider = Provider(self.session, interactive=True, recreate_failed=True) self.stubber = Stubber(self.provider.cloudformation)
def setUp(self): self.context = self._get_context() self.provider = Provider(None, interactive=False, recreate_failed=False) self.build_action = build.Action(self.context, provider=self.provider) self.stack = mock.MagicMock() self.stack.name = 'vpc' self.stack.fqn = 'vpc' self.stack.locked = False self.stack_status = None plan = self.build_action._generate_plan() _, self.step = plan.list_pending()[0] self.step.stack = self.stack def patch_object(*args, **kwargs): m = mock.patch.object(*args, **kwargs) self.addCleanup(m.stop) m.start() def get_stack(name, *args, **kwargs): if name != self.stack.name or not self.stack_status: raise StackDoesNotExist(name) return {'StackName': self.stack.name, 'StackStatus': self.stack_status, 'Tags': []} patch_object(self.provider, 'get_stack', side_effect=get_stack) patch_object(self.provider, 'update_stack') patch_object(self.provider, 'create_stack') patch_object(self.provider, 'destroy_stack') patch_object(self.build_action, "s3_stack_push")
def _make_provider(self): provider = Provider(self.session, interactive=False, recreate_failed=False) self._patch_object(provider, 'get_stack', side_effect=self._get_stack) self._patch_object(provider, 'update_stack') self._patch_object(provider, 'create_stack') self._patch_object(provider, 'destroy_stack') return provider
def test_ensure_cfn_bucket_exists(self): provider = Provider("us-east-1") action = BaseAction(context=mock_context("mynamespace"), provider=provider) stubber = Stubber(action.s3_conn) stubber.add_response("head_bucket", service_response={}, expected_params={ "Bucket": ANY, }) with stubber: action.ensure_cfn_bucket()
def setUp(self): self.context = self._get_context() self.session = get_session(region=None) self.provider = Provider(self.session, interactive=False, recreate_failed=False) provider_builder = MockProviderBuilder(self.provider) self.build_action = build.Action(self.context, provider_builder=provider_builder, cancel=MockThreadingEvent()) self.stack = mock.MagicMock() self.stack.region = None self.stack.name = 'vpc' self.stack.fqn = 'vpc' self.stack.blueprint.rendered = '{}' self.stack.locked = False self.stack_status = None plan = self.build_action._generate_plan() self.step = plan.steps[0] self.step.stack = self.stack def patch_object(*args, **kwargs): m = mock.patch.object(*args, **kwargs) self.addCleanup(m.stop) m.start() def get_stack(name, *args, **kwargs): if name != self.stack.name or not self.stack_status: raise StackDoesNotExist(name) return { 'StackName': self.stack.name, 'StackStatus': self.stack_status, 'Outputs': [], 'Tags': [] } def get_events(name, *args, **kwargs): return [{ 'ResourceStatus': 'ROLLBACK_IN_PROGRESS', 'ResourceStatusReason': 'CFN fail' }] patch_object(self.provider, 'get_stack', side_effect=get_stack) patch_object(self.provider, 'update_stack') patch_object(self.provider, 'create_stack') patch_object(self.provider, 'destroy_stack') patch_object(self.provider, 'get_events', side_effect=get_events) patch_object(self.build_action, "s3_stack_push")
def test_ensure_cfn_forbidden(self): provider = Provider("us-west-1") action = BaseAction(context=mock_context("mynamespace"), provider=provider) stubber = Stubber(action.s3_conn) stubber.add_client_error( "head_bucket", service_error_code="AccessDenied", service_message="Forbidden", http_status_code=403, ) with stubber: with self.assertRaises(botocore.exceptions.ClientError): action.ensure_cfn_bucket()
def test_stack_template_url(self): test_cases = ( ("us-east-1", "s3.amazonaws.com"), ("us-west-1", "s3-us-west-1.amazonaws.com"), ("eu-west-1", "s3-eu-west-1.amazonaws.com"), ("sa-east-1", "s3-sa-east-1.amazonaws.com"), ) context = mock_context("mynamespace") blueprint = TestBlueprint(name="myblueprint", context=context) for region, endpoint in test_cases: provider = Provider(region) action = BaseAction(context=context, provider=provider) self.assertEqual( action.stack_template_url(blueprint), "https://%s/%s/%s-%s.json" % (endpoint, "stacker-mynamespace", "myblueprint", MOCK_VERSION))
def test_ensure_cfn_bucket_doesnt_exist_us_east(self): provider = Provider("us-east-1") action = BaseAction(context=mock_context("mynamespace"), provider=provider) stubber = Stubber(action.s3_conn) stubber.add_client_error( "head_bucket", service_error_code="NoSuchBucket", service_message="Not Found", http_status_code=404, ) stubber.add_response("create_bucket", service_response={}, expected_params={ "Bucket": ANY, }) with stubber: action.ensure_cfn_bucket()
def test_stack_template_url(self): context = mock_context("mynamespace") blueprint = TestBlueprint(name="myblueprint", context=context) region = "us-east-1" endpoint = "https://example.com" session = get_session(region) provider = Provider(session) action = BaseAction(context=context, provider_builder=MockProviderBuilder( provider, region=region)) with mock.patch('stacker.actions.base.get_s3_endpoint', autospec=True, return_value=endpoint): self.assertEqual( action.stack_template_url(blueprint), "%s/%s/stack_templates/%s/%s-%s.json" % (endpoint, "stacker-mynamespace", "mynamespace-myblueprint", "myblueprint", MOCK_VERSION))
def test_ensure_cfn_bucket_doesnt_exist_us_west(self): session = get_session("us-west-1") provider = Provider(session) action = BaseAction(context=mock_context("mynamespace"), provider_builder=MockProviderBuilder( provider, region="us-west-1")) stubber = Stubber(action.s3_conn) stubber.add_client_error( "head_bucket", service_error_code="NoSuchBucket", service_message="Not Found", http_status_code=404, ) stubber.add_response("create_bucket", service_response={}, expected_params={ "Bucket": ANY, "CreateBucketConfiguration": { "LocationConstraint": "us-west-1", } }) with stubber: action.ensure_cfn_bucket()
def test_successful_init(self): replacements = True p = Provider(self.session, interactive=True, replacements_only=replacements) self.assertEqual(p.replacements_only, replacements)