Best Python code snippet using localstack_python
test_cloudformation.py
Source:test_cloudformation.py
...29def stream_exists(name):30 kinesis_client = aws_stack.connect_to_service('kinesis')31 streams = kinesis_client.list_streams()32 return name in streams['StreamNames']33def get_stack_details(stack_name):34 cloudformation = aws_stack.connect_to_service('cloudformation')35 stacks = cloudformation.describe_stacks(StackName=stack_name)36 for stack in stacks['Stacks']:37 if stack['StackName'] == stack_name:38 return stack39def describe_stack_resource(stack_name, resource_logical_id):40 cloudformation = aws_stack.connect_to_service('cloudformation')41 response = cloudformation.describe_stack_resources(StackName=stack_name)42 for resource in response['StackResources']:43 if resource['LogicalResourceId'] == resource_logical_id:44 return resource45def list_stack_resources(stack_name):46 cloudformation = aws_stack.connect_to_service('cloudformation')47 response = cloudformation.list_stack_resources(StackName=stack_name)48 return response['StackResourceSummaries']49def get_queue_urls():50 sqs = aws_stack.connect_to_service('sqs')51 response = sqs.list_queues()52 return response['QueueUrls']53def get_topic_arns():54 sqs = aws_stack.connect_to_service('sns')55 response = sqs.list_topics()56 return [t['TopicArn'] for t in response['Topics']]57class CloudFormationTest(unittest.TestCase):58 def test_apply_template(self):59 cloudformation = aws_stack.connect_to_resource('cloudformation')60 template = template_deployer.template_to_json(load_file(TEST_TEMPLATE_1))61 # deploy template62 cloudformation.create_stack(StackName=TEST_STACK_NAME, TemplateBody=template)63 # wait for deployment to finish64 def check_stack():65 stack = get_stack_details(TEST_STACK_NAME)66 assert stack['StackStatus'] == 'CREATE_COMPLETE'67 retry(check_stack, retries=3, sleep=2)68 # assert that bucket has been created69 assert bucket_exists('cf-test-bucket-1')70 # assert that queue has been created71 assert queue_exists('cf-test-queue-1')72 # assert that stream has been created73 assert stream_exists('cf-test-stream-1')74 # assert that queue has been created75 resource = describe_stack_resource(TEST_STACK_NAME, 'SQSQueueNoNameProperty')76 assert queue_exists(resource['PhysicalResourceId'])77 def test_validate_template(self):78 cloudformation = aws_stack.connect_to_service('cloudformation')79 template = template_deployer.template_to_json(load_file(TEST_TEMPLATE_1))80 response = cloudformation.validate_template(TemplateBody=template)81 assert response['ResponseMetadata']['HTTPStatusCode'] == 20082 def test_validate_invalid_json_template_should_fail(self):83 cloudformation = aws_stack.connect_to_service('cloudformation')84 invalid_json = '{"this is invalid JSON"="bobbins"}'85 try:86 cloudformation.validate_template(TemplateBody=invalid_json)87 self.fail('Should raise ValidationError')88 except (ClientError, ResponseParserError) as err:89 if isinstance(err, ClientError):90 self.assertEqual(err.response['ResponseMetadata']['HTTPStatusCode'], 400)91 self.assertEqual(err.response['Error']['Message'], 'Template Validation Error')92 def test_list_stack_resources_returns_queue_urls(self):93 cloudformation = aws_stack.connect_to_resource('cloudformation')94 template = template_deployer.template_to_json(load_file(TEST_TEMPLATE_2))95 cloudformation.create_stack(StackName=TEST_STACK_NAME_2, TemplateBody=template)96 def check_stack():97 stack = get_stack_details(TEST_STACK_NAME_2)98 assert stack['StackStatus'] == 'CREATE_COMPLETE'99 retry(check_stack, retries=3, sleep=2)100 list_stack_summaries = list_stack_resources(TEST_STACK_NAME_2)101 queue_urls = get_queue_urls()102 topic_arns = get_topic_arns()103 stack_queues = [r for r in list_stack_summaries if r['ResourceType'] == 'AWS::SQS::Queue']104 for resource in stack_queues:105 url = aws_stack.get_sqs_queue_url(resource['PhysicalResourceId'])106 self.assertIn(url, queue_urls)107 stack_topics = [r for r in list_stack_summaries if r['ResourceType'] == 'AWS::SNS::Topic']108 for resource in stack_topics:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!