| 
									
										
										
										
											2020-09-13 16:08:23 +01:00
										 |  |  | import boto3 | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2021-10-18 19:44:29 +00:00
										 |  |  | import sure  # noqa # pylint: disable=unused-import | 
					
						
							| 
									
										
										
										
											2020-09-13 16:08:23 +01:00
										 |  |  | from moto import mock_sqs, mock_cloudformation | 
					
						
							| 
									
										
										
										
											2022-08-13 09:49:43 +00:00
										 |  |  | from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  | from string import Template | 
					
						
							|  |  |  | from random import randint | 
					
						
							|  |  |  | from uuid import uuid4 | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  | simple_queue = Template( | 
					
						
							|  |  |  |     """{
 | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  |     "AWSTemplateFormatVersion": "2010-09-09", | 
					
						
							|  |  |  |     "Resources": { | 
					
						
							|  |  |  |         "QueueGroup": { | 
					
						
							|  |  |  |             "Type": "AWS::SQS::Queue", | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |             "Properties": {"QueueName": $q_name, "VisibilityTimeout": 60}, | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  | }"""
 | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-13 16:08:23 +01:00
										 |  |  | sqs_template_with_tags = """
 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     "AWSTemplateFormatVersion": "2010-09-09", | 
					
						
							|  |  |  |     "Resources": { | 
					
						
							|  |  |  |         "SQSQueue": { | 
					
						
							|  |  |  |             "Type": "AWS::SQS::Queue", | 
					
						
							|  |  |  |             "Properties": { | 
					
						
							|  |  |  |                 "Tags" : [ | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         "Key" : "keyname1", | 
					
						
							|  |  |  |                         "Value" : "value1" | 
					
						
							|  |  |  |                     }, | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         "Key" : "keyname2", | 
					
						
							|  |  |  |                         "Value" : "value2" | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 ] | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }"""
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | @mock_sqs | 
					
						
							|  |  |  | @mock_cloudformation | 
					
						
							|  |  |  | def test_describe_stack_subresources(): | 
					
						
							|  |  |  |     res = boto3.resource("cloudformation", region_name="us-east-1") | 
					
						
							|  |  |  |     cf = boto3.client("cloudformation", region_name="us-east-1") | 
					
						
							|  |  |  |     client = boto3.client("sqs", region_name="us-east-1") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     stack_name = str(uuid4())[0:6] | 
					
						
							|  |  |  |     q_name = str(uuid4())[0:6] | 
					
						
							|  |  |  |     template_body = simple_queue.substitute(q_name=q_name) | 
					
						
							|  |  |  |     cf.create_stack(StackName=stack_name, TemplateBody=template_body) | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 20:10:46 -01:00
										 |  |  |     queue_urls = client.list_queues(QueueNamePrefix=q_name)["QueueUrls"] | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |     assert any([f"{ACCOUNT_ID}/{q_name}" in url for url in queue_urls]) | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     stack = res.Stack(stack_name) | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  |     for s in stack.resource_summaries.all(): | 
					
						
							|  |  |  |         s.resource_type.should.equal("AWS::SQS::Queue") | 
					
						
							|  |  |  |         s.logical_id.should.equal("QueueGroup") | 
					
						
							| 
									
										
										
										
											2022-02-15 22:26:36 -01:00
										 |  |  |         s.physical_resource_id.should.contain(f"/{q_name}") | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_sqs | 
					
						
							|  |  |  | @mock_cloudformation | 
					
						
							|  |  |  | def test_list_stack_resources(): | 
					
						
							|  |  |  |     cf = boto3.client("cloudformation", region_name="us-east-1") | 
					
						
							|  |  |  |     client = boto3.client("sqs", region_name="us-east-1") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     stack_name = str(uuid4())[0:6] | 
					
						
							|  |  |  |     q_name = str(uuid4())[0:6] | 
					
						
							|  |  |  |     template_body = simple_queue.substitute(q_name=q_name) | 
					
						
							|  |  |  |     cf.create_stack(StackName=stack_name, TemplateBody=template_body) | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 20:10:46 -01:00
										 |  |  |     queue_urls = client.list_queues(QueueNamePrefix=q_name)["QueueUrls"] | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |     assert any([f"{ACCOUNT_ID}/{q_name}" in url for url in queue_urls]) | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     queue = cf.list_stack_resources(StackName=stack_name)["StackResourceSummaries"][0] | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     queue.should.have.key("ResourceType").equal("AWS::SQS::Queue") | 
					
						
							|  |  |  |     queue.should.have.key("LogicalResourceId").should.equal("QueueGroup") | 
					
						
							| 
									
										
										
										
											2022-02-15 22:26:36 -01:00
										 |  |  |     expected_url = f"https://sqs.us-east-1.amazonaws.com/{ACCOUNT_ID}/{q_name}" | 
					
						
							|  |  |  |     queue.should.have.key("PhysicalResourceId").should.equal(expected_url) | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-13 16:08:23 +01:00
										 |  |  | @mock_sqs | 
					
						
							|  |  |  | @mock_cloudformation | 
					
						
							|  |  |  | def test_create_from_cloudformation_json_with_tags(): | 
					
						
							|  |  |  |     cf = boto3.client("cloudformation", region_name="us-east-1") | 
					
						
							|  |  |  |     client = boto3.client("sqs", region_name="us-east-1") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     stack_name = str(uuid4())[0:6] | 
					
						
							|  |  |  |     cf.create_stack(StackName=stack_name, TemplateBody=sqs_template_with_tags) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     response = cf.describe_stack_resources(StackName=stack_name) | 
					
						
							| 
									
										
										
										
											2022-02-15 22:26:36 -01:00
										 |  |  |     q_name = response["StackResources"][0]["PhysicalResourceId"].split("/")[-1] | 
					
						
							| 
									
										
										
										
											2020-09-13 16:08:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 20:10:46 -01:00
										 |  |  |     all_urls = client.list_queues(QueueNamePrefix=q_name)["QueueUrls"] | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     queue_url = [url for url in all_urls if url.endswith(q_name)][0] | 
					
						
							| 
									
										
										
										
											2020-09-13 16:08:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     queue_tags = client.list_queue_tags(QueueUrl=queue_url)["Tags"] | 
					
						
							|  |  |  |     queue_tags.should.equal({"keyname1": "value1", "keyname2": "value2"}) | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_cloudformation | 
					
						
							|  |  |  | @mock_sqs | 
					
						
							|  |  |  | def test_update_stack(): | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     q_name = str(uuid4())[0:6] | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  |     sqs_template = { | 
					
						
							|  |  |  |         "AWSTemplateFormatVersion": "2010-09-09", | 
					
						
							|  |  |  |         "Resources": { | 
					
						
							|  |  |  |             "QueueGroup": { | 
					
						
							|  |  |  |                 "Type": "AWS::SQS::Queue", | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |                 "Properties": {"QueueName": q_name, "VisibilityTimeout": 60}, | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     sqs_template_json = json.dumps(sqs_template) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     cf = boto3.client("cloudformation", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     stack_name = str(uuid4())[0:6] | 
					
						
							|  |  |  |     cf.create_stack(StackName=stack_name, TemplateBody=sqs_template_json) | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     client = boto3.client("sqs", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     queues = client.list_queues(QueueNamePrefix=q_name)["QueueUrls"] | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  |     queues.should.have.length_of(1) | 
					
						
							|  |  |  |     attrs = client.get_queue_attributes(QueueUrl=queues[0], AttributeNames=["All"])[ | 
					
						
							|  |  |  |         "Attributes" | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |     attrs["VisibilityTimeout"].should.equal("60") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # when updating | 
					
						
							|  |  |  |     sqs_template["Resources"]["QueueGroup"]["Properties"]["VisibilityTimeout"] = 100 | 
					
						
							|  |  |  |     sqs_template_json = json.dumps(sqs_template) | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     cf.update_stack(StackName=stack_name, TemplateBody=sqs_template_json) | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # then the attribute should be updated | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     queues = client.list_queues(QueueNamePrefix=q_name)["QueueUrls"] | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  |     queues.should.have.length_of(1) | 
					
						
							|  |  |  |     attrs = client.get_queue_attributes(QueueUrl=queues[0], AttributeNames=["All"])[ | 
					
						
							|  |  |  |         "Attributes" | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |     attrs["VisibilityTimeout"].should.equal("100") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_cloudformation | 
					
						
							|  |  |  | @mock_sqs | 
					
						
							|  |  |  | def test_update_stack_and_remove_resource(): | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     q_name = str(uuid4())[0:6] | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  |     sqs_template = { | 
					
						
							|  |  |  |         "AWSTemplateFormatVersion": "2010-09-09", | 
					
						
							|  |  |  |         "Resources": { | 
					
						
							|  |  |  |             "QueueGroup": { | 
					
						
							|  |  |  |                 "Type": "AWS::SQS::Queue", | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |                 "Properties": {"QueueName": q_name, "VisibilityTimeout": 60}, | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     sqs_template_json = json.dumps(sqs_template) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     cf = boto3.client("cloudformation", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     stack_name = str(uuid4())[0:6] | 
					
						
							|  |  |  |     cf.create_stack(StackName=stack_name, TemplateBody=sqs_template_json) | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     client = boto3.client("sqs", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     client.list_queues(QueueNamePrefix=q_name)["QueueUrls"].should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     sqs_template["Resources"].pop("QueueGroup") | 
					
						
							|  |  |  |     sqs_template_json = json.dumps(sqs_template) | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     cf.update_stack(StackName=stack_name, TemplateBody=sqs_template_json) | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     # No queues exist, so the key is not passed through | 
					
						
							|  |  |  |     client.list_queues(QueueNamePrefix=q_name).shouldnt.have.key("QueueUrls") | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_cloudformation | 
					
						
							|  |  |  | @mock_sqs | 
					
						
							|  |  |  | def test_update_stack_and_add_resource(): | 
					
						
							|  |  |  |     sqs_template = {"AWSTemplateFormatVersion": "2010-09-09", "Resources": {}} | 
					
						
							|  |  |  |     sqs_template_json = json.dumps(sqs_template) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     q_name = str(uuid4())[0:6] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  |     cf = boto3.client("cloudformation", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     stack_name = str(uuid4())[0:6] | 
					
						
							|  |  |  |     cf.create_stack(StackName=stack_name, TemplateBody=sqs_template_json) | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     client = boto3.client("sqs", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     client.list_queues(QueueNamePrefix=q_name).shouldnt.have.key("QueueUrls") | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     sqs_template = { | 
					
						
							|  |  |  |         "AWSTemplateFormatVersion": "2010-09-09", | 
					
						
							|  |  |  |         "Resources": { | 
					
						
							|  |  |  |             "QueueGroup": { | 
					
						
							|  |  |  |                 "Type": "AWS::SQS::Queue", | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |                 "Properties": {"QueueName": q_name, "VisibilityTimeout": 60}, | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     sqs_template_json = json.dumps(sqs_template) | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     cf.update_stack(StackName=stack_name, TemplateBody=sqs_template_json) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     client.list_queues(QueueNamePrefix=q_name)["QueueUrls"].should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_sqs | 
					
						
							|  |  |  | @mock_cloudformation | 
					
						
							|  |  |  | def test_create_queue_passing_integer_as_name(): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Passing the queue name as an Integer in the CloudFormation template | 
					
						
							|  |  |  |     This works in AWS - it simply converts the integer to a string, and treats it as any other name | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     cf = boto3.client("cloudformation", region_name="us-east-1") | 
					
						
							|  |  |  |     client = boto3.client("sqs", region_name="us-east-1") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     stack_name = str(uuid4())[0:6] | 
					
						
							|  |  |  |     q_name = f"{randint(10000000, 99999999)}" | 
					
						
							|  |  |  |     template_body = simple_queue.substitute(q_name=q_name) | 
					
						
							|  |  |  |     cf.create_stack(StackName=stack_name, TemplateBody=template_body) | 
					
						
							| 
									
										
										
										
											2021-10-04 13:47:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     queue_urls = client.list_queues(QueueNamePrefix=q_name[0:6])["QueueUrls"] | 
					
						
							|  |  |  |     queue_urls.should.have.length_of(1) |