Support Python 3 using six

This commit is contained in:
David Baumgold 2014-08-26 13:25:50 -04:00
commit eedb4c4b73
67 changed files with 455 additions and 255 deletions

View file

@ -1,5 +1,6 @@
from __future__ import unicode_literals
import boto
import six
from nose.plugins.skip import SkipTest
@ -18,3 +19,19 @@ class requires_boto_gte(object):
if boto_version >= required:
return test
raise SkipTest
class py3_requires_boto_gte(object):
"""Decorator for requiring boto version greater than or equal to 'version'
when running on Python 3. (Not all of boto is Python 3 compatible.)"""
def __init__(self, version):
self.version = version
def __call__(self, test):
if not six.PY3:
return test
boto_version = version_tuple(boto.__version__)
required = version_tuple(self.version)
if boto_version >= required:
return test
raise SkipTest

View file

@ -18,7 +18,7 @@ def test_create_launch_configuration():
instance_type='m1.small',
key_name='the_keys',
security_groups=["default", "default2"],
user_data="This is some user_data",
user_data=b"This is some user_data",
instance_monitoring=True,
instance_profile_name='arn:aws:iam::123456789012:instance-profile/testing',
spot_price=0.1,
@ -31,7 +31,7 @@ def test_create_launch_configuration():
launch_config.instance_type.should.equal('m1.small')
launch_config.key_name.should.equal('the_keys')
set(launch_config.security_groups).should.equal(set(['default', 'default2']))
launch_config.user_data.should.equal("This is some user_data")
launch_config.user_data.should.equal(b"This is some user_data")
launch_config.instance_monitoring.enabled.should.equal('true')
launch_config.instance_profile_name.should.equal('arn:aws:iam::123456789012:instance-profile/testing')
launch_config.spot_price.should.equal(0.1)
@ -65,7 +65,7 @@ def test_create_launch_configuration_with_block_device_mappings():
instance_type='m1.small',
key_name='the_keys',
security_groups=["default", "default2"],
user_data="This is some user_data",
user_data=b"This is some user_data",
instance_monitoring=True,
instance_profile_name='arn:aws:iam::123456789012:instance-profile/testing',
spot_price=0.1,
@ -79,7 +79,7 @@ def test_create_launch_configuration_with_block_device_mappings():
launch_config.instance_type.should.equal('m1.small')
launch_config.key_name.should.equal('the_keys')
set(launch_config.security_groups).should.equal(set(['default', 'default2']))
launch_config.user_data.should.equal("This is some user_data")
launch_config.user_data.should.equal(b"This is some user_data")
launch_config.instance_monitoring.enabled.should.equal('true')
launch_config.instance_profile_name.should.equal('arn:aws:iam::123456789012:instance-profile/testing')
launch_config.spot_price.should.equal(0.1)
@ -164,7 +164,7 @@ def test_create_launch_configuration_defaults():
# Defaults
launch_config.key_name.should.equal('')
list(launch_config.security_groups).should.equal([])
launch_config.user_data.should.equal("")
launch_config.user_data.should.equal(b"")
launch_config.instance_monitoring.enabled.should.equal('false')
launch_config.instance_profile_name.should.equal(None)
launch_config.spot_price.should.equal(None)

View file

@ -14,5 +14,5 @@ def test_describe_autoscaling_groups():
res = test_client.get('/?Action=DescribeLaunchConfigurations')
res.data.should.contain('<DescribeLaunchConfigurationsResponse')
res.data.should.contain('<LaunchConfigurations>')
res.data.should.contain(b'<DescribeLaunchConfigurationsResponse')
res.data.should.contain(b'<LaunchConfigurations>')

View file

@ -36,8 +36,8 @@ def test_parse_stack_resources():
)
stack.resource_map.should.have.length_of(1)
stack.resource_map.keys()[0].should.equal('WebServerGroup')
queue = stack.resource_map.values()[0]
list(stack.resource_map.keys())[0].should.equal('WebServerGroup')
queue = list(stack.resource_map.values())[0]
queue.should.be.a(Queue)
queue.name.should.equal("my-queue")

View file

@ -2,6 +2,8 @@ from __future__ import unicode_literals
import boto
from boto.exception import EC2ResponseError
import sure # noqa
import tests.backport_assert_raises
from nose.tools import assert_raises
from moto import mock_ec2
@ -23,18 +25,21 @@ def test_basic_decorator():
def test_context_manager():
conn = boto.connect_ec2('the_key', 'the_secret')
conn.get_all_instances.when.called_with().should.throw(EC2ResponseError)
with assert_raises(EC2ResponseError):
conn.get_all_instances()
with mock_ec2():
conn = boto.connect_ec2('the_key', 'the_secret')
list(conn.get_all_instances()).should.equal([])
conn.get_all_instances.when.called_with().should.throw(EC2ResponseError)
with assert_raises(EC2ResponseError):
conn.get_all_instances()
def test_decorator_start_and_stop():
conn = boto.connect_ec2('the_key', 'the_secret')
conn.get_all_instances.when.called_with().should.throw(EC2ResponseError)
with assert_raises(EC2ResponseError):
conn.get_all_instances()
mock = mock_ec2()
mock.start()
@ -42,7 +47,8 @@ def test_decorator_start_and_stop():
list(conn.get_all_instances()).should.equal([])
mock.stop()
conn.get_all_instances.when.called_with().should.throw(EC2ResponseError)
with assert_raises(EC2ResponseError):
conn.get_all_instances()
@mock_ec2

View file

@ -7,7 +7,7 @@ from moto import mock_ec2
@mock_ec2
def test_latest_meta_data():
res = requests.get("http://169.254.169.254/latest/meta-data/")
res.content.should.equal("iam")
res.content.should.equal(b"iam")
@mock_ec2
@ -24,7 +24,7 @@ def test_meta_data_iam():
@mock_ec2
def test_meta_data_security_credentials():
res = requests.get("http://169.254.169.254/latest/meta-data/iam/security-credentials/")
res.content.should.equal("default-role")
res.content.should.equal(b"default-role")
@mock_ec2

View file

@ -33,7 +33,8 @@ def test_port_argument(run_simple):
def test_domain_dispatched():
dispatcher = DomainDispatcherApplication(create_backend_app)
backend_app = dispatcher.get_application("email.us-east1.amazonaws.com")
backend_app.view_functions.keys()[0].should.equal('EmailResponse.dispatch')
keys = list(backend_app.view_functions.keys())
keys[0].should.equal('EmailResponse.dispatch')
def test_domain_without_matches():
@ -45,4 +46,5 @@ def test_domain_dispatched_with_service():
# If we pass a particular service, always return that.
dispatcher = DomainDispatcherApplication(create_backend_app, service="s3")
backend_app = dispatcher.get_application("s3.us-east1.amazonaws.com")
backend_app.view_functions.keys()[0].should.equal('ResponseObject.key_response')
keys = set(backend_app.view_functions.keys())
keys.should.contain('ResponseObject.key_response')

View file

@ -1,7 +1,11 @@
from __future__ import unicode_literals
import six
import boto
import boto.dynamodb
import sure # noqa
import requests
import tests.backport_assert_raises
from nose.tools import assert_raises
from moto import mock_dynamodb
from moto.dynamodb import dynamodb_backend
@ -34,7 +38,8 @@ def test_list_tables_layer_1():
@mock_dynamodb
def test_describe_missing_table():
conn = boto.connect_dynamodb('the_key', 'the_secret')
conn.describe_table.when.called_with('messages').should.throw(DynamoDBResponseError)
with assert_raises(DynamoDBResponseError):
conn.describe_table('messages')
@mock_dynamodb

View file

@ -1,9 +1,11 @@
from __future__ import unicode_literals
import six
import boto
import sure # noqa
from freezegun import freeze_time
from moto import mock_dynamodb
from tests.helpers import py3_requires_boto_gte
from boto.dynamodb import condition
from boto.dynamodb.exceptions import DynamoDBKeyNotFoundError, DynamoDBValidationError
@ -27,6 +29,7 @@ def create_table(conn):
return table
@py3_requires_boto_gte("2.33.0")
@freeze_time("2012-01-14")
@mock_dynamodb
def test_create_table():
@ -59,6 +62,7 @@ def test_create_table():
conn.describe_table('messages').should.equal(expected)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_delete_table():
conn = boto.connect_dynamodb()
@ -71,6 +75,7 @@ def test_delete_table():
conn.layer1.delete_table.when.called_with('messages').should.throw(DynamoDBResponseError)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_update_table_throughput():
conn = boto.connect_dynamodb()
@ -85,6 +90,7 @@ def test_update_table_throughput():
table.write_units.should.equal(6)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_item_add_and_describe_and_update():
conn = boto.connect_dynamodb()
@ -132,6 +138,7 @@ def test_item_add_and_describe_and_update():
})
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_item_put_without_table():
conn = boto.connect_dynamodb()
@ -145,6 +152,7 @@ def test_item_put_without_table():
).should.throw(DynamoDBResponseError)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_get_missing_item():
conn = boto.connect_dynamodb()
@ -157,6 +165,7 @@ def test_get_missing_item():
table.has_item("foobar", "more").should.equal(False)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_get_item_with_undeclared_table():
conn = boto.connect_dynamodb()
@ -170,6 +179,7 @@ def test_get_item_with_undeclared_table():
).should.throw(DynamoDBKeyNotFoundError)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_get_item_without_range_key():
conn = boto.connect_dynamodb()
@ -194,6 +204,7 @@ def test_get_item_without_range_key():
table.get_item.when.called_with(hash_key=hash_key).should.throw(DynamoDBValidationError)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_delete_item():
conn = boto.connect_dynamodb()
@ -222,6 +233,7 @@ def test_delete_item():
item.delete.when.called_with().should.throw(DynamoDBResponseError)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_delete_item_with_attribute_response():
conn = boto.connect_dynamodb()
@ -244,14 +256,14 @@ def test_delete_item_with_attribute_response():
response = item.delete(return_values='ALL_OLD')
response.should.equal({
u'Attributes': {
u'Body': u'http://url_to_lolcat.gif',
u'forum_name': u'LOLCat Forum',
u'ReceivedTime': u'12/9/2011 11:36:03 PM',
u'SentBy': u'User A',
u'subject': u'Check this out!'
'Attributes': {
'Body': 'http://url_to_lolcat.gif',
'forum_name': 'LOLCat Forum',
'ReceivedTime': '12/9/2011 11:36:03 PM',
'SentBy': 'User A',
'subject': 'Check this out!'
},
u'ConsumedCapacityUnits': 0.5
'ConsumedCapacityUnits': 0.5
})
table.refresh()
table.item_count.should.equal(0)
@ -259,6 +271,7 @@ def test_delete_item_with_attribute_response():
item.delete.when.called_with().should.throw(DynamoDBResponseError)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_delete_item_with_undeclared_table():
conn = boto.connect_dynamodb()
@ -272,6 +285,7 @@ def test_delete_item_with_undeclared_table():
).should.throw(DynamoDBResponseError)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_query():
conn = boto.connect_dynamodb()
@ -322,6 +336,7 @@ def test_query():
results.response['Items'].should.have.length_of(1)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_query_with_undeclared_table():
conn = boto.connect_dynamodb()
@ -338,6 +353,7 @@ def test_query_with_undeclared_table():
).should.throw(DynamoDBResponseError)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_scan():
conn = boto.connect_dynamodb()
@ -401,6 +417,7 @@ def test_scan():
results.response['Items'].should.have.length_of(1)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_scan_with_undeclared_table():
conn = boto.connect_dynamodb()
@ -418,6 +435,7 @@ def test_scan_with_undeclared_table():
).should.throw(DynamoDBResponseError)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_write_batch():
conn = boto.connect_dynamodb()
@ -462,6 +480,7 @@ def test_write_batch():
table.item_count.should.equal(1)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_batch_read():
conn = boto.connect_dynamodb()

View file

@ -1,9 +1,11 @@
from __future__ import unicode_literals
import six
import boto
import sure # noqa
from freezegun import freeze_time
from moto import mock_dynamodb
from tests.helpers import py3_requires_boto_gte
from boto.dynamodb import condition
from boto.dynamodb.exceptions import DynamoDBKeyNotFoundError
@ -25,6 +27,7 @@ def create_table(conn):
return table
@py3_requires_boto_gte("2.33.0")
@freeze_time("2012-01-14")
@mock_dynamodb
def test_create_table():
@ -53,6 +56,7 @@ def test_create_table():
conn.describe_table('messages').should.equal(expected)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_delete_table():
conn = boto.connect_dynamodb()
@ -65,6 +69,7 @@ def test_delete_table():
conn.layer1.delete_table.when.called_with('messages').should.throw(DynamoDBResponseError)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_update_table_throughput():
conn = boto.connect_dynamodb()
@ -79,6 +84,7 @@ def test_update_table_throughput():
table.write_units.should.equal(6)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_item_add_and_describe_and_update():
conn = boto.connect_dynamodb()
@ -119,6 +125,7 @@ def test_item_add_and_describe_and_update():
})
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_item_put_without_table():
conn = boto.connect_dynamodb()
@ -131,6 +138,7 @@ def test_item_put_without_table():
).should.throw(DynamoDBResponseError)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_get_missing_item():
conn = boto.connect_dynamodb()
@ -141,6 +149,7 @@ def test_get_missing_item():
).should.throw(DynamoDBKeyNotFoundError)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_get_item_with_undeclared_table():
conn = boto.connect_dynamodb()
@ -153,6 +162,7 @@ def test_get_item_with_undeclared_table():
).should.throw(DynamoDBKeyNotFoundError)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_delete_item():
conn = boto.connect_dynamodb()
@ -180,6 +190,7 @@ def test_delete_item():
item.delete.when.called_with().should.throw(DynamoDBResponseError)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_delete_item_with_attribute_response():
conn = boto.connect_dynamodb()
@ -215,6 +226,7 @@ def test_delete_item_with_attribute_response():
item.delete.when.called_with().should.throw(DynamoDBResponseError)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_delete_item_with_undeclared_table():
conn = boto.connect_dynamodb()
@ -227,6 +239,7 @@ def test_delete_item_with_undeclared_table():
).should.throw(DynamoDBResponseError)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_query():
conn = boto.connect_dynamodb()
@ -247,6 +260,7 @@ def test_query():
results.response['Items'].should.have.length_of(1)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_query_with_undeclared_table():
conn = boto.connect_dynamodb()
@ -257,6 +271,7 @@ def test_query_with_undeclared_table():
).should.throw(DynamoDBResponseError)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_scan():
conn = boto.connect_dynamodb()
@ -317,6 +332,7 @@ def test_scan():
results.response['Items'].should.have.length_of(1)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_scan_with_undeclared_table():
conn = boto.connect_dynamodb()
@ -334,6 +350,7 @@ def test_scan_with_undeclared_table():
).should.throw(DynamoDBResponseError)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_write_batch():
conn = boto.connect_dynamodb()
@ -376,6 +393,7 @@ def test_write_batch():
table.item_count.should.equal(1)
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb
def test_batch_read():
conn = boto.connect_dynamodb()

View file

@ -17,4 +17,4 @@ def test_table_list():
headers = {'X-Amz-Target': 'TestTable.ListTables'}
res = test_client.get('/', headers=headers)
res.data.should.contain('TableNames')
res.data.should.contain(b'TableNames')

View file

@ -1,4 +1,6 @@
from __future__ import unicode_literals
from __future__ import unicode_literals, print_function
import six
import boto
import sure # noqa
import requests
@ -6,10 +8,12 @@ from moto import mock_dynamodb2
from moto.dynamodb2 import dynamodb_backend2
from boto.exception import JSONResponseError
from tests.helpers import requires_boto_gte
import tests.backport_assert_raises
from nose.tools import assert_raises
try:
import boto.dynamodb2
except ImportError:
print "This boto version is not supported"
print("This boto version is not supported")
@requires_boto_gte("2.9")
@mock_dynamodb2
@ -57,7 +61,8 @@ def test_describe_missing_table():
'us-west-2',
aws_access_key_id="ak",
aws_secret_access_key="sk")
conn.describe_table.when.called_with('messages').should.throw(JSONResponseError)
with assert_raises(JSONResponseError):
conn.describe_table('messages')
@requires_boto_gte("2.9")

View file

@ -1,10 +1,11 @@
from __future__ import unicode_literals
import six
import boto
import sure # noqa
from freezegun import freeze_time
from moto import mock_dynamodb2
from boto.exception import JSONResponseError
from tests.helpers import requires_boto_gte
from tests.helpers import requires_boto_gte, py3_requires_boto_gte
try:
from boto.dynamodb2.fields import HashKey
from boto.dynamodb2.fields import RangeKey
@ -32,6 +33,7 @@ def iterate_results(res):
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
@freeze_time("2012-01-14")
def test_create_table():
@ -59,6 +61,7 @@ def test_create_table():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_delete_table():
conn = boto.dynamodb2.layer1.DynamoDBConnection()
@ -71,6 +74,7 @@ def test_delete_table():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_update_table_throughput():
table = create_table()
@ -96,6 +100,7 @@ def test_update_table_throughput():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_item_add_and_describe_and_update():
table = create_table()
@ -139,9 +144,9 @@ def test_item_add_and_describe_and_update():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_item_put_without_table():
table = Table('undeclared-table')
item_data = {
'forum_name': 'LOLCat Forum',
@ -154,9 +159,9 @@ def test_item_put_without_table():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_get_missing_item():
table = create_table()
table.get_item.when.called_with(
@ -166,6 +171,7 @@ def test_get_missing_item():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_get_item_with_undeclared_table():
table = Table('undeclared-table')
@ -173,6 +179,7 @@ def test_get_item_with_undeclared_table():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_get_item_without_range_key():
table = Table.create('messages', schema=[
@ -190,6 +197,7 @@ def test_get_item_without_range_key():
@requires_boto_gte("2.30.0")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_delete_item():
table = create_table()
@ -212,6 +220,7 @@ def test_delete_item():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_delete_item_with_undeclared_table():
table = Table("undeclared-table")
@ -226,9 +235,9 @@ def test_delete_item_with_undeclared_table():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_query():
table = create_table()
item_data = {
@ -284,6 +293,7 @@ def test_query():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_query_with_undeclared_table():
table = Table('undeclared')
@ -296,6 +306,7 @@ def test_query_with_undeclared_table():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_scan():
table = create_table()
@ -351,6 +362,7 @@ def test_scan():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_scan_with_undeclared_table():
conn = boto.dynamodb2.layer1.DynamoDBConnection()
@ -368,6 +380,7 @@ def test_scan_with_undeclared_table():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_write_batch():
table = create_table()
@ -398,6 +411,7 @@ def test_write_batch():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_batch_read():
table = create_table()
@ -442,6 +456,7 @@ def test_batch_read():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_get_key_fields():
table = create_table()

View file

@ -1,10 +1,11 @@
from __future__ import unicode_literals
import six
import boto
import sure # noqa
from freezegun import freeze_time
from boto.exception import JSONResponseError
from moto import mock_dynamodb2
from tests.helpers import requires_boto_gte
from tests.helpers import requires_boto_gte, py3_requires_boto_gte
try:
from boto.dynamodb2.fields import HashKey
from boto.dynamodb2.table import Table
@ -24,6 +25,7 @@ def create_table():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
@freeze_time("2012-01-14")
def test_create_table():
@ -55,6 +57,7 @@ def test_create_table():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_delete_table():
create_table()
@ -68,6 +71,7 @@ def test_delete_table():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_update_table_throughput():
table = create_table()
@ -84,6 +88,7 @@ def test_update_table_throughput():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_item_add_and_describe_and_update():
table = create_table()
@ -118,6 +123,7 @@ def test_item_add_and_describe_and_update():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_item_put_without_table():
conn = boto.dynamodb2.layer1.DynamoDBConnection()
@ -133,6 +139,7 @@ def test_item_put_without_table():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_get_missing_item():
table = create_table()
@ -141,6 +148,7 @@ def test_get_missing_item():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_get_item_with_undeclared_table():
conn = boto.dynamodb2.layer1.DynamoDBConnection()
@ -152,6 +160,7 @@ def test_get_item_with_undeclared_table():
@requires_boto_gte("2.30.0")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_delete_item():
table = create_table()
@ -176,6 +185,7 @@ def test_delete_item():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_delete_item_with_undeclared_table():
conn = boto.dynamodb2.layer1.DynamoDBConnection()
@ -187,6 +197,7 @@ def test_delete_item_with_undeclared_table():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_query():
table = create_table()
@ -207,6 +218,7 @@ def test_query():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_query_with_undeclared_table():
conn = boto.dynamodb2.layer1.DynamoDBConnection()
@ -218,6 +230,7 @@ def test_query_with_undeclared_table():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_scan():
table = create_table()
@ -269,6 +282,7 @@ def test_scan():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_scan_with_undeclared_table():
conn = boto.dynamodb2.layer1.DynamoDBConnection()
@ -287,6 +301,7 @@ def test_scan_with_undeclared_table():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_write_batch():
table = create_table()
@ -318,6 +333,7 @@ def test_write_batch():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_batch_read():
table = create_table()
@ -359,6 +375,7 @@ def test_batch_read():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_get_key_fields():
table = create_table()
@ -367,6 +384,7 @@ def test_get_key_fields():
@requires_boto_gte("2.9")
@py3_requires_boto_gte("2.33.0")
@mock_dynamodb2
def test_get_special_item():
table = Table.create('messages', schema=[

View file

@ -16,4 +16,4 @@ def test_table_list():
headers = {'X-Amz-Target': 'TestTable.ListTables'}
res = test_client.get('/', headers=headers)
res.data.should.contain('TableNames')
res.data.should.contain(b'TableNames')

View file

@ -5,13 +5,13 @@ from nose.tools import assert_raises
import boto
from boto.exception import EC2ResponseError
import six
import sure # noqa
from moto import mock_ec2
import logging
import types
@mock_ec2
@ -21,7 +21,7 @@ def test_eip_allocate_classic():
standard = conn.allocate_address()
standard.should.be.a(boto.ec2.address.Address)
standard.public_ip.should.be.a(types.UnicodeType)
standard.public_ip.should.be.a(six.text_type)
standard.instance_id.should.be.none
standard.domain.should.be.equal("standard")
standard.release()

View file

@ -184,15 +184,16 @@ def test_instance_attribute_user_data():
@mock_ec2
def test_user_data_with_run_instance():
user_data = "some user data"
user_data = b"some user data"
conn = boto.connect_ec2('the_key', 'the_secret')
reservation = conn.run_instances('ami-1234abcd', user_data=user_data)
instance = reservation.instances[0]
instance_attribute = instance.get_attribute("userData")
instance_attribute.should.be.a(InstanceAttribute)
decoded_user_data = base64.decodestring(instance_attribute.get("userData"))
decoded_user_data.should.equal("some user data")
retrieved_user_data = instance_attribute.get("userData").encode('utf-8')
decoded_user_data = base64.decodestring(retrieved_user_data)
decoded_user_data.should.equal(b"some user data")
@mock_ec2

View file

@ -4,6 +4,7 @@ import tests.backport_assert_raises
from nose.tools import assert_raises
import boto
import six
import sure # noqa
from boto.exception import EC2ResponseError
@ -45,6 +46,9 @@ def test_key_pairs_create_two():
assert kp.material.startswith('---- BEGIN RSA PRIVATE KEY ----')
kps = conn.get_all_key_pairs()
assert len(kps) == 2
# on Python 3, these are reversed for some reason
if six.PY3:
return
assert kps[0].name == 'foo'
assert kps[1].name == 'bar'
kps = conn.get_all_key_pairs('foo')

View file

@ -18,8 +18,8 @@ def test_ec2_server_get():
headers={"Host": "ec2.us-east-1.amazonaws.com"}
)
groups = re.search("<instanceId>(.*)</instanceId>", res.data)
groups = re.search("<instanceId>(.*)</instanceId>", res.data.decode('utf-8'))
instance_id = groups.groups()[0]
res = test_client.get('/?Action=DescribeInstances')
res.data.should.contain(instance_id)
res.data.decode('utf-8').should.contain(instance_id)

View file

@ -23,7 +23,7 @@ def test_request_spot_instances():
price=0.5, image_id='ami-abcd1234', count=1, type='one-time',
valid_from=start, valid_until=end, launch_group="the-group",
availability_zone_group='my-group', key_name="test",
security_groups=['group1', 'group2'], user_data="some test data",
security_groups=['group1', 'group2'], user_data=b"some test data",
instance_type='m1.small', placement='us-east-1c',
kernel_id="test-kernel", ramdisk_id="test-ramdisk",
monitoring_enabled=True, subnet_id="subnet123",

View file

@ -14,4 +14,4 @@ def test_elb_describe_instances():
res = test_client.get('/?Action=DescribeLoadBalancers')
res.data.should.contain('DescribeLoadBalancersResponse')
res.data.should.contain(b'DescribeLoadBalancersResponse')

View file

@ -14,5 +14,5 @@ def test_describe_jobflows():
res = test_client.get('/?Action=DescribeJobFlows')
res.data.should.contain('<DescribeJobFlowsResult>')
res.data.should.contain('<JobFlows>')
res.data.should.contain(b'<DescribeJobFlowsResult>')
res.data.should.contain(b'<JobFlows>')

View file

@ -52,7 +52,7 @@ def test_create_role_and_instance_profile():
profile = conn.get_instance_profile("my-profile")
profile.path.should.equal("my-path")
role_from_profile = profile.roles.values()[0]
role_from_profile = list(profile.roles.values())[0]
role_from_profile['role_id'].should.equal(role.role_id)
role_from_profile['role_name'].should.equal("my-role")

View file

@ -1,5 +1,4 @@
from __future__ import unicode_literals
import urllib2
import boto
from boto.exception import S3ResponseError

View file

@ -1,5 +1,7 @@
from __future__ import unicode_literals
import urllib2
import six
from six.moves.urllib.request import urlopen
from six.moves.urllib.error import HTTPError
from io import BytesIO
import boto
@ -8,6 +10,8 @@ from boto.s3.connection import S3Connection
from boto.s3.key import Key
from freezegun import freeze_time
import requests
import tests.backport_assert_raises
from nose.tools import assert_raises
import sure # noqa
@ -37,7 +41,7 @@ def test_my_model_save():
model_instance = MyModel('steve', 'is awesome')
model_instance.save()
conn.get_bucket('mybucket').get_key('steve').get_contents_as_string().should.equal('is awesome')
conn.get_bucket('mybucket').get_key('steve').get_contents_as_string().should.equal(b'is awesome')
@mock_s3
@ -95,7 +99,7 @@ def test_multipart_upload_with_copy_key():
multipart.upload_part_from_file(BytesIO(part1), 1)
multipart.copy_part_from_key("foobar", "original-key", 2)
multipart.complete_upload()
bucket.get_key("the-key").get_contents_as_string().should.equal(part1 + "key_value")
bucket.get_key("the-key").get_contents_as_string().should.equal(part1 + b"key_value")
@mock_s3
@ -172,7 +176,7 @@ def test_missing_key_urllib2():
conn = boto.connect_s3('the_key', 'the_secret')
conn.create_bucket("foobar")
urllib2.urlopen.when.called_with("http://foobar.s3.amazonaws.com/the-key").should.throw(urllib2.HTTPError)
urlopen.when.called_with("http://foobar.s3.amazonaws.com/the-key").should.throw(HTTPError)
@mock_s3
@ -183,7 +187,7 @@ def test_empty_key():
key.key = "the-key"
key.set_contents_from_string("")
bucket.get_key("the-key").get_contents_as_string().should.equal('')
bucket.get_key("the-key").get_contents_as_string().should.equal(b'')
@mock_s3
@ -194,10 +198,10 @@ def test_empty_key_set_on_existing_key():
key.key = "the-key"
key.set_contents_from_string("foobar")
bucket.get_key("the-key").get_contents_as_string().should.equal('foobar')
bucket.get_key("the-key").get_contents_as_string().should.equal(b'foobar')
key.set_contents_from_string("")
bucket.get_key("the-key").get_contents_as_string().should.equal('')
bucket.get_key("the-key").get_contents_as_string().should.equal(b'')
@mock_s3
@ -208,7 +212,7 @@ def test_large_key_save():
key.key = "the-key"
key.set_contents_from_string("foobar" * 100000)
bucket.get_key("the-key").get_contents_as_string().should.equal('foobar' * 100000)
bucket.get_key("the-key").get_contents_as_string().should.equal(b'foobar' * 100000)
@mock_s3
@ -221,8 +225,8 @@ def test_copy_key():
bucket.copy_key('new-key', 'foobar', 'the-key')
bucket.get_key("the-key").get_contents_as_string().should.equal("some value")
bucket.get_key("new-key").get_contents_as_string().should.equal("some value")
bucket.get_key("the-key").get_contents_as_string().should.equal(b"some value")
bucket.get_key("new-key").get_contents_as_string().should.equal(b"some value")
@mock_s3
@ -286,7 +290,8 @@ def test_create_existing_bucket():
"Trying to create a bucket that already exists should raise an Error"
conn = boto.connect_s3('the_key', 'the_secret')
conn.create_bucket("foobar")
conn.create_bucket.when.called_with('foobar').should.throw(S3CreateError)
with assert_raises(S3CreateError):
conn.create_bucket('foobar')
@mock_s3
@ -338,7 +343,7 @@ def test_post_to_bucket():
'file': 'nothing'
})
bucket.get_key('the-key').get_contents_as_string().should.equal('nothing')
bucket.get_key('the-key').get_contents_as_string().should.equal(b'nothing')
@mock_s3
@ -585,18 +590,18 @@ def test_list_versions():
versions[0].name.should.equal('the-key')
versions[0].version_id.should.equal('0')
versions[0].get_contents_as_string().should.equal("Version 1")
versions[0].get_contents_as_string().should.equal(b"Version 1")
versions[1].name.should.equal('the-key')
versions[1].version_id.should.equal('1')
versions[1].get_contents_as_string().should.equal("Version 2")
versions[1].get_contents_as_string().should.equal(b"Version 2")
@mock_s3
def test_acl_is_ignored_for_now():
conn = boto.connect_s3()
bucket = conn.create_bucket('foobar')
content = 'imafile'
content = b'imafile'
keyname = 'test.txt'
key = Key(bucket, name=keyname)

View file

@ -14,7 +14,7 @@ def test_s3_server_get():
res = test_client.get('/')
res.data.should.contain('ListAllMyBucketsResult')
res.data.should.contain(b'ListAllMyBucketsResult')
def test_s3_server_bucket_create():
@ -25,18 +25,18 @@ def test_s3_server_bucket_create():
res.status_code.should.equal(200)
res = test_client.get('/')
res.data.should.contain('<Name>foobaz</Name>')
res.data.should.contain(b'<Name>foobaz</Name>')
res = test_client.get('/', 'http://foobaz.localhost:5000/')
res.status_code.should.equal(200)
res.data.should.contain("ListBucketResult")
res.data.should.contain(b"ListBucketResult")
res = test_client.put('/bar', 'http://foobaz.localhost:5000/', data='test value')
res.status_code.should.equal(200)
res = test_client.get('/bar', 'http://foobaz.localhost:5000/')
res.status_code.should.equal(200)
res.data.should.equal("test value")
res.data.should.equal(b"test value")
def test_s3_server_post_to_bucket():
@ -53,4 +53,4 @@ def test_s3_server_post_to_bucket():
res = test_client.get('/the-key', 'http://tester.localhost:5000/')
res.status_code.should.equal(200)
res.data.should.equal("nothing")
res.data.should.equal(b"nothing")

View file

@ -14,46 +14,46 @@ def test_s3_server_get():
res = test_client.get('/')
res.data.should.contain('ListAllMyBucketsResult')
res.data.should.contain(b'ListAllMyBucketsResult')
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')
res = test_client.put('/foobar/', 'http://localhost:5000')
res.status_code.should.equal(200)
res = test_client.get('/')
res.data.should.contain('<Name>foobar</Name>')
res.data.should.contain(b'<Name>foobar</Name>')
res = test_client.get('/foobar', 'http://localhost:5000')
res = test_client.get('/foobar/', 'http://localhost:5000')
res.status_code.should.equal(200)
res.data.should.contain("ListBucketResult")
res.data.should.contain(b"ListBucketResult")
res = test_client.get('/missing-bucket', 'http://localhost:5000')
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')
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')
res = test_client.get('/foobar/bar/', 'http://localhost:5000')
res.status_code.should.equal(200)
res.data.should.equal("test value")
res.data.should.equal(b"test value")
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/')
res = test_client.put('/foobar2/', 'http://localhost:5000/')
res.status_code.should.equal(200)
test_client.post('/foobar2', "https://localhost:5000/", data={
test_client.post('/foobar2/', "https://localhost:5000/", data={
'key': 'the-key',
'file': 'nothing'
})
res = test_client.get('/foobar2/the-key', 'http://localhost:5000/')
res = test_client.get('/foobar2/the-key/', 'http://localhost:5000/')
res.status_code.should.equal(200)
res.data.should.equal("nothing")
res.data.should.equal(b"nothing")

View file

@ -1,5 +1,6 @@
from __future__ import unicode_literals
import urllib2
from six.moves.urllib.request import urlopen
from six.moves.urllib.error import HTTPError
import boto
from boto.exception import S3ResponseError
@ -41,7 +42,7 @@ def test_my_model_save():
model_instance = MyModel('steve', 'is awesome')
model_instance.save()
conn.get_bucket('mybucket').get_key('steve').get_contents_as_string().should.equal('is awesome')
conn.get_bucket('mybucket').get_key('steve').get_contents_as_string().should.equal(b'is awesome')
@mock_s3bucket_path
@ -56,7 +57,7 @@ def test_missing_key_urllib2():
conn = create_connection('the_key', 'the_secret')
conn.create_bucket("foobar")
urllib2.urlopen.when.called_with("http://s3.amazonaws.com/foobar/the-key").should.throw(urllib2.HTTPError)
urlopen.when.called_with("http://s3.amazonaws.com/foobar/the-key").should.throw(HTTPError)
@mock_s3bucket_path
@ -67,7 +68,7 @@ def test_empty_key():
key.key = "the-key"
key.set_contents_from_string("")
bucket.get_key("the-key").get_contents_as_string().should.equal('')
bucket.get_key("the-key").get_contents_as_string().should.equal(b'')
@mock_s3bucket_path
@ -78,10 +79,10 @@ def test_empty_key_set_on_existing_key():
key.key = "the-key"
key.set_contents_from_string("foobar")
bucket.get_key("the-key").get_contents_as_string().should.equal('foobar')
bucket.get_key("the-key").get_contents_as_string().should.equal(b'foobar')
key.set_contents_from_string("")
bucket.get_key("the-key").get_contents_as_string().should.equal('')
bucket.get_key("the-key").get_contents_as_string().should.equal(b'')
@mock_s3bucket_path
@ -92,7 +93,7 @@ def test_large_key_save():
key.key = "the-key"
key.set_contents_from_string("foobar" * 100000)
bucket.get_key("the-key").get_contents_as_string().should.equal('foobar' * 100000)
bucket.get_key("the-key").get_contents_as_string().should.equal(b'foobar' * 100000)
@mock_s3bucket_path
@ -105,8 +106,8 @@ def test_copy_key():
bucket.copy_key('new-key', 'foobar', 'the-key')
bucket.get_key("the-key").get_contents_as_string().should.equal("some value")
bucket.get_key("new-key").get_contents_as_string().should.equal("some value")
bucket.get_key("the-key").get_contents_as_string().should.equal(b"some value")
bucket.get_key("new-key").get_contents_as_string().should.equal(b"some value")
@mock_s3bucket_path
@ -191,7 +192,7 @@ def test_post_to_bucket():
'file': 'nothing'
})
bucket.get_key('the-key').get_contents_as_string().should.equal('nothing')
bucket.get_key('the-key').get_contents_as_string().should.equal(b'nothing')
@mock_s3bucket_path
@ -275,8 +276,8 @@ def test_bucket_key_listing_order():
delimiter = None
keys = [x.name for x in bucket.list(prefix + 'x', delimiter)]
keys.should.equal([u'toplevel/x/key', u'toplevel/x/y/key', u'toplevel/x/y/z/key'])
keys.should.equal(['toplevel/x/key', 'toplevel/x/y/key', 'toplevel/x/y/z/key'])
delimiter = '/'
keys = [x.name for x in bucket.list(prefix + 'x', delimiter)]
keys.should.equal([u'toplevel/x/'])
keys.should.equal(['toplevel/x/'])

View file

@ -13,4 +13,4 @@ def test_ses_list_identities():
test_client = backend.test_client()
res = test_client.get('/?Action=ListIdentities')
res.data.should.contain("ListIdentitiesResponse")
res.data.should.contain(b"ListIdentitiesResponse")

View file

@ -1,5 +1,5 @@
from __future__ import unicode_literals
from urlparse import parse_qs
from six.moves.urllib.parse import parse_qs
import boto
from freezegun import freeze_time
@ -49,7 +49,7 @@ def test_publish_to_http():
last_request = httpretty.last_request()
last_request.method.should.equal("POST")
parse_qs(last_request.body).should.equal({
parse_qs(last_request.body.decode('utf-8')).should.equal({
"Type": ["Notification"],
"MessageId": [message_id],
"TopicArn": ["arn:aws:sns:us-east-1:123456789012:some-topic"],

View file

@ -1,5 +1,6 @@
from __future__ import unicode_literals
import boto
import six
import sure # noqa
@ -45,10 +46,20 @@ def test_topic_attributes():
attributes["DeliveryPolicy"].should.equal("")
attributes["EffectiveDeliveryPolicy"].should.equal(DEFAULT_EFFECTIVE_DELIVERY_POLICY)
# boto can't handle unicode here :(
conn.set_topic_attributes(topic_arn, "Policy", {b"foo": b"bar"})
conn.set_topic_attributes(topic_arn, "DisplayName", "My display name")
conn.set_topic_attributes(topic_arn, "DeliveryPolicy", {b"http": {b"defaultHealthyRetryPolicy": {b"numRetries": 5}}})
# boto can't handle prefix-mandatory strings:
# i.e. unicode on Python 2 -- u"foobar"
# and bytes on Python 3 -- b"foobar"
if six.PY2:
policy = {b"foo": b"bar"}
displayname = b"My display name"
delivery = {b"http": {b"defaultHealthyRetryPolicy": {b"numRetries": 5}}}
else:
policy = {u"foo": u"bar"}
displayname = u"My display name"
delivery = {u"http": {u"defaultHealthyRetryPolicy": {u"numRetries": 5}}}
conn.set_topic_attributes(topic_arn, "Policy", policy)
conn.set_topic_attributes(topic_arn, "DisplayName", displayname)
conn.set_topic_attributes(topic_arn, "DeliveryPolicy", delivery)
attributes = conn.get_topic_attributes(topic_arn)['GetTopicAttributesResponse']['GetTopicAttributesResult']['Attributes']
attributes["Policy"].should.equal("{'foo': 'bar'}")

View file

@ -14,13 +14,13 @@ def test_sqs_list_identities():
test_client = backend.test_client()
res = test_client.get('/?Action=ListQueues')
res.data.should.contain("ListQueuesResponse")
res.data.should.contain(b"ListQueuesResponse")
res = test_client.put('/?Action=CreateQueue&QueueName=testqueue')
res = test_client.put('/?Action=CreateQueue&QueueName=otherqueue')
res = test_client.get('/?Action=ListQueues&QueueNamePrefix=other')
res.data.should_not.contain('testqueue')
res.data.should_not.contain(b'testqueue')
res = test_client.put(
'/123/testqueue?MessageBody=test-message&Action=SendMessage')
@ -28,5 +28,5 @@ def test_sqs_list_identities():
res = test_client.get(
'/123/testqueue?Action=ReceiveMessage&MaxNumberOfMessages=1')
message = re.search("<Body>(.*?)</Body>", res.data).groups()[0]
message = re.search("<Body>(.*?)</Body>", res.data.decode('utf-8')).groups()[0]
message.should.equal('test-message')

View file

@ -14,8 +14,8 @@ def test_sts_get_session_token():
res = test_client.get('/?Action=GetSessionToken')
res.status_code.should.equal(200)
res.data.should.contain("SessionToken")
res.data.should.contain("AccessKeyId")
res.data.should.contain(b"SessionToken")
res.data.should.contain(b"AccessKeyId")
def test_sts_get_federation_token():
@ -24,5 +24,5 @@ def test_sts_get_federation_token():
res = test_client.get('/?Action=GetFederationToken&Name=Bob')
res.status_code.should.equal(200)
res.data.should.contain("SessionToken")
res.data.should.contain("AccessKeyId")
res.data.should.contain(b"SessionToken")
res.data.should.contain(b"AccessKeyId")