Skip to content

Commit

Permalink
Merge pull request #2424 from felixonmars/ec2-py3
Browse files Browse the repository at this point in the history
ec2 module: add backward-compatible support for Python 3.3+. Fixes #2424.
  • Loading branch information
danielgtaylor committed Jul 17, 2014
2 parents 356da91 + dd41b0b commit 5e5dc4c
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 57 deletions.
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -30,7 +30,7 @@ At the moment, boto supports:

* Compute

* Amazon Elastic Compute Cloud (EC2)
* Amazon Elastic Compute Cloud (EC2) (Python 3)
* Amazon Elastic Map Reduce (EMR) (Python 3)
* AutoScaling (Python 3)
* Amazon Kinesis (Python 3)
Expand Down
3 changes: 3 additions & 0 deletions boto/ec2/connection.py
Expand Up @@ -65,6 +65,7 @@
from boto.ec2.attributes import AccountAttribute, VPCAttribute
from boto.ec2.blockdevicemapping import BlockDeviceMapping, BlockDeviceType
from boto.exception import EC2ResponseError
from boto.compat import six

#boto.set_stream_logger('ec2')

Expand Down Expand Up @@ -918,6 +919,8 @@ def run_instances(self, image_id, min_count=1, max_count=1,
l.append(group)
self.build_list_params(params, l, 'SecurityGroup')
if user_data:
if isinstance(user_data, six.text_type):
user_data = user_data.encode('utf-8')
params['UserData'] = base64.b64encode(user_data).decode('utf-8')
if addressing_type:
params['AddressingType'] = addressing_type
Expand Down
2 changes: 1 addition & 1 deletion docs/source/index.rst
Expand Up @@ -32,7 +32,7 @@ Currently Supported Services

* **Compute**

* :doc:`Elastic Compute Cloud (EC2) <ec2_tut>` -- (:doc:`API Reference <ref/ec2>`)
* :doc:`Elastic Compute Cloud (EC2) <ec2_tut>` -- (:doc:`API Reference <ref/ec2>`) (Python 3)
* :doc:`Elastic MapReduce (EMR) <emr_tut>` -- (:doc:`API Reference <ref/emr>`) (Python 3)
* :doc:`Auto Scaling <autoscale_tut>` -- (:doc:`API Reference <ref/autoscale>`) (Python 3)
* Kinesis -- (:doc:`API Reference <ref/kinesis>`) (Python 3)
Expand Down
14 changes: 7 additions & 7 deletions tests/integration/ec2/test_connection.py
Expand Up @@ -43,7 +43,7 @@ def test_launch_permissions(self):
# this is my user_id, if you want to run these tests you should
# replace this with yours or they won't work
user_id = '963068290131'
print '--- running EC2Connection tests ---'
print('--- running EC2Connection tests ---')
c = EC2Connection()
# get list of private AMI's
rs = c.get_all_images(owners=[user_id])
Expand Down Expand Up @@ -126,7 +126,7 @@ def test_1_basic(self):
reservation = image.run(security_groups=[group.name])
instance = reservation.instances[0]
while instance.state != 'running':
print '\tinstance is %s' % instance.state
print('\tinstance is %s' % instance.state)
time.sleep(30)
instance.update()
# instance in now running, try to telnet to port 80
Expand Down Expand Up @@ -190,7 +190,7 @@ def test_1_basic(self):
assert len(l[0].product_codes) == 1
assert l[0].product_codes[0] == demo_paid_ami_product_code

print '--- tests completed ---'
print('--- tests completed ---')

def test_dry_run(self):
c = EC2Connection()
Expand All @@ -199,7 +199,7 @@ def test_dry_run(self):
try:
rs = c.get_all_images(dry_run=True)
self.fail("Should have gotten an exception")
except EC2ResponseError, e:
except EC2ResponseError as e:
self.assertTrue(dry_run_msg in str(e))

try:
Expand All @@ -209,7 +209,7 @@ def test_dry_run(self):
dry_run=True
)
self.fail("Should have gotten an exception")
except EC2ResponseError, e:
except EC2ResponseError as e:
self.assertTrue(dry_run_msg in str(e))

# Need an actual instance for the rest of this...
Expand All @@ -225,7 +225,7 @@ def test_dry_run(self):
dry_run=True
)
self.fail("Should have gotten an exception")
except EC2ResponseError, e:
except EC2ResponseError as e:
self.assertTrue(dry_run_msg in str(e))

try:
Expand All @@ -234,7 +234,7 @@ def test_dry_run(self):
dry_run=True
)
self.fail("Should have gotten an exception")
except EC2ResponseError, e:
except EC2ResponseError as e:
self.assertTrue(dry_run_msg in str(e))

# And kill it.
Expand Down
4 changes: 1 addition & 3 deletions tests/test.py
Expand Up @@ -47,9 +47,7 @@
'tests/unit/emr',
'tests/unit/glacier',
'tests/unit/iam',
'tests/unit/ec2/autoscale',
'tests/unit/ec2/cloudwatch',
'tests/unit/ec2/elb',
'tests/unit/ec2',
'tests/unit/manage',
'tests/unit/mws',
'tests/unit/provider',
Expand Down
11 changes: 7 additions & 4 deletions tests/unit/ec2/test_blockdevicemapping.py
Expand Up @@ -3,6 +3,7 @@

from boto.ec2.connection import EC2Connection
from boto.ec2.blockdevicemapping import BlockDeviceType, BlockDeviceMapping
from boto.compat import OrderedDict

from tests.unit import AWSMockServiceTestCase

Expand Down Expand Up @@ -85,7 +86,7 @@ class TestLaunchConfiguration(AWSMockServiceTestCase):

def default_body(self):
# This is a dummy response
return """
return b"""
<DescribeLaunchConfigurationsResponse>
</DescribeLaunchConfigurationsResponse>
"""
Expand All @@ -98,9 +99,11 @@ def test_run_instances_block_device_mapping(self):
dev_sdf = BlockDeviceType(snapshot_id='snap-12345')
dev_sdg = BlockDeviceType(snapshot_id='snap-12346', delete_on_termination=True)

bdm = BlockDeviceMapping()
bdm['/dev/sdf'] = dev_sdf
bdm['/dev/sdg'] = dev_sdg
class OrderedBlockDeviceMapping(OrderedDict, BlockDeviceMapping):
pass

bdm = OrderedBlockDeviceMapping()
bdm.update(OrderedDict((('/dev/sdf', dev_sdf), ('/dev/sdg', dev_sdg))))

response = self.service_connection.run_instances(
image_id='123456',
Expand Down
48 changes: 24 additions & 24 deletions tests/unit/ec2/test_connection.py
Expand Up @@ -26,7 +26,7 @@ def setUp(self):
class TestReservedInstanceOfferings(TestEC2ConnectionBase):

def default_body(self):
return """
return b"""
<DescribeReservedInstancesOfferingsResponse>
<requestId>d3253568-edcf-4897-9a3d-fb28e0b3fa38</requestId>
<reservedInstancesOfferingsSet>
Expand Down Expand Up @@ -139,7 +139,7 @@ def test_get_reserved_instance_offerings_params(self):

class TestPurchaseReservedInstanceOffering(TestEC2ConnectionBase):
def default_body(self):
return """<PurchaseReservedInstancesOffering />"""
return b"""<PurchaseReservedInstancesOffering />"""

def test_serialized_api_args(self):
self.set_http_response(status_code=200)
Expand All @@ -158,7 +158,7 @@ def test_serialized_api_args(self):

class TestCreateImage(TestEC2ConnectionBase):
def default_body(self):
return """<CreateImageResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-01/">
return b"""<CreateImageResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-01/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<imageId>ami-4fa54026</imageId>
</CreateImageResponse>"""
Expand Down Expand Up @@ -194,7 +194,7 @@ def test_block_device_mapping(self):

class TestCancelReservedInstancesListing(TestEC2ConnectionBase):
def default_body(self):
return """
return b"""
<CancelReservedInstancesListingResponse>
<requestId>request_id</requestId>
<reservedInstancesListingsSet>
Expand Down Expand Up @@ -283,7 +283,7 @@ def test_reserved_instances_listing(self):

class TestCreateReservedInstancesListing(TestEC2ConnectionBase):
def default_body(self):
return """
return b"""
<CreateReservedInstancesListingResponse>
<requestId>request_id</requestId>
<reservedInstancesListingsSet>
Expand Down Expand Up @@ -422,7 +422,7 @@ def test_create_reserved_instances_listing(self):

class TestDescribeSpotInstanceRequests(TestEC2ConnectionBase):
def default_body(self):
return """
return b"""
<DescribeSpotInstanceRequestsResponse>
<requestId>requestid</requestId>
<spotInstanceRequestSet>
Expand Down Expand Up @@ -499,7 +499,7 @@ def test_describe_spot_instance_requets(self):

class TestCopySnapshot(TestEC2ConnectionBase):
def default_body(self):
return """
return b"""
<CopySnapshotResponse xmlns="http://ec2.amazonaws.com/doc/2012-12-01/">
<requestId>request_id</requestId>
<snapshotId>snap-copied-id</snapshotId>
Expand All @@ -523,7 +523,7 @@ def test_copy_snapshot(self):

class TestCopyImage(TestEC2ConnectionBase):
def default_body(self):
return """
return b"""
<CopyImageResponse xmlns="http://ec2.amazonaws.com/doc/2013-07-15/">
<requestId>request_id</requestId>
<imageId>ami-copied-id</imageId>
Expand Down Expand Up @@ -565,7 +565,7 @@ def test_copy_image_without_name(self):

class TestAccountAttributes(TestEC2ConnectionBase):
def default_body(self):
return """
return b"""
<DescribeAccountAttributesResponse xmlns="http://ec2.amazonaws.com/doc/2012-12-01/">
<requestId>6d042e8a-4bc3-43e8-8265-3cbc54753f14</requestId>
<accountAttributeSet>
Expand Down Expand Up @@ -624,7 +624,7 @@ def test_describe_account_attributes(self):

class TestDescribeVPCAttribute(TestEC2ConnectionBase):
def default_body(self):
return """
return b"""
<DescribeVpcAttributeResponse xmlns="http://ec2.amazonaws.com/doc/2013-02-01/">
<requestId>request_id</requestId>
<vpcId>vpc-id</vpcId>
Expand All @@ -651,7 +651,7 @@ def test_describe_vpc_attribute(self):

class TestGetAllNetworkInterfaces(TestEC2ConnectionBase):
def default_body(self):
return """
return b"""
<DescribeNetworkInterfacesResponse xmlns="http://ec2.amazonaws.com/\
doc/2013-06-15/">
<requestId>fc45294c-006b-457b-bab9-012f5b3b0e40</requestId>
Expand Down Expand Up @@ -722,7 +722,7 @@ def test_attachment_has_device_index(self):

class TestGetAllImages(TestEC2ConnectionBase):
def default_body(self):
return """
return b"""
<DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2013-02-01/">
<requestId>e32375e8-4ac3-4099-a8bf-3ec902b9023e</requestId>
<imagesSet>
Expand Down Expand Up @@ -814,7 +814,7 @@ def test_get_all_images(self):

class TestModifyInterfaceAttribute(TestEC2ConnectionBase):
def default_body(self):
return """
return b"""
<ModifyNetworkInterfaceAttributeResponse \
xmlns="http://ec2.amazonaws.com/doc/2013-06-15/">
<requestId>657a4623-5620-4232-b03b-427e852d71cf</requestId>
Expand Down Expand Up @@ -1069,7 +1069,7 @@ def test_trim_months(self):

class TestModifyReservedInstances(TestEC2ConnectionBase):
def default_body(self):
return """<ModifyReservedInstancesResponse xmlns='http://ec2.amazonaws.com/doc/2013-08-15/'>
return b"""<ModifyReservedInstancesResponse xmlns='http://ec2.amazonaws.com/doc/2013-08-15/'>
<requestId>bef729b6-0731-4489-8881-2258746ae163</requestId>
<reservedInstancesModificationId>rimod-3aae219d-3d63-47a9-a7e9-e764example</reservedInstancesModificationId>
</ModifyReservedInstancesResponse>"""
Expand Down Expand Up @@ -1107,7 +1107,7 @@ def test_serialized_api_args(self):

class TestDescribeReservedInstancesModifications(TestEC2ConnectionBase):
def default_body(self):
return """<DescribeReservedInstancesModificationsResponse xmlns='http://ec2.amazonaws.com/doc/2013-08-15/'>
return b"""<DescribeReservedInstancesModificationsResponse xmlns='http://ec2.amazonaws.com/doc/2013-08-15/'>
<requestId>eb4a6e3c-3689-445c-b536-19e38df35898</requestId>
<reservedInstancesModificationsSet>
<item>
Expand Down Expand Up @@ -1207,7 +1207,7 @@ def test_serialized_api_args(self):

class TestRegisterImage(TestEC2ConnectionBase):
def default_body(self):
return """
return b"""
<RegisterImageResponse xmlns="http://ec2.amazonaws.com/doc/2013-08-15/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<imageId>ami-1a2b3c4d</imageId>
Expand Down Expand Up @@ -1307,7 +1307,7 @@ def test_volume_delete_on_termination_default(self):

class TestTerminateInstances(TestEC2ConnectionBase):
def default_body(self):
return """<?xml version="1.0" ?>
return b"""<?xml version="1.0" ?>
<TerminateInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2013-07-15/">
<requestId>req-59a9ad52-0434-470c-ad48-4f89ded3a03e</requestId>
<instancesSet>
Expand All @@ -1334,7 +1334,7 @@ def test_terminate_bad_response(self):
class TestDescribeInstances(TestEC2ConnectionBase):

def default_body(self):
return """
return b"""
<DescribeInstancesResponse>
</DescribeInstancesResponse>
"""
Expand Down Expand Up @@ -1384,7 +1384,7 @@ def test_next_token(self):
class TestDescribeTags(TestEC2ConnectionBase):

def default_body(self):
return """
return b"""
<DescribeTagsResponse>
</DescribeTagsResponse>
"""
Expand Down Expand Up @@ -1436,7 +1436,7 @@ def test_switched(self):

class TestAssociateAddress(TestEC2ConnectionBase):
def default_body(self):
return """
return b"""
<AssociateAddressResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<return>true</return>
Expand All @@ -1459,7 +1459,7 @@ def test_associate_address_object(self):

class TestAssociateAddressFail(TestEC2ConnectionBase):
def default_body(self):
return """
return b"""
<Response>
<Errors>
<Error>
Expand All @@ -1481,7 +1481,7 @@ def test_associate_address(self):

class TestDescribeVolumes(TestEC2ConnectionBase):
def default_body(self):
return """
return b"""
<DescribeVolumesResponse xmlns="http://ec2.amazonaws.com/doc/2014-02-01/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<volumeSet>
Expand Down Expand Up @@ -1548,7 +1548,7 @@ def test_get_all_volumes(self):

class TestDescribeSnapshots(TestEC2ConnectionBase):
def default_body(self):
return """
return b"""
<DescribeSnapshotsResponse xmlns="http://ec2.amazonaws.com/doc/2014-02-01/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<snapshotSet>
Expand Down Expand Up @@ -1601,7 +1601,7 @@ def test_get_all_snapshots(self):

class TestCreateVolume(TestEC2ConnectionBase):
def default_body(self):
return """
return b"""
<CreateVolumeResponse xmlns="http://ec2.amazonaws.com/doc/2014-05-01/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<volumeId>vol-1a2b3c4d</volumeId>
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/ec2/test_ec2object.py
Expand Up @@ -7,15 +7,15 @@
from boto.ec2.ec2object import TaggedEC2Object


CREATE_TAGS_RESPONSE = r"""<?xml version="1.0" encoding="UTF-8"?>
CREATE_TAGS_RESPONSE = br"""<?xml version="1.0" encoding="UTF-8"?>
<CreateTagsResponse xmlns="http://ec2.amazonaws.com/doc/2014-05-01/">
<requestId>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</requestId>
<return>true</return>
</CreateTagsResponse>
"""


DELETE_TAGS_RESPONSE = r"""<?xml version="1.0" encoding="UTF-8"?>
DELETE_TAGS_RESPONSE = br"""<?xml version="1.0" encoding="UTF-8"?>
<DeleteTagsResponse xmlns="http://ec2.amazonaws.com/doc/2014-05-01/">
<requestId>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</requestId>
<return>true</return>
Expand Down

0 comments on commit 5e5dc4c

Please sign in to comment.