Best Python code snippet using localstack_python
test_cr_get_vpce_subnets.py
Source:test_cr_get_vpce_subnets.py
1import pytest2from mock import patch, MagicMock3from backend.lambdas.custom_resources.get_vpce_subnets import (4 create,5 delete,6 handler,7)8pytestmark = [pytest.mark.unit, pytest.mark.task]9@patch("backend.lambdas.custom_resources.get_vpce_subnets.ec2_client")10def test_it_returns_valid_subnets(mock_client):11 event = {12 "ResourceProperties": {13 "ServiceName": "com.amazonaws.eu-west-2.monitoring",14 "SubnetIds": ["subnet-0123456789abcdef0", "subnet-0123456789abcdef1"],15 "VpcEndpointType": "Interface",16 }17 }18 mock_client.describe_subnets.return_value = {19 "Subnets": [20 {21 "AvailabilityZone": "eu-west-2a",22 "SubnetId": "subnet-0123456789abcdef0",23 },24 {25 "AvailabilityZone": "eu-west-2b",26 "SubnetId": "subnet-0123456789abcdef1",27 },28 ]29 }30 mock_client.describe_vpc_endpoint_services.return_value = {31 "ServiceDetails": [32 {33 "ServiceName": "com.amazonaws.eu-west-2.monitoring",34 "AvailabilityZones": [35 "eu-west-2a",36 "eu-west-2b",37 ],38 }39 ]40 }41 resp = create(event, MagicMock())42 mock_client.describe_subnets.assert_called_with(43 SubnetIds=["subnet-0123456789abcdef0", "subnet-0123456789abcdef1"]44 )45 mock_client.describe_vpc_endpoint_services.assert_called_with(46 Filters=[47 {48 "Name": "service-name",49 "Values": [50 "cn.com.amazonaws.eu-west-2.monitoring",51 "com.amazonaws.eu-west-2.monitoring",52 ],53 },54 {"Name": "service-type", "Values": ["Interface"]},55 ]56 )57 assert resp == "subnet-0123456789abcdef0,subnet-0123456789abcdef1"58@patch("backend.lambdas.custom_resources.get_vpce_subnets.ec2_client")59def test_it_raises_exception(mock_client):60 event = {61 "ResourceProperties": {62 "ServiceName": "com.amazonaws.eu-west-2.dummy",63 "SubnetIds": [],64 "VpcEndpointType": "Interface",65 }66 }67 mock_client.describe_subnets.return_value = {68 "Subnets": [69 {70 "AvailabilityZone": "eu-west-2a",71 "SubnetId": "subnet-0123456789abcdef0",72 },73 {74 "AvailabilityZone": "eu-west-2b",75 "SubnetId": "subnet-0123456789abcdef1",76 },77 ]78 }79 mock_client.describe_vpc_endpoint_services.return_value = {"ServiceDetails": []}80 with pytest.raises(Exception) as e_info:81 create(event, MagicMock())82 mock_client.describe_subnets.assert_called_with(SubnetIds=[])83 mock_client.describe_vpc_endpoint_services.assert_called_with(84 Filters=[85 {86 "Name": "service-name",87 "Values": [88 "cn.com.amazonaws.eu-west-2.dummy",89 "com.amazonaws.eu-west-2.dummy",90 ],91 },92 {"Name": "service-type", "Values": ["Interface"]},93 ]94 )95 assert e_info.typename == "IndexError"96@patch("backend.lambdas.custom_resources.get_vpce_subnets.ec2_client")97def test_it_does_nothing_on_delete(mock_client):98 resp = delete({}, MagicMock())99 mock_client.assert_not_called()100 assert not resp101@patch("backend.lambdas.custom_resources.get_vpce_subnets.helper")102def test_it_delegates_to_cr_helper(cr_helper):103 handler(1, 2)...
vpc_endpoint_services.py
Source:vpc_endpoint_services.py
1"""2A helper script to call describe_vpc_endpoint_services.3"""4import click5import logging6import yaml7from helper.aws import AwsApiHelper8logging.getLogger().setLevel(logging.DEBUG)9class Helper(AwsApiHelper):10 def __init__(self, endpoint_type, name_only):11 super().__init__()12 self._endpoint_type = endpoint_type13 self._name_only = name_only14 def process_request(self, session, account_id, region, kwargs):15 cnt = 016 client = session.client("ec2", region_name=region)17 for item in self.paginate(client, "describe_vpc_endpoint_services", kwargs):18 if self.match_service_type(item):19 if self._name_only:20 if "ServiceName" in item:21 print(item["ServiceName"])22 else:23 print(item)24 else:25 print(yaml.dump(item))26 print("--------------------------------------------------------------------------------")27 cnt += 128 logging.debug(f"{account_id} {region}: Total items: {cnt}")29 def match_service_type(self, item):30 if self._endpoint_type is None:31 return True32 33 for subitem in item["ServiceType"]:34 curr = subitem.get("ServiceType")35 if curr is not None and curr.lower() == self._endpoint_type:36 return True37 return False38@click.command()39@click.option("--endpointtype", "-t", help="VPC endpoint type (Gateway|Interface). Return all if not specified.")40@click.option("--nameonly", "-n", is_flag=True, show_default=True, help="Show names only")41@click.option("--servicename", "-s", help="Service name. Optional. E.g. com.amazonaws.ap-southeast-2.s3")42@click.option("--profile", "-p", help="AWS profile name. Use profiles in ~/.aws if not specified.")43@click.option("--region", "-r", default="ap-southeast-2", show_default=True, help="AWS Region. Use 'all' for all regions.")44def main(endpointtype, nameonly, servicename, profile, region):45 kwargs = {"ServiceNames": [servicename]} if servicename else {}46 Helper(endpointtype, nameonly).start(profile, region, "ec2", kwargs)47if __name__ == "__main__":...
test_vpc_endpoint_services.py
Source:test_vpc_endpoint_services.py
1import unittest2from cloudwanderer import URN3from ..helpers import CloudWandererCalls, ExpectedCall, MultipleResourceScenario, NoMotoMock, SingleResourceScenario4class TestVPCEndpointServices(NoMotoMock, unittest.TestCase):5 vpc_endpoint_service_payload = {6 "ServiceName": "com.amazonaws.vpce.eu-west-1.vpce-svc-11111111111111111",7 "ServiceId": "vpce-svc-11111111111111111",8 "ServiceType": [{"ServiceType": "Interface"}],9 "AvailabilityZones": ["eu-west-1a"],10 "Owner": "111111111111",11 "BaseEndpointDnsNames": ["vpce-svc-11111111111111111.eu-west-1.vpce.amazonaws.com"],12 "VpcEndpointPolicySupported": False,13 "AcceptanceRequired": True,14 "ManagesVpcEndpoints": False,15 "Tags": [],16 }17 mock = {18 "ec2": {19 "describe_vpc_endpoint_services.return_value": {20 "ServiceDetails": [vpc_endpoint_service_payload],21 "ServiceNames": ["com.amazonaws.vpce.eu-west-1.vpce-svc-11111111111111111"],22 }23 }24 }25 single_resource_scenarios = [26 SingleResourceScenario(27 urn=URN.from_string(28 "urn:aws:123456789012:eu-west-2:ec2:vpc_endpoint_service:com.amazonaws.vpce.eu-west-1.vpce-svc-11111111111111111" # noqa29 ),30 expected_results=[vpc_endpoint_service_payload],31 expected_call=ExpectedCall(32 "ec2",33 "describe_vpc_endpoint_services",34 [],35 {"ServiceNames": ["com.amazonaws.vpce.eu-west-1.vpce-svc-11111111111111111"]},36 ),37 )38 ]39 multiple_resource_scenarios = [40 MultipleResourceScenario(41 arguments=CloudWandererCalls(42 regions=["eu-west-2"], service_names=["ec2"], resource_types=["vpc_endpoint_service"]43 ),44 expected_results=[vpc_endpoint_service_payload],45 )...
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!!