diff --git a/moto/elbv2/exceptions.py b/moto/elbv2/exceptions.py index 569fa7eed..8ac4ef428 100644 --- a/moto/elbv2/exceptions.py +++ b/moto/elbv2/exceptions.py @@ -166,3 +166,11 @@ class DuplicatePriorityError(ELBClientError): super(DuplicatePriorityError, self).__init__( "ValidationError", "Priority '%s' was provided multiple times" % invalid_value) + + +class InvalidTargetGroupNameError(ELBClientError): + + def __init__(self, invalid_name): + super(InvalidTargetGroupNameError, self).__init__( + "ValidationError", + "Target group name '%s' cannot be longer than '32' characters" % invalid_name) diff --git a/moto/elbv2/models.py b/moto/elbv2/models.py index b30ab5764..cb7be361c 100644 --- a/moto/elbv2/models.py +++ b/moto/elbv2/models.py @@ -21,7 +21,8 @@ from .exceptions import ( ActionTargetGroupNotFoundError, InvalidDescribeRulesRequest, RuleNotFoundError, - DuplicatePriorityError + DuplicatePriorityError, + InvalidTargetGroupNameError ) @@ -264,6 +265,8 @@ class ELBv2Backend(BaseBackend): return [rule] def create_target_group(self, name, **kwargs): + if len(name) > 32: + raise InvalidTargetGroupNameError(name) for target_group in self.target_groups.values(): if target_group.name == name: raise DuplicateTargetGroupName() diff --git a/tests/test_elbv2/test_elbv2.py b/tests/test_elbv2/test_elbv2.py index a37eaa9bc..f69e9a963 100644 --- a/tests/test_elbv2/test_elbv2.py +++ b/tests/test_elbv2/test_elbv2.py @@ -327,6 +327,23 @@ def test_create_target_group_and_listeners(): response = conn.describe_target_groups() response.get('TargetGroups').should.have.length_of(0) + # Fail to create target group with name which length is 33 + long_name = 'A' * 33 + with assert_raises(ClientError): + conn.create_target_group( + Name=long_name, + Protocol='HTTP', + Port=8080, + VpcId=vpc.id, + HealthCheckProtocol='HTTP', + HealthCheckPort='8080', + HealthCheckPath='/', + HealthCheckIntervalSeconds=5, + HealthCheckTimeoutSeconds=5, + HealthyThresholdCount=5, + UnhealthyThresholdCount=2, + Matcher={'HttpCode': '200'}) + @mock_elbv2 @mock_ec2