Add sub-minimal mocking of elasticbeanstalk:create_application()

This commit is contained in:
Niels Laukens 2019-09-03 16:10:32 +02:00
parent c95d472bf5
commit 336f50349a
No known key found for this signature in database
GPG Key ID: D1397B5A6435A6D8
7 changed files with 181 additions and 5 deletions

View File

@ -18,6 +18,7 @@ from .datapipeline import mock_datapipeline, mock_datapipeline_deprecated # fla
from .dynamodb import mock_dynamodb, mock_dynamodb_deprecated # flake8: noqa
from .dynamodb2 import mock_dynamodb2, mock_dynamodb2_deprecated # flake8: noqa
from .dynamodbstreams import mock_dynamodbstreams # flake8: noqa
from .eb import mock_eb # flake8: noqa
from .ec2 import mock_ec2, mock_ec2_deprecated # flake8: noqa
from .ecr import mock_ecr, mock_ecr_deprecated # flake8: noqa
from .ecs import mock_ecs, mock_ecs_deprecated # flake8: noqa

4
moto/eb/__init__.py Normal file
View File

@ -0,0 +1,4 @@
from .models import eb_backends
from moto.core.models import base_decorator
mock_eb = base_decorator(eb_backends)

7
moto/eb/exceptions.py Normal file
View File

@ -0,0 +1,7 @@
from moto.core.exceptions import RESTError
class InvalidParameterValueError(RESTError):
def __init__(self, message):
super(InvalidParameterValueError, self).__init__(
"InvalidParameterValue", message)

37
moto/eb/models.py Normal file
View File

@ -0,0 +1,37 @@
import boto.beanstalk
from moto.core import BaseBackend, BaseModel
from .exceptions import InvalidParameterValueError
class FakeApplication(BaseModel):
def __init__(self, application_name):
self.application_name = application_name
class EBBackend(BaseBackend):
def __init__(self, region):
self.region = region
self.applications = dict()
def reset(self):
# preserve region
region = self.region
self._reset_model_refs()
self.__dict__ = {}
self.__init__(region)
def create_application(self, application_name):
if application_name in self.applications:
raise InvalidParameterValueError(
"Application {} already exists.".format(application_name)
)
new_app = FakeApplication(
application_name=application_name,
)
self.applications[application_name] = new_app
return new_app
eb_backends = dict((region.name, EBBackend(region.name))
for region in boto.beanstalk.regions())

92
moto/eb/responses.py Normal file
View File

@ -0,0 +1,92 @@
from moto.core.responses import BaseResponse
from .models import eb_backends
EB_CREATE_APPLICATION = """
<CreateApplicationResponse xmlns="http://elasticbeanstalk.amazonaws.com/docs/2010-12-01/">
<CreateApplicationResult>
<Application>
<ConfigurationTemplates/>
<DateCreated>2019-09-03T13:08:29.049Z</DateCreated>
<ResourceLifecycleConfig>
<VersionLifecycleConfig>
<MaxAgeRule>
<DeleteSourceFromS3>false</DeleteSourceFromS3>
<MaxAgeInDays>180</MaxAgeInDays>
<Enabled>false</Enabled>
</MaxAgeRule>
<MaxCountRule>
<DeleteSourceFromS3>false</DeleteSourceFromS3>
<MaxCount>200</MaxCount>
<Enabled>false</Enabled>
</MaxCountRule>
</VersionLifecycleConfig>
</ResourceLifecycleConfig>
<ApplicationArn>arn:aws:elasticbeanstalk:{{ region_name }}:111122223333:application/{{ application_name }}</ApplicationArn>
<ApplicationName>{{ application.application_name }}</ApplicationName>
<DateUpdated>2019-09-03T13:08:29.049Z</DateUpdated>
</Application>
</CreateApplicationResult>
<ResponseMetadata>
<RequestId>1b6173c8-13aa-4b0a-99e9-eb36a1fb2778</RequestId>
</ResponseMetadata>
</CreateApplicationResponse>
"""
EB_DESCRIBE_APPLICATIONS = """
<DescribeApplicationsResponse xmlns="http://elasticbeanstalk.amazonaws.com/docs/2010-12-01/">
<DescribeApplicationsResult>
<Applications>
{% for application in applications %}
<member>
<ConfigurationTemplates/>
<DateCreated>2019-09-03T13:08:29.049Z</DateCreated>
<ResourceLifecycleConfig>
<VersionLifecycleConfig>
<MaxAgeRule>
<MaxAgeInDays>180</MaxAgeInDays>
<DeleteSourceFromS3>false</DeleteSourceFromS3>
<Enabled>false</Enabled>
</MaxAgeRule>
<MaxCountRule>
<DeleteSourceFromS3>false</DeleteSourceFromS3>
<MaxCount>200</MaxCount>
<Enabled>false</Enabled>
</MaxCountRule>
</VersionLifecycleConfig>
</ResourceLifecycleConfig>
<ApplicationArn>arn:aws:elasticbeanstalk:{{ region_name }}:387323646340:application/{{ application.name }}</ApplicationArn>
<ApplicationName>{{ application.application_name }}</ApplicationName>
<DateUpdated>2019-09-03T13:08:29.049Z</DateUpdated>
</member>
{% endfor %}
</Applications>
</DescribeApplicationsResult>
<ResponseMetadata>
<RequestId>015a05eb-282e-4b76-bd18-663fdfaf42e4</RequestId>
</ResponseMetadata>
</DescribeApplicationsResponse>
"""
class EBResponse(BaseResponse):
@property
def backend(self):
return eb_backends[self.region]
def create_application(self):
app = self.backend.create_application(
application_name=self._get_param('ApplicationName'),
)
template = self.response_template(EB_CREATE_APPLICATION)
return template.render(
region_name=self.backend.region,
application=app,
)
def describe_applications(self):
template = self.response_template(EB_DESCRIBE_APPLICATIONS)
return template.render(
applications=self.backend.applications.values(),
)

11
moto/eb/urls.py Normal file
View File

@ -0,0 +1,11 @@
from __future__ import unicode_literals
from .responses import EBResponse
url_bases = [
r"https?://elasticbeanstalk.(?P<region>[a-zA-Z0-9\-_]+).amazonaws.com",
]
url_paths = {
'{0}/$': EBResponse.dispatch,
}

View File

@ -1,15 +1,39 @@
import boto3
import sure # noqa
from botocore.exceptions import ClientError
from moto import mock_eb
@mock_eb
def test_application():
def test_create_application():
# Create Elastic Beanstalk Application
eb_client = boto3.client('elasticbeanstalk', region_name='us-east-1')
conn = boto3.client('elasticbeanstalk', region_name='us-east-1')
app = conn.create_application(
ApplicationName="myapp",
)
app['Application']['ApplicationName'].should.equal("myapp")
eb_client.create_application(
@mock_eb
def test_create_application_dup():
conn = boto3.client('elasticbeanstalk', region_name='us-east-1')
conn.create_application(
ApplicationName="myapp",
)
conn.create_application.when.called_with(
ApplicationName="myapp",
).should.throw(ClientError)
@mock_eb
def test_describe_applications():
# Create Elastic Beanstalk Application
conn = boto3.client('elasticbeanstalk', region_name='us-east-1')
conn.create_application(
ApplicationName="myapp",
)
eb_apps = eb_client.describe_applications()
eb_apps['Applications'][0]['ApplicationName'].should.equal("myapp")
apps = conn.describe_applications()
len(apps['Applications']).should.equal(1)
apps['Applications'][0]['ApplicationName'].should.equal('myapp')