From c8794e842d89991224d92a11de9d364948a5685f Mon Sep 17 00:00:00 2001 From: Jack Danger Canty Date: Mon, 12 Jun 2017 16:42:42 -0700 Subject: [PATCH] create_load_balancer requires port definitions Throw the appropriate error when defining a loadbalancer with no ports --- moto/elb/exceptions.py | 8 ++++++++ moto/elb/models.py | 7 +++++-- tests/test_elb/test_elb.py | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/moto/elb/exceptions.py b/moto/elb/exceptions.py index 897bd6dd1..071181a6c 100644 --- a/moto/elb/exceptions.py +++ b/moto/elb/exceptions.py @@ -47,3 +47,11 @@ class DuplicateLoadBalancerName(ELBClientError): "DuplicateLoadBalancerName", "The specified load balancer name already exists for this account: {0}" .format(name)) + + +class EmptyListenersError(ELBClientError): + + def __init__(self): + super(EmptyListenersError, self).__init__( + "ValidationError", + "Listeners cannot be empty") diff --git a/moto/elb/models.py b/moto/elb/models.py index 9ca6bdb4d..5b6a58bb9 100644 --- a/moto/elb/models.py +++ b/moto/elb/models.py @@ -16,10 +16,11 @@ from moto.compat import OrderedDict from moto.core import BaseBackend, BaseModel from moto.ec2.models import ec2_backends from .exceptions import ( - LoadBalancerNotFoundError, - TooManyTagsError, BadHealthCheckDefinition, DuplicateLoadBalancerName, + EmptyListenersError, + LoadBalancerNotFoundError, + TooManyTagsError, ) @@ -239,6 +240,8 @@ class ELBBackend(BaseBackend): vpc_id = subnet.vpc_id if name in self.load_balancers: raise DuplicateLoadBalancerName(name) + if not ports: + raise EmptyListenersError() new_load_balancer = FakeLoadBalancer( name=name, zones=zones, ports=ports, scheme=scheme, subnets=subnets, vpc_id=vpc_id) self.load_balancers[name] = new_load_balancer diff --git a/tests/test_elb/test_elb.py b/tests/test_elb/test_elb.py index f413e4731..36f96c0e2 100644 --- a/tests/test_elb/test_elb.py +++ b/tests/test_elb/test_elb.py @@ -15,7 +15,9 @@ from boto.ec2.elb.policies import ( LBCookieStickinessPolicy, OtherPolicy, ) +from botocore.exceptions import ClientError from boto.exception import BotoServerError +from nose.tools import assert_raises import sure # noqa from moto import mock_elb, mock_ec2, mock_elb_deprecated, mock_ec2_deprecated @@ -109,6 +111,18 @@ def test_create_and_delete_boto3_support(): 'LoadBalancerDescriptions']).should.have.length_of(0) +@mock_elb +def test_create_load_balancer_with_no_listeners_defined(): + client = boto3.client('elb', region_name='us-east-1') + + with assert_raises(ClientError): + client.create_load_balancer( + LoadBalancerName='my-lb', + Listeners=[], + AvailabilityZones=['us-east-1a', 'us-east-1b'] + ) + + @mock_elb def test_describe_paginated_balancers(): client = boto3.client('elb', region_name='us-east-1')