2013-02-22 04:13:01 +00:00
|
|
|
import boto
|
2013-02-23 22:37:55 +00:00
|
|
|
from boto.exception import EC2ResponseError
|
2013-08-03 21:21:25 +00:00
|
|
|
import sure # noqa
|
2013-02-22 04:13:01 +00:00
|
|
|
|
|
|
|
from moto import mock_ec2
|
2014-08-20 11:14:13 +00:00
|
|
|
from moto.ec2.models import ec2_backend
|
2013-02-22 04:13:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
2013-02-23 22:37:55 +00:00
|
|
|
def test_create_and_delete_volume():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
volume = conn.create_volume(80, "us-east-1a")
|
|
|
|
|
|
|
|
all_volumes = conn.get_all_volumes()
|
|
|
|
all_volumes.should.have.length_of(1)
|
|
|
|
all_volumes[0].size.should.equal(80)
|
|
|
|
all_volumes[0].zone.should.equal("us-east-1a")
|
|
|
|
|
|
|
|
volume = all_volumes[0]
|
|
|
|
volume.delete()
|
|
|
|
|
|
|
|
conn.get_all_volumes().should.have.length_of(0)
|
|
|
|
|
|
|
|
# Deleting something that was already deleted should throw an error
|
|
|
|
volume.delete.when.called_with().should.throw(EC2ResponseError)
|
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_volume_attach_and_detach():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
2013-03-05 13:14:43 +00:00
|
|
|
reservation = conn.run_instances('ami-1234abcd')
|
2013-02-23 22:37:55 +00:00
|
|
|
instance = reservation.instances[0]
|
|
|
|
volume = conn.create_volume(80, "us-east-1a")
|
|
|
|
|
|
|
|
volume.update()
|
|
|
|
volume.volume_state().should.equal('available')
|
|
|
|
|
|
|
|
volume.attach(instance.id, "/dev/sdh")
|
|
|
|
|
|
|
|
volume.update()
|
|
|
|
volume.volume_state().should.equal('in-use')
|
|
|
|
|
|
|
|
volume.attach_data.instance_id.should.equal(instance.id)
|
|
|
|
|
|
|
|
volume.detach()
|
|
|
|
|
|
|
|
volume.update()
|
|
|
|
volume.volume_state().should.equal('available')
|
|
|
|
|
2013-03-11 04:12:22 +00:00
|
|
|
volume.attach.when.called_with(
|
|
|
|
'i-1234abcd', "/dev/sdh").should.throw(EC2ResponseError)
|
|
|
|
|
2013-03-05 13:14:43 +00:00
|
|
|
conn.detach_volume.when.called_with(
|
|
|
|
volume.id, instance.id, "/dev/sdh").should.throw(EC2ResponseError)
|
2013-02-23 23:01:41 +00:00
|
|
|
|
2013-03-11 04:12:22 +00:00
|
|
|
conn.detach_volume.when.called_with(
|
|
|
|
volume.id, 'i-1234abcd', "/dev/sdh").should.throw(EC2ResponseError)
|
|
|
|
|
2013-02-23 23:01:41 +00:00
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_create_snapshot():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
volume = conn.create_volume(80, "us-east-1a")
|
|
|
|
|
|
|
|
volume.create_snapshot('a test snapshot')
|
|
|
|
|
|
|
|
snapshots = conn.get_all_snapshots()
|
|
|
|
snapshots.should.have.length_of(1)
|
|
|
|
snapshots[0].description.should.equal('a test snapshot')
|
|
|
|
|
|
|
|
# Create snapshot without description
|
|
|
|
snapshot = volume.create_snapshot()
|
|
|
|
conn.get_all_snapshots().should.have.length_of(2)
|
|
|
|
|
|
|
|
snapshot.delete()
|
|
|
|
conn.get_all_snapshots().should.have.length_of(1)
|
|
|
|
|
|
|
|
# Deleting something that was already deleted should throw an error
|
|
|
|
snapshot.delete.when.called_with().should.throw(EC2ResponseError)
|
2014-08-20 02:38:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_modify_attribute_blockDeviceMapping():
|
|
|
|
"""
|
|
|
|
Reproduces the missing feature explained at [0], where we want to mock a
|
|
|
|
call to modify an instance attribute of type: blockDeviceMapping.
|
|
|
|
|
|
|
|
[0] https://github.com/spulec/moto/issues/160
|
|
|
|
"""
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
|
|
|
|
reservation = conn.run_instances('ami-1234abcd')
|
|
|
|
|
|
|
|
instance = reservation.instances[0]
|
2014-08-20 11:14:13 +00:00
|
|
|
|
|
|
|
instance.modify_attribute('blockDeviceMapping', {'/dev/sda1': True})
|
|
|
|
|
|
|
|
instance = ec2_backend.get_instance(instance.id)
|
|
|
|
instance.block_device_mapping.should.have.key('/dev/sda1')
|
|
|
|
instance.block_device_mapping['/dev/sda1'].delete_on_termination.should.be(True)
|