| 1 |
# (c) 2017 Red Hat Inc. |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import boto_exception |
| 9 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 10 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import RetryingBotoClientWrapper |
| 11 |
from ansible_collections.amazon.aws.plugins.modules import cloudformation as cfn_module |
| 12 |
|
| 13 |
basic_yaml_tpl = """ |
| 14 |
--- |
| 15 |
AWSTemplateFormatVersion: '2010-09-09' |
| 16 |
Description: 'Basic template that creates an S3 bucket' |
| 17 |
Resources: |
| 18 |
MyBucket: |
| 19 |
Type: "AWS::S3::Bucket" |
| 20 |
Outputs: |
| 21 |
TheName: |
| 22 |
Value: |
| 23 |
!Ref MyBucket |
| 24 |
""" |
| 25 |
|
| 26 |
bad_json_tpl = """{ |
| 27 |
"AWSTemplateFormatVersion": "2010-09-09", |
| 28 |
"Description": "Broken template, no comma here ->" |
| 29 |
"Resources": { |
| 30 |
"MyBucket": { |
| 31 |
"Type": "AWS::S3::Bucket" |
| 32 |
} |
| 33 |
} |
| 34 |
}""" |
| 35 |
|
| 36 |
failing_yaml_tpl = """ |
| 37 |
--- |
| 38 |
AWSTemplateFormatVersion: 2010-09-09 |
| 39 |
Resources: |
| 40 |
ECRRepo: |
| 41 |
Type: AWS::ECR::Repository |
| 42 |
Properties: |
| 43 |
RepositoryPolicyText: |
| 44 |
Version: 3000-10-17 # <--- invalid version |
| 45 |
Statement: |
| 46 |
- Effect: Allow |
| 47 |
Action: |
| 48 |
- 'ecr:*' |
| 49 |
Principal: |
| 50 |
AWS: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:root |
| 51 |
""" |
| 52 |
|
| 53 |
default_events_limit = 10 |
| 54 |
|
| 55 |
|
| 56 |
class FakeModule: |
| 57 |
def __init__(self, **kwargs): |
| 58 |
self.params = kwargs |
| 59 |
|
| 60 |
def fail_json(self, *args, **kwargs): |
| 61 |
self.exit_args = args |
| 62 |
self.exit_kwargs = kwargs |
| 63 |
raise Exception("FAIL") |
| 64 |
|
| 65 |
def fail_json_aws(self, *args, **kwargs): |
| 66 |
self.exit_args = args |
| 67 |
self.exit_kwargs = kwargs |
| 68 |
raise Exception("FAIL") |
| 69 |
|
| 70 |
def exit_json(self, *args, **kwargs): |
| 71 |
self.exit_args = args |
| 72 |
self.exit_kwargs = kwargs |
| 73 |
raise Exception("EXIT") |
| 74 |
|
| 75 |
|
| 76 |
def _create_wrapped_client(placeboify): |
| 77 |
connection = placeboify.client("cloudformation") |
| 78 |
retry_decorator = AWSRetry.jittered_backoff() |
| 79 |
wrapped_conn = RetryingBotoClientWrapper(connection, retry_decorator) |
| 80 |
return wrapped_conn |
| 81 |
|
| 82 |
|
| 83 |
def test_invalid_template_json(placeboify): |
| 84 |
connection = _create_wrapped_client(placeboify) |
| 85 |
params = { |
| 86 |
"StackName": "ansible-test-wrong-json", |
| 87 |
"TemplateBody": bad_json_tpl, |
| 88 |
} |
| 89 |
m = FakeModule(disable_rollback=False) |
| 90 |
with pytest.raises(Exception) as exc_info: |
| 91 |
cfn_module.create_stack(m, params, connection, default_events_limit) |
| 92 |
pytest.fail("Expected malformed JSON to have caused the call to fail") |
| 93 |
|
| 94 |
assert exc_info.match("FAIL") |
| 95 |
assert "ValidationError" in boto_exception(m.exit_args[0]) |
| 96 |
|
| 97 |
|
| 98 |
def test_client_request_token_s3_stack(maybe_sleep, placeboify): |
| 99 |
connection = _create_wrapped_client(placeboify) |
| 100 |
params = { |
| 101 |
"StackName": "ansible-test-client-request-token-yaml", |
| 102 |
"TemplateBody": basic_yaml_tpl, |
| 103 |
"ClientRequestToken": "3faf3fb5-b289-41fc-b940-44151828f6cf", |
| 104 |
} |
| 105 |
m = FakeModule(disable_rollback=False) |
| 106 |
result = cfn_module.create_stack(m, params, connection, default_events_limit) |
| 107 |
assert result["changed"] |
| 108 |
assert len(result["events"]) > 1 |
| 109 |
# require that the final recorded stack state was CREATE_COMPLETE |
| 110 |
# events are retrieved newest-first, so 0 is the latest |
| 111 |
assert "CREATE_COMPLETE" in result["events"][0] |
| 112 |
connection.delete_stack(StackName="ansible-test-client-request-token-yaml") |
| 113 |
|
| 114 |
|
| 115 |
def test_basic_s3_stack(maybe_sleep, placeboify): |
| 116 |
connection = _create_wrapped_client(placeboify) |
| 117 |
params = {"StackName": "ansible-test-basic-yaml", "TemplateBody": basic_yaml_tpl} |
| 118 |
m = FakeModule(disable_rollback=False) |
| 119 |
result = cfn_module.create_stack(m, params, connection, default_events_limit) |
| 120 |
assert result["changed"] |
| 121 |
assert len(result["events"]) > 1 |
| 122 |
# require that the final recorded stack state was CREATE_COMPLETE |
| 123 |
# events are retrieved newest-first, so 0 is the latest |
| 124 |
assert "CREATE_COMPLETE" in result["events"][0] |
| 125 |
connection.delete_stack(StackName="ansible-test-basic-yaml") |
| 126 |
|
| 127 |
|
| 128 |
def test_delete_nonexistent_stack(maybe_sleep, placeboify): |
| 129 |
connection = _create_wrapped_client(placeboify) |
| 130 |
# module is only used if we threw an unexpected error |
| 131 |
module = None |
| 132 |
result = cfn_module.stack_operation(module, connection, "ansible-test-nonexist", "DELETE", default_events_limit) |
| 133 |
assert result["changed"] |
| 134 |
assert "Stack does not exist." in result["log"] |
| 135 |
|
| 136 |
|
| 137 |
def test_get_nonexistent_stack(placeboify): |
| 138 |
connection = _create_wrapped_client(placeboify) |
| 139 |
# module is only used if we threw an unexpected error |
| 140 |
module = None |
| 141 |
assert cfn_module.get_stack_facts(module, connection, "ansible-test-nonexist") is None |
| 142 |
|
| 143 |
|
| 144 |
def test_missing_template_body(): |
| 145 |
m = FakeModule() |
| 146 |
with pytest.raises(Exception) as exc_info: |
| 147 |
cfn_module.create_stack(module=m, stack_params={}, cfn=None, events_limit=default_events_limit) |
| 148 |
pytest.fail("Expected module to have failed with no template") |
| 149 |
|
| 150 |
assert exc_info.match("FAIL") |
| 151 |
assert not m.exit_args |
| 152 |
assert ( |
| 153 |
"Either 'template', 'template_body' or 'template_url' is required when the stack does not exist." |
| 154 |
== m.exit_kwargs["msg"] |
| 155 |
) |
| 156 |
|
| 157 |
|
| 158 |
def test_on_create_failure_delete(maybe_sleep, placeboify): |
| 159 |
m = FakeModule( |
| 160 |
on_create_failure="DELETE", |
| 161 |
disable_rollback=False, |
| 162 |
) |
| 163 |
connection = _create_wrapped_client(placeboify) |
| 164 |
params = {"StackName": "ansible-test-on-create-failure-delete", "TemplateBody": failing_yaml_tpl} |
| 165 |
result = cfn_module.create_stack(m, params, connection, default_events_limit) |
| 166 |
assert result["changed"] |
| 167 |
assert result["failed"] |
| 168 |
assert len(result["events"]) > 1 |
| 169 |
# require that the final recorded stack state was DELETE_COMPLETE |
| 170 |
# events are retrieved newest-first, so 0 is the latest |
| 171 |
assert "DELETE_COMPLETE" in result["events"][0] |
| 172 |
|
| 173 |
|
| 174 |
def test_on_create_failure_rollback(maybe_sleep, placeboify): |
| 175 |
m = FakeModule( |
| 176 |
on_create_failure="ROLLBACK", |
| 177 |
disable_rollback=False, |
| 178 |
) |
| 179 |
connection = _create_wrapped_client(placeboify) |
| 180 |
params = {"StackName": "ansible-test-on-create-failure-rollback", "TemplateBody": failing_yaml_tpl} |
| 181 |
result = cfn_module.create_stack(m, params, connection, default_events_limit) |
| 182 |
assert result["changed"] |
| 183 |
assert result["failed"] |
| 184 |
assert len(result["events"]) > 1 |
| 185 |
# require that the final recorded stack state was ROLLBACK_COMPLETE |
| 186 |
# events are retrieved newest-first, so 0 is the latest |
| 187 |
assert "ROLLBACK_COMPLETE" in result["events"][0] |
| 188 |
connection.delete_stack(StackName=params["StackName"]) |
| 189 |
|
| 190 |
|
| 191 |
def test_on_create_failure_do_nothing(maybe_sleep, placeboify): |
| 192 |
m = FakeModule( |
| 193 |
on_create_failure="DO_NOTHING", |
| 194 |
disable_rollback=False, |
| 195 |
) |
| 196 |
connection = _create_wrapped_client(placeboify) |
| 197 |
params = {"StackName": "ansible-test-on-create-failure-do-nothing", "TemplateBody": failing_yaml_tpl} |
| 198 |
result = cfn_module.create_stack(m, params, connection, default_events_limit) |
| 199 |
assert result["changed"] |
| 200 |
assert result["failed"] |
| 201 |
assert len(result["events"]) > 1 |
| 202 |
# require that the final recorded stack state was CREATE_FAILED |
| 203 |
# events are retrieved newest-first, so 0 is the latest |
| 204 |
assert "CREATE_FAILED" in result["events"][0] |
| 205 |
connection.delete_stack(StackName=params["StackName"]) |