|
| 1 | +from unittest import TestCase |
| 2 | + |
| 3 | +import botocore.session |
| 4 | + |
| 5 | +from botocore.exceptions import ClientError |
| 6 | +from botocore.stub import Stubber |
| 7 | + |
| 8 | +from samcli.commands.exceptions import UserException |
| 9 | +from samcli.lib.bootstrap.bootstrap import _create_or_get_stack, _get_stack_template, SAM_CLI_STACK_NAME |
| 10 | + |
| 11 | + |
| 12 | +class TestBootstrapManagedStack(TestCase): |
| 13 | + def _stubbed_cf_client(self): |
| 14 | + cf = botocore.session.get_session().create_client("cloudformation") |
| 15 | + return [cf, Stubber(cf)] |
| 16 | + |
| 17 | + def test_new_stack(self): |
| 18 | + stub_cf, stubber = self._stubbed_cf_client() |
| 19 | + # first describe_stacks call will fail |
| 20 | + ds_params = {"StackName": SAM_CLI_STACK_NAME} |
| 21 | + stubber.add_client_error("describe_stacks", service_error_code="ClientError", expected_params=ds_params) |
| 22 | + # creating change set |
| 23 | + ccs_params = { |
| 24 | + "StackName": SAM_CLI_STACK_NAME, |
| 25 | + "TemplateBody": _get_stack_template(), |
| 26 | + "Tags": [{"Key": "ManagedStackSource", "Value": "AwsSamCli"}], |
| 27 | + "ChangeSetType": "CREATE", |
| 28 | + "ChangeSetName": "InitialCreation", |
| 29 | + } |
| 30 | + ccs_resp = {"Id": "id", "StackId": "aws-sam-cli-managed-stack"} |
| 31 | + stubber.add_response("create_change_set", ccs_resp, ccs_params) |
| 32 | + # describe change set creation status for waiter |
| 33 | + dcs_params = {"ChangeSetName": "InitialCreation", "StackName": SAM_CLI_STACK_NAME} |
| 34 | + dcs_resp = {"Status": "CREATE_COMPLETE"} |
| 35 | + stubber.add_response("describe_change_set", dcs_resp, dcs_params) |
| 36 | + # executing change set |
| 37 | + ecs_params = {"ChangeSetName": "InitialCreation", "StackName": SAM_CLI_STACK_NAME} |
| 38 | + ecs_resp = {} |
| 39 | + stubber.add_response("execute_change_set", ecs_resp, ecs_params) |
| 40 | + # two describe_stacks calls will succeed - one for waiter, one direct |
| 41 | + post_create_ds_resp = { |
| 42 | + "Stacks": [ |
| 43 | + { |
| 44 | + "StackName": SAM_CLI_STACK_NAME, |
| 45 | + "CreationTime": "2019-11-13", |
| 46 | + "StackStatus": "CREATE_COMPLETE", |
| 47 | + "Tags": [{"Key": "ManagedStackSource", "Value": "AwsSamCli"}], |
| 48 | + "Outputs": [{"OutputKey": "SourceBucket", "OutputValue": "generated-src-bucket"}], |
| 49 | + } |
| 50 | + ] |
| 51 | + } |
| 52 | + stubber.add_response("describe_stacks", post_create_ds_resp, ds_params) |
| 53 | + stubber.add_response("describe_stacks", post_create_ds_resp, ds_params) |
| 54 | + stubber.activate() |
| 55 | + _create_or_get_stack(stub_cf) |
| 56 | + stubber.assert_no_pending_responses() |
| 57 | + stubber.deactivate() |
| 58 | + |
| 59 | + def test_stack_exists(self): |
| 60 | + stub_cf, stubber = self._stubbed_cf_client() |
| 61 | + ds_resp = { |
| 62 | + "Stacks": [ |
| 63 | + { |
| 64 | + "StackName": SAM_CLI_STACK_NAME, |
| 65 | + "CreationTime": "2019-11-13", |
| 66 | + "StackStatus": "CREATE_COMPLETE", |
| 67 | + "Tags": [{"Key": "ManagedStackSource", "Value": "AwsSamCli"}], |
| 68 | + "Outputs": [{"OutputKey": "SourceBucket", "OutputValue": "generated-src-bucket"}], |
| 69 | + } |
| 70 | + ] |
| 71 | + } |
| 72 | + ds_params = {"StackName": SAM_CLI_STACK_NAME} |
| 73 | + stubber.add_response("describe_stacks", ds_resp, ds_params) |
| 74 | + stubber.activate() |
| 75 | + _create_or_get_stack(stub_cf) |
| 76 | + stubber.assert_no_pending_responses() |
| 77 | + stubber.deactivate() |
| 78 | + |
| 79 | + def test_stack_missing_bucket(self): |
| 80 | + stub_cf, stubber = self._stubbed_cf_client() |
| 81 | + ds_resp = { |
| 82 | + "Stacks": [ |
| 83 | + { |
| 84 | + "StackName": SAM_CLI_STACK_NAME, |
| 85 | + "CreationTime": "2019-11-13", |
| 86 | + "StackStatus": "CREATE_COMPLETE", |
| 87 | + "Tags": [{"Key": "ManagedStackSource", "Value": "AwsSamCli"}], |
| 88 | + "Outputs": [], |
| 89 | + } |
| 90 | + ] |
| 91 | + } |
| 92 | + ds_params = {"StackName": SAM_CLI_STACK_NAME} |
| 93 | + stubber.add_response("describe_stacks", ds_resp, ds_params) |
| 94 | + stubber.activate() |
| 95 | + with self.assertRaises(UserException): |
| 96 | + _create_or_get_stack(stub_cf) |
| 97 | + stubber.assert_no_pending_responses() |
| 98 | + stubber.deactivate() |
| 99 | + |
| 100 | + def test_stack_missing_tag(self): |
| 101 | + stub_cf, stubber = self._stubbed_cf_client() |
| 102 | + ds_resp = { |
| 103 | + "Stacks": [ |
| 104 | + { |
| 105 | + "StackName": SAM_CLI_STACK_NAME, |
| 106 | + "CreationTime": "2019-11-13", |
| 107 | + "StackStatus": "CREATE_COMPLETE", |
| 108 | + "Tags": [], |
| 109 | + "Outputs": [{"OutputKey": "SourceBucket", "OutputValue": "generated-src-bucket"}], |
| 110 | + } |
| 111 | + ] |
| 112 | + } |
| 113 | + ds_params = {"StackName": SAM_CLI_STACK_NAME} |
| 114 | + stubber.add_response("describe_stacks", ds_resp, ds_params) |
| 115 | + stubber.activate() |
| 116 | + with self.assertRaises(UserException): |
| 117 | + _create_or_get_stack(stub_cf) |
| 118 | + stubber.assert_no_pending_responses() |
| 119 | + stubber.deactivate() |
| 120 | + |
| 121 | + def test_stack_wrong_tag(self): |
| 122 | + stub_cf, stubber = self._stubbed_cf_client() |
| 123 | + ds_resp = { |
| 124 | + "Stacks": [ |
| 125 | + { |
| 126 | + "StackName": SAM_CLI_STACK_NAME, |
| 127 | + "CreationTime": "2019-11-13", |
| 128 | + "StackStatus": "CREATE_COMPLETE", |
| 129 | + "Tags": [{"Key": "ManagedStackSource", "Value": "WHY WOULD YOU EVEN DO THIS"}], |
| 130 | + "Outputs": [{"OutputKey": "SourceBucket", "OutputValue": "generated-src-bucket"}], |
| 131 | + } |
| 132 | + ] |
| 133 | + } |
| 134 | + ds_params = {"StackName": SAM_CLI_STACK_NAME} |
| 135 | + stubber.add_response("describe_stacks", ds_resp, ds_params) |
| 136 | + stubber.activate() |
| 137 | + with self.assertRaises(UserException): |
| 138 | + _create_or_get_stack(stub_cf) |
| 139 | + stubber.assert_no_pending_responses() |
| 140 | + stubber.deactivate() |
| 141 | + |
| 142 | + def test_change_set_creation_fails(self): |
| 143 | + stub_cf, stubber = self._stubbed_cf_client() |
| 144 | + # first describe_stacks call will fail |
| 145 | + ds_params = {"StackName": SAM_CLI_STACK_NAME} |
| 146 | + stubber.add_client_error("describe_stacks", service_error_code="ClientError", expected_params=ds_params) |
| 147 | + # creating change set - fails |
| 148 | + ccs_params = { |
| 149 | + "StackName": SAM_CLI_STACK_NAME, |
| 150 | + "TemplateBody": _get_stack_template(), |
| 151 | + "Tags": [{"Key": "ManagedStackSource", "Value": "AwsSamCli"}], |
| 152 | + "ChangeSetType": "CREATE", |
| 153 | + "ChangeSetName": "InitialCreation", |
| 154 | + } |
| 155 | + stubber.add_client_error("create_change_set", service_error_code="ClientError", expected_params=ccs_params) |
| 156 | + stubber.activate() |
| 157 | + with self.assertRaises(ClientError): |
| 158 | + _create_or_get_stack(stub_cf) |
| 159 | + stubber.assert_no_pending_responses() |
| 160 | + stubber.deactivate() |
| 161 | + |
| 162 | + def test_change_set_execution_fails(self): |
| 163 | + stub_cf, stubber = self._stubbed_cf_client() |
| 164 | + # first describe_stacks call will fail |
| 165 | + ds_params = {"StackName": SAM_CLI_STACK_NAME} |
| 166 | + stubber.add_client_error("describe_stacks", service_error_code="ClientError", expected_params=ds_params) |
| 167 | + # creating change set |
| 168 | + ccs_params = { |
| 169 | + "StackName": SAM_CLI_STACK_NAME, |
| 170 | + "TemplateBody": _get_stack_template(), |
| 171 | + "Tags": [{"Key": "ManagedStackSource", "Value": "AwsSamCli"}], |
| 172 | + "ChangeSetType": "CREATE", |
| 173 | + "ChangeSetName": "InitialCreation", |
| 174 | + } |
| 175 | + ccs_resp = {"Id": "id", "StackId": "aws-sam-cli-managed-stack"} |
| 176 | + stubber.add_response("create_change_set", ccs_resp, ccs_params) |
| 177 | + # describe change set creation status for waiter |
| 178 | + dcs_params = {"ChangeSetName": "InitialCreation", "StackName": SAM_CLI_STACK_NAME} |
| 179 | + dcs_resp = {"Status": "CREATE_COMPLETE"} |
| 180 | + stubber.add_response("describe_change_set", dcs_resp, dcs_params) |
| 181 | + # executing change set - fails |
| 182 | + ecs_params = {"ChangeSetName": "InitialCreation", "StackName": SAM_CLI_STACK_NAME} |
| 183 | + stubber.add_client_error( |
| 184 | + "execute_change_set", service_error_code="InsufficientCapabilities", expected_params=ecs_params |
| 185 | + ) |
| 186 | + stubber.activate() |
| 187 | + with self.assertRaises(ClientError): |
| 188 | + _create_or_get_stack(stub_cf) |
| 189 | + stubber.assert_no_pending_responses() |
| 190 | + stubber.deactivate() |
0 commit comments