"""Fixtures for lambda testing.""" import os import boto3 import pytest from moto import mock_aws @pytest.fixture() def aws_credentials(): """Mock AWS Credentials for moto.""" os.environ["AWS_ACCESS_KEY_ID"] = "testing" os.environ["AWS_SECRET_ACCESS_KEY"] = "testing" os.environ["AWS_SECURITY_TOKEN"] = "testing" os.environ["AWS_SESSION_TOKEN"] = "testing" os.environ["AWS_DEFAULT_REGION"] = "us-east-1" @pytest.fixture() def ecs_client(aws_credentials): """Return an ECS client.""" with mock_aws(): yield boto3.client("ecs") @pytest.fixture() def ec2_client(aws_credentials): """Return an EC2 client.""" with mock_aws(): yield boto3.client("ec2") @pytest.fixture() def networking(ec2_client): """Creates vpc and subnet, returns subnet id.""" vpc_response = ec2_client.create_vpc( CidrBlock="10.0.0.0/16", TagSpecifications=[{"ResourceType": "vpc", "Tags": [{"Key": "Name", "Value": "test-vpc"}]}], ) vpc_id = vpc_response["Vpc"]["VpcId"] subnet_response = ec2_client.create_subnet(VpcId=vpc_id, CidrBlock="10.0.1.0/24", AvailabilityZone="us-east-1a") subnet_id = subnet_response["Subnet"]["SubnetId"] sg_response = ec2_client.create_security_group(GroupName="test-sg", Description="Test security group", VpcId=vpc_id) sg_id = sg_response["GroupId"] return vpc_id, subnet_id, sg_id @pytest.fixture() def ecs_service(ecs_client, networking): """Create ECS service and all resources it depends on.""" vpc_id, subnet_id, sg_id = networking cluster_name = "mock-service" ecs_client.create_cluster(clusterName=cluster_name) task_def_response = ecs_client.register_task_definition( family="mock-task-def", networkMode="awsvpc", requiresCompatibilities=["FARGATE"], cpu="256", memory="512", containerDefinitions=[{"name": "mock-container", "image": "amazonlinux", "essential": True}], ) task_def_arn = task_def_response["taskDefinition"]["taskDefinitionArn"] # Create service service_name = "mock-service" ecs_client.create_service( cluster=cluster_name, serviceName=service_name, taskDefinition=task_def_arn, desiredCount=1, launchType="FARGATE", networkConfiguration={ "awsvpcConfiguration": { "subnets": [subnet_id], "securityGroups": [sg_id], "assignPublicIp": "DISABLED", } }, ) return (cluster_name, service_name) @pytest.fixture() def update_ecs_service_response(): return {"service": {"deployments": [{"id": "ecs-svc/1234567", "status": "PRIMARY", "rolloutState": "IN_PROGRESS"}]}} @pytest.fixture() def describe_services_response(): return { "services": [{"deployments": [{"id": "ecs-svc/1234567", "status": "PRIMARY", "rolloutState": "IN_PROGRESS"}]}] }