8000 Ensure tests work correctly with the updated ec2_create_security_grou… · localstack/localstack@fd20316 · GitHub
[go: up one dir, main page]

Skip to content

Commit fd20316

Browse files
< 8000 div data-testid="author-avatar" class="Box-sc-g0xbh4-0 AuthorAvatar-module__AuthorAvatarContainer--NQwZ2">iamramtiniamramtin
committed
Ensure tests work correctly with the updated ec2_create_security_group fixture (#12602)
Signed-off-by: Ramtin Mesgari <26694963+iamramtin@users.noreply.github.com>
1 parent eb6b6c6 commit fd20316

File tree

3 files changed

+40
-87
lines changed

3 files changed

+40
-87
lines changed

tests/aws/services/ec2/test_ec2.py

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -694,85 +694,77 @@ def _create_security_group() -> dict:
694694
assert e.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
695695
assert e.value.response["Error"]["Code"] == "InvalidSecurityGroupId.DuplicateCustomId"
696696

697-
@markers.snapshot.skip_snapshot_verify(
698-
paths=[
699-
"$..Tags",
700-
"$..SecurityGroupForVpcs..Description",
701-
"$..SecurityGroupForVpcs..GroupId",
702-
"$..SecurityGroupForVpcs..GroupName",
703-
]
704-
)
697+
@markers.snapshot.skip_snapshot_verify(paths=["$..Tags"])
705698
@markers.aws.validated
706699
def test_get_security_groups_for_vpc(
707-
self, snapshot, cleanups, aws_client, create_vpc, ec2_create_security_group
700+
self, snapshot, aws_client, create_vpc, ec2_create_security_group
708701
):
709-
snapshot.add_transformers_list(
710-
[
711-
snapshot.transform.key_value("GroupId"),
712-
snapshot.transform.key_value("SecurityGroupArn"),
713-
snapshot.transform.key_value("vpc-id"),
714-
]
702+
snapshot.add_transformers_list([snapshot.transform.key_value("GroupId")])
703+
704+
# Get the default VPC
705+
default_vpc = aws_client.ec2.describe_vpcs(
706+
Filters=[{"Name": "isDefault", "Values": ["true"]}]
715707
)
708+
default_vpc_id = default_vpc["Vpcs"][0]["VpcId"]
716709

717-
# Create a VPC
718-
vpc: dict = create_vpc(
710+
# Create a custom VPC for testing
711+
custom_vpc: dict = create_vpc(
719712
cidr_block="10.0.0.0/16",
720713
tag_specifications=[
721714
{
722715
"ResourceType": "vpc",
723716
"Tags": [
724-
{"Key": "Name", "Value": "test-vpc"},
717+
{"Key": "Name", "Value": f"test-vpc-{short_uid()}"},
725718
],
726719
}
727720
],
728721
)
729-
vpc_id: str = vpc["Vpc"]["VpcId"]
730-
snapshot.match("create_vpc", {"vpc-id": vpc_id})
722+
custom_vpc_id: str = custom_vpc["Vpc"]["VpcId"]
731723

732-
# Create security groups in the VPC
724+
# Create security groups in the default VPC
733725
sg1 = ec2_create_security_group(
734-
GroupName="test-security-group-1",
726+
GroupName=f"test-security-group-{short_uid()}",
735727
Description="Test Security Group 1 Description",
736-
VpcId=vpc_id,
728+
VpcId=default_vpc_id,
729+
ports=[22],
737730
)
738731
sg1_id = sg1["GroupId"]
739732
snapshot.match("create_security_group_1", sg1)
740733

741734
sg2 = ec2_create_security_group(
742-
GroupName="test-security-group-2",
735+
GroupName=f"test-security-group-{short_uid()}",
743736
Description="Test Security Group 2 Description",
744-
VpcId=vpc_id,
737+
VpcId=default_vpc_id,
738+
ports=[22],
745739
)
746740
sg2_id = sg2["GroupId"]
747741
snapshot.match("create_security_group_2", sg2)
748742

749-
# Create a security group in a different VPC (default VPC)
750-
default_vpc = aws_client.ec2.describe_vpcs(
751-
Filters=[{"Name": "isDefault", "Values": ["true"]}]
752-
)
753-
default_vpc_id = default_vpc["Vpcs"][0]["VpcId"]
754-
743+
# Create a security group in the custom VPC
755744
sg3 = ec2_create_security_group(
756-
GroupName="test-security-group-3",
745+
GroupName=f"test-security-group-{short_uid()}",
757746
Description="Test Security Group 3 Description",
758-
VpcId=default_vpc_id,
747+
VpcId=custom_vpc_id,
748+
ports=[22],
759749
)
760750
sg3_id = sg3["GroupId"]
761751
snapshot.match("create_security_group_3", sg3)
762752

763-
vpc_sgs = aws_client.ec2.get_security_groups_for_vpc(VpcId=vpc_id)
764-
snapshot.match("get_security_groups_for_vpc", vpc_sgs)
765-
766-
# Should only include the security groups created in the VPC
767-
vpc_sg_ids = [sg["GroupId"] for sg in vpc_sgs["SecurityGroupForVpcs"]]
768-
assert sg1_id in vpc_sg_ids
769-
assert sg2_id in vpc_sg_ids
770-
assert sg3_id not in vpc_sg_ids
771-
772-
cleanups.append(lambda: aws_client.ec2.delete_vpc(VpcId=vpc_id))
773-
cleanups.append(lambda: aws_client.ec2.delete_security_group(GroupId=sg1_id))
774-
cleanups.append(lambda: aws_client.ec2.delete_security_group(GroupId=sg2_id))
775-
cleanups.append(lambda: aws_client.ec2.delete_security_group(GroupId=sg3_id))
753+
# Should only include the security groups created in the default VPC
754+
default_vpc_sgs = aws_client.ec2.get_security_groups_for_vpc(VpcId=default_vpc_id)
755+
default_vpc_sg_ids = [sg["GroupId"] for sg in default_vpc_sgs["SecurityGroupForVpcs"]]
756+
assert "SecurityGroupForVpcs" in default_vpc_sgs
757+
assert sg1_id in default_vpc_sg_ids
758+
assert sg2_id in default_vpc_sg_ids
759+
assert sg3_id not in default_vpc_sg_ids
760+
761+
# Should only include the security group created in the custom VPC
762+
custom_vpc_sgs = aws_client.ec2.get_security_groups_for_vpc(VpcId=custom_vpc_id)
763+
custom_vpc_sg_ids = [sg["GroupId"] for sg in custom_vpc_sgs["SecurityGroupForVpcs"]]
764+
assert "SecurityGroupForVpcs" in custom_vpc_sgs
765+
assert sg1_id not in custom_vpc_sg_ids
766+
assert sg2_id not in custom_vpc_sg_ids
767+
assert sg3_id in custom_vpc_sg_ids
776768

777769

778770
@markers.snapshot.skip_snapshot_verify(

tests/aws/services/ec2/test_ec2.snapshot.json

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,8 @@
337337
}
338338
},
339339
"tests/aws/services/ec2/test_ec2.py::TestEc2Integrations::test_get_security_groups_for_vpc": {
340-
"recorded-date": "14-05-2025, 20:05:26",
340+
"recorded-date": "17-05-2025, 11:57:25",
341341
"recorded-content": {
342-
"create_vpc": {
343-
"vpc-id": "<vpc-id:1>"
344-
},
345342
"create_security_group_1": {
346343
"GroupId": "<group-id:1>",
347344
"SecurityGroupArn": "arn:<partition>:ec2:<region>:111111111111:security-group/<group-id:1>",
@@ -365,43 +362,7 @@
365362
"HTTPHeaders": {},
366363
"HTTPStatusCode": 200
367364
}
368-
},
369-
"get_security_groups_for_vpc": {
370-
"SecurityGroupForVpcs": [
371-
{
372-
"Description": "Test Security Group 2 Description",
373-
"GroupId": "<group-id:2>",
374-
"GroupName": "test-security-group-2",
375-
"OwnerId": "111111111111",
376-
"PrimaryVpcId": "<vpc-id:1>",
377-
"Tags": []
378-
},
379-
{
380-
"Description": "default VPC security group",
381-
"GroupId": "<group-id:4>",
382-
"GroupName": "default",
383-
"OwnerId": "111111111111",
384-
"PrimaryVpcId": "<vpc-id:1>",
385-
"Tags": []
386-
},
387-
{
388-
"Description": "Test Security Group 1 Description",
389-
"GroupId": "<group-id:1>",
390-
"GroupName": "test-security-group-1",
391-
"OwnerId": "111111111111",
392-
"PrimaryVpcId": "<vpc-id:1>",
393-
"Tags": []
394-
}
395-
],
396-
"ResponseMetadata": {
397-
"HTTPHeaders": {},
398-
"HTTPStatusCode": 200
399-
}
400365
}
401366
}
402-
},
403-
"tests/aws/services/ec2/test_ec2.py::TestEc2Integrations::test_get_security_groups_for_vpc_invalid_inputs": {
404-
"recorded-date": "14-05-2025, 20:07:05",
405-
"recorded-content": {}
406367
}
407368
}

tests/aws/services/ec2/test_ec2.validation.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"last_validated_date": "2024-06-07T01:11:12+00:00"
1313
},
1414
"tests/aws/services/ec2/test_ec2.py::TestEc2Integrations::test_get_security_groups_for_vpc": {
15-
"last_validated_date": "2025-05-14T20:05:19+00:00"
15+
"last_validated_date": "2025-05-17T11:57:23+00:00"
1616
},
1717
"tests/aws/services/ec2/test_ec2.py::TestEc2Integrations::test_vcp_peering_difference_regions": {
1818
"last_validated_date": "2024-06-07T21:28:25+00:00"

0 commit comments

Comments
 (0)
0