Skip to content

Commit 3cb2e03

Browse files
committed
update custom_id test to validate both strategies
1 parent cd4fb51 commit 3cb2e03

File tree

1 file changed

+54
-39
lines changed

1 file changed

+54
-39
lines changed

tests/aws/services/ec2/test_ec2.py

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
)
1212

1313
from localstack.constants import TAG_KEY_CUSTOM_ID
14-
from localstack.services.ec2.patches import VpcIdentifier
14+
from localstack.services.ec2.patches import SecurityGroupIdentifier, VpcIdentifier
1515
from localstack.testing.pytest import markers
16+
from localstack.utils.id_generator import localstack_id_manager
1617
from localstack.utils.strings import short_uid
1718
from localstack.utils.sync import retry
1819

@@ -618,30 +619,57 @@ def test_create_subnet_with_custom_id_and_vpc_id(self, cleanups, aws_client, cre
618619
assert subnet["Tags"][0]["Value"] == custom_subnet_id
619620

620621
@markers.aws.only_localstack
621-
def test_create_security_group_with_custom_id(self, cleanups, aws_client, create_vpc):
622+
@pytest.mark.parametrize("strategy", ["tag", "id_manager"])
623+
@pytest.mark.parametrize("default_vpc", [True, False])
624+
def test_create_security_group_with_custom_id(
625+
self, cleanups, aws_client, create_vpc, strategy, account_id, region_name, default_vpc
626+
):
622627
custom_id = random_security_group_id()
628+
group_name = f"test-security-group-{short_uid()}"
629+
vpc_id = None
623630

624631
# Create necessary VPC resource
625-
vpc: dict = create_vpc(
626-
cidr_block="10.0.0.0/24",
627-
tag_specifications=[],
628-
)
632+
if default_vpc:
633+
vpc: dict = aws_client.ec2.describe_vpcs(
634+
Filters=[{"Name": "is-default", "Values": ["true"]}]
635+
)["Vpcs"][0]
636+
vpc_id = vpc["VpcId"]
637+
else:
638+
vpc: dict = create_vpc(
639+
cidr_block="10.0.0.0/24",
640+
tag_specifications=[],
641+
)
642+
vpc_id = vpc["Vpc"]["VpcId"]
643+
644+
def _create_security_group() -> dict:
645+
req_kwargs = {"Description": "Test security group", "GroupName": group_name}
646+
if not default_vpc:
647+
# vpc_id does not need to be provided for default vpc
648+
req_kwargs["VpcId"] = vpc_id
649+
if strategy == "tag":
650+
req_kwargs["TagSpecifications"] = [
651+
{
652+
"ResourceType": "security-group",
653+
"Tags": [{"Key": TAG_KEY_CUSTOM_ID, "Value": custom_id}],
654+
}
655+
]
656+
return aws_client.ec2.create_security_group(**req_kwargs)
657+
else:
658+
with localstack_id_manager.custom_id(
659+
SecurityGroupIdentifier(
660+
account_id=account_id,
661+
region=region_name,
662+
vpc_id=vpc_id,
663+
group_name=group_name,
664+
),
665+
custom_id,
666+
):
667+
return aws_client.ec2.create_security_group(**req_kwargs)
668+
669+
security_group: dict = _create_security_group()
629670

630-
# Check if security group ID matches the custom ID
631-
security_group: dict = aws_client.ec2.create_security_group(
632-
Description="Test security group",
633-
GroupName="test-security-group-0",
634-
VpcId=vpc["Vpc"]["VpcId"],
635-
TagSpecifications=[
636-
{
637-
"ResourceType": "security-group",
638-
"Tags": [
639-
{"Key": TAG_KEY_CUSTOM_ID, "Value": custom_id},
640-
],
641-
}
642-
],
643-
)
644671
cleanups.append(lambda: aws_client.ec2.delete_security_group(GroupId=custom_id))
672+
# Check if security group ID matches the custom ID
645673
assert security_group["GroupId"] == custom_id, (
646674
f"Security group ID does not match custom ID: {security_group}"
647675
)
@@ -652,29 +680,16 @@ def test_create_security_group_with_custom_id(self, cleanups, aws_client, create
652680
)["SecurityGroups"]
653681

654682
# Get security group that match a given VPC id
655-
security_group = next(
656-
(sg for sg in security_groups if sg["VpcId"] == vpc["Vpc"]["VpcId"]), None
657-
)
683+
security_group = next((sg for sg in security_groups if sg["VpcId"] == vpc_id), None)
658684
assert security_group["GroupId"] == custom_id
659-
assert len(security_group["Tags"]) == 1
660-
assert security_group["Tags"][0]["Key"] == TAG_KEY_CUSTOM_ID
661-
assert security_group["Tags"][0]["Value"] == custom_id
685+
if strategy == "tag":
686+
assert len(security_group["Tags"]) == 1
687+
assert security_group["Tags"][0]["Key"] == TAG_KEY_CUSTOM_ID
688+
assert security_group["Tags"][0]["Value"] == custom_id
662689

663690
# Check if a duplicate custom ID exception is thrown if we try to recreate the security group with the same custom ID
664691
with pytest.raises(ClientError) as e:
665-
aws_client.ec2.create_security_group(
666-
Description="Test security group",
667-
GroupName="test-security-group-1",
668-
VpcId=vpc["Vpc"]["VpcId"],
669-
TagSpecifications=[
670-
{
671-
"ResourceType": "security-group",
672-
"Tags": [
673-
{"Key": TAG_KEY_CUSTOM_ID, "Value": custom_id},
674-
],
675-
}
676-
],
677-
)
692+
_create_security_group()
678693

679694
assert e.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
680695
assert e.value.response["Error"]["Code"] == "InvalidSecurityGroupId.DuplicateCustomId"

0 commit comments

Comments
 (0)