"""Lambda test module.""" import boto3 import pytest from moto import mock_ec2 from src import app @pytest.fixture() def patch_ec2_client(ec2_client, monkeypatch): """Patch the global ECS client with a mocked one.""" monkeypatch.setattr(app, 'ec2_client', ec2_client) return ec2_client @pytest.fixture() def ec2_client(aws_credentials): """Return an ec2 client.""" with mock_ec2(): yield boto3.client('ec2', region_name='us-east-1') @pytest.fixture() def create_vpc(ec2_client): """Create VPC.""" ec2_client.create_vpc(CidrBlock='10.212.0.0/20') vpc = ec2_client.describe_vpcs( Filters=[ { 'Name': 'cidr-block-association.cidr-block', 'Values': [ '10.212.0.0/20', ] }, ] ) ec2_client.create_tags( Resources=[ vpc['Vpcs'][0]['VpcId'] ], Tags=[ { 'Key': 'Name', 'Value': 'backup-terraform-aws-vpc', }, ], ) vpc_id = vpc['Vpcs'][0]['VpcId'] return vpc_id @pytest.fixture() def create_subnet(ec2_client, create_vpc): """Create subnet.""" ec2_client.create_subnet( VpcId=create_vpc, CidrBlock='10.212.0.0/24' ) subnet = ec2_client.describe_subnets( Filters=[ { 'Name': 'vpc-id', 'Values': [ create_vpc, ] } ] ) ec2_client.create_tags( Resources=[ subnet['Subnets'][0]['SubnetId'] ], Tags=[ { 'Key': 'Name', 'Value': 'backup_private_subnet_0', }, { 'Key': 'tier', 'Value': 'private', } ], ) subnet_ids = [] subnet_ids.append(subnet['Subnets'][0]['SubnetId']) return subnet_ids @pytest.fixture() def create_security_group(ec2_client, create_vpc): """Create security group.""" ec2_client.create_security_group( Description='test security group', GroupName='backup-orcd-secrets-manager-secrets-task-security-group', VpcId=create_vpc, ) security_group = ec2_client.describe_security_groups( Filters=[ { 'Name': 'group-name', 'Values': [ 'backup-orcd-secrets-manager-secrets-task-security-group', ] }, { 'Name': 'vpc-id', 'Values': [ create_vpc ] } ] ) return security_group def test_get_vpc_id(patch_ec2_client, create_vpc): """Test get_vpc_id.""" assert create_vpc == app.get_vpc_id() def test_get_subnets(patch_ec2_client, create_subnet, create_vpc): """Test get_subnet_ids.""" assert create_subnet == app.get_subnets(create_vpc) def test_get_security_group(patch_ec2_client, create_security_group, create_vpc): """Test get_subnet_ids.""" assert create_security_group['SecurityGroups'][0]['GroupId'] == app.get_security_group(create_vpc)