11
11
)
12
12
13
13
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
15
15
from localstack .testing .pytest import markers
16
+ from localstack .utils .id_generator import localstack_id_manager
16
17
from localstack .utils .strings import short_uid
17
18
from localstack .utils .sync import retry
18
19
@@ -618,30 +619,57 @@ def test_create_subnet_with_custom_id_and_vpc_id(self, cleanups, aws_client, cre
618
619
assert subnet ["Tags" ][0 ]["Value" ] == custom_subnet_id
619
620
620
621
@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
+ ):
622
627
custom_id = random_security_group_id ()
628
+ group_name = f"test-security-group-{ short_uid ()} "
629
+ vpc_id = None
623
630
624
631
# 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 ()
629
670
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
- )
644
671
cleanups .append (lambda : aws_client .ec2 .delete_security_group (GroupId = custom_id ))
672
+ # Check if security group ID matches the custom ID
645
673
assert security_group ["GroupId" ] == custom_id , (
646
674
f"Security group ID does not match custom ID: { security_group } "
647
675
)
@@ -652,29 +680,16 @@ def test_create_security_group_with_custom_id(self, cleanups, aws_client, create
652
680
)["SecurityGroups" ]
653
681
654
682
# 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 )
658
684
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
662
689
663
690
# Check if a duplicate custom ID exception is thrown if we try to recreate the security group with the same custom ID
664
691
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 ()
678
693
679
694
assert e .value .response ["ResponseMetadata" ]["HTTPStatusCode" ] == 400
680
695
assert e .value .response ["Error" ]["Code" ] == "InvalidSecurityGroupId.DuplicateCustomId"
0 commit comments