Best Python code snippet using localstack_python
test_fail_all_resources.py
Source:test_fail_all_resources.py
1import pytest2import fail_all_resources3import os4@pytest.fixture5def config(mocker):6 return mocker.patch('fail_all_resources.config')7@pytest.fixture8def context(mocker):9 os.environ['StackName'] = STACK_NAME10 mock = mocker.Mock()11 mock.function_name = LAMBDA_FUNCTION12 return mock13OVERSIZED_EVENT = {14 "invokingEvent": "{\"configurationItemSummary\": {\"changeType\": \"UPDATE\",\"configurationItemVersion\": \"1.2\",\"configurationItemCaptureTime\":\"2016-10-06T16:46:16.261Z\",\"configurationStateId\": 0,\"awsAccountId\":\"123456789012\",\"configurationItemStatus\": \"OK\",\"resourceType\": \"AWS::EC2::Instance\",\"resourceId\":\"i-00000000\",\"resourceName\":null,\"ARN\":\"arn:aws:ec2:us-west-2:123456789012:instance/i-00000000\",\"awsRegion\": \"us-west-2\",\"availabilityZone\":\"us-west-2a\",\"configurationStateMd5Hash\":\"8f1ee69b287895a0f8bc5753eca68e96\",\"resourceCreationTime\":\"2016-10-06T16:46:10.489Z\"},\"messageType\":\"OversizedConfigurationItemChangeNotification\"}", # noqa15 "ruleParameters": "{\"myParameterKey\":\"myParameterValue\"}",16 "resultToken": "myResultToken",17 "eventLeftScope": False,18 "executionRoleArn": "arn:aws:iam::123456789012:role/config-role",19 "configRuleArn": "arn:aws:config:us-east-2:123456789012:config-rule/config-rule-ec2-managed-instance-inventory",20 "configRuleName": "change-triggered-config-rule",21 "configRuleId": "config-rule-0123456",22 "accountId": "123456789012",23 "version": "1.0"24}25EVENT = {26 "invokingEvent": "{\"configurationItem\":{\"configurationItemCaptureTime\":\"2016-02-17T01:36:34.043Z\",\"awsAccountId\":\"123456789012\",\"configurationItemStatus\":\"OK\",\"resourceId\":\"i-00000000\",\"ARN\":\"arn:aws:ec2:us-east-2:123456789012:instance/i-00000000\",\"awsRegion\":\"us-east-2\",\"availabilityZone\":\"us-east-2a\",\"resourceType\":\"AWS::EC2::Instance\",\"tags\":{\"Foo\":\"Bar\"},\"relationships\":[{\"resourceId\":\"eipalloc-00000000\",\"resourceType\":\"AWS::EC2::EIP\",\"name\":\"Is attached to ElasticIp\"}],\"configuration\":{\"foo\":\"bar\"}},\"messageType\":\"ConfigurationItemChangeNotification\"}", # noqa27 "ruleParameters": "{\"myParameterKey\":\"myParameterValue\"}",28 "resultToken": "myResultToken",29 "eventLeftScope": False,30 "executionRoleArn": "arn:aws:iam::123456789012:role/config-role",31 "configRuleArn": "arn:aws:config:us-east-2:123456789012:config-rule/config-rule-0123456",32 "configRuleName": "change-triggered-config-rule",33 "configRuleId": "config-rule-0123456",34 "accountId": "123456789012",35 "version": "1.0"36}37STACK_NAME = 'TestStack'38STACK_EVENT = {39 "invokingEvent": "{\"configurationItem\":{\"configurationItemCaptureTime\":\"2016-02-17T01:36:34.043Z\",\"resourceId\":\"TestStack\",\"resourceType\":\"AWS::CloudFormation::Stack\"}}", # noqa40 "resultToken": "myResultToken"41}42LAMBDA_FUNCTION = 'LambdaFunction'43LAMBDA_FUNCTION_EVENT = {44 "invokingEvent": "{\"configurationItem\":{\"configurationItemCaptureTime\":\"2016-02-17T01:36:34.043Z\",\"resourceId\":\"LambdaFunction\",\"resourceType\":\"AWS::Lambda::Function\"}}", # noqa45 "resultToken": "myResultToken"46}47AUDIT_STACK_SET_EVENT = {48 "invokingEvent": "{\"configurationItem\":{\"configurationItemCaptureTime\":\"2016-02-17T01:36:34.043Z\",\"resourceId\":\"StackSet-audit-test-something\",\"resourceType\":\"AWS::CloudFormation::Stack\"}}", # noqa49 "resultToken": "myResultToken"50}51def test_all_resources_with_event(config, context):52 fail_all_resources.handler(EVENT, context)53 config.put_evaluations.assert_called_with(54 Evaluations=[55 {56 'ComplianceResourceType': 'AWS::EC2::Instance',57 'ComplianceResourceId': 'i-00000000',58 'ComplianceType': 'NON_COMPLIANT',59 'Annotation': 'No Resources should be deployed in this Region',60 'OrderingTimestamp': '2016-02-17T01:36:34.043Z'61 }62 ],63 ResultToken='myResultToken'64 )65def test_all_resources_with_oversized_event(config, context):66 fail_all_resources.handler(OVERSIZED_EVENT, context)67 config.put_evaluations.assert_called_with(68 Evaluations=[69 {70 'ComplianceResourceType': 'AWS::EC2::Instance',71 'ComplianceResourceId': 'i-00000000',72 'ComplianceType': 'NON_COMPLIANT',73 'Annotation': 'No Resources should be deployed in this Region',74 'OrderingTimestamp': '2016-10-06T16:46:16.261Z'75 }76 ],77 ResultToken='myResultToken'78 )79def test_config_stack_is_compliant(config, mocker, context):80 fail_all_resources.handler(STACK_EVENT, context)81 config.put_evaluations.assert_called_with(82 Evaluations=[83 {84 'ComplianceResourceType': 'AWS::CloudFormation::Stack',85 'ComplianceResourceId': 'TestStack',86 'ComplianceType': 'COMPLIANT',87 'Annotation': 'Compliant',88 'OrderingTimestamp': '2016-02-17T01:36:34.043Z'89 }90 ],91 ResultToken='myResultToken'92 )93def test_ignores_audit_stack_set(config, mocker, context):94 fail_all_resources.handler(AUDIT_STACK_SET_EVENT, context)95 config.put_evaluations.assert_called_with(96 Evaluations=[97 {98 'ComplianceResourceType': 'AWS::CloudFormation::Stack',99 'ComplianceResourceId': 'StackSet-audit-test-something',100 'ComplianceType': 'COMPLIANT',101 'Annotation': 'Compliant',102 'OrderingTimestamp': '2016-02-17T01:36:34.043Z'103 }104 ],105 ResultToken='myResultToken'106 )107def test_ignores_lambda(config, mocker, context):108 fail_all_resources.handler(LAMBDA_FUNCTION_EVENT, context)109 config.put_evaluations.assert_called_with(110 Evaluations=[111 {112 'ComplianceResourceType': 'AWS::Lambda::Function',113 'ComplianceResourceId': LAMBDA_FUNCTION,114 'ComplianceType': 'COMPLIANT',115 'Annotation': 'Compliant',116 'OrderingTimestamp': '2016-02-17T01:36:34.043Z'117 }118 ],119 ResultToken='myResultToken'...
remove_restricted_ingress_rules.py
Source:remove_restricted_ingress_rules.py
...22 prefixlistids_array = permissions['prefixListIds']23 if ip == "0.0.0.0/0"and ipprotocol != "-1" and sgname != "Jumphost" and (int(toport) == int(rp) or int(fromport) == int(rp)):24 status = "NON_COMPLIANT"25 config = boto3.client("config")26 response = config.put_evaluations(27 Evaluations=[28 {29 "ComplianceResourceType":30 'AWS::EC2::SecurityGroup',31 "ComplianceResourceId":32 sgid,33 "ComplianceType":34 status,35 "Annotation":36 'Has restricted ports open',37 "OrderingTimestamp":38 msg['configurationItem']['configurationItemCaptureTime']39 },40 ],41 ResultToken=result_token42 )43 response = ec2.revoke_security_group_ingress(44 DryRun = False,45 IpProtocol = ipprotocol,46 FromPort = fromport,47 ToPort = toport,48 CidrIp = ip,49 GroupId = strsgid50 )51 elif "launch-wizard" in sgname:52 status = "NON_COMPLIANT"53 config = boto3.client("config")54 response = config.put_evaluations(55 Evaluations=[56 {57 "ComplianceResourceType":58 'AWS::EC2::SecurityGroup',59 "ComplianceResourceId":60 sgid,61 "ComplianceType":62 status,63 "Annotation":64 'Has restricted ports open',65 "OrderingTimestamp":66 msg['configurationItem']['configurationItemCaptureTime']67 },68 ],69 ResultToken=result_token70 )71 delete_response = ec2.delete_security_group(72 DryRun=False,73 GroupId=strsgid74 )75 delete_response76 else:77 status = "COMPLIANT"78 config = boto3.client("config")79 response = config.put_evaluations(80 Evaluations=[81 {82 "ComplianceResourceType":83 'AWS::EC2::SecurityGroup',84 "ComplianceResourceId":85 sgid,86 "ComplianceType":87 status,88 "Annotation":89 'Does not have restricted ports open.',90 "OrderingTimestamp":91 msg['configurationItem']['configurationItemCaptureTime']92 },93 ],...
validate_stack_set_deployments.py
Source:validate_stack_set_deployments.py
...37 for x in range(0, el, M):38 put_evaluations = evaluations[x:x + M]39 put_evaluation_options = dict(Evaluations=put_evaluations,40 ResultToken=result_token)41 print(c.put_evaluations(**put_evaluation_options))42def vs(event, context):43 evaluations = []44 # TODO Use Paginator when available45 sse = [s['StackSetName'] for page in p(cf.list_stack_sets, Status='ACTIVE') for s in page['Summaries']]46 print(sse)47 for sn in sse:48 print(sn)49 v(event, sn, cf.describe_stack_set(50 StackSetName=sn51 )['StackSet'], evaluations)52 se(evaluations, event['resultToken'])53def v(e, sn, ss, ev):54 inv = json.loads(e['invokingEvent'])55 t = {tag['Key']: tag['Value'] for tag in ss['Tags']}...
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!!