cleanup code.

This commit is contained in:
Steve Pulec 2015-11-27 14:43:03 -05:00
parent 1df454a632
commit 0df03ba409
9 changed files with 47 additions and 92 deletions

View File

@ -15,7 +15,6 @@ from moto.kms import kms_backend
from moto.rds import rds_backend
from moto.redshift import redshift_backend
from moto.s3 import s3_backend
from moto.s3bucket_path import s3bucket_path_backend
from moto.ses import ses_backend
from moto.sns import sns_backend
from moto.sqs import sqs_backend
@ -39,7 +38,7 @@ BACKENDS = {
'redshift': redshift_backend,
'rds': rds_backend,
's3': s3_backend,
's3bucket_path': s3bucket_path_backend,
's3bucket_path': s3_backend,
'ses': ses_backend,
'sns': sns_backend,
'sqs': sqs_backend,

View File

@ -29,20 +29,9 @@ def is_delete_keys(path, bucket_name):
class ResponseObject(_TemplateEnvironmentMixin):
def __init__(self, backend, parse_key_name, bucket_name_from_url,
is_delete_keys=None):
def __init__(self, backend):
super(ResponseObject, self).__init__()
self.backend = backend
# self.bucket_name_from_url = bucket_name_from_url
# self.parse_key_name = parse_key_name
# if is_delete_keys:
# self.is_delete_keys = is_delete_keys
def is_delete_keys(self, request, path, bucket_name):
if self.is_path_based_buckets(request):
return bucketpath_is_delete_keys(path, bucket_name)
else:
return is_delete_keys(path, bucket_name)
def all_buckets(self):
# No bucket specified. Listing all buckets
@ -50,29 +39,39 @@ class ResponseObject(_TemplateEnvironmentMixin):
template = self.response_template(S3_ALL_BUCKETS)
return template.render(buckets=all_buckets)
def is_path_based_buckets(self, request):
return request.headers['host'] == 's3.amazonaws.com'
def subdomain_based_buckets(self, request):
host = request.headers['host']
if host.startswith("localhost"):
# For localhost, default to path-based buckets
return False
return host != 's3.amazonaws.com' and not re.match("s3.(.*).amazonaws.com", host)
def is_delete_keys(self, request, path, bucket_name):
if self.subdomain_based_buckets(request):
return is_delete_keys(path, bucket_name)
else:
return bucketpath_is_delete_keys(path, bucket_name)
def parse_bucket_name_from_url(self, request, url):
if self.is_path_based_buckets(request):
return bucketpath_bucket_name_from_url(url)
else:
if self.subdomain_based_buckets(request):
return bucket_name_from_url(url)
else:
return bucketpath_bucket_name_from_url(url)
def parse_key_name(self, request, url):
if self.is_path_based_buckets(request):
return bucketpath_parse_key_name(url)
else:
if self.subdomain_based_buckets(request):
return parse_key_name(url)
else:
return bucketpath_parse_key_name(url)
def response(self, request, full_url, headers):
def ambiguous_response(self, request, full_url, headers):
# Depending on which calling format the client is using, we don't know
# if this is a bucket or key request so we have to check
if self.is_path_based_buckets(request):
if self.subdomain_based_buckets(request):
return self.key_response(request, full_url, headers)
else:
# Using path-based buckets
return self.bucket_response(request, full_url, headers)
else:
return self.key_response(request, full_url, headers)
def bucket_response(self, request, full_url, headers):
try:
@ -559,7 +558,7 @@ class ResponseObject(_TemplateEnvironmentMixin):
else:
raise NotImplementedError("Method POST had only been implemented for multipart uploads and restore operations, so far")
S3ResponseInstance = ResponseObject(s3_backend, bucket_name_from_url, parse_key_name)
S3ResponseInstance = ResponseObject(s3_backend)
S3_ALL_BUCKETS = """<ListAllMyBucketsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01">
<Owner>

View File

@ -11,14 +11,8 @@ url_paths = {
'{0}/$': S3ResponseInstance.bucket_response,
# subdomain key of path-based bucket
'{0}/(?P<key_name>.+)': S3ResponseInstance.response,
'{0}/(?P<key_or_bucket_name>.+)': S3ResponseInstance.ambiguous_response,
# path-based bucket + key
'{0}/(?P<bucket_name_path>[a-zA-Z0-9\-_./]+)/(?P<key_name>.+)': S3ResponseInstance.key_response,
# '{0}/(?P<bucket_name>[a-zA-Z0-9\-_.]+)$': ro.bucket_response,
# '{0}/(?P<bucket_name>[a-zA-Z0-9\-_.]+)/$': bucket_response2,
}

View File

@ -1,6 +1,4 @@
from __future__ import unicode_literals
# from .models import s3bucket_path_backend
from moto import mock_s3
# mock_s3bucket_path = s3bucket_path_backend.decorator
from moto import mock_s3
mock_s3bucket_path = mock_s3

View File

@ -1,8 +0,0 @@
from __future__ import unicode_literals
from moto.s3.models import S3Backend
class S3BucketPathBackend(S3Backend):
pass
s3bucket_path_backend = S3BucketPathBackend()

View File

@ -1,14 +0,0 @@
from __future__ import unicode_literals
from .models import s3bucket_path_backend
from .utils import bucket_name_from_url, parse_key_name, is_delete_keys
from moto.s3.responses import ResponseObject
S3BucketPathResponseInstance = ResponseObject(
s3bucket_path_backend,
bucket_name_from_url,
parse_key_name,
is_delete_keys,
)

View File

@ -1,21 +0,0 @@
from __future__ import unicode_literals
from .responses import S3BucketPathResponseInstance as ro
url_bases = [
"https?://s3(.*).amazonaws.com"
]
def bucket_response2(*args):
return ro.bucket_response(*args)
def bucket_response3(*args):
return ro.bucket_response(*args)
url_paths = {
'{0}/$': bucket_response3,
'{0}/(?P<bucket_name>[a-zA-Z0-9\-_.]+)$': ro.bucket_response,
'{0}/(?P<bucket_name>[a-zA-Z0-9\-_.]+)/$': bucket_response2,
'{0}/(?P<bucket_name>[a-zA-Z0-9\-_./]+)/(?P<key_name>.+)': ro.key_response
}

View File

@ -961,6 +961,16 @@ def test_boto3_bucket_create():
s3.Object('blah', 'hello.txt').get()['Body'].read().decode("utf-8").should.equal("some text")
@mock_s3
def test_boto3_bucket_create_eu_central():
s3 = boto3.resource('s3', region_name='eu-central-1')
s3.create_bucket(Bucket="blah")
s3.Object('blah', 'hello.txt').put(Body="some text")
s3.Object('blah', 'hello.txt').get()['Body'].read().decode("utf-8").should.equal("some text")
@mock_s3
def test_boto3_head_object():
s3 = boto3.resource('s3', region_name='us-east-1')

View File

@ -7,14 +7,12 @@ import moto.server as server
Test the different server responses
'''
HEADERS = {'host': 's3.amazonaws.com'}
def test_s3_server_get():
backend = server.create_backend_app("s3bucket_path")
test_client = backend.test_client()
res = test_client.get('/', headers=HEADERS)
res = test_client.get('/')
res.data.should.contain(b'ListAllMyBucketsResult')
@ -23,23 +21,23 @@ def test_s3_server_bucket_create():
backend = server.create_backend_app("s3bucket_path")
test_client = backend.test_client()
res = test_client.put('/foobar/', 'http://localhost:5000', headers=HEADERS)
res = test_client.put('/foobar/', 'http://localhost:5000')
res.status_code.should.equal(200)
res = test_client.get('/', headers=HEADERS)
res = test_client.get('/')
res.data.should.contain(b'<Name>foobar</Name>')
res = test_client.get('/foobar/', 'http://localhost:5000', headers=HEADERS)
res = test_client.get('/foobar/', 'http://localhost:5000')
res.status_code.should.equal(200)
res.data.should.contain(b"ListBucketResult")
res = test_client.get('/missing-bucket/', 'http://localhost:5000', headers=HEADERS)
res = test_client.get('/missing-bucket/', 'http://localhost:5000')
res.status_code.should.equal(404)
res = test_client.put('/foobar/bar/', 'http://localhost:5000', data='test value', headers=HEADERS)
res = test_client.put('/foobar/bar/', 'http://localhost:5000', data='test value')
res.status_code.should.equal(200)
res = test_client.get('/foobar/bar/', 'http://localhost:5000', headers=HEADERS)
res = test_client.get('/foobar/bar/', 'http://localhost:5000')
res.status_code.should.equal(200)
res.data.should.equal(b"test value")
@ -48,14 +46,14 @@ def test_s3_server_post_to_bucket():
backend = server.create_backend_app("s3bucket_path")
test_client = backend.test_client()
res = test_client.put('/foobar2/', 'http://localhost:5000/', headers=HEADERS)
res = test_client.put('/foobar2/', 'http://localhost:5000/')
res.status_code.should.equal(200)
test_client.post('/foobar2/', "https://localhost:5000/", data={
'key': 'the-key',
'file': 'nothing'
}, headers=HEADERS)
})
res = test_client.get('/foobar2/the-key/', 'http://localhost:5000/', headers=HEADERS)
res = test_client.get('/foobar2/the-key/', 'http://localhost:5000/')
res.status_code.should.equal(200)
res.data.should.equal(b"nothing")