Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(cloudformation): validate import change sets, refuse to execute them, and report the Type of every change (#1793)

iot-rocket69d0131841a2
diff --git a/ministack/services/cloudformation/changesets.py b/ministack/services/cloudformation/changesets.py
index 7f190ab5..30485123 100644
--- a/ministack/services/cloudformation/changesets.py
+++ b/ministack/services/cloudformation/changesets.py
@@ -7,6 +7,11 @@ CloudFormation change set handlers — Create, Describe, Execute, Delete, List c
import copy
import logging
+import ministack.services.dynamodb as _dynamodb
+import ministack.services.s3 as _s3
+import ministack.services.sns as _sns
+import ministack.services.sqs as _sqs
+import ministack.services.ssm as _ssm
from ministack.core.responses import get_account_id, get_region, new_uuid, now_iso
from .engine import (
@@ -27,6 +32,8 @@ from .helpers import (
_request_problems,
_resolve_template,
_xml,
+ stack_name_problems,
+ validation_error_message,
)
from .stacks import (
_add_event,
@@ -40,6 +47,241 @@ from .stacks import (
logger = logging.getLogger("cloudformation")
+def _extract_resources_to_import(params):
+ """``ResourcesToImport.member.N`` as a list of
+ ``{ResourceType, LogicalResourceId, ResourceIdentifier}``, the shape
+ CreateChangeSet takes for ``ChangeSetType=IMPORT``."""
+ result = []
+ i = 1
+ while True:
+ rtype = _p(params, f"ResourcesToImport.member.{i}.ResourceType")
+ logical = _p(params, f"ResourcesToImport.member.{i}.LogicalResourceId")
+ if not rtype and not logical:
+ break
+ identifier = {}
+ j = 1
+ while True:
+ key = _p(params, f"ResourcesToImport.member.{i}.ResourceIdentifier.entry.{j}.key")
+ if not key:
+ break
+ identifier[key] = _p(
+ params, f"ResourcesToImport.member.{i}.ResourceIdentifier.entry.{j}.value", "")
+ j += 1
+ result.append({
+ "ResourceType": rtype,
+ "LogicalResourceId": logical,
+ "ResourceIdentifier": identifier,
+ })
+ i += 1
+ return result
+
+
+# AWS's answer when a ResourceIdentifier names nothing, for the types that do
+# not answer with a message of their own.
+_IMPORT_NOT_FOUND = "Resource of type '{type}' with identifier '{value}' was not found."
+
+
+def _sdk_error(message, service, status, request_id=None):
+ """A service error as the import's resource handler words it in the
+ StatusReason: the service's own message, then its SDK's suffix."""
+ return (f"{message} (Service: {service}, Status Code: {status}, "
+ f"Request ID: {request_id or new_uuid()}) (SDK Attempt Count: 1)")
+
+
+def _queue_import_problem(url):
+ if not url:
+ return "QueueUrl is not found"
+ try:
+ _sqs._get_q(url)
+ except _sqs._Err:
+ return _IMPORT_NOT_FOUND.format(type="AWS::SQS::Queue", value=url)
+ return None
+
+
+def _topic_import_problem(arn):
+ elements = len(arn.split(":"))
+ if elements < 6:
+ return _sdk_error("Invalid parameter: TopicArn Reason: An ARN must have at least "
+ f"6 elements, not {elements}", "Sns", 400)
+ if arn not in _sns._topics:
+ return _sdk_error("Topic does not exist", "Sns", 404)
+ return None
+
+
+def _bucket_import_problem(name):
+ if not name:
+ return "Unable to marshall request to JSON: Bucket cannot be empty."
+ return None if name in _s3._buckets else "Bucket not found"
+
+
+def _table_import_problem(name):
+ problems = []
+ if len(name) < 3:
+ problems.append(f"Value '{name}' at 'tableName' failed to satisfy constraint: "
+ "Member must have length greater than or equal to 3")
+ if not _dynamodb._TABLE_NAME_RE.match(name):
+ problems.append(f"Value '{name}' at 'tableName' failed to satisfy constraint: "
+ "Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+")
+ if problems:
+ # DynamoDB's request ids are 52 upper-case letters and digits.
+ request_id = (new_uuid() + new_uuid()).replace("-", "")[:52].upper()
+ return _sdk_error(validation_error_message(problems), "DynamoDb", 400, request_id)
+ return None if name in _dynamodb._tables else f"Table: {name} does not exist."
+
+
+def _parameter_import_problem(name):
+ if not name:
+ return _sdk_error(
+ "1 validation error detected: Value '[]' at 'names' failed to satisfy constraint: "
+ "Member must satisfy constraint: [Member must have length less than or equal to "
+ "2048, Member must have length greater than or equal to 1]", "Ssm", 400)
+ if _ssm._lookup_parameter(name)[1] is None:
+ return _IMPORT_NOT_FOUND.format(type="AWS::SSM::Parameter", value=name)
+ return None
+
+
+# The resource types an IMPORT change set looks up before it describes the
+# import: the one ResourceIdentifier key AWS expects, and a lookup in this
+# emulator's own store that returns the StatusReason of the FAILED change set
+# AWS leaves behind when the identifier is invalid or names nothing (each
+# measured for an existing, a missing, a blank and an empty value). Types not
+# listed are accepted without a lookup.
+_IMPORT_LOOKUPS = {
+ "AWS::SQS::Queue": ("QueueUrl", _queue_import_problem),
+ "AWS::SNS::Topic": ("TopicArn", _topic_import_problem),
+ "AWS::S3::Bucket": ("BucketName", _bucket_import_problem),
+ "AWS::DynamoDB::Table": ("TableName", _table_import_problem),
+ "AWS::SSM::Parameter": ("Name", _parameter_import_problem),
+}
+
+
+def _listed(ids):
+ return "[" + ", ".join(ids) + "]"
+
+
+def _import_changes(resources_to_import, template, diff):
+ """The Import changes of an IMPORT change set.
+
+ Raises ValueError with the message CreateChangeSet refuses the request
+ with, checking in the order AWS does where two problems meet. ``diff`` is
+ the template diff against the stack: an import may add only the resources
+ it imports, and must leave everything already in the stack untouched.
+ """
+ declared = (template or {}).get("Resources", {})
+ logicals = [entry["LogicalResourceId"] for entry in resources_to_import]
+ if len(set(logicals)) != len(logicals):
+ raise ValueError("Every resource to import must have unique LogicalResourceId")
+ undeclared = [lid for lid in logicals if lid not in declared]
+ if undeclared:
+ raise ValueError(f"The logical resource ids {_listed(undeclared)} provided in "
+ "ResourceToImport do not exist in the template.")
+ for entry in resources_to_import:
+ logical, rtype = entry["LogicalResourceId"], entry.get("ResourceType")
+ identifier = entry.get("ResourceIdentifier") or {}
+ if rtype in _IMPORT_LOOKUPS and list(identifier) != [_IMPORT_LOOKUPS[rtype][0]]:
+ raise ValueError(f"Invalid resource identifier for resource type {rtype}. "
+ f"Expected [{_IMPORT_LOOKUPS[rtype][0]}]")
+ if rtype != declared[logical].get("Type"):
+ raise ValueError(
+ f"Resource type of [{logical}] passed in ResourceToImport does not match with "
+ "resource type defined in the template. Resource type in ResourceToImport: "
+ f"{rtype}; Resource type in Template: {declared[logical].get('Type')}.")
+
+ touched = {}
+ for change in diff:
+ rc = change["ResourceChange"]
+ touched.setdefault(rc["Action"], []).append(rc["LogicalResourceId"])
+ added = touched.get("Add", [])
+ if not added:
+ raise ValueError("The template should contain at least one new resource to import.")
+ in_stack = [lid for lid in logicals if lid not in added]
+ if in_stack:
+ raise ValueError(f"Resources {_listed(in_stack)} passed in ResourceToImport are already "
+ "in a stack and cannot be imported.")
+ not_imported = [lid for lid in added if lid not in logicals]
+ if not_imported:
+ raise ValueError(f"Resources {_listed(not_imported)} is missing from ResourceToImport list")
+ modified = sorted(touched.get("Modify", []) + touched.get("Remove", []))
+ if modified:
+ raise ValueError(f"You have modified resources {_listed(modified)} in your template that "
+ "are not being imported. Update, create or delete operations cannot be "
+ "executed during import operations.")
+ no_policy = [lid for lid in logicals if "DeletionPolicy" not in declared[lid]]
+ if no_policy:
+ raise ValueError(f"The following resources to import {_listed(no_policy)} must have "
+ "DeletionPolicy attribute specified in the template.")
+
+ changes = []
+ for entry in resources_to_import:
+ change = {
+ "Action": "Import",
+ "LogicalResourceId": entry["LogicalResourceId"],
+ "ResourceType": entry["ResourceType"],
+ "Scope": [],
+ "Details": [],
+ }
+ # AWS reports the identifier value as the physical id (measured for the
+ # five types in _IMPORT_LOOKUPS). How it joins a multi-key identifier
+ # (AWS::IAM::RolePolicy's PolicyName + RoleName, say) is unmeasured, so
+ # none is invented for one.
+ if len(entry["ResourceIdentifier"]) == 1:
+ change["PhysicalResourceId"] = next(iter(entry["ResourceIdentifier"].values()))
+ changes.append({"ResourceChange": change})
+ return changes
+
+
+def _import_ref(rtype, value):
+ """What an import identifier and a stack's physical id are compared on:
+ a queue's account and name, since its URL's host is the endpoint it was
+ read through, and the value itself for any other type."""
+ if rtype == "AWS::SQS::Queue":
+ return _sqs._queue_ref_from_urlish(value)
+ return value
+
+
+def _import_owner(resources_to_import):
+ """The StatusReason of an import naming a resource a stack already holds,
+ or None. AWS fails the change set for it, as for a resource that does not
+ exist. Evaluate it in the stack's region."""
+ from ministack.services.cloudformation import _stacks
+ for entry in resources_to_import:
+ rtype, identifier = entry["ResourceType"], entry["ResourceIdentifier"]
+ if len(identifier) != 1:
+ continue
+ value = next(iter(identifier.values()))
+ ref = _import_ref(rtype, value)
+ for stack in _stacks.values():
+ if stack.get("StackStatus") == "DELETE_COMPLETE":
+ continue
+ for res in stack.get("_resources", {}).values():
+ pid = res.get("PhysicalResourceId")
+ if res.get("ResourceType") == rtype and pid and _import_ref(rtype, pid) == ref:
+ return f"{value} already exists in stack {stack['StackId']}"
+ return None
+
+
+def _import_not_found(resources_to_import):
+ """The StatusReason of an import whose identifier is invalid or names
+ nothing, or None.
+
+ AWS accepts such a request and fails the change set once it looks the
+ resource up, so this is not a refusal. Evaluate it in the stack's region.
+ """
+ for entry in resources_to_import:
+ rtype, identifier = entry["ResourceType"], entry["ResourceIdentifier"]
+ if rtype in _IMPORT_LOOKUPS:
+ key, problem = _IMPORT_LOOKUPS[rtype]
+ reason = problem(identifier[key])
+ if reason:
+ return reason
+ continue
+ # A blank name can name nothing, whatever the type.
+ blank = next((value for value in identifier.values() if not value.strip()), None)
+ if blank is not None:
+ return _IMPORT_NOT_FOUND.format(type=rtype, value=blank)
+ return None
+
+
def _find_change_set(cs_name, stack_name=""):
"""Look up a change set by ID or by name+stack. Returns (cs_id, cs_dict) or (None, None)."""
from ministack.services.cloudformation import _change_sets
@@ -120,6 +362,16 @@ def _create_change_set(params):
template_given = bool(template_body)
provided_params = _extract_members(params, "Parameters")
+ resources_to_import = _extract_resources_to_import(params)
+ # The API's parameter validation: a member without a ResourceIdentifier.
+ if missing := [
+ f"Value null at 'resourcesToImport.{i}.member.resourceIdentifier' failed to satisfy "
+ "constraint: Member must not be null"
+ for i, entry in enumerate(resources_to_import, 1) if not entry["ResourceIdentifier"]
+ ]:
+ return _error("ValidationError", validation_error_message(missing))
+ if cs_type == "IMPORT" and not resources_to_import:
+ return _error("ValidationError", "Must Provide at least one resource to import")
tags = _extract_members(params, "Tags")
# An empty Tags list arrives as ``Tags=``: given-empty clears the stack's
# tags on execute, an omitted Tags keeps them (as UpdateStack does).
@@ -135,7 +387,14 @@ def _create_change_set(params):
# by stack id carries on under the name.
stack_name = stack.get("StackName", stack_name)
- if cs_type == "CREATE":
+ # An IMPORT set naming a stack that does not exist creates it in
+ # REVIEW_IN_PROGRESS, as a CREATE set does, and imports into it.
+ new_stack_import = cs_type == "IMPORT" and (
+ not stack or stack.get("StackStatus") == "DELETE_COMPLETE")
+ if new_stack_import and (problems := stack_name_problems(_p(params, "StackName"))):
+ return _error("ValidationError", validation_error_message(problems))
+
+ if cs_type == "CREATE" or new_stack_import:
if stack and stack.get("StackStatus") not in (
"DELETE_COMPLETE", "ROLLBACK_COMPLETE", "REVIEW_IN_PROGRESS"
):
@@ -153,7 +412,9 @@ def _create_change_set(params):
"StackName": stack_name,
"StackId": stack_id,
"StackStatus": "REVIEW_IN_PROGRESS",
- "StackStatusReason": "",
+ # Measured on AWS for the stack an import creates; a CREATE set's
+ # placeholder keeps the empty reason it always had.
+ "StackStatusReason": "User Initiated" if new_stack_import else "",
"CreationTime": now_iso(),
"LastUpdatedTime": now_iso(),
"Description": "",
@@ -172,7 +433,7 @@ def _create_change_set(params):
_stack_events[stack_id] = []
_add_event(stack_id, stack_name, stack_name,
"AWS::CloudFormation::Stack", "REVIEW_IN_PROGRESS",
- physical_id=stack_id)
+ stack["StackStatusReason"], physical_id=stack_id)
else:
# An UPDATE change set against a deleted stack: the name no longer
# resolves (deleted stacks are addressable only by stack ID), so this is
@@ -187,9 +448,9 @@ def _create_change_set(params):
def _rejected(reason):
# A rejected CreateChangeSet leaves no stack behind on AWS; drop the
- # REVIEW_IN_PROGRESS placeholder created above for CREATE sets.
+ # REVIEW_IN_PROGRESS placeholder created above for a new stack.
# ``reason`` is a ValidationError message or a ready error response.
- if cs_type == "CREATE":
+ if cs_type == "CREATE" or new_stack_import:
_stacks.pop(stack_name, None)
_stack_events.pop(stack_id, None)
if isinstance(reason, tuple):
@@ -229,19 +490,44 @@ def _create_change_set(params):
# parameter-driven changes (the `aws cloudformation deploy
# --parameter-overrides` pattern, e.g. a Lambda Code S3Key behind a Ref) are
# detected instead of compared as identical raw nodes (#897).
- old_template = stack.get("_template", {}) if cs_type == "UPDATE" else {}
- old_params = stack.get("_resolved_params", {}) if cs_type == "UPDATE" else {}
+ # An import is checked against the stack it imports into, like an update.
+ diffs_the_stack = cs_type in ("UPDATE", "IMPORT")
+ old_template = stack.get("_template", {}) if diffs_the_stack else {}
+ old_params = stack.get("_resolved_params", {}) if diffs_the_stack else {}
with _stack_region_context(stack, stack_id):
old_resolved = _resolve_props_for_diff(old_template, old_params, stack_name, stack_id)
new_resolved = _resolve_props_for_diff(template, param_values, stack_name, stack_id)
changes = _diff_resources(old_resolved, new_resolved)
+ import_failure = None
+ if cs_type == "IMPORT":
+ # An import describes the resources being adopted, not the template
+ # diff, which would call each of them `Add`; the diff only decides
+ # what else the template may not do during an import.
+ try:
+ changes = _import_changes(resources_to_import, template, changes)
+ except ValueError as exc:
+ return _rejected(str(exc))
+ with _stack_region_context(stack, stack_id):
+ # AWS looks the resource up before it asks whether a stack holds
+ # it: a stack's queue deleted behind its back is "not found".
+ import_failure = (_import_not_found(resources_to_import)
+ or _import_owner(resources_to_import))
+ if import_failure:
+ changes = []
cs_id = (
f"arn:aws:cloudformation:{_stack_region(stack, stack_id)}:{get_account_id()}:"
f"changeSet/{cs_name}/{new_uuid()}"
)
- if changes:
+ if import_failure:
+ # AWS accepts an import of a resource that does not exist and fails
+ # the change set, with no changes, once it looks the resource up.
+ _cs_status, _cs_exec, _cs_reason = "FAILED", "UNAVAILABLE", import_failure
+ elif cs_type == "IMPORT":
+ _cs_status, _cs_exec = "CREATE_COMPLETE", "UNAVAILABLE"
+ _cs_reason = "Resource import is not supported by this emulator"
+ elif changes:
_cs_status, _cs_exec, _cs_reason = "CREATE_COMPLETE", "AVAILABLE", ""
else:
# Real AWS: a change set with no changes ends FAILED and cannot be
@@ -324,12 +610,24 @@ def _describe_change_set(params):
f"<ChangeSource>{_esc(d.get('ChangeSource', 'DirectModification'))}</ChangeSource>"
"</member>"
)
+ # botocore reads an empty element as "", so a member the change does
+ # not carry (an Import's Replacement, a Remove's unknown physical id)
+ # is left out instead of written empty.
+ physical_xml = (
+ f"<PhysicalResourceId>{_esc(rc['PhysicalResourceId'])}</PhysicalResourceId>"
+ if rc.get("PhysicalResourceId") else ""
+ )
+ replacement_xml = (
+ f"<Replacement>{rc['Replacement']}</Replacement>" if "Replacement" in rc else ""
+ )
+ # "Resource" is the one ChangeType, and AWS reports it on every change.
changes_xml += (
- "<member><ResourceChange>"
+ "<member><Type>Resource</Type><ResourceChange>"
f"<Action>{rc.get('Action', '')}</Action>"
f"<LogicalResourceId>{_esc(rc.get('LogicalResourceId', ''))}</LogicalResourceId>"
+ f"{physical_xml}"
f"<ResourceType>{_esc(rc.get('ResourceType', ''))}</ResourceType>"
- f"<Replacement>{rc.get('Replacement', '')}</Replacement>"
+ f"{replacement_xml}"
f"<Scope>{scope_xml}</Scope>"
f"<Details>{details_xml}</Details>"
"</ResourceChange></member>"
@@ -396,6 +694,15 @@ def _execute_change_set(params):
return _error("ChangeSetNotFound",
f"ChangeSet [{cs_name}] does not exist", 404)
+ if cs.get("ChangeSetType") == "IMPORT":
+ # The ordinary create path would recreate the imported resource and
+ # could delete the stack's existing resources when that create fails.
+ return _error(
+ "InvalidChangeSetStatus",
+ f"ChangeSet [{cs_name}] cannot be executed: resource import is not "
+ "supported by this emulator.",
+ )
+
if cs["ExecutionStatus"] != "AVAILABLE":
return _error("InvalidChangeSetStatus",
f"ChangeSet [{cs_name}] is in {cs['ExecutionStatus']} status")
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index e0ec91a2..aa66f317 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -1619,6 +1619,34 @@ def test_cfn_change_set_no_changes_is_failed(cfn, ssm):
cfn.delete_stack(StackName=S)
+def test_cfn_change_set_changes_carry_their_type(cfn):
+ """Every entry of a change set's Changes carries Type "Resource", the one
+ ChangeType there is; measured on AWS for Add, Modify, Remove and Import."""
+ S = f"cfn-change-type-{_uuid_mod.uuid4().hex[:8]}"
+
+ def param(suffix, value="v"):
+ return {"Type": "AWS::SSM::Parameter", "Properties": {
+ "Name": f"/{S}/{suffix}", "Type": "String", "Value": value}}
+
+ cfn.create_change_set(StackName=S, ChangeSetName="create", ChangeSetType="CREATE",
+ TemplateBody=json.dumps({"Resources": {"X": param("x"), "Y": param("y")}}))
+ try:
+ created = cfn.describe_change_set(ChangeSetName="create", StackName=S)["Changes"]
+ assert [(c.get("Type"), c["ResourceChange"]["Action"]) for c in created] == [
+ ("Resource", "Add"), ("Resource", "Add")]
+ cfn.execute_change_set(ChangeSetName="create", StackName=S)
+ _wait_stack(cfn, S)
+ cfn.create_change_set(StackName=S, ChangeSetName="update", ChangeSetType="UPDATE",
+ TemplateBody=json.dumps({"Resources": {
+ "X": param("x", "w"), "Z": param("z")}}))
+ updated = cfn.describe_change_set(ChangeSetName="update", StackName=S)["Changes"]
+ assert sorted((c.get("Type"), c["ResourceChange"]["Action"]) for c in updated) == [
+ ("Resource", "Add"), ("Resource", "Modify"), ("Resource", "Remove")]
+ finally:
+ cfn.delete_stack(StackName=S)
+ _wait_stack(cfn, S)
+
+
def test_cfn_change_set_sees_policy_and_metadata_changes(cfn, sqs):
"""A change set lists a resource whose DeletionPolicy, UpdateReplacePolicy
or Metadata changed and nothing else: Modify, Replacement False, the
diff --git a/tests/test_cfn_import_change_set.py b/tests/test_cfn_import_change_set.py
new file mode 100644
index 00000000..846780ef
--- /dev/null
+++ b/tests/test_cfn_import_change_set.py
@@ -0,0 +1,656 @@
+"""IMPORT change sets: what CreateChangeSet refuses or fails, the shape of an
+Import change, and that the unsupported execution leaves stack and service
+state intact.
+
+Every expected message is the one AWS answers for the same request.
+"""
+
+import copy
+import json
+import re
+import uuid
+
+import boto3
+import pytest
+from botocore.config import Config
+from botocore.exceptions import ClientError
+from conftest import ENDPOINT, make_client
+
+_MODIFIED = (
+ "You have modified resources [X] in your template that are not being imported. "
+ "Update, create or delete operations cannot be executed during import operations."
+)
+
+
+def adopted(cfn, sqs, ssm):
+ tag = uuid.uuid4().hex[:10]
+ qname, stack, pname = f"imp-adopt-{tag}", f"imp-{tag}", f"/imp/{tag}"
+ url = sqs.create_queue(QueueName=qname, Attributes={"VisibilityTimeout": "17"})["QueueUrl"]
+ sqs.send_message(QueueUrl=url, MessageBody="keep this message")
+ original = {"Resources": {"X": {"Type": "AWS::SSM::Parameter", "Properties": {
+ "Type": "String", "Name": pname, "Value": "keep this value"}}}}
+ created = False
+ try:
+ cfn.create_stack(StackName=stack, TemplateBody=json.dumps(original))
+ created = True
+ cfn.get_waiter("stack_create_complete").wait(
+ StackName=stack, WaiterConfig={"Delay": 1, "MaxAttempts": 30})
+ template = copy.deepcopy(original)
+ template["Resources"]["Q"] = {
+ "Type": "AWS::SQS::Queue", "DeletionPolicy": "Retain", "Properties": {"QueueName": qname},
+ }
+ to_import = [{"ResourceType": "AWS::SQS::Queue", "LogicalResourceId": "Q",
+ "ResourceIdentifier": {"QueueUrl": url}}]
+ yield stack, url, pname, template, to_import
+ finally:
+ if created:
+ cfn.delete_stack(StackName=stack)
+ cfn.get_waiter("stack_delete_complete").wait(
+ StackName=stack, WaiterConfig={"Delay": 1, "MaxAttempts": 30})
+ sqs.delete_queue(QueueUrl=url)
+
+
+def _create(cfn, adopted, template=None, to_import=None, client=None):
+ stack, _url, _pname, default_template, default_import = adopted
+ result = (client or cfn).create_change_set(
+ StackName=stack, ChangeSetName="imp", ChangeSetType="IMPORT",
+ ResourcesToImport=default_import if to_import is None else to_import,
+ TemplateBody=json.dumps(default_template if template is None else template))
+ return cfn.describe_change_set(ChangeSetName=result["Id"])
+
+
+def _refusal(cfn, adopted, template=None, to_import=None):
+ """The ValidationError message CreateChangeSet answers; no change set is left."""
+ with pytest.raises(ClientError) as exc:
+ _create(cfn, adopted, template, to_import)
+ assert exc.value.response["Error"]["Code"] == "ValidationError"
+ assert cfn.list_change_sets(StackName=adopted[0])["Summaries"] == []
+ return exc.value.response["Error"]["Message"]
+
+
+def _failed(cfn, adopted, cs):
+ """A change set AWS accepts and then fails: no changes, not executable."""
+ assert (cs["Status"], cs["ExecutionStatus"], cs["Changes"]) == ("FAILED", "UNAVAILABLE", [])
+ summaries = cfn.list_change_sets(StackName=adopted[0])["Summaries"]
+ assert [(s["Status"], s["ExecutionStatus"], s["StatusReason"]) for s in summaries] == [
+ ("FAILED", "UNAVAILABLE", cs["StatusReason"])]
+ return cs["StatusReason"]
+
+
+# --- one resource of each looked-up type, outside any stack ---
+
+_TYPES = {
+ "sqs": ("AWS::SQS::Queue", "QueueUrl", "QueueName"),
+ "sns": ("AWS::SNS::Topic", "TopicArn", "TopicName"),
+ "s3": ("AWS::S3::Bucket", "BucketName", "BucketName"),
+ "ddb": ("AWS::DynamoDB::Table", "TableName", "TableName"),
+ "ssm": ("AWS::SSM::Parameter", "Name", "Name"),
+}
+
+_TABLE_PROPS = {
+ "BillingMode": "PAY_PER_REQUEST",
+ "AttributeDefinitions": [{"AttributeName": "id", "AttributeType": "S"}],
+ "KeySchema": [{"AttributeName": "id", "KeyType": "HASH"}],
+}
+
+# AWS's StatusReason for an identifier that names nothing, per type.
+_NOT_FOUND = {
+ "sqs": "Resource of type 'AWS::SQS::Queue' with identifier '{value}' was not found.",
+ "sns": ("Topic does not exist (Service: Sns, Status Code: 404, Request ID: {request_id}) "
+ "(SDK Attempt Count: 1)"),
+ "s3": "Bucket not found",
+ "ddb": "Table: {value} does not exist.",
+ "ssm": "Resource of type 'AWS::SSM::Parameter' with identifier '{value}' was not found.",
+}
+
+
+def _declaration(kind, name):
+ rtype, _key, name_prop = _TYPES[kind]
+ props = {name_prop: name}
+ if kind == "ddb":
+ props.update(_TABLE_PROPS)
+ elif kind == "ssm":
+ props.update({"Type": "String", "Value": "m"})
+ return {"Type": rtype, "DeletionPolicy": "Retain", "Properties": props}
+
+
+def identifier_value(sts):
+ """The ResourceIdentifier value naming a resource of ``kind`` called ``name``,
+ in the caller's account and region."""
+ account, region = sts.get_caller_identity()["Account"], sts.meta.region_name
+
+ def value(kind, name):
+ if kind == "sqs":
+ return f"{ENDPOINT}/{account}/{name}"
+ if kind == "sns":
+ return f"arn:aws:sns:{region}:{account}:{name}"
+ return name
+ return value
+
+
+def _name(kind, tag):
+ return f"/imp/{kind}-{tag}" if kind == "ssm" else f"imp-{kind}-{tag}"
+
+
+def outside(request, sqs, sns, s3, ddb, ssm):
+ """Create one resource of the parametrized kind; yield its name."""
+ kind = request.param
+ name = _name(kind, uuid.uuid4().hex[:10])
+ if kind == "sqs":
+ url = sqs.create_queue(QueueName=name)["QueueUrl"]
+ delete, key = sqs.delete_queue, {"QueueUrl": url}
+ elif kind == "sns":
+ arn = sns.create_topic(Name=name)["TopicArn"]
+ delete, key = sns.delete_topic, {"TopicArn": arn}
+ elif kind == "s3":
+ s3.create_bucket(Bucket=name)
+ delete, key = s3.delete_bucket, {"Bucket": name}
+ elif kind == "ddb":
+ ddb.create_table(TableName=name, **_TABLE_PROPS)
+ delete, key = ddb.delete_table, {"TableName": name}
+ else:
+ ssm.put_parameter(Name=name, Type="String", Value="m")
+ delete, key = ssm.delete_parameter, {"Name": name}
+ try:
+ yield kind, name
+ finally:
+ delete(**key)
+
+
+def _import_of(kind, value):
+ rtype, key, _prop = _TYPES[kind]
+ return [{"ResourceType": rtype, "LogicalResourceId": "Q", "ResourceIdentifier": {key: value}}]
+
+
+# --- accepted imports ---
+
+def test_import_change_set_describes_only_imports_and_is_unavailable(cfn, adopted):
+ cs = _create(cfn, adopted)
+ assert cs["Status"] == "CREATE_COMPLETE"
+ # AWS answers AVAILABLE and executes the import; the emulator refuses it by design.
+ assert cs["ExecutionStatus"] == "UNAVAILABLE"
+ assert "import" in cs["StatusReason"].lower()
+ assert [(r["Type"], r["ResourceChange"]["LogicalResourceId"], r["ResourceChange"]["Action"],
+ r["ResourceChange"]["ResourceType"]) for r in cs["Changes"]] == [
+ ("Resource", "Q", "Import", "AWS::SQS::Queue")]
+
+
[email protected]("outside", list(_TYPES), indirect=True)
+def test_import_change_names_the_resource_and_has_no_replacement(cfn, adopted, outside, identifier_value):
+ # AWS: PhysicalResourceId is the identifier value, and an Import change has
+ # no Replacement member at all (botocore would turn an empty element into "").
+ kind, name = outside
+ template = copy.deepcopy(adopted[3])
+ template["Resources"]["Q"] = _declaration(kind, name)
+ to_import = _import_of(kind, identifier_value(kind, name))
+ cs = _create(cfn, adopted, template, to_import)
+ assert cs["Status"] == "CREATE_COMPLETE"
+ assert cs["Changes"] == [{"Type": "Resource", "ResourceChange": {
+ "Action": "Import",
+ "LogicalResourceId": "Q",
+ "PhysicalResourceId": identifier_value(kind, name),
+ "ResourceType": _TYPES[kind][0],
+ "Scope": [],
+ "Details": [],
+ }}]
+
+
+def test_update_change_set_changes_keep_their_shape(cfn, ssm):
+ # Guard, not a fix: the Import change's shape must not leak into the
+ # Add / Modify / Remove changes of an ordinary UPDATE change set.
+ tag = uuid.uuid4().hex[:10]
+ stack = f"imp-upd-{tag}"
+
+ def param(suffix, value="v"):
+ return {"Type": "AWS::SSM::Parameter",
+ "Properties": {"Type": "String", "Name": f"/imp/upd-{tag}-{suffix}", "Value": value}}
+
+ cfn.create_stack(StackName=stack, TemplateBody=json.dumps(
+ {"Resources": {"X": param("x"), "Y": param("y")}}))
+ try:
+ cfn.get_waiter("stack_create_complete").wait(
+ StackName=stack, WaiterConfig={"Delay": 1, "MaxAttempts": 30})
+ cs_id = cfn.create_change_set(
+ StackName=stack, ChangeSetName="upd", ChangeSetType="UPDATE",
+ TemplateBody=json.dumps({"Resources": {"X": param("x", "w"), "Z": param("z")}}))["Id"]
+ changes = {c["ResourceChange"]["LogicalResourceId"]: c["ResourceChange"]
+ for c in cfn.describe_change_set(ChangeSetName=cs_id)["Changes"]}
+ assert {k: v["Action"] for k, v in changes.items()} == {"X": "Modify", "Y": "Remove", "Z": "Add"}
+ assert "Replacement" in changes["X"]
+ assert "PhysicalResourceId" not in changes["Z"]
+ finally:
+ cfn.delete_stack(StackName=stack)
+ cfn.get_waiter("stack_delete_complete").wait(
+ StackName=stack, WaiterConfig={"Delay": 1, "MaxAttempts": 30})
+
+
+# --- accepted, then FAILED: the identifier names nothing ---
+
[email protected]("kind", list(_TYPES))
+def test_import_of_a_resource_that_does_not_exist_fails_the_change_set(cfn, adopted, kind, identifier_value):
+ name = _name(kind, uuid.uuid4().hex[:10]) + "-ghost"
+ template = copy.deepcopy(adopted[3])
+ template["Resources"]["Q"] = _declaration(kind, name)
+ cs = _create(cfn, adopted, template, _import_of(kind, identifier_value(kind, name)))
+ reason = _failed(cfn, adopted, cs)
+ expected = re.escape(_NOT_FOUND[kind]).replace(
+ re.escape("{value}"), re.escape(identifier_value(kind, name))).replace(
+ re.escape("{request_id}"), "[0-9a-f-]{36}")
+ assert re.fullmatch(expected, reason), reason
+
+
+# AWS's StatusReason for a blank identifier, per type and value: the resource
+# handler's own lookup error where the service refuses the value outright.
+_SDK = r" \(Service: {service}, Status Code: 400, Request ID: {request_id}\) \(SDK Attempt Count: 1\)"
+_UUID = "[0-9a-f-]{36}"
+_TABLE_NAME = ("2 validation errors detected: Value '{v}' at 'tableName' failed to satisfy constraint: "
+ "Member must have length greater than or equal to 3; Value '{v}' at 'tableName' failed "
+ "to satisfy constraint: Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+")
+_BLANK = {
+ ("sqs", " "): re.escape("Resource of type 'AWS::SQS::Queue' with identifier ' ' was not found."),
+ ("sqs", ""): re.escape("QueueUrl is not found"),
+ ("sns", " "): re.escape("Invalid parameter: TopicArn Reason: An ARN must have at least 6 elements, "
+ "not 1") + _SDK.format(service="Sns", request_id=_UUID),
+ ("s3", " "): re.escape("Bucket not found"),
+ ("s3", ""): re.escape("Unable to marshall request to JSON: Bucket cannot be empty."),
+ ("ddb", " "): re.escape(_TABLE_NAME.format(v=" ")) + _SDK.format(
+ service="DynamoDb", request_id="[0-9A-Z]{52}"),
+ ("ddb", ""): re.escape(_TABLE_NAME.format(v="")) + _SDK.format(
+ service="DynamoDb", request_id="[0-9A-Z]{52}"),
+ ("ssm", " "): re.escape("Resource of type 'AWS::SSM::Parameter' with identifier ' ' was not found."),
+ ("ssm", ""): re.escape(
+ "1 validation error detected: Value '[]' at 'names' failed to satisfy constraint: Member must "
+ "satisfy constraint: [Member must have length less than or equal to 2048, Member must have "
+ "length greater than or equal to 1]") + _SDK.format(service="Ssm", request_id=_UUID),
+}
+_BLANK[("sns", "")] = _BLANK[("sns", " ")]
+
+
[email protected]("kind, value", sorted(_BLANK), ids=lambda v: v if v.strip() else repr(v))
+def test_blank_identifier_fails_the_change_set(cfn, adopted, kind, value):
+ # botocore refuses an empty value client-side; AWS itself accepts the
+ # request and fails the change set.
+ unvalidated = make_client("cloudformation", {"parameter_validation": False})
+ template = copy.deepcopy(adopted[3])
+ template["Resources"]["Q"] = _declaration(kind, _name(kind, uuid.uuid4().hex[:10]))
+ to_import = [{"ResourceType": _TYPES[kind][0], "LogicalResourceId": "Q",
+ "ResourceIdentifier": {_TYPES[kind][1]: value}}]
+ cs = _create(cfn, adopted, template, to_import, client=unvalidated)
+ reason = _failed(cfn, adopted, cs)
+ assert re.fullmatch(_BLANK[(kind, value)], reason), reason
+
+
+def test_queue_in_another_region_is_not_found(cfn, adopted):
+ # The lookup runs in the stack's region: a queue of the same account in
+ # another region is not the one being imported.
+ west = boto3.client("sqs", endpoint_url=ENDPOINT, region_name="us-west-2",
+ aws_access_key_id="test", aws_secret_access_key="test",
+ config=Config(region_name="us-west-2", retries={"mode": "standard"}))
+ name = f"imp-west-{uuid.uuid4().hex[:10]}"
+ url = west.create_queue(QueueName=name)["QueueUrl"]
+ try:
+ template = copy.deepcopy(adopted[3])
+ template["Resources"]["Q"] = _declaration("sqs", name)
+ to_import = [{"ResourceType": "AWS::SQS::Queue", "LogicalResourceId": "Q",
+ "ResourceIdentifier": {"QueueUrl": url}}]
+ cs = _create(cfn, adopted, template, to_import)
+ assert _failed(cfn, adopted, cs) == (
+ f"Resource of type 'AWS::SQS::Queue' with identifier '{url}' was not found.")
+ finally:
+ west.delete_queue(QueueUrl=url)
+
+
+def _owner_stack(cfn, qname, retain=False):
+ """A stack holding one queue named ``qname``; returns its StackId."""
+ queue = {"Type": "AWS::SQS::Queue", "Properties": {"QueueName": qname}}
+ if retain:
+ queue["DeletionPolicy"] = "Retain"
+ stack = f"imp-own-{uuid.uuid4().hex[:10]}"
+ stack_id = cfn.create_stack(StackName=stack, TemplateBody=json.dumps({"Resources": {"K": queue}}))["StackId"]
+ cfn.get_waiter("stack_create_complete").wait(StackName=stack, WaiterConfig={"Delay": 1, "MaxAttempts": 30})
+ return stack_id
+
+
+def _drop_stack(cfn, stack):
+ cfn.delete_stack(StackName=stack)
+ cfn.get_waiter("stack_delete_complete").wait(StackName=stack, WaiterConfig={"Delay": 1, "MaxAttempts": 30})
+
+
+def _import_queue(qname, url):
+ template = {"Type": "AWS::SQS::Queue", "DeletionPolicy": "Retain", "Properties": {"QueueName": qname}}
+ to_import = [{"ResourceType": "AWS::SQS::Queue", "LogicalResourceId": "Q",
+ "ResourceIdentifier": {"QueueUrl": url}}]
+ return template, to_import
+
+
+def test_import_of_a_queue_another_stack_holds_fails_the_change_set(cfn, sqs, adopted):
+ # The URL read through the SQS API and the stack's physical id name the
+ # same queue even where their hosts differ.
+ qname = f"imp-owned-{uuid.uuid4().hex[:10]}"
+ owner = _owner_stack(cfn, qname)
+ try:
+ url = sqs.get_queue_url(QueueName=qname)["QueueUrl"]
+ template = copy.deepcopy(adopted[3])
+ template["Resources"]["Q"], to_import = _import_queue(qname, url)
+ cs = _create(cfn, adopted, template, to_import)
+ assert _failed(cfn, adopted, cs) == f"{url} already exists in stack {owner}"
+ finally:
+ _drop_stack(cfn, owner)
+
+
+def test_import_of_a_resource_this_stack_holds_under_another_id_fails_the_change_set(cfn, adopted):
+ stack, _url, pname, template, _to_import = adopted
+ del template["Resources"]["Q"]
+ template["Resources"]["Y"] = {"Type": "AWS::SSM::Parameter", "DeletionPolicy": "Retain",
+ "Properties": {"Type": "String", "Name": pname, "Value": "keep this value"}}
+ to_import = [{"ResourceType": "AWS::SSM::Parameter", "LogicalResourceId": "Y",
+ "ResourceIdentifier": {"Name": pname}}]
+ cs = _create(cfn, adopted, template, to_import)
+ stack_id = cfn.describe_stacks(StackName=stack)["Stacks"][0]["StackId"]
+ assert _failed(cfn, adopted, cs) == f"{pname} already exists in stack {stack_id}"
+
+
+def test_a_queue_deleted_behind_its_stack_is_not_found(cfn, sqs, adopted):
+ # AWS looks the resource up before it asks which stack holds it.
+ qname = f"imp-gone-{uuid.uuid4().hex[:10]}"
+ owner = _owner_stack(cfn, qname)
+ try:
+ url = sqs.get_queue_url(QueueName=qname)["QueueUrl"]
+ sqs.delete_queue(QueueUrl=url)
+ template = copy.deepcopy(adopted[3])
+ template["Resources"]["Q"], to_import = _import_queue(qname, url)
+ cs = _create(cfn, adopted, template, to_import)
+ assert _failed(cfn, adopted, cs) == (
+ f"Resource of type 'AWS::SQS::Queue' with identifier '{url}' was not found.")
+ finally:
+ _drop_stack(cfn, owner)
+
+
+def test_a_queue_retained_by_a_deleted_stack_can_be_imported(cfn, sqs, adopted):
+ qname = f"imp-kept-{uuid.uuid4().hex[:10]}"
+ owner = _owner_stack(cfn, qname, retain=True)
+ try:
+ _drop_stack(cfn, owner)
+ url = sqs.get_queue_url(QueueName=qname)["QueueUrl"]
+ template = copy.deepcopy(adopted[3])
+ template["Resources"]["Q"], to_import = _import_queue(qname, url)
+ cs = _create(cfn, adopted, template, to_import)
+ assert cs["Status"] == "CREATE_COMPLETE"
+ assert [c["ResourceChange"]["Action"] for c in cs["Changes"]] == ["Import"]
+ finally:
+ _drop_stack(cfn, owner)
+ sqs.delete_queue(QueueUrl=sqs.get_queue_url(QueueName=qname)["QueueUrl"])
+
+
+# --- refused at creation ---
+
+def test_import_without_resources_to_import_is_refused(cfn, adopted):
+ stack, _url, _pname, template, _to_import = adopted
+ with pytest.raises(ClientError) as exc:
+ cfn.create_change_set(StackName=stack, ChangeSetName="imp", ChangeSetType="IMPORT",
+ TemplateBody=json.dumps(template))
+ assert exc.value.response["Error"]["Code"] == "ValidationError"
+ assert exc.value.response["Error"]["Message"] == "Must Provide at least one resource to import"
+ assert cfn.list_change_sets(StackName=stack)["Summaries"] == []
+
+
+def _mismatch(listed, declared):
+ return ("Resource type of [Q] passed in ResourceToImport does not match with resource type defined "
+ f"in the template. Resource type in ResourceToImport: {listed}; Resource type in Template: "
+ f"{declared}.")
+
+
+_ENTRY_FAULTS = {
+ "missing-resource":
+ "The logical resource ids [Missing] provided in ResourceToImport do not exist in the template.",
+ "wrong-key": "Invalid resource identifier for resource type AWS::SNS::Topic. Expected [TopicArn]",
+ "type-mismatch": _mismatch("AWS::SNS::Topic", "AWS::SQS::Queue"),
+ "unlooked-type-mismatch": _mismatch("AWS::IAM::Role", "AWS::SQS::Queue"),
+ "missing-identifier": (
+ "1 validation error detected: Value null at 'resourcesToImport.1.member.resourceIdentifier' "
+ "failed to satisfy constraint: Member must not be null"),
+ "duplicate": "Every resource to import must have unique LogicalResourceId",
+ "queue-name-key": "Invalid resource identifier for resource type AWS::SQS::Queue. Expected [QueueUrl]",
+ "extra-key": "Invalid resource identifier for resource type AWS::SQS::Queue. Expected [QueueUrl]",
+}
+
+
[email protected]("fault", list(_ENTRY_FAULTS))
+def test_invalid_import_entries_are_rejected(cfn, adopted, fault):
+ _stack, _url, _pname, template, to_import = adopted
+ if fault == "missing-resource":
+ to_import[0]["LogicalResourceId"] = "Missing"
+ elif fault == "wrong-key":
+ # Two faults: AWS names the identifier key before the type.
+ to_import[0]["ResourceType"] = "AWS::SNS::Topic"
+ elif fault == "type-mismatch":
+ to_import[0].update(ResourceType="AWS::SNS::Topic",
+ ResourceIdentifier={"TopicArn": "arn:aws:sns:us-east-1:000000000000:t"})
+ elif fault == "unlooked-type-mismatch":
+ to_import[0].update(ResourceType="AWS::IAM::Role", ResourceIdentifier={"RoleName": "r"})
+ elif fault == "missing-identifier":
+ to_import[0]["ResourceIdentifier"] = {}
+ elif fault == "queue-name-key":
+ to_import[0]["ResourceIdentifier"] = {"QueueName": template["Resources"]["Q"]["Properties"]["QueueName"]}
+ elif fault == "extra-key":
+ to_import[0]["ResourceIdentifier"]["Bogus"] = "x"
+ else:
+ to_import.append(copy.deepcopy(to_import[0]))
+ assert _refusal(cfn, adopted, template, to_import) == _ENTRY_FAULTS[fault]
+
+
+def _with_x(template, value):
+ changed = copy.deepcopy(template)
+ changed["Resources"]["X"]["Properties"]["Value"] = value
+ return changed
+
+
+def _with_n(template, pname):
+ changed = copy.deepcopy(template)
+ changed["Resources"]["N"] = {"Type": "AWS::SSM::Parameter", "DeletionPolicy": "Retain",
+ "Properties": {"Type": "String", "Name": pname + "-new", "Value": "m"}}
+ return changed
+
+
+def test_import_that_also_adds_a_resource_is_refused(cfn, adopted):
+ _stack, _url, pname, template, _to_import = adopted
+ assert _refusal(cfn, adopted, _with_n(template, pname)) == (
+ "Resources [N] is missing from ResourceToImport list")
+
+
+def test_import_that_modifies_a_stack_resource_is_refused(cfn, adopted):
+ template = adopted[3]
+ assert _refusal(cfn, adopted, _with_x(template, "changed")) == _MODIFIED
+
+
+def test_import_that_removes_a_stack_resource_is_refused(cfn, adopted):
+ template = copy.deepcopy(adopted[3])
+ del template["Resources"]["X"]
+ assert _refusal(cfn, adopted, template) == _MODIFIED
+
+
+def test_import_that_renames_a_stack_queue_is_refused_and_keeps_the_queue(cfn, sqs, adopted):
+ # Executing such a set once created the renamed queue and left the
+ # original outside the stack; AWS refuses the set at creation.
+ _stack, url, _pname, _template, to_import = adopted
+ kept = f"imp-held-{uuid.uuid4().hex[:10]}"
+ owner = _owner_stack(cfn, kept)
+ try:
+ held = cfn.describe_stack_resources(StackName=owner)["StackResources"]
+ queue = sqs.get_queue_url(QueueName=kept)["QueueUrl"]
+ template = {"Resources": {
+ "K": {"Type": "AWS::SQS::Queue", "Properties": {"QueueName": kept + "-renamed"}},
+ "Q": copy.deepcopy(adopted[3]["Resources"]["Q"]),
+ }}
+ with pytest.raises(ClientError) as exc:
+ cfn.create_change_set(StackName=owner, ChangeSetName="imp", ChangeSetType="IMPORT",
+ ResourcesToImport=to_import, TemplateBody=json.dumps(template))
+ assert (exc.value.response["Error"]["Code"], exc.value.response["Error"]["Message"]) == (
+ "ValidationError", _MODIFIED.replace("[X]", "[K]"))
+ assert cfn.list_change_sets(StackName=owner)["Summaries"] == []
+ assert cfn.describe_stack_resources(StackName=owner)["StackResources"] == held
+ assert sqs.get_queue_url(QueueName=kept)["QueueUrl"] == queue
+ with pytest.raises(ClientError) as exc:
+ sqs.get_queue_url(QueueName=kept + "-renamed")
+ assert exc.value.response["Error"]["Code"] == "AWS.SimpleQueueService.NonExistentQueue"
+ finally:
+ _drop_stack(cfn, owner)
+
+
+def test_an_unimported_add_is_reported_before_a_modification(cfn, adopted):
+ _stack, _url, pname, template, _to_import = adopted
+ assert _refusal(cfn, adopted, _with_n(_with_x(template, "changed"), pname)) == (
+ "Resources [N] is missing from ResourceToImport list")
+
+
+def test_import_without_a_new_resource_is_refused(cfn, adopted):
+ _stack, _url, pname, template, _to_import = adopted
+ del template["Resources"]["Q"]
+ to_import = [{"ResourceType": "AWS::SSM::Parameter", "LogicalResourceId": "X",
+ "ResourceIdentifier": {"Name": pname}}]
+ assert _refusal(cfn, adopted, template, to_import) == (
+ "The template should contain at least one new resource to import.")
+
+
+def test_import_of_a_resource_already_in_the_stack_is_refused(cfn, adopted):
+ _stack, _url, pname, template, _to_import = adopted
+ del template["Resources"]["Q"]
+ to_import = [{"ResourceType": "AWS::SSM::Parameter", "LogicalResourceId": "X",
+ "ResourceIdentifier": {"Name": pname}}]
+ assert _refusal(cfn, adopted, _with_n(template, pname), to_import) == (
+ "Resources [X] passed in ResourceToImport are already in a stack and cannot be imported.")
+
+
[email protected]("ghost", [False, True])
+def test_import_without_deletion_policy_is_refused(cfn, adopted, ghost):
+ # Refused at creation even when the queue does not exist: the missing
+ # DeletionPolicy is found before the lookup that would fail the set.
+ _stack, url, _pname, template, to_import = adopted
+ del template["Resources"]["Q"]["DeletionPolicy"]
+ if ghost:
+ to_import[0]["ResourceIdentifier"]["QueueUrl"] = url + "-ghost"
+ assert _refusal(cfn, adopted, template, to_import) == (
+ "The following resources to import [Q] must have DeletionPolicy attribute specified in the template.")
+
+
+# --- a stack that does not exist yet ---
+
+def fresh(cfn, sqs):
+ """A new stack name and a queue outside every stack, to import into it."""
+ tag = uuid.uuid4().hex[:10]
+ qname, stack = f"imp-fresh-{tag}", f"imp-fresh-{tag}"
+ url = sqs.create_queue(QueueName=qname)["QueueUrl"]
+ try:
+ yield stack, qname, url
+ finally:
+ _drop_stack(cfn, stack)
+ sqs.delete_queue(QueueUrl=url)
+
+
+def _stack_row(cfn, stack):
+ s = cfn.describe_stacks(StackName=stack)["Stacks"][0]
+ return s["StackStatus"], s.get("StackStatusReason"), s.get("ChangeSetId")
+
+
+def test_import_into_a_new_stack_creates_it_for_review(cfn, fresh):
+ stack, qname, url = fresh
+ template, to_import = _import_queue(qname, url)
+ body = json.dumps({"Resources": {"Q": template}})
+ cs_id = cfn.create_change_set(StackName=stack, ChangeSetName="imp", ChangeSetType="IMPORT",
+ ResourcesToImport=to_import, TemplateBody=body)["Id"]
+ cs = cfn.describe_change_set(ChangeSetName=cs_id)
+ # AWS answers AVAILABLE and executes the import; the emulator refuses it by design.
+ assert (cs["Status"], cs["ExecutionStatus"]) == ("CREATE_COMPLETE", "UNAVAILABLE")
+ assert cs["Changes"] == [{"Type": "Resource", "ResourceChange": {
+ "Action": "Import", "LogicalResourceId": "Q", "PhysicalResourceId": url,
+ "ResourceType": "AWS::SQS::Queue", "Scope": [], "Details": []}}]
+ assert _stack_row(cfn, stack) == ("REVIEW_IN_PROGRESS", "User Initiated", None)
+ assert cfn.describe_stack_resources(StackName=stack)["StackResources"] == []
+ events = cfn.describe_stack_events(StackName=stack)["StackEvents"]
+ assert [(e["ResourceType"], e["ResourceStatus"], e.get("ResourceStatusReason")) for e in events] == [
+ ("AWS::CloudFormation::Stack", "REVIEW_IN_PROGRESS", "User Initiated")]
+ assert [s["ChangeSetName"] for s in cfn.list_change_sets(StackName=stack)["Summaries"]] == ["imp"]
+ with pytest.raises(ClientError) as exc:
+ cfn.execute_change_set(ChangeSetName=cs_id)
+ # AWS executes the import; the emulator refuses it by design.
+ assert exc.value.response["Error"]["Code"] == "InvalidChangeSetStatus"
+ # A second import set against the stack under review is accepted too, and
+ # deleting the change sets leaves the stack for DeleteStack to remove.
+ again = cfn.create_change_set(StackName=stack, ChangeSetName="again", ChangeSetType="IMPORT",
+ ResourcesToImport=to_import, TemplateBody=body)["Id"]
+ assert cfn.describe_change_set(ChangeSetName=again)["Status"] == "CREATE_COMPLETE"
+ cfn.delete_change_set(ChangeSetName=cs_id)
+ cfn.delete_change_set(ChangeSetName=again)
+ assert _stack_row(cfn, stack) == ("REVIEW_IN_PROGRESS", "User Initiated", None)
+ _drop_stack(cfn, stack)
+ with pytest.raises(ClientError) as exc:
+ cfn.describe_stacks(StackName=stack)
+ assert exc.value.response["Error"]["Message"] == f"Stack with id {stack} does not exist"
+
+
[email protected]("fault", ["unlisted", "no-deletion-policy"])
+def test_refused_import_into_a_new_stack_leaves_no_stack(cfn, fresh, fault):
+ stack, qname, url = fresh
+ template, to_import = _import_queue(qname, url)
+ resources = {"Q": template}
+ if fault == "unlisted":
+ resources["X"] = {"Type": "AWS::SSM::Parameter",
+ "Properties": {"Type": "String", "Name": f"/{stack}", "Value": "v"}}
+ message = "Resources [X] is missing from ResourceToImport list"
+ else:
+ del template["DeletionPolicy"]
+ message = ("The following resources to import [Q] must have DeletionPolicy attribute specified "
+ "in the template.")
+ with pytest.raises(ClientError) as exc:
+ cfn.create_change_set(StackName=stack, ChangeSetName="imp", ChangeSetType="IMPORT",
+ ResourcesToImport=to_import, TemplateBody=json.dumps({"Resources": resources}))
+ assert (exc.value.response["Error"]["Code"], exc.value.response["Error"]["Message"]) == (
+ "ValidationError", message)
+ with pytest.raises(ClientError) as exc:
+ cfn.describe_stacks(StackName=stack)
+ assert exc.value.response["Error"]["Message"] == f"Stack with id {stack} does not exist"
+
+
+def test_failed_import_into_a_new_stack_leaves_the_stack_for_review(cfn, fresh):
+ stack, qname, url = fresh
+ template, to_import = _import_queue(qname + "-ghost", url + "-ghost")
+ cs_id = cfn.create_change_set(StackName=stack, ChangeSetName="imp", ChangeSetType="IMPORT",
+ ResourcesToImport=to_import,
+ TemplateBody=json.dumps({"Resources": {"Q": template}}))["Id"]
+ cs = cfn.describe_change_set(ChangeSetName=cs_id)
+ assert (cs["Status"], cs["ExecutionStatus"], cs["Changes"]) == ("FAILED", "UNAVAILABLE", [])
+ assert cs["StatusReason"] == (
+ f"Resource of type 'AWS::SQS::Queue' with identifier '{url}-ghost' was not found.")
+ assert _stack_row(cfn, stack) == ("REVIEW_IN_PROGRESS", "User Initiated", None)
+
+
+# --- execution stays refused and harmless ---
+
[email protected]("by_arn", [False, True])
+def test_repeated_execution_preserves_stack_and_service_resources(cfn, sqs, ssm, adopted, by_arn):
+ stack, url, pname, _template, _to_import = adopted
+ cs = _create(cfn, adopted)
+ before = cfn.describe_stacks(StackName=stack)["Stacks"]
+ resources = cfn.describe_stack_resources(StackName=stack)["StackResources"]
+ events = cfn.describe_stack_events(StackName=stack)["StackEvents"]
+ parameter = ssm.get_parameter(Name=pname)["Parameter"]
+ template = cfn.get_template(StackName=stack)["TemplateBody"]
+ target = {"ChangeSetName": cs["ChangeSetId"]} if by_arn else {"StackName": stack, "ChangeSetName": "imp"}
+ for _ in range(2):
+ with pytest.raises(ClientError) as exc:
+ cfn.execute_change_set(**target)
+ # AWS executes the import; the emulator refuses it by design.
+ assert exc.value.response["Error"]["Code"] == "InvalidChangeSetStatus"
+ assert "import" in exc.value.response["Error"]["Message"].lower()
+ assert cfn.describe_stacks(StackName=stack)["Stacks"] == before
+ assert cfn.describe_stack_resources(StackName=stack)["StackResources"] == resources
+ assert cfn.describe_stack_events(StackName=stack)["StackEvents"] == events
+ assert cfn.get_template(StackName=stack)["TemplateBody"] == template
+ assert ssm.get_parameter(Name=pname)["Parameter"] == parameter
+ attrs = sqs.get_queue_attributes(QueueUrl=url, AttributeNames=["VisibilityTimeout"])["Attributes"]
+ assert attrs["VisibilityTimeout"] == "17"
+ assert sqs.receive_message(QueueUrl=url)["Messages"][0]["Body"] == "keep this message"