Skip to content

Commit

Permalink
fixes type error in SG Filter; adds test
Browse files Browse the repository at this point in the history
  • Loading branch information
ksugden committed Jan 24, 2025
1 parent 0b21909 commit cb1b925
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dbt_platform_helper/providers/vpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def _get_vpc_id_by_name(self, vpc_name: str) -> str:
def _get_security_groups(self, app: str, env: str, vpc_id: str) -> list:

vpc_filter = {"Name": "vpc-id", "Values": [vpc_id]}
tag_filter = {"Name": f"tag:Name", "Values": f"copilot-{app}-{env}-env"}
tag_filter = {"Name": f"tag:Name", "Values": [f"copilot-{app}-{env}-env"]}
response = self.ec2_client.describe_security_groups(Filters=[vpc_filter, tag_filter])

return [sg.get("GroupId") for sg in response.get("SecurityGroups")]
Expand Down
71 changes: 70 additions & 1 deletion tests/platform_helper/providers/test_vpc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from unittest.mock import Mock

import boto3
import pytest
from moto import mock_aws

Expand All @@ -7,6 +10,72 @@
from tests.platform_helper.utils.test_aws import mock_vpc_info_session


@mock_aws
def test_get_vpc_success_against_mocked_aws_environment():
client = boto3.client("ec2")

vpc1 = client.create_vpc(CidrBlock="10.0.0.0/16")
client.create_tags(
Resources=[vpc1["Vpc"]["VpcId"]], Tags=[{"Key": "Name", "Value": "test-vpc"}]
)
vpc1_public_subnet = client.create_subnet(CidrBlock="10.0.1.0/24", VpcId=vpc1["Vpc"]["VpcId"])
vpc1_private_subnet = client.create_subnet(CidrBlock="10.0.2.0/24", VpcId=vpc1["Vpc"]["VpcId"])

client.create_tags(
Resources=[vpc1_public_subnet["Subnet"]["SubnetId"]],
Tags=[{"Key": "vpc-id", "Value": "test-vpc"}, {"Key": "subnet_type", "Value": "public"}],
)
client.create_tags(
Resources=[vpc1_private_subnet["Subnet"]["SubnetId"]],
Tags=[{"Key": "vpc-id", "Value": "test-vpc"}, {"Key": "subnet_type", "Value": "private"}],
)

sg_tag = "copilot-test-app-test-env-env"
sg_1 = client.create_security_group(
GroupName="test_vpc_sg",
Description="SG tagged with expected name",
VpcId=vpc1["Vpc"]["VpcId"],
)
client.create_tags(Resources=[sg_1["GroupId"]], Tags=[{"Key": "Name", "Value": sg_tag}])

vpc2 = client.create_vpc(CidrBlock="172.16.0.0/16")
client.create_tags(
Resources=[vpc2["Vpc"]["VpcId"]], Tags=[{"Key": "Name", "Value": "test-vpc-2"}]
)

vpc2_public_subnet = client.create_subnet(CidrBlock="172.16.1.0/24", VpcId=vpc2["Vpc"]["VpcId"])
vpc2_private_subnet = client.create_subnet(
CidrBlock="172.16.2.0/24", VpcId=vpc2["Vpc"]["VpcId"]
)

client.create_tags(
Resources=[vpc2_public_subnet["Subnet"]["SubnetId"]],
Tags=[{"Key": "vpc-id", "Value": "test-vpc-2"}, {"Key": "subnet_type", "Value": "public"}],
)
client.create_tags(
Resources=[vpc2_private_subnet["Subnet"]["SubnetId"]],
Tags=[{"Key": "vpc-id", "Value": "test-vpc-2"}, {"Key": "subnet_type", "Value": "private"}],
)

sg_tag = "copilot-test-app-test-env-env"
sg_2 = client.create_security_group(
GroupName="test_vpc_2_sg",
Description="SG tagged with expected name",
VpcId=vpc2["Vpc"]["VpcId"],
)
client.create_tags(Resources=[sg_2["GroupId"]], Tags=[{"Key": "Name", "Value": sg_tag}])

mock_session = Mock()
mock_session.client.return_value = client

result = VpcProvider(mock_session).get_vpc("test-app", "test-env", "test-vpc")

assert result.private_subnets == [vpc1_private_subnet["Subnet"]["SubnetId"]]
assert result.public_subnets == [vpc1_public_subnet["Subnet"]["SubnetId"]]
assert result.id == vpc1["Vpc"]["VpcId"]
assert result.security_groups == [sg_1["GroupId"]]


@mock_aws
def test_get_vpc_success():
mock_session, mock_client, _ = mock_vpc_info_session()
Expand All @@ -26,7 +95,7 @@ def test_get_vpc_success():
mock_client.describe_security_groups.assert_called_once_with(
Filters=[
{"Name": "vpc-id", "Values": ["vpc-123456"]},
{"Name": "tag:Name", "Values": "copilot-my_app-my_env-env"},
{"Name": "tag:Name", "Values": ["copilot-my_app-my_env-env"]},
]
)

Expand Down

0 comments on commit cb1b925

Please sign in to comment.