Best Python code snippet using localstack_python
test_cloudformation_dynamic_parameters.py
Source:test_cloudformation_dynamic_parameters.py
...43 ][0]44 assert topic_name == parameter_value45 finally:46 cleanup_changesets([change_set_id])47 cleanup_stacks([stack_id])48def test_resolve_ssm_withversion(49 ssm_client,50 cfn_client,51 is_change_set_created_and_available,52 is_stack_created,53 cleanup_changesets,54 cleanup_stacks,55 create_parameter,56):57 stack_name = f"stack-{short_uid()}"58 change_set_name = f"change-set-{short_uid()}"59 parameter_key = f"param-key-{short_uid()}"60 parameter_value_v0 = f"param-value-{short_uid()}"61 parameter_value_v1 = f"param-value-{short_uid()}"62 parameter_value_v2 = f"param-value-{short_uid()}"63 create_parameter(Name=parameter_key, Type="String", Value=parameter_value_v0)64 v1 = ssm_client.put_parameter(65 Name=parameter_key, Overwrite=True, Type="String", Value=parameter_value_v166 )67 ssm_client.put_parameter(68 Name=parameter_key, Overwrite=True, Type="String", Value=parameter_value_v269 )70 template_rendered = jinja2.Template(load_template_raw("resolve_ssm_withversion.yaml")).render(71 parameter_key=parameter_key, parameter_version=str(v1["Version"])72 )73 response = cfn_client.create_change_set(74 StackName=stack_name,75 ChangeSetName=change_set_name,76 TemplateBody=template_rendered,77 ChangeSetType="CREATE",78 )79 change_set_id = response["Id"]80 stack_id = response["StackId"]81 try:82 wait_until(is_change_set_created_and_available(change_set_id))83 cfn_client.execute_change_set(ChangeSetName=change_set_id)84 wait_until(is_stack_created(stack_id))85 describe_result = cfn_client.describe_stacks(StackName=stack_id)["Stacks"][0]86 assert describe_result["StackStatus"] == "CREATE_COMPLETE"87 topic_name = [88 o["OutputValue"] for o in describe_result["Outputs"] if o["OutputKey"] == "TopicName"89 ][0]90 assert topic_name == parameter_value_v191 finally:92 cleanup_changesets([change_set_id])93 cleanup_stacks([stack_id])94def test_resolve_ssm_secure(95 ssm_client,96 cfn_client,97 is_change_set_created_and_available,98 is_stack_created,99 cleanup_changesets,100 cleanup_stacks,101 create_parameter,102):103 stack_name = f"stack-{short_uid()}"104 change_set_name = f"change-set-{short_uid()}"105 parameter_key = f"param-key-{short_uid()}"106 parameter_value = f"param-value-{short_uid()}"107 create_parameter(Name=parameter_key, Value=parameter_value, Type="SecureString")108 template_rendered = jinja2.Template(load_template_raw("resolve_ssm_secure.yaml")).render(109 parameter_key=parameter_key,110 )111 response = cfn_client.create_change_set(112 StackName=stack_name,113 ChangeSetName=change_set_name,114 TemplateBody=template_rendered,115 ChangeSetType="CREATE",116 )117 change_set_id = response["Id"]118 stack_id = response["StackId"]119 try:120 wait_until(is_change_set_created_and_available(change_set_id))121 cfn_client.execute_change_set(ChangeSetName=change_set_id)122 wait_until(is_stack_created(stack_id))123 describe_result = cfn_client.describe_stacks(StackName=stack_id)["Stacks"][0]124 assert describe_result["StackStatus"] == "CREATE_COMPLETE"125 topic_name = [126 o["OutputValue"] for o in describe_result["Outputs"] if o["OutputKey"] == "TopicName"127 ][0]128 assert topic_name == parameter_value129 finally:130 cleanup_changesets([change_set_id])131 cleanup_stacks([stack_id])132@pytest.mark.parametrize(133 "template_name", ["resolve_secretsmanager_full.yaml", "resolve_secretsmanager.yaml"]134)135def test_resolve_secretsmanager(136 secretsmanager_client,137 cfn_client,138 is_change_set_created_and_available,139 is_stack_created,140 create_secret,141 create_parameter,142 cleanup_changesets,143 cleanup_stacks,144 template_name,145):146 stack_name = f"stack-{short_uid()}"147 change_set_name = f"change-set-{short_uid()}"148 parameter_key = f"param-key-{short_uid()}"149 parameter_value = f"param-value-{short_uid()}"150 create_secret(Name=parameter_key, SecretString=parameter_value)151 template_rendered = jinja2.Template(load_template_raw(template_name)).render(152 parameter_key=parameter_key,153 )154 response = cfn_client.create_change_set(155 StackName=stack_name,156 ChangeSetName=change_set_name,157 TemplateBody=template_rendered,158 ChangeSetType="CREATE",159 )160 change_set_id = response["Id"]161 stack_id = response["StackId"]162 try:163 wait_until(is_change_set_created_and_available(change_set_id))164 cfn_client.execute_change_set(ChangeSetName=change_set_id)165 wait_until(is_stack_created(stack_id))166 describe_result = cfn_client.describe_stacks(StackName=stack_id)["Stacks"][0]167 assert describe_result["StackStatus"] == "CREATE_COMPLETE"168 topic_name = [169 o["OutputValue"] for o in describe_result["Outputs"] if o["OutputKey"] == "TopicName"170 ][0]171 assert topic_name == parameter_value172 finally:173 cleanup_changesets([change_set_id])...
test_cloudformation_resource_sns.py
Source:test_cloudformation_resource_sns.py
...36 topic_arns = [t["TopicArn"] for t in topics]37 assert len([t for t in topic_arns if topic_name in t]) == 138 finally:39 cleanup_changesets([change_set_id])40 cleanup_stacks([stack_id])41def test_sns_topic_fifo_without_suffix_fails(42 cfn_client,43 sns_client,44 cleanup_stacks,45 cleanup_changesets,46 is_change_set_created_and_available,47 is_stack_created,48):49 """topic name needs .fifo suffix to be valid"""50 stack_name = f"stack-{short_uid()}"51 change_set_name = f"change-set-{short_uid()}"52 topic_name = f"topic-{short_uid()}"53 template_rendered = jinja2.Template(load_template_raw("sns_topic_fifo_dedup.yaml")).render(54 sns_topic=topic_name55 )56 response = cfn_client.create_change_set(57 StackName=stack_name,58 ChangeSetName=change_set_name,59 TemplateBody=template_rendered,60 ChangeSetType="CREATE",61 )62 change_set_id = response["Id"]63 stack_id = response["StackId"]64 try:65 wait_until(is_change_set_created_and_available(change_set_id))66 cfn_client.execute_change_set(ChangeSetName=change_set_id)67 wait_until(is_stack_created(stack_id))68 stack = cfn_client.describe_stacks(StackName=stack_id)["Stacks"][0]69 assert stack.get("StackStatus") == "CREATE_FAILED" # TODO: might be different on AWS, check70 finally:71 cleanup_changesets([change_set_id])72 cleanup_stacks([stack_id])73def test_sns_subscription(74 cfn_client,75 sns_client,76 cleanup_stacks,77 cleanup_changesets,78 is_change_set_created_and_available,79 is_stack_created,80):81 stack_name = f"stack-{short_uid()}"82 change_set_name = f"change-set-{short_uid()}"83 topic_name = f"topic-{short_uid()}"84 queue_name = f"topic-{short_uid()}"85 template_rendered = jinja2.Template(load_template_raw("sns_topic_subscription.yaml")).render(86 topic_name=topic_name, queue_name=queue_name87 )88 response = cfn_client.create_change_set(89 StackName=stack_name,90 ChangeSetName=change_set_name,91 TemplateBody=template_rendered,92 ChangeSetType="CREATE",93 )94 change_set_id = response["Id"]95 stack_id = response["StackId"]96 try:97 wait_until(is_change_set_created_and_available(change_set_id))98 cfn_client.execute_change_set(ChangeSetName=change_set_id)99 wait_until(is_stack_created(stack_id))100 outputs = cfn_client.describe_stacks(StackName=stack_id)["Stacks"][0]["Outputs"]101 assert len(outputs) == 1 and outputs[0]["OutputKey"] == "TopicArnOutput"102 topic_arn = outputs[0]["OutputValue"]103 assert topic_arn is not None104 subscriptions = sns_client.list_subscriptions_by_topic(TopicArn=topic_arn)105 assert len(subscriptions["Subscriptions"]) > 0106 finally:107 cleanup_changesets([change_set_id])...
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!!