def test_fetch_stack_resources(self, _, __): m_cf = mock.Mock() region = AWSRegionObject("us-west-2", m_cf) region.s3bucket = mock.Mock() region.client = mock_client_method m_template = mock.Mock() stack = Stack.create(region, "stack_name", m_template) stack._timer.cancel() stack.client = mock.Mock() class Paging: @staticmethod def paginate(**kwargs): return [{ "StackResourceSummaries": [{ "StackId": "arn:aws:cloudformation:us-east-1:" "123456789012:stack/" "SampleStack/e722ae60-fe62-11e8-9a0e-0ae8cc519968" }] }] stack.client.get_paginator.return_value = Paging() stack._fetch_stack_resources() stack.client.get_paginator.assert_called_once() self.assertEqual(len(stack._resources), 1)
def test_descentants(self, m_evnts, __): m_cf = mock.Mock() region = AWSRegionObject("us-west-2", m_cf) region.s3bucket = mock.Mock() region.client = mock_client_method test_proj = (Path(__file__).parent / "./data/nested-fail").resolve() c = Config( project_config_path=test_proj / "ci" / "taskcat.yml", project_root=test_proj, create_clients=False, ) stack = Stack.create(region, "stack_name", c.tests["taskcat-json"].template) stack._timer.cancel() child = event_template.copy() grandchild = event_template.copy() child["PhysicalResourceId"] = ( "arn:aws:cloudformation:us-east-1:123456789012:" "stack/Child/e722ae60-fe62-11e8-9a0e-0ae8cc519969") child["ResourceProperties"] = ( '{"TemplateURL": "https://test.s3.amazonaws.com/templates/' 'test.template_inner.yaml"}') grandchild["PhysicalResourceId"] = ( "arn:aws:cloudformation:us-east-1:123456789012:stack/GrandChild/" "e722ae60-fe62-11e8-9a0e-0ae8cc519970") grandchild["ResourceProperties"] = ( '{"TemplateURL": "https://test.s3.amazonaws.com/templates/' 'test.template_middle.yaml"}') m_evnts.return_value = Events([Event(child), Event(grandchild)]) desc = stack.descendants() self.assertEqual(len(desc), 2)
def test_create(self, m_s3_url_maker): m_cf = mock.Mock() region = AWSRegionObject("us-west-2", m_cf) region.s3bucket = mock.Mock() region.client = mock_client_method m_template = mock.Mock() stack = Stack.create(region, "stack_name", m_template) self.assertIsInstance(stack._timer, Timer) self.assertEqual(stack._timer.is_alive(), True) stack._timer.cancel() m_s3_url_maker.assert_called_once()
def test_delete(self, _, __): m_cf = mock.Mock() region = AWSRegionObject("us-west-2", m_cf) region.s3bucket = mock.Mock() region.client = mock_client_method m_template = mock.Mock() stack = Stack.create(region, "stack_name", m_template) stack._timer.cancel() stack.client = mock.Mock() stack.refresh.reset_mock() stack.delete() stack.client.delete_stack.assert_called_once() stack.refresh.assert_called_once()
def test_idempotent_properties(self, _): m_cf = mock.Mock() region = AWSRegionObject("us-west-2", m_cf) region.s3bucket = mock.Mock() region.client = mock_client_method m_template = mock.Mock() stack = Stack.create(region, "stack_name", m_template) stack._timer.cancel() # re-invoke timer function manually to check for idempotence stack.set_stack_properties() stack._timer.cancel() self.assertEqual(len(stack.outputs), 1) self.assertEqual(len(stack.parameters), 1) self.assertEqual(len(stack.tags), 1)
def test_import_existing(self, _): m_cf = mock.Mock() region = AWSRegionObject("us-west-2", m_cf) region.s3bucket = mock.Mock() region.client = mock_client_method m_template = mock.Mock() stack = Stack.import_existing( { "StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/" "SampleStack/e722ae60-fe62-11e8-9a0e-0ae8cc519968" }, m_template, region, "test_test", mock.Mock(), ) stack._timer.cancel() self.assertEqual(stack.name, "SampleStack")
def test_resources(self, m_res, _): m_cf = mock.Mock() region = AWSRegionObject("us-west-2", m_cf) region.s3bucket = mock.Mock() region.client = mock_client_method m_template = mock.Mock() stack = Stack.create(region, "stack_name", m_template) stack._timer.cancel() stack._resources = Resources( [Resource("test_stack_id", resource_template)]) stack.resources() m_res.assert_called_once() stack._last_resource_refresh = datetime.now() stack.resources() m_res.assert_called_once() m_res.reset_mock() stack.resources(refresh=True) m_res.assert_called_once()
def test_events(self, m_eve, _): m_cf = mock.Mock() region = AWSRegionObject("us-west-2", m_cf) region.s3bucket = mock.Mock() region.client = mock_client_method m_template = mock.Mock() stack = Stack.create(region, "stack_name", m_template) stack._timer.cancel() generic_evnt = event_template.copy() not_generic_evnt = event_template.copy() generic_evnt["ResourceStatusReason"] = "Resource creation cancelled" generic_evnt["LogicalResourceId"] = "generic" not_generic_evnt["LogicalResourceId"] = "not-generic" stack._events = Events([Event(generic_evnt), Event(not_generic_evnt)]) actual = stack.events() m_eve.assert_called_once() self.assertEqual(len(actual), 2) stack._last_event_refresh = datetime.now() actual = stack.events(include_generic=False) m_eve.assert_called_once() self.assertEqual(len(actual), 1)
def test_refresh(self, m_kids, m_res, m_eve, m_prop, _): m_cf = mock.Mock() region = AWSRegionObject("us-west-2", m_cf) region.s3bucket = mock.Mock() region.client = mock_client_method m_template = mock.Mock() stack = Stack.create(region, "stack_name", m_template) stack._timer.cancel() m_prop.reset_mock() stack.refresh() m_prop.assert_called_once() m_res.assert_not_called() m_eve.assert_not_called() m_kids.assert_not_called() m_prop.reset_mock() stack.refresh(properties=False, events=True) m_eve.assert_called_once() m_res.assert_not_called() m_prop.assert_not_called() m_kids.assert_not_called() m_eve.reset_mock() stack.refresh(properties=False, resources=True) m_res.assert_called_once() m_eve.assert_not_called() m_prop.assert_not_called() m_kids.assert_not_called() m_res.reset_mock() stack.refresh(properties=False, children=True) m_kids.assert_called_once() m_res.assert_not_called() m_prop.assert_not_called() m_res.assert_not_called()