@@ -694,85 +694,77 @@ def _create_security_group() -> dict:
694
694
assert e .value .response ["ResponseMetadata" ]["HTTPStatusCode" ] == 400
695
695
assert e .value .response ["Error" ]["Code" ] == "InvalidSecurityGroupId.DuplicateCustomId"
696
696
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" ])
705
698
@markers .aws .validated
706
699
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
708
701
):
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" ]}]
715
707
)
708
+ default_vpc_id = default_vpc ["Vpcs" ][0 ]["VpcId" ]
716
709
717
- # Create a VPC
718
- vpc : dict = create_vpc (
710
+ # Create a custom VPC for testing
711
+ custom_vpc : dict = create_vpc (
719
712
cidr_block = "10.0.0.0/16" ,
720
713
tag_specifications = [
721
714
{
722
715
"ResourceType" : "vpc" ,
723
716
"Tags" : [
724
- {"Key" : "Name" , "Value" : "test-vpc" },
717
+ {"Key" : "Name" , "Value" : f "test-vpc- { short_uid () } " },
725
718
],
726
719
}
727
720
],
728
721
)
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" ]
731
723
732
- # Create security groups in the VPC
724
+ # Create security groups in the default VPC
733
725
sg1 = ec2_create_security_group (
734
- GroupName = "test-security-group-1 " ,
726
+ GroupName = f "test-security-group-{ short_uid () } " ,
735
727
Description = "Test Security Group 1 Description" ,
736
- VpcId = vpc_id ,
728
+ VpcId = default_vpc_id ,
729
+ ports = [22 ],
737
730
)
738
731
sg1_id = sg1 ["GroupId" ]
739
732
snapshot .match ("create_security_group_1" , sg1 )
740
733
741
734
sg2 = ec2_create_security_group (
742
- GroupName = "test-security-group-2 " ,
735
+ GroupName = f "test-security-group-{ short_uid () } " ,
743
736
Description = "Test Security Group 2 Description" ,
744
- VpcId = vpc_id ,
737
+ VpcId = default_vpc_id ,
738
+ ports = [22 ],
745
739
)
746
740
sg2_id = sg2 ["GroupId" ]
747
741
snapshot .match ("create_security_group_2" , sg2 )
748
742
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
755
744
sg3 = ec2_create_security_group (
756
- GroupName = "test-security-group-3 " ,
745
+ GroupName = f "test-security-group-{ short_uid () } " ,
757
746
Description = "Test Security Group 3 Description" ,
758
- VpcId = default_vpc_id ,
747
+ VpcId = custom_vpc_id ,
748
+ ports = [22 ],
759
749
)
760
750
sg3_id = sg3 ["GroupId" ]
761
751
snapshot .match ("create_security_group_3" , sg3 )
762
752
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
776
768
777
769
778
770
@markers .snapshot .skip_snapshot_verify (
0 commit comments