Best Python code snippet using localstack_python
stack.py
Source:stack.py
...103 if not stack.options.skip_version_check:104 cf.helpers.exit_when_version_not_found(version, stack, self.ec2_conn)105 cf.helpers.add_version_param(version, previous_version, stack.params)106 def lock(self, stack_name):107 return self.cf_conn.set_stack_policy(stack_name, self.__stack_policy('Deny'))108 def __stack_policy(self, effect):109 return """{110 "Statement" : [111 {112 "Effect" : \"""" + effect + """\",113 "Action" : "Update:*",114 "Principal": "*",115 "Resource" : "*"116 }117 ]118 }"""119 def unlock(self, stack_name):120 return self.cf_conn.set_stack_policy(stack_name, self.__stack_policy('Allow'))121 def describe(self, stack_name):122 print("Describing:", stack_name)123 return cf.helpers.describe_stack(self.cf_conn, stack_name)124 def delete(self, stack_name):125 print("Deleting:", stack_name)126 return self.cf_conn.delete_stack(stack_name)127 def create(self, stack):128 print(stack.template_uri)129 print("Creating:", stack.stack_name)130 print("with params:", stack.params)131 return self.cf_conn.create_stack(132 stack.stack_name,133 template_url=stack.template_uri,134 parameters=stack.params,...
test_cloudformation_stack_policies.py
Source:test_cloudformation_stack_policies.py
...7@mock_cloudformation8def test_set_stack_policy_on_nonexisting_stack():9 cf_conn = boto3.client("cloudformation", region_name="us-east-1")10 with pytest.raises(ClientError) as exc:11 cf_conn.set_stack_policy(StackName="unknown", StackPolicyBody="{}")12 err = exc.value.response["Error"]13 err["Code"].should.equal("ValidationError")14 err["Message"].should.equal("Stack: unknown does not exist")15 err["Type"].should.equal("Sender")16@mock_cloudformation17def test_get_stack_policy_on_nonexisting_stack():18 cf_conn = boto3.client("cloudformation", region_name="us-east-1")19 with pytest.raises(ClientError) as exc:20 cf_conn.get_stack_policy(StackName="unknown")21 err = exc.value.response["Error"]22 err["Code"].should.equal("ValidationError")23 err["Message"].should.equal("Stack: unknown does not exist")24 err["Type"].should.equal("Sender")25@mock_cloudformation26def test_get_stack_policy_on_stack_without_policy():27 cf_conn = boto3.client("cloudformation", region_name="us-east-1")28 cf_conn.create_stack(StackName="test_stack", TemplateBody=dummy_template_json)29 resp = cf_conn.get_stack_policy(StackName="test_stack")30 resp.shouldnt.have.key("StackPolicyBody")31@mock_cloudformation32def test_set_stack_policy_with_both_body_and_url():33 cf_conn = boto3.client("cloudformation", region_name="us-east-1")34 cf_conn.create_stack(StackName="test_stack", TemplateBody=dummy_template_json)35 with pytest.raises(ClientError) as exc:36 cf_conn.set_stack_policy(37 StackName="test_stack", StackPolicyBody="{}", StackPolicyURL="..."38 )39 err = exc.value.response["Error"]40 err["Code"].should.equal("ValidationError")41 err["Message"].should.equal(42 "You cannot specify both StackPolicyURL and StackPolicyBody"43 )44 err["Type"].should.equal("Sender")45@mock_cloudformation46def test_set_stack_policy_with_body():47 cf_conn = boto3.client("cloudformation", region_name="us-east-1")48 cf_conn.create_stack(StackName="test_stack", TemplateBody=dummy_template_json)49 policy = json.dumps({"policy": "yes"})50 cf_conn.set_stack_policy(StackName="test_stack", StackPolicyBody=policy)51 resp = cf_conn.get_stack_policy(StackName="test_stack")52 resp.should.have.key("StackPolicyBody").equals(policy)53@mock_cloudformation54@mock_s355def test_set_stack_policy_with_url():56 cf_conn = boto3.client("cloudformation", region_name="us-east-1")57 cf_conn.create_stack(StackName="test_stack", TemplateBody=dummy_template_json)58 policy = json.dumps({"policy": "yes"})59 s3 = boto3.client("s3", region_name="us-east-1")60 s3.create_bucket(Bucket="foobar")61 s3.put_object(Bucket="foobar", Key="test", Body=policy)62 key_url = s3.generate_presigned_url(63 ClientMethod="get_object", Params={"Bucket": "foobar", "Key": "test"}64 )65 cf_conn.set_stack_policy(StackName="test_stack", StackPolicyURL=key_url)66 resp = cf_conn.get_stack_policy(StackName="test_stack")67 resp.should.have.key("StackPolicyBody").equals(policy)68@mock_cloudformation69@mock_s370def test_set_stack_policy_with_url_pointing_to_unknown_key():71 cf_conn = boto3.client("cloudformation", region_name="us-east-1")72 cf_conn.create_stack(StackName="test_stack", TemplateBody=dummy_template_json)73 with pytest.raises(ClientError) as exc:74 cf_conn.set_stack_policy(StackName="test_stack", StackPolicyURL="...")75 err = exc.value.response["Error"]76 err["Code"].should.equal("ValidationError")77 err["Message"].should.contain("S3 error: Access Denied")...
setstackpolicy.py
Source:setstackpolicy.py
...20 stackId = stacksplit[1]21 def test(stackId):22 try:23 if 'disable' in status:24 cf_conn.set_stack_policy(stackId, stack_policy_url=disable_url)25 elif 'enable' in status:26 cf_conn.set_stack_policy(stackId, stack_policy_url=enable_url)27 except Exception as error:28 print("Error setting the stack_policy for {}. ****Stack Trace: {} ****".format(test,error))29 return (1)30 test(stackId)31 except Exception as error:32 print("Error when getting information about stack resource {} in stack {}. ****Stack Trace: {} ****".format(stackName,error))33 return (1)34# Here, we query the master stack for the ID of the database stack. We pass this value into the Jenkins job to carry to the next job....
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!!