2014-08-27 15:17:06 +00:00
from __future__ import unicode_literals
2014-08-25 22:09:38 +00:00
# Ensure 'assert_raises' context manager support for Python 2.6
import tests . backport_assert_raises
from nose . tools import assert_raises
2014-10-31 02:46:24 +00:00
from moto . ec2 import ec2_backends
2013-02-22 04:13:01 +00:00
import boto
2016-10-15 13:08:44 +00:00
from boto . exception import EC2ResponseError , JSONResponseError
2013-08-03 21:21:25 +00:00
import sure # noqa
2013-02-22 04:13:01 +00:00
from moto import mock_ec2
@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 " )
2016-07-04 13:21:31 +00:00
all_volumes [ 0 ] . encrypted . should . be ( False )
2013-02-23 22:37:55 +00:00
volume = all_volumes [ 0 ]
2016-10-15 13:08:44 +00:00
with assert_raises ( JSONResponseError ) as ex :
volume . delete ( dry_run = True )
ex . exception . reason . should . equal ( ' DryRunOperation ' )
ex . exception . status . should . equal ( 400 )
ex . exception . message . should . equal ( ' An error occurred (DryRunOperation) when calling the DeleteVolume operation: Request would have succeeded, but DryRun flag is set ' )
2013-02-23 22:37:55 +00:00
volume . delete ( )
conn . get_all_volumes ( ) . should . have . length_of ( 0 )
# Deleting something that was already deleted should throw an error
2014-08-25 17:54:47 +00:00
with assert_raises ( EC2ResponseError ) as cm :
volume . delete ( )
cm . exception . code . should . equal ( ' InvalidVolume.NotFound ' )
cm . exception . status . should . equal ( 400 )
cm . exception . request_id . should_not . be . none
2013-02-23 22:37:55 +00:00
2015-11-03 14:37:02 +00:00
2016-10-15 13:08:44 +00:00
@mock_ec2
def test_create_encrypted_volume_dryrun ( ) :
conn = boto . connect_ec2 ( ' the_key ' , ' the_secret ' )
with assert_raises ( JSONResponseError ) as ex :
conn . create_volume ( 80 , " us-east-1a " , encrypted = True , dry_run = True )
ex . exception . reason . should . equal ( ' DryRunOperation ' )
ex . exception . status . should . equal ( 400 )
ex . exception . message . should . equal ( ' An error occurred (DryRunOperation) when calling the CreateVolume operation: Request would have succeeded, but DryRun flag is set ' )
2016-07-04 10:01:48 +00:00
@mock_ec2
def test_create_encrypted_volume ( ) :
conn = boto . connect_ec2 ( ' the_key ' , ' the_secret ' )
conn . create_volume ( 80 , " us-east-1a " , encrypted = True )
2016-10-15 13:08:44 +00:00
with assert_raises ( JSONResponseError ) as ex :
conn . create_volume ( 80 , " us-east-1a " , encrypted = True , dry_run = True )
ex . exception . reason . should . equal ( ' DryRunOperation ' )
ex . exception . status . should . equal ( 400 )
ex . exception . message . should . equal ( ' An error occurred (DryRunOperation) when calling the CreateVolume operation: Request would have succeeded, but DryRun flag is set ' )
2016-07-04 10:01:48 +00:00
all_volumes = conn . get_all_volumes ( )
2016-07-04 13:21:31 +00:00
all_volumes [ 0 ] . encrypted . should . be ( True )
2016-07-04 10:01:48 +00:00
2015-07-25 23:53:03 +00:00
@mock_ec2
def test_filter_volume_by_id ( ) :
conn = boto . connect_ec2 ( ' the_key ' , ' the_secret ' )
volume1 = conn . create_volume ( 80 , " us-east-1a " )
volume2 = conn . create_volume ( 36 , " us-east-1b " )
volume3 = conn . create_volume ( 20 , " us-east-1c " )
vol1 = conn . get_all_volumes ( volume_ids = volume3 . id )
vol1 . should . have . length_of ( 1 )
vol1 [ 0 ] . size . should . equal ( 20 )
vol1 [ 0 ] . zone . should . equal ( ' us-east-1c ' )
vol2 = conn . get_all_volumes ( volume_ids = [ volume1 . id , volume2 . id ] )
vol2 . should . have . length_of ( 2 )
2013-02-23 22:37:55 +00:00
2016-01-11 05:44:29 +00:00
@mock_ec2
def test_volume_filters ( ) :
conn = boto . connect_ec2 ( ' the_key ' , ' the_secret ' )
reservation = conn . run_instances ( ' ami-1234abcd ' )
instance = reservation . instances [ 0 ]
instance . update ( )
2016-07-04 10:01:48 +00:00
volume1 = conn . create_volume ( 80 , " us-east-1a " , encrypted = True )
volume2 = conn . create_volume ( 36 , " us-east-1b " , encrypted = False )
2016-07-04 13:09:15 +00:00
volume3 = conn . create_volume ( 20 , " us-east-1c " , encrypted = True )
2016-01-11 05:44:29 +00:00
snapshot = volume3 . create_snapshot ( description = ' testsnap ' )
volume4 = conn . create_volume ( 25 , " us-east-1a " , snapshot = snapshot )
conn . create_tags ( [ volume1 . id ] , { ' testkey1 ' : ' testvalue1 ' } )
conn . create_tags ( [ volume2 . id ] , { ' testkey2 ' : ' testvalue2 ' } )
volume1 . update ( )
volume2 . update ( )
volume3 . update ( )
volume4 . update ( )
block_mapping = instance . block_device_mapping [ ' /dev/sda1 ' ]
volumes_by_attach_time = conn . get_all_volumes ( filters = { ' attachment.attach-time ' : block_mapping . attach_time } )
set ( [ vol . id for vol in volumes_by_attach_time ] ) . should . equal ( set ( [ block_mapping . volume_id ] ) )
volumes_by_attach_device = conn . get_all_volumes ( filters = { ' attachment.device ' : ' /dev/sda1 ' } )
set ( [ vol . id for vol in volumes_by_attach_device ] ) . should . equal ( set ( [ block_mapping . volume_id ] ) )
volumes_by_attach_instance_id = conn . get_all_volumes ( filters = { ' attachment.instance-id ' : instance . id } )
set ( [ vol . id for vol in volumes_by_attach_instance_id ] ) . should . equal ( set ( [ block_mapping . volume_id ] ) )
2016-07-22 18:23:42 +00:00
volumes_by_attach_status = conn . get_all_volumes ( filters = { ' attachment.status ' : ' attached ' } )
set ( [ vol . id for vol in volumes_by_attach_status ] ) . should . equal ( set ( [ block_mapping . volume_id ] ) )
2016-01-11 05:44:29 +00:00
volumes_by_create_time = conn . get_all_volumes ( filters = { ' create-time ' : volume4 . create_time } )
set ( [ vol . create_time for vol in volumes_by_create_time ] ) . should . equal ( set ( [ volume4 . create_time ] ) )
volumes_by_size = conn . get_all_volumes ( filters = { ' size ' : volume2 . size } )
set ( [ vol . id for vol in volumes_by_size ] ) . should . equal ( set ( [ volume2 . id ] ) )
volumes_by_snapshot_id = conn . get_all_volumes ( filters = { ' snapshot-id ' : snapshot . id } )
set ( [ vol . id for vol in volumes_by_snapshot_id ] ) . should . equal ( set ( [ volume4 . id ] ) )
volumes_by_status = conn . get_all_volumes ( filters = { ' status ' : ' in-use ' } )
set ( [ vol . id for vol in volumes_by_status ] ) . should . equal ( set ( [ block_mapping . volume_id ] ) )
2016-01-20 02:24:33 +00:00
volumes_by_id = conn . get_all_volumes ( filters = { ' volume-id ' : volume1 . id } )
set ( [ vol . id for vol in volumes_by_id ] ) . should . equal ( set ( [ volume1 . id ] ) )
2016-01-11 05:44:29 +00:00
volumes_by_tag_key = conn . get_all_volumes ( filters = { ' tag-key ' : ' testkey1 ' } )
set ( [ vol . id for vol in volumes_by_tag_key ] ) . should . equal ( set ( [ volume1 . id ] ) )
volumes_by_tag_value = conn . get_all_volumes ( filters = { ' tag-value ' : ' testvalue1 ' } )
set ( [ vol . id for vol in volumes_by_tag_value ] ) . should . equal ( set ( [ volume1 . id ] ) )
volumes_by_tag = conn . get_all_volumes ( filters = { ' tag:testkey1 ' : ' testvalue1 ' } )
set ( [ vol . id for vol in volumes_by_tag ] ) . should . equal ( set ( [ volume1 . id ] ) )
2016-07-04 13:09:15 +00:00
volumes_by_unencrypted = conn . get_all_volumes ( filters = { ' encrypted ' : ' false ' } )
set ( [ vol . id for vol in volumes_by_unencrypted ] ) . should . equal (
set ( [ block_mapping . volume_id , volume2 . id ] )
)
2016-07-04 10:01:48 +00:00
volumes_by_encrypted = conn . get_all_volumes ( filters = { ' encrypted ' : ' true ' } )
2016-07-04 13:09:15 +00:00
set ( [ vol . id for vol in volumes_by_encrypted ] ) . should . equal (
set ( [ volume1 . id , volume3 . id , volume4 . id ] )
)
2016-07-04 10:01:48 +00:00
2016-01-11 05:44:29 +00:00
2013-02-23 22:37:55 +00:00
@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 ' )
2016-10-15 13:08:44 +00:00
with assert_raises ( JSONResponseError ) as ex :
volume . attach ( instance . id , " /dev/sdh " , dry_run = True )
ex . exception . reason . should . equal ( ' DryRunOperation ' )
ex . exception . status . should . equal ( 400 )
ex . exception . message . should . equal ( ' An error occurred (DryRunOperation) when calling the AttachVolume operation: Request would have succeeded, but DryRun flag is set ' )
2013-02-23 22:37:55 +00:00
volume . attach ( instance . id , " /dev/sdh " )
volume . update ( )
volume . volume_state ( ) . should . equal ( ' in-use ' )
2016-07-22 18:23:42 +00:00
volume . attachment_state ( ) . should . equal ( ' attached ' )
2013-02-23 22:37:55 +00:00
volume . attach_data . instance_id . should . equal ( instance . id )
2016-10-15 13:08:44 +00:00
with assert_raises ( JSONResponseError ) as ex :
volume . detach ( dry_run = True )
ex . exception . reason . should . equal ( ' DryRunOperation ' )
ex . exception . status . should . equal ( 400 )
ex . exception . message . should . equal ( ' An error occurred (DryRunOperation) when calling the DetachVolume operation: Request would have succeeded, but DryRun flag is set ' )
2013-02-23 22:37:55 +00:00
volume . detach ( )
volume . update ( )
volume . volume_state ( ) . should . equal ( ' available ' )
2014-08-25 17:54:47 +00:00
with assert_raises ( EC2ResponseError ) as cm1 :
volume . attach ( ' i-1234abcd ' , " /dev/sdh " )
cm1 . exception . code . should . equal ( ' InvalidInstanceID.NotFound ' )
cm1 . exception . status . should . equal ( 400 )
cm1 . exception . request_id . should_not . be . none
2013-03-11 04:12:22 +00:00
2014-08-25 17:54:47 +00:00
with assert_raises ( EC2ResponseError ) as cm2 :
conn . detach_volume ( volume . id , instance . id , " /dev/sdh " )
cm2 . exception . code . should . equal ( ' InvalidAttachment.NotFound ' )
cm2 . exception . status . should . equal ( 400 )
cm2 . exception . request_id . should_not . be . none
2013-02-23 23:01:41 +00:00
2014-08-25 17:54:47 +00:00
with assert_raises ( EC2ResponseError ) as cm3 :
conn . detach_volume ( volume . id , ' i-1234abcd ' , " /dev/sdh " )
cm3 . exception . code . should . equal ( ' InvalidInstanceID.NotFound ' )
cm3 . exception . status . should . equal ( 400 )
cm3 . exception . request_id . should_not . be . none
2013-03-11 04:12:22 +00:00
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 " )
2016-10-15 13:08:44 +00:00
with assert_raises ( JSONResponseError ) as ex :
snapshot = volume . create_snapshot ( ' a dryrun snapshot ' , dry_run = True )
ex . exception . reason . should . equal ( ' DryRunOperation ' )
ex . exception . status . should . equal ( 400 )
ex . exception . message . should . equal ( ' An error occurred (DryRunOperation) when calling the CreateSnapshot operation: Request would have succeeded, but DryRun flag is set ' )
2015-10-13 15:07:47 +00:00
snapshot = volume . create_snapshot ( ' a test snapshot ' )
snapshot . update ( )
snapshot . status . should . equal ( ' completed ' )
2013-02-23 23:01:41 +00:00
snapshots = conn . get_all_snapshots ( )
snapshots . should . have . length_of ( 1 )
snapshots [ 0 ] . description . should . equal ( ' a test snapshot ' )
2015-07-25 23:53:03 +00:00
snapshots [ 0 ] . start_time . should_not . be . none
2016-07-04 13:19:29 +00:00
snapshots [ 0 ] . encrypted . should . be ( False )
2013-02-23 23:01:41 +00:00
# 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
2014-08-25 17:54:47 +00:00
with assert_raises ( EC2ResponseError ) as cm :
snapshot . delete ( )
cm . exception . code . should . equal ( ' InvalidSnapshot.NotFound ' )
cm . exception . status . should . equal ( 400 )
cm . exception . request_id . should_not . be . none
2014-08-20 02:38:36 +00:00
2015-11-03 14:37:02 +00:00
2016-07-04 13:19:29 +00:00
@mock_ec2
def test_create_encrypted_snapshot ( ) :
conn = boto . connect_ec2 ( ' the_key ' , ' the_secret ' )
volume = conn . create_volume ( 80 , " us-east-1a " , encrypted = True )
snapshot = volume . create_snapshot ( ' a test snapshot ' )
snapshot . update ( )
snapshot . status . should . equal ( ' completed ' )
snapshots = conn . get_all_snapshots ( )
snapshots . should . have . length_of ( 1 )
snapshots [ 0 ] . description . should . equal ( ' a test snapshot ' )
snapshots [ 0 ] . start_time . should_not . be . none
snapshots [ 0 ] . encrypted . should . be ( True )
2015-07-25 23:53:03 +00:00
@mock_ec2
def test_filter_snapshot_by_id ( ) :
conn = boto . connect_ec2 ( ' the_key ' , ' the_secret ' )
volume1 = conn . create_volume ( 36 , " us-east-1a " )
snap1 = volume1 . create_snapshot ( ' a test snapshot 1 ' )
volume2 = conn . create_volume ( 42 , ' us-east-1a ' )
snap2 = volume2 . create_snapshot ( ' a test snapshot 2 ' )
volume3 = conn . create_volume ( 84 , ' us-east-1a ' )
snap3 = volume3 . create_snapshot ( ' a test snapshot 3 ' )
snapshots1 = conn . get_all_snapshots ( snapshot_ids = snap2 . id )
snapshots1 . should . have . length_of ( 1 )
snapshots1 [ 0 ] . volume_id . should . equal ( volume2 . id )
2015-08-03 14:45:01 +00:00
snapshots1 [ 0 ] . region . name . should . equal ( conn . region . name )
2015-07-25 23:53:03 +00:00
snapshots2 = conn . get_all_snapshots ( snapshot_ids = [ snap2 . id , snap3 . id ] )
snapshots2 . should . have . length_of ( 2 )
for s in snapshots2 :
s . start_time . should_not . be . none
s . volume_id . should . be . within ( [ volume2 . id , volume3 . id ] )
2015-08-03 14:45:01 +00:00
s . region . name . should . equal ( conn . region . name )
2014-08-20 02:38:36 +00:00
2015-11-03 14:37:02 +00:00
2016-01-11 05:44:29 +00:00
@mock_ec2
def test_snapshot_filters ( ) :
conn = boto . connect_ec2 ( ' the_key ' , ' the_secret ' )
2016-07-04 13:19:29 +00:00
volume1 = conn . create_volume ( 20 , " us-east-1a " , encrypted = False )
volume2 = conn . create_volume ( 25 , " us-east-1a " , encrypted = True )
2016-01-11 05:44:29 +00:00
snapshot1 = volume1 . create_snapshot ( description = ' testsnapshot1 ' )
snapshot2 = volume1 . create_snapshot ( description = ' testsnapshot2 ' )
snapshot3 = volume2 . create_snapshot ( description = ' testsnapshot3 ' )
2016-07-04 10:01:48 +00:00
2016-01-11 05:44:29 +00:00
conn . create_tags ( [ snapshot1 . id ] , { ' testkey1 ' : ' testvalue1 ' } )
conn . create_tags ( [ snapshot2 . id ] , { ' testkey2 ' : ' testvalue2 ' } )
snapshots_by_description = conn . get_all_snapshots ( filters = { ' description ' : ' testsnapshot1 ' } )
set ( [ snap . id for snap in snapshots_by_description ] ) . should . equal ( set ( [ snapshot1 . id ] ) )
snapshots_by_id = conn . get_all_snapshots ( filters = { ' snapshot-id ' : snapshot1 . id } )
set ( [ snap . id for snap in snapshots_by_id ] ) . should . equal ( set ( [ snapshot1 . id ] ) )
snapshots_by_start_time = conn . get_all_snapshots ( filters = { ' start-time ' : snapshot1 . start_time } )
set ( [ snap . start_time for snap in snapshots_by_start_time ] ) . should . equal ( set ( [ snapshot1 . start_time ] ) )
snapshots_by_volume_id = conn . get_all_snapshots ( filters = { ' volume-id ' : volume1 . id } )
set ( [ snap . id for snap in snapshots_by_volume_id ] ) . should . equal ( set ( [ snapshot1 . id , snapshot2 . id ] ) )
snapshots_by_volume_size = conn . get_all_snapshots ( filters = { ' volume-size ' : volume1 . size } )
set ( [ snap . id for snap in snapshots_by_volume_size ] ) . should . equal ( set ( [ snapshot1 . id , snapshot2 . id ] ) )
snapshots_by_tag_key = conn . get_all_snapshots ( filters = { ' tag-key ' : ' testkey1 ' } )
set ( [ snap . id for snap in snapshots_by_tag_key ] ) . should . equal ( set ( [ snapshot1 . id ] ) )
snapshots_by_tag_value = conn . get_all_snapshots ( filters = { ' tag-value ' : ' testvalue1 ' } )
set ( [ snap . id for snap in snapshots_by_tag_value ] ) . should . equal ( set ( [ snapshot1 . id ] ) )
snapshots_by_tag = conn . get_all_snapshots ( filters = { ' tag:testkey1 ' : ' testvalue1 ' } )
set ( [ snap . id for snap in snapshots_by_tag ] ) . should . equal ( set ( [ snapshot1 . id ] ) )
2016-07-04 13:19:29 +00:00
snapshots_by_encrypted = conn . get_all_snapshots ( filters = { ' encrypted ' : ' true ' } )
set ( [ snap . id for snap in snapshots_by_encrypted ] ) . should . equal ( set ( [ snapshot3 . id ] ) )
2016-01-11 05:44:29 +00:00
2014-08-26 22:16:58 +00:00
@mock_ec2
def test_snapshot_attribute ( ) :
2016-10-15 13:08:44 +00:00
import copy
2014-08-26 22:16:58 +00:00
conn = boto . connect_ec2 ( ' the_key ' , ' the_secret ' )
volume = conn . create_volume ( 80 , " us-east-1a " )
snapshot = volume . create_snapshot ( )
# Baseline
attributes = conn . get_snapshot_attribute ( snapshot . id , attribute = ' createVolumePermission ' )
attributes . name . should . equal ( ' create_volume_permission ' )
attributes . attrs . should . have . length_of ( 0 )
ADD_GROUP_ARGS = { ' snapshot_id ' : snapshot . id ,
' attribute ' : ' createVolumePermission ' ,
' operation ' : ' add ' ,
' groups ' : ' all ' }
REMOVE_GROUP_ARGS = { ' snapshot_id ' : snapshot . id ,
' attribute ' : ' createVolumePermission ' ,
' operation ' : ' remove ' ,
' groups ' : ' all ' }
# Add 'all' group and confirm
2016-10-15 13:08:44 +00:00
with assert_raises ( JSONResponseError ) as ex :
conn . modify_snapshot_attribute ( * * dict ( ADD_GROUP_ARGS , * * { ' dry_run ' : True } ) )
ex . exception . reason . should . equal ( ' DryRunOperation ' )
ex . exception . status . should . equal ( 400 )
ex . exception . message . should . equal ( ' An error occurred (DryRunOperation) when calling the ModifySnapshotAttribute operation: Request would have succeeded, but DryRun flag is set ' )
2014-08-26 22:16:58 +00:00
conn . modify_snapshot_attribute ( * * ADD_GROUP_ARGS )
attributes = conn . get_snapshot_attribute ( snapshot . id , attribute = ' createVolumePermission ' )
attributes . attrs [ ' groups ' ] . should . have . length_of ( 1 )
attributes . attrs [ ' groups ' ] . should . equal ( [ ' all ' ] )
# Add is idempotent
conn . modify_snapshot_attribute . when . called_with ( * * ADD_GROUP_ARGS ) . should_not . throw ( EC2ResponseError )
# Remove 'all' group and confirm
2016-10-15 13:08:44 +00:00
with assert_raises ( JSONResponseError ) as ex :
conn . modify_snapshot_attribute ( * * dict ( REMOVE_GROUP_ARGS , * * { ' dry_run ' : True } ) )
ex . exception . reason . should . equal ( ' DryRunOperation ' )
ex . exception . status . should . equal ( 400 )
ex . exception . message . should . equal ( ' An error occurred (DryRunOperation) when calling the ModifySnapshotAttribute operation: Request would have succeeded, but DryRun flag is set ' )
2014-08-26 22:16:58 +00:00
conn . modify_snapshot_attribute ( * * REMOVE_GROUP_ARGS )
attributes = conn . get_snapshot_attribute ( snapshot . id , attribute = ' createVolumePermission ' )
attributes . attrs . should . have . length_of ( 0 )
# Remove is idempotent
conn . modify_snapshot_attribute . when . called_with ( * * REMOVE_GROUP_ARGS ) . should_not . throw ( EC2ResponseError )
# Error: Add with group != 'all'
with assert_raises ( EC2ResponseError ) as cm :
conn . modify_snapshot_attribute ( snapshot . id ,
attribute = ' createVolumePermission ' ,
operation = ' add ' ,
groups = ' everyone ' )
cm . exception . code . should . equal ( ' InvalidAMIAttributeItemValue ' )
cm . exception . status . should . equal ( 400 )
cm . exception . request_id . should_not . be . none
# Error: Add with invalid snapshot ID
with assert_raises ( EC2ResponseError ) as cm :
conn . modify_snapshot_attribute ( " snapshot-abcd1234 " ,
attribute = ' createVolumePermission ' ,
operation = ' add ' ,
groups = ' all ' )
cm . exception . code . should . equal ( ' InvalidSnapshot.NotFound ' )
cm . exception . status . should . equal ( 400 )
cm . exception . request_id . should_not . be . none
# Error: Remove with invalid snapshot ID
with assert_raises ( EC2ResponseError ) as cm :
conn . modify_snapshot_attribute ( " snapshot-abcd1234 " ,
attribute = ' createVolumePermission ' ,
operation = ' remove ' ,
groups = ' all ' )
cm . exception . code . should . equal ( ' InvalidSnapshot.NotFound ' )
cm . exception . status . should . equal ( 400 )
cm . exception . request_id . should_not . be . none
# Error: Add or remove with user ID instead of group
conn . modify_snapshot_attribute . when . called_with ( snapshot . id ,
attribute = ' createVolumePermission ' ,
operation = ' add ' ,
user_ids = [ ' user ' ] ) . should . throw ( NotImplementedError )
conn . modify_snapshot_attribute . when . called_with ( snapshot . id ,
attribute = ' createVolumePermission ' ,
operation = ' remove ' ,
user_ids = [ ' user ' ] ) . should . throw ( NotImplementedError )
2015-11-03 14:37:02 +00:00
@mock_ec2
def test_create_volume_from_snapshot ( ) :
conn = boto . connect_ec2 ( ' the_key ' , ' the_secret ' )
volume = conn . create_volume ( 80 , " us-east-1a " )
2016-10-15 13:08:44 +00:00
snapshot = volume . create_snapshot ( ' a test snapshot ' )
with assert_raises ( JSONResponseError ) as ex :
snapshot = volume . create_snapshot ( ' a test snapshot ' , dry_run = True )
ex . exception . reason . should . equal ( ' DryRunOperation ' )
ex . exception . status . should . equal ( 400 )
ex . exception . message . should . equal ( ' An error occurred (DryRunOperation) when calling the CreateSnapshot operation: Request would have succeeded, but DryRun flag is set ' )
2015-11-03 14:37:02 +00:00
snapshot = volume . create_snapshot ( ' a test snapshot ' )
snapshot . update ( )
snapshot . status . should . equal ( ' completed ' )
new_volume = snapshot . create_volume ( ' us-east-1a ' )
new_volume . size . should . equal ( 80 )
new_volume . snapshot_id . should . equal ( snapshot . id )
2016-07-04 13:19:29 +00:00
@mock_ec2
def test_create_volume_from_encrypted_snapshot ( ) :
conn = boto . connect_ec2 ( ' the_key ' , ' the_secret ' )
volume = conn . create_volume ( 80 , " us-east-1a " , encrypted = True )
snapshot = volume . create_snapshot ( ' a test snapshot ' )
snapshot . update ( )
snapshot . status . should . equal ( ' completed ' )
new_volume = snapshot . create_volume ( ' us-east-1a ' )
new_volume . size . should . equal ( 80 )
new_volume . snapshot_id . should . equal ( snapshot . id )
new_volume . encrypted . should . be ( True )
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
"""
2014-11-15 18:50:58 +00:00
conn = boto . ec2 . connect_to_region ( " us-east-1 " )
2014-08-20 02:38:36 +00:00
reservation = conn . run_instances ( ' ami-1234abcd ' )
instance = reservation . instances [ 0 ]
2014-08-20 11:14:13 +00:00
2016-10-15 13:08:44 +00:00
with assert_raises ( JSONResponseError ) as ex :
instance . modify_attribute ( ' blockDeviceMapping ' , { ' /dev/sda1 ' : True } , dry_run = True )
ex . exception . reason . should . equal ( ' DryRunOperation ' )
ex . exception . status . should . equal ( 400 )
ex . exception . message . should . equal ( ' An error occurred (DryRunOperation) when calling the ModifyInstanceAttribute operation: Request would have succeeded, but DryRun flag is set ' )
2014-08-20 11:14:13 +00:00
instance . modify_attribute ( ' blockDeviceMapping ' , { ' /dev/sda1 ' : True } )
2014-10-31 02:46:24 +00:00
instance = ec2_backends [ conn . region . name ] . get_instance ( instance . id )
2014-08-20 11:14:13 +00:00
instance . block_device_mapping . should . have . key ( ' /dev/sda1 ' )
instance . block_device_mapping [ ' /dev/sda1 ' ] . delete_on_termination . should . be ( True )
2015-11-03 14:25:47 +00:00
@mock_ec2
def test_volume_tag_escaping ( ) :
conn = boto . connect_ec2 ( ' the_key ' , ' the_secret ' )
vol = conn . create_volume ( 10 , ' us-east-1a ' )
snapshot = conn . create_snapshot ( vol . id , ' Desc ' )
2016-10-15 13:08:44 +00:00
with assert_raises ( JSONResponseError ) as ex :
snapshot . add_tags ( { ' key ' : ' </closed> ' } , dry_run = True )
ex . exception . reason . should . equal ( ' DryRunOperation ' )
ex . exception . status . should . equal ( 400 )
ex . exception . message . should . equal ( ' An error occurred (DryRunOperation) when calling the CreateTags operation: Request would have succeeded, but DryRun flag is set ' )
dict ( conn . get_all_snapshots ( ) [ 0 ] . tags ) . should_not . be . equal ( { ' key ' : ' </closed> ' } )
2015-11-03 14:25:47 +00:00
snapshot . add_tags ( { ' key ' : ' </closed> ' } )
dict ( conn . get_all_snapshots ( ) [ 0 ] . tags ) . should . equal ( { ' key ' : ' </closed> ' } )