| 
									
										
										
										
											2014-08-27 11:17:06 -04:00
										 |  |  | from __future__ import unicode_literals | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | import boto | 
					
						
							| 
									
										
										
										
											2018-01-29 13:53:44 +00:00
										 |  |  | import boto3 | 
					
						
							| 
									
										
										
										
											2020-11-11 15:55:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 18:14:50 +01:00
										 |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2013-08-03 17:21:25 -04:00
										 |  |  | import sure  # noqa | 
					
						
							| 
									
										
										
										
											2020-11-10 18:14:50 +01:00
										 |  |  | from boto.exception import EC2ResponseError | 
					
						
							|  |  |  | from botocore.exceptions import ClientError | 
					
						
							|  |  |  | from moto import mock_ec2, mock_ec2_deprecated | 
					
						
							|  |  |  | from moto.ec2 import ec2_backends | 
					
						
							| 
									
										
										
										
											2019-07-22 21:50:09 -05:00
										 |  |  | from moto.ec2.models import OWNER_ID | 
					
						
							| 
									
										
										
										
											2020-10-14 07:18:50 -07:00
										 |  |  | from moto.kms import mock_kms | 
					
						
							| 
									
										
										
										
											2021-01-13 09:02:11 +00:00
										 |  |  | from tests import EXAMPLE_AMI_ID | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-02-23 17:37:55 -05:00
										 |  |  | def test_create_and_delete_volume(): | 
					
						
							| 
									
										
										
										
											2019-05-25 03:18:16 -07:00
										 |  |  |     conn = boto.ec2.connect_to_region("us-east-1") | 
					
						
							| 
									
										
										
										
											2013-02-23 17:37:55 -05:00
										 |  |  |     volume = conn.create_volume(80, "us-east-1a") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     all_volumes = conn.get_all_volumes() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-12 11:18:25 +00:00
										 |  |  |     current_volume = [item for item in all_volumes if item.id == volume.id] | 
					
						
							|  |  |  |     current_volume.should.have.length_of(1) | 
					
						
							|  |  |  |     current_volume[0].size.should.equal(80) | 
					
						
							|  |  |  |     current_volume[0].zone.should.equal("us-east-1a") | 
					
						
							|  |  |  |     current_volume[0].encrypted.should.be(False) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     volume = current_volume[0] | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  |         volume.delete(dry_run=True) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the DeleteVolume operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-23 17:37:55 -05:00
										 |  |  |     volume.delete() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-12 11:18:25 +00:00
										 |  |  |     all_volumes = conn.get_all_volumes() | 
					
						
							|  |  |  |     my_volume = [item for item in all_volumes if item.id == volume.id] | 
					
						
							|  |  |  |     my_volume.should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2013-02-23 17:37:55 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Deleting something that was already deleted should throw an error | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  |         volume.delete() | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidVolume.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2013-02-23 17:37:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-03 09:37:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 01:50:17 +01:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							|  |  |  | def test_delete_attached_volume(): | 
					
						
							|  |  |  |     conn = boto.ec2.connect_to_region("us-east-1") | 
					
						
							| 
									
										
										
										
											2021-01-13 09:02:11 +00:00
										 |  |  |     reservation = conn.run_instances(EXAMPLE_AMI_ID) | 
					
						
							| 
									
										
										
										
											2020-04-03 01:50:17 +01:00
										 |  |  |     # create an instance | 
					
						
							|  |  |  |     instance = reservation.instances[0] | 
					
						
							|  |  |  |     # create a volume | 
					
						
							|  |  |  |     volume = conn.create_volume(80, "us-east-1a") | 
					
						
							|  |  |  |     # attach volume to instance | 
					
						
							|  |  |  |     volume.attach(instance.id, "/dev/sdh") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     volume.update() | 
					
						
							|  |  |  |     volume.volume_state().should.equal("in-use") | 
					
						
							|  |  |  |     volume.attachment_state().should.equal("attached") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     volume.attach_data.instance_id.should.equal(instance.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # attempt to delete volume | 
					
						
							|  |  |  |     # assert raises VolumeInUseError | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2020-04-03 01:50:17 +01:00
										 |  |  |         volume.delete() | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("VolumeInUse") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2020-04-03 14:17:55 +01:00
										 |  |  |         "Volume {0} is currently attached to {1}".format(volume.id, instance.id) | 
					
						
							| 
									
										
										
										
											2020-04-03 02:27:46 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2020-04-03 01:50:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     volume.detach() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     volume.update() | 
					
						
							|  |  |  |     volume.volume_state().should.equal("available") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     volume.delete() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     all_volumes = conn.get_all_volumes() | 
					
						
							|  |  |  |     my_volume = [item for item in all_volumes if item.id == volume.id] | 
					
						
							|  |  |  |     my_volume.should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | def test_create_encrypted_volume_dryrun(): | 
					
						
							| 
									
										
										
										
											2019-05-25 03:18:16 -07:00
										 |  |  |     conn = boto.ec2.connect_to_region("us-east-1") | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  |         conn.create_volume(80, "us-east-1a", encrypted=True, dry_run=True) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the CreateVolume operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2016-07-04 11:01:48 +01:00
										 |  |  | def test_create_encrypted_volume(): | 
					
						
							| 
									
										
										
										
											2019-05-25 03:18:16 -07:00
										 |  |  |     conn = boto.ec2.connect_to_region("us-east-1") | 
					
						
							| 
									
										
										
										
											2017-11-12 11:18:25 +00:00
										 |  |  |     volume = conn.create_volume(80, "us-east-1a", encrypted=True) | 
					
						
							| 
									
										
										
										
											2016-07-04 11:01:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  |         conn.create_volume(80, "us-east-1a", encrypted=True, dry_run=True) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the CreateVolume operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-12 11:18:25 +00:00
										 |  |  |     all_volumes = [vol for vol in conn.get_all_volumes() if vol.id == volume.id] | 
					
						
							| 
									
										
										
										
											2016-07-04 14:21:31 +01:00
										 |  |  |     all_volumes[0].encrypted.should.be(True) | 
					
						
							| 
									
										
										
										
											2016-07-04 11:01:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2015-07-26 09:53:03 +10:00
										 |  |  | def test_filter_volume_by_id(): | 
					
						
							| 
									
										
										
										
											2019-05-25 03:18:16 -07:00
										 |  |  |     conn = boto.ec2.connect_to_region("us-east-1") | 
					
						
							| 
									
										
										
										
											2015-07-26 09:53:03 +10:00
										 |  |  |     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) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vol1[0].zone.should.equal("us-east-1c") | 
					
						
							| 
									
										
										
										
											2015-07-26 09:53:03 +10:00
										 |  |  |     vol2 = conn.get_all_volumes(volume_ids=[volume1.id, volume2.id]) | 
					
						
							|  |  |  |     vol2.should.have.length_of(2) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.get_all_volumes(volume_ids=["vol-does_not_exist"]) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidVolume.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2017-09-18 23:12:39 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-23 17:37:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | def test_volume_filters(): | 
					
						
							| 
									
										
										
										
											2019-05-25 03:18:16 -07:00
										 |  |  |     conn = boto.ec2.connect_to_region("us-east-1") | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 09:02:11 +00:00
										 |  |  |     reservation = conn.run_instances(EXAMPLE_AMI_ID) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  |     instance = reservation.instances[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     instance.update() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 11:01:48 +01:00
										 |  |  |     volume1 = conn.create_volume(80, "us-east-1a", encrypted=True) | 
					
						
							|  |  |  |     volume2 = conn.create_volume(36, "us-east-1b", encrypted=False) | 
					
						
							| 
									
										
										
										
											2016-07-04 14:09:15 +01:00
										 |  |  |     volume3 = conn.create_volume(20, "us-east-1c", encrypted=True) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshot = volume3.create_snapshot(description="testsnap") | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  |     volume4 = conn.create_volume(25, "us-east-1a", snapshot=snapshot) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.create_tags([volume1.id], {"testkey1": "testvalue1"}) | 
					
						
							|  |  |  |     conn.create_tags([volume2.id], {"testkey2": "testvalue2"}) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     volume1.update() | 
					
						
							|  |  |  |     volume2.update() | 
					
						
							|  |  |  |     volume3.update() | 
					
						
							|  |  |  |     volume4.update() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     block_mapping = instance.block_device_mapping["/dev/sda1"] | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     volume_ids = ( | 
					
						
							|  |  |  |         volume1.id, | 
					
						
							|  |  |  |         volume2.id, | 
					
						
							|  |  |  |         volume3.id, | 
					
						
							|  |  |  |         volume4.id, | 
					
						
							|  |  |  |         block_mapping.volume_id, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-11-12 11:18:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     volumes_by_attach_time = conn.get_all_volumes( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         filters={"attachment.attach-time": block_mapping.attach_time} | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     set([vol.id for vol in volumes_by_attach_time]).should.equal( | 
					
						
							|  |  |  |         {block_mapping.volume_id} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     volumes_by_attach_device = conn.get_all_volumes( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         filters={"attachment.device": "/dev/sda1"} | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     set([vol.id for vol in volumes_by_attach_device]).should.equal( | 
					
						
							|  |  |  |         {block_mapping.volume_id} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     volumes_by_attach_instance_id = conn.get_all_volumes( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         filters={"attachment.instance-id": instance.id} | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     set([vol.id for vol in volumes_by_attach_instance_id]).should.equal( | 
					
						
							|  |  |  |         {block_mapping.volume_id} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     volumes_by_attach_status = conn.get_all_volumes( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         filters={"attachment.status": "attached"} | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     set([vol.id for vol in volumes_by_attach_status]).should.equal( | 
					
						
							|  |  |  |         {block_mapping.volume_id} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-07-22 14:23:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     volumes_by_create_time = conn.get_all_volumes( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         filters={"create-time": volume4.create_time} | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     set([vol.create_time for vol in volumes_by_create_time]).should.equal( | 
					
						
							|  |  |  |         {volume4.create_time} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     volumes_by_size = conn.get_all_volumes(filters={"size": volume2.size}) | 
					
						
							| 
									
										
										
										
											2017-11-12 11:18:25 +00:00
										 |  |  |     set([vol.id for vol in volumes_by_size]).should.equal({volume2.id}) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     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({volume4.id}) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     volumes_by_status = conn.get_all_volumes(filters={"status": "in-use"}) | 
					
						
							|  |  |  |     set([vol.id for vol in volumes_by_status]).should.equal({block_mapping.volume_id}) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     volumes_by_id = conn.get_all_volumes(filters={"volume-id": volume1.id}) | 
					
						
							| 
									
										
										
										
											2017-11-12 11:18:25 +00:00
										 |  |  |     set([vol.id for vol in volumes_by_id]).should.equal({volume1.id}) | 
					
						
							| 
									
										
										
										
											2016-01-20 12:24:33 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     volumes_by_tag_key = conn.get_all_volumes(filters={"tag-key": "testkey1"}) | 
					
						
							| 
									
										
										
										
											2017-11-12 11:18:25 +00:00
										 |  |  |     set([vol.id for vol in volumes_by_tag_key]).should.equal({volume1.id}) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     volumes_by_tag_value = conn.get_all_volumes(filters={"tag-value": "testvalue1"}) | 
					
						
							|  |  |  |     set([vol.id for vol in volumes_by_tag_value]).should.equal({volume1.id}) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     volumes_by_tag = conn.get_all_volumes(filters={"tag:testkey1": "testvalue1"}) | 
					
						
							| 
									
										
										
										
											2017-11-12 11:18:25 +00:00
										 |  |  |     set([vol.id for vol in volumes_by_tag]).should.equal({volume1.id}) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     volumes_by_unencrypted = conn.get_all_volumes(filters={"encrypted": "false"}) | 
					
						
							|  |  |  |     set( | 
					
						
							|  |  |  |         [vol.id for vol in volumes_by_unencrypted if vol.id in volume_ids] | 
					
						
							|  |  |  |     ).should.equal({block_mapping.volume_id, volume2.id}) | 
					
						
							| 
									
										
										
										
											2016-07-04 14:09:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     volumes_by_encrypted = conn.get_all_volumes(filters={"encrypted": "true"}) | 
					
						
							| 
									
										
										
										
											2017-11-12 11:18:25 +00:00
										 |  |  |     set([vol.id for vol in volumes_by_encrypted if vol.id in volume_ids]).should.equal( | 
					
						
							|  |  |  |         {volume1.id, volume3.id, volume4.id} | 
					
						
							| 
									
										
										
										
											2016-07-04 14:09:15 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-07-04 11:01:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     volumes_by_availability_zone = conn.get_all_volumes( | 
					
						
							|  |  |  |         filters={"availability-zone": "us-east-1b"} | 
					
						
							| 
									
										
										
										
											2018-03-21 15:58:32 +00:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     set( | 
					
						
							|  |  |  |         [vol.id for vol in volumes_by_availability_zone if vol.id in volume_ids] | 
					
						
							|  |  |  |     ).should.equal({volume2.id}) | 
					
						
							| 
									
										
										
										
											2018-03-21 15:58:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-02-23 17:37:55 -05:00
										 |  |  | def test_volume_attach_and_detach(): | 
					
						
							| 
									
										
										
										
											2019-05-25 03:18:16 -07:00
										 |  |  |     conn = boto.ec2.connect_to_region("us-east-1") | 
					
						
							| 
									
										
										
										
											2021-01-13 09:02:11 +00:00
										 |  |  |     reservation = conn.run_instances(EXAMPLE_AMI_ID) | 
					
						
							| 
									
										
										
										
											2013-02-23 17:37:55 -05:00
										 |  |  |     instance = reservation.instances[0] | 
					
						
							|  |  |  |     volume = conn.create_volume(80, "us-east-1a") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     volume.update() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     volume.volume_state().should.equal("available") | 
					
						
							| 
									
										
										
										
											2013-02-23 17:37:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  |         volume.attach(instance.id, "/dev/sdh", dry_run=True) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the AttachVolume operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-23 17:37:55 -05:00
										 |  |  |     volume.attach(instance.id, "/dev/sdh") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     volume.update() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     volume.volume_state().should.equal("in-use") | 
					
						
							|  |  |  |     volume.attachment_state().should.equal("attached") | 
					
						
							| 
									
										
										
										
											2013-02-23 17:37:55 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     volume.attach_data.instance_id.should.equal(instance.id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  |         volume.detach(dry_run=True) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the DetachVolume operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-23 17:37:55 -05:00
										 |  |  |     volume.detach() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     volume.update() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     volume.volume_state().should.equal("available") | 
					
						
							| 
									
										
										
										
											2013-02-23 17:37:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm1: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         volume.attach("i-1234abcd", "/dev/sdh") | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm1.value.code.should.equal("InvalidInstanceID.NotFound") | 
					
						
							|  |  |  |     cm1.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm1.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2013-03-11 00:12:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm2: | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  |         conn.detach_volume(volume.id, instance.id, "/dev/sdh") | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm2.value.code.should.equal("InvalidAttachment.NotFound") | 
					
						
							|  |  |  |     cm2.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm2.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2013-02-23 18:01:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm3: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.detach_volume(volume.id, "i-1234abcd", "/dev/sdh") | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm3.value.code.should.equal("InvalidInstanceID.NotFound") | 
					
						
							|  |  |  |     cm3.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm3.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2013-03-11 00:12:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-23 18:01:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-02-23 18:01:41 -05:00
										 |  |  | def test_create_snapshot(): | 
					
						
							| 
									
										
										
										
											2019-05-25 03:18:16 -07:00
										 |  |  |     conn = boto.ec2.connect_to_region("us-east-1") | 
					
						
							| 
									
										
										
										
											2013-02-23 18:01:41 -05:00
										 |  |  |     volume = conn.create_volume(80, "us-east-1a") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         snapshot = volume.create_snapshot("a dryrun snapshot", dry_run=True) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the CreateSnapshot operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshot = volume.create_snapshot("a test snapshot") | 
					
						
							| 
									
										
										
										
											2015-10-14 02:07:47 +11:00
										 |  |  |     snapshot.update() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshot.status.should.equal("completed") | 
					
						
							| 
									
										
										
										
											2013-02-23 18:01:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-12 11:18:25 +00:00
										 |  |  |     snapshots = [snap for snap in conn.get_all_snapshots() if snap.id == snapshot.id] | 
					
						
							| 
									
										
										
										
											2013-02-23 18:01:41 -05:00
										 |  |  |     snapshots.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshots[0].description.should.equal("a test snapshot") | 
					
						
							| 
									
										
										
										
											2015-07-26 09:53:03 +10:00
										 |  |  |     snapshots[0].start_time.should_not.be.none | 
					
						
							| 
									
										
										
										
											2016-07-04 14:19:29 +01:00
										 |  |  |     snapshots[0].encrypted.should.be(False) | 
					
						
							| 
									
										
										
										
											2013-02-23 18:01:41 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create snapshot without description | 
					
						
							| 
									
										
										
										
											2017-11-12 11:18:25 +00:00
										 |  |  |     num_snapshots = len(conn.get_all_snapshots()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-23 18:01:41 -05:00
										 |  |  |     snapshot = volume.create_snapshot() | 
					
						
							| 
									
										
										
										
											2017-11-12 11:18:25 +00:00
										 |  |  |     conn.get_all_snapshots().should.have.length_of(num_snapshots + 1) | 
					
						
							| 
									
										
										
										
											2013-02-23 18:01:41 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     snapshot.delete() | 
					
						
							| 
									
										
										
										
											2017-11-12 11:18:25 +00:00
										 |  |  |     conn.get_all_snapshots().should.have.length_of(num_snapshots) | 
					
						
							| 
									
										
										
										
											2013-02-23 18:01:41 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Deleting something that was already deleted should throw an error | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  |         snapshot.delete() | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidSnapshot.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2014-08-19 23:38:36 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-03 09:37:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2016-07-04 14:19:29 +01:00
										 |  |  | def test_create_encrypted_snapshot(): | 
					
						
							| 
									
										
										
										
											2019-05-25 03:18:16 -07:00
										 |  |  |     conn = boto.ec2.connect_to_region("us-east-1") | 
					
						
							| 
									
										
										
										
											2016-07-04 14:19:29 +01:00
										 |  |  |     volume = conn.create_volume(80, "us-east-1a", encrypted=True) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshot = volume.create_snapshot("a test snapshot") | 
					
						
							| 
									
										
										
										
											2016-07-04 14:19:29 +01:00
										 |  |  |     snapshot.update() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshot.status.should.equal("completed") | 
					
						
							| 
									
										
										
										
											2016-07-04 14:19:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-12 11:18:25 +00:00
										 |  |  |     snapshots = [snap for snap in conn.get_all_snapshots() if snap.id == snapshot.id] | 
					
						
							| 
									
										
										
										
											2016-07-04 14:19:29 +01:00
										 |  |  |     snapshots.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshots[0].description.should.equal("a test snapshot") | 
					
						
							| 
									
										
										
										
											2016-07-04 14:19:29 +01:00
										 |  |  |     snapshots[0].start_time.should_not.be.none | 
					
						
							|  |  |  |     snapshots[0].encrypted.should.be(True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2015-07-26 09:53:03 +10:00
										 |  |  | def test_filter_snapshot_by_id(): | 
					
						
							| 
									
										
										
										
											2019-05-25 03:18:16 -07:00
										 |  |  |     conn = boto.ec2.connect_to_region("us-east-1") | 
					
						
							| 
									
										
										
										
											2015-07-26 09:53:03 +10:00
										 |  |  |     volume1 = conn.create_volume(36, "us-east-1a") | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     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") | 
					
						
							| 
									
										
										
										
											2015-07-26 09:53:03 +10:00
										 |  |  |     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 10:45:01 -04:00
										 |  |  |     snapshots1[0].region.name.should.equal(conn.region.name) | 
					
						
							| 
									
										
										
										
											2015-07-26 09:53:03 +10: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 10:45:01 -04:00
										 |  |  |         s.region.name.should.equal(conn.region.name) | 
					
						
							| 
									
										
										
										
											2014-08-19 23:38:36 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.get_all_snapshots(snapshot_ids=["snap-does_not_exist"]) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidSnapshot.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2017-09-18 23:12:39 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-03 09:37:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | def test_snapshot_filters(): | 
					
						
							| 
									
										
										
										
											2019-05-25 03:18:16 -07:00
										 |  |  |     conn = boto.ec2.connect_to_region("us-east-1") | 
					
						
							| 
									
										
										
										
											2016-07-04 14:19:29 +01:00
										 |  |  |     volume1 = conn.create_volume(20, "us-east-1a", encrypted=False) | 
					
						
							|  |  |  |     volume2 = conn.create_volume(25, "us-east-1a", encrypted=True) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshot1 = volume1.create_snapshot(description="testsnapshot1") | 
					
						
							|  |  |  |     snapshot2 = volume1.create_snapshot(description="testsnapshot2") | 
					
						
							|  |  |  |     snapshot3 = volume2.create_snapshot(description="testsnapshot3") | 
					
						
							| 
									
										
										
										
											2016-07-04 11:01:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.create_tags([snapshot1.id], {"testkey1": "testvalue1"}) | 
					
						
							|  |  |  |     conn.create_tags([snapshot2.id], {"testkey2": "testvalue2"}) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     snapshots_by_description = conn.get_all_snapshots( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         filters={"description": "testsnapshot1"} | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     set([snap.id for snap in snapshots_by_description]).should.equal({snapshot1.id}) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshots_by_id = conn.get_all_snapshots(filters={"snapshot-id": snapshot1.id}) | 
					
						
							|  |  |  |     set([snap.id for snap in snapshots_by_id]).should.equal({snapshot1.id}) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     snapshots_by_start_time = conn.get_all_snapshots( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         filters={"start-time": snapshot1.start_time} | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     set([snap.start_time for snap in snapshots_by_start_time]).should.equal( | 
					
						
							|  |  |  |         {snapshot1.start_time} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     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( | 
					
						
							|  |  |  |         {snapshot1.id, snapshot2.id} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshots_by_status = conn.get_all_snapshots(filters={"status": "completed"}) | 
					
						
							|  |  |  |     ( | 
					
						
							|  |  |  |         {snapshot1.id, snapshot2.id, snapshot3.id} | 
					
						
							|  |  |  |         - {snap.id for snap in snapshots_by_status} | 
					
						
							|  |  |  |     ).should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2017-05-10 21:58:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     snapshots_by_volume_size = conn.get_all_snapshots( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         filters={"volume-size": volume1.size} | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     set([snap.id for snap in snapshots_by_volume_size]).should.equal( | 
					
						
							|  |  |  |         {snapshot1.id, snapshot2.id} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshots_by_tag_key = conn.get_all_snapshots(filters={"tag-key": "testkey1"}) | 
					
						
							|  |  |  |     set([snap.id for snap in snapshots_by_tag_key]).should.equal({snapshot1.id}) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshots_by_tag_value = conn.get_all_snapshots(filters={"tag-value": "testvalue1"}) | 
					
						
							|  |  |  |     set([snap.id for snap in snapshots_by_tag_value]).should.equal({snapshot1.id}) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshots_by_tag = conn.get_all_snapshots(filters={"tag:testkey1": "testvalue1"}) | 
					
						
							|  |  |  |     set([snap.id for snap in snapshots_by_tag]).should.equal({snapshot1.id}) | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshots_by_encrypted = conn.get_all_snapshots(filters={"encrypted": "true"}) | 
					
						
							|  |  |  |     set([snap.id for snap in snapshots_by_encrypted]).should.equal({snapshot3.id}) | 
					
						
							| 
									
										
										
										
											2016-07-04 14:19:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshots_by_owner_id = conn.get_all_snapshots(filters={"owner-id": OWNER_ID}) | 
					
						
							|  |  |  |     set([snap.id for snap in snapshots_by_owner_id]).should.equal( | 
					
						
							|  |  |  |         {snapshot1.id, snapshot2.id, snapshot3.id} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-05-25 05:34:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-11 15:44:29 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-08-26 15:16:58 -07:00
										 |  |  | def test_snapshot_attribute(): | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  |     import copy | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-25 03:18:16 -07:00
										 |  |  |     conn = boto.ec2.connect_to_region("us-east-1") | 
					
						
							| 
									
										
										
										
											2014-08-26 15:16:58 -07:00
										 |  |  |     volume = conn.create_volume(80, "us-east-1a") | 
					
						
							|  |  |  |     snapshot = volume.create_snapshot() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Baseline | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     attributes = conn.get_snapshot_attribute( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         snapshot.id, attribute="createVolumePermission" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     attributes.name.should.equal("create_volume_permission") | 
					
						
							| 
									
										
										
										
											2014-08-26 15:16:58 -07:00
										 |  |  |     attributes.attrs.should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ADD_GROUP_ARGS = { | 
					
						
							|  |  |  |         "snapshot_id": snapshot.id, | 
					
						
							|  |  |  |         "attribute": "createVolumePermission", | 
					
						
							|  |  |  |         "operation": "add", | 
					
						
							|  |  |  |         "groups": "all", | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-08-26 15:16:58 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     REMOVE_GROUP_ARGS = { | 
					
						
							|  |  |  |         "snapshot_id": snapshot.id, | 
					
						
							|  |  |  |         "attribute": "createVolumePermission", | 
					
						
							|  |  |  |         "operation": "remove", | 
					
						
							|  |  |  |         "groups": "all", | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-08-26 15:16:58 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Add 'all' group and confirm | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.modify_snapshot_attribute(**dict(ADD_GROUP_ARGS, **{"dry_run": True})) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the ModifySnapshotAttribute operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-26 15:16:58 -07:00
										 |  |  |     conn.modify_snapshot_attribute(**ADD_GROUP_ARGS) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     attributes = conn.get_snapshot_attribute( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         snapshot.id, attribute="createVolumePermission" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     attributes.attrs["groups"].should.have.length_of(1) | 
					
						
							|  |  |  |     attributes.attrs["groups"].should.equal(["all"]) | 
					
						
							| 
									
										
										
										
											2014-08-26 15:16:58 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Add is idempotent | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.modify_snapshot_attribute.when.called_with(**ADD_GROUP_ARGS).should_not.throw( | 
					
						
							|  |  |  |         EC2ResponseError | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-08-26 15:16:58 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Remove 'all' group and confirm | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.modify_snapshot_attribute(**dict(REMOVE_GROUP_ARGS, **{"dry_run": True})) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the ModifySnapshotAttribute operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-26 15:16:58 -07:00
										 |  |  |     conn.modify_snapshot_attribute(**REMOVE_GROUP_ARGS) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     attributes = conn.get_snapshot_attribute( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         snapshot.id, attribute="createVolumePermission" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-08-26 15:16:58 -07:00
										 |  |  |     attributes.attrs.should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Remove is idempotent | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     conn.modify_snapshot_attribute.when.called_with( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         **REMOVE_GROUP_ARGS | 
					
						
							|  |  |  |     ).should_not.throw(EC2ResponseError) | 
					
						
							| 
									
										
										
										
											2014-08-26 15:16:58 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Error: Add with group != 'all' | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.modify_snapshot_attribute( | 
					
						
							|  |  |  |             snapshot.id, | 
					
						
							|  |  |  |             attribute="createVolumePermission", | 
					
						
							|  |  |  |             operation="add", | 
					
						
							|  |  |  |             groups="everyone", | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidAMIAttributeItemValue") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2014-08-26 15:16:58 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Error: Add with invalid snapshot ID | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.modify_snapshot_attribute( | 
					
						
							|  |  |  |             "snapshot-abcd1234", | 
					
						
							|  |  |  |             attribute="createVolumePermission", | 
					
						
							|  |  |  |             operation="add", | 
					
						
							|  |  |  |             groups="all", | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidSnapshot.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2014-08-26 15:16:58 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Error: Remove with invalid snapshot ID | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.modify_snapshot_attribute( | 
					
						
							|  |  |  |             "snapshot-abcd1234", | 
					
						
							|  |  |  |             attribute="createVolumePermission", | 
					
						
							|  |  |  |             operation="remove", | 
					
						
							|  |  |  |             groups="all", | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidSnapshot.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2014-08-26 15:16:58 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 12:03:54 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_modify_snapshot_attribute(): | 
					
						
							|  |  |  |     import copy | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ec2_client = boto3.client("ec2", region_name="us-east-1") | 
					
						
							|  |  |  |     response = ec2_client.create_volume(Size=80, AvailabilityZone="us-east-1a") | 
					
						
							|  |  |  |     volume = boto3.resource("ec2", region_name="us-east-1").Volume(response["VolumeId"]) | 
					
						
							|  |  |  |     snapshot = volume.create_snapshot() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Baseline | 
					
						
							|  |  |  |     attributes = ec2_client.describe_snapshot_attribute( | 
					
						
							|  |  |  |         SnapshotId=snapshot.id, Attribute="createVolumePermission" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     assert not attributes[ | 
					
						
							|  |  |  |         "CreateVolumePermissions" | 
					
						
							|  |  |  |     ], "Snapshot should have no permissions." | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ADD_GROUP_ARGS = { | 
					
						
							|  |  |  |         "SnapshotId": snapshot.id, | 
					
						
							|  |  |  |         "Attribute": "createVolumePermission", | 
					
						
							|  |  |  |         "OperationType": "add", | 
					
						
							|  |  |  |         "GroupNames": ["all"], | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REMOVE_GROUP_ARGS = { | 
					
						
							|  |  |  |         "SnapshotId": snapshot.id, | 
					
						
							|  |  |  |         "Attribute": "createVolumePermission", | 
					
						
							|  |  |  |         "OperationType": "remove", | 
					
						
							|  |  |  |         "GroupNames": ["all"], | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Add 'all' group and confirm | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     with pytest.raises(ClientError) as cm: | 
					
						
							| 
									
										
										
										
											2020-08-01 12:03:54 -04:00
										 |  |  |         ec2_client.modify_snapshot_attribute(**dict(ADD_GROUP_ARGS, **{"DryRun": True})) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.response["Error"]["Code"].should.equal("DryRunOperation") | 
					
						
							|  |  |  |     cm.value.response["ResponseMetadata"]["RequestId"].should_not.be.none | 
					
						
							|  |  |  |     cm.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) | 
					
						
							| 
									
										
										
										
											2020-08-01 12:03:54 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ec2_client.modify_snapshot_attribute(**ADD_GROUP_ARGS) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     attributes = ec2_client.describe_snapshot_attribute( | 
					
						
							|  |  |  |         SnapshotId=snapshot.id, Attribute="createVolumePermission" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     assert attributes["CreateVolumePermissions"] == [ | 
					
						
							|  |  |  |         {"Group": "all"} | 
					
						
							|  |  |  |     ], "This snapshot should have public group permissions." | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Add is idempotent | 
					
						
							|  |  |  |     ec2_client.modify_snapshot_attribute.when.called_with( | 
					
						
							|  |  |  |         **ADD_GROUP_ARGS | 
					
						
							|  |  |  |     ).should_not.throw(ClientError) | 
					
						
							|  |  |  |     assert attributes["CreateVolumePermissions"] == [ | 
					
						
							|  |  |  |         {"Group": "all"} | 
					
						
							|  |  |  |     ], "This snapshot should have public group permissions." | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Remove 'all' group and confirm | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     with pytest.raises(ClientError) as ex: | 
					
						
							| 
									
										
										
										
											2020-08-01 12:03:54 -04:00
										 |  |  |         ec2_client.modify_snapshot_attribute( | 
					
						
							|  |  |  |             **dict(REMOVE_GROUP_ARGS, **{"DryRun": True}) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.response["Error"]["Code"].should.equal("DryRunOperation") | 
					
						
							|  |  |  |     cm.value.response["ResponseMetadata"]["RequestId"].should_not.be.none | 
					
						
							|  |  |  |     cm.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) | 
					
						
							| 
									
										
										
										
											2020-08-01 12:03:54 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ec2_client.modify_snapshot_attribute(**REMOVE_GROUP_ARGS) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     attributes = ec2_client.describe_snapshot_attribute( | 
					
						
							|  |  |  |         SnapshotId=snapshot.id, Attribute="createVolumePermission" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     assert not attributes[ | 
					
						
							|  |  |  |         "CreateVolumePermissions" | 
					
						
							|  |  |  |     ], "This snapshot should have no permissions." | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Remove is idempotent | 
					
						
							|  |  |  |     ec2_client.modify_snapshot_attribute.when.called_with( | 
					
						
							|  |  |  |         **REMOVE_GROUP_ARGS | 
					
						
							|  |  |  |     ).should_not.throw(ClientError) | 
					
						
							|  |  |  |     assert not attributes[ | 
					
						
							|  |  |  |         "CreateVolumePermissions" | 
					
						
							|  |  |  |     ], "This snapshot should have no permissions." | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Error: Add with group != 'all' | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     with pytest.raises(ClientError) as cm: | 
					
						
							| 
									
										
										
										
											2020-08-01 12:03:54 -04:00
										 |  |  |         ec2_client.modify_snapshot_attribute( | 
					
						
							|  |  |  |             SnapshotId=snapshot.id, | 
					
						
							|  |  |  |             Attribute="createVolumePermission", | 
					
						
							|  |  |  |             OperationType="add", | 
					
						
							|  |  |  |             GroupNames=["everyone"], | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.response["Error"]["Code"].should.equal("InvalidAMIAttributeItemValue") | 
					
						
							|  |  |  |     cm.value.response["ResponseMetadata"]["RequestId"].should_not.be.none | 
					
						
							|  |  |  |     cm.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) | 
					
						
							| 
									
										
										
										
											2020-08-01 12:03:54 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Error: Add with invalid snapshot ID | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     with pytest.raises(ClientError) as cm: | 
					
						
							| 
									
										
										
										
											2020-08-01 12:03:54 -04:00
										 |  |  |         ec2_client.modify_snapshot_attribute( | 
					
						
							|  |  |  |             SnapshotId="snapshot-abcd1234", | 
					
						
							|  |  |  |             Attribute="createVolumePermission", | 
					
						
							|  |  |  |             OperationType="add", | 
					
						
							|  |  |  |             GroupNames=["all"], | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.response["Error"]["Code"].should.equal("InvalidSnapshot.NotFound") | 
					
						
							|  |  |  |     cm.value.response["ResponseMetadata"]["RequestId"].should_not.be.none | 
					
						
							|  |  |  |     cm.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) | 
					
						
							| 
									
										
										
										
											2020-08-01 12:03:54 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Error: Remove with invalid snapshot ID | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     with pytest.raises(ClientError) as cm: | 
					
						
							| 
									
										
										
										
											2020-08-01 12:03:54 -04:00
										 |  |  |         ec2_client.modify_snapshot_attribute( | 
					
						
							|  |  |  |             SnapshotId="snapshot-abcd1234", | 
					
						
							|  |  |  |             Attribute="createVolumePermission", | 
					
						
							|  |  |  |             OperationType="remove", | 
					
						
							|  |  |  |             GroupNames=["all"], | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.response["Error"]["Code"].should.equal("InvalidSnapshot.NotFound") | 
					
						
							|  |  |  |     cm.value.response["ResponseMetadata"]["RequestId"].should_not.be.none | 
					
						
							|  |  |  |     cm.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) | 
					
						
							| 
									
										
										
										
											2020-08-01 12:03:54 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Test adding user id | 
					
						
							|  |  |  |     ec2_client.modify_snapshot_attribute( | 
					
						
							|  |  |  |         SnapshotId=snapshot.id, | 
					
						
							|  |  |  |         Attribute="createVolumePermission", | 
					
						
							|  |  |  |         OperationType="add", | 
					
						
							|  |  |  |         UserIds=["1234567891"], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     attributes = ec2_client.describe_snapshot_attribute( | 
					
						
							|  |  |  |         SnapshotId=snapshot.id, Attribute="createVolumePermission" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     assert len(attributes["CreateVolumePermissions"]) == 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Test adding user id again along with additional. | 
					
						
							|  |  |  |     ec2_client.modify_snapshot_attribute( | 
					
						
							|  |  |  |         SnapshotId=snapshot.id, | 
					
						
							|  |  |  |         Attribute="createVolumePermission", | 
					
						
							|  |  |  |         OperationType="add", | 
					
						
							|  |  |  |         UserIds=["1234567891", "2345678912"], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     attributes = ec2_client.describe_snapshot_attribute( | 
					
						
							|  |  |  |         SnapshotId=snapshot.id, Attribute="createVolumePermission" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     assert len(attributes["CreateVolumePermissions"]) == 2 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Test removing both user IDs. | 
					
						
							|  |  |  |     ec2_client.modify_snapshot_attribute( | 
					
						
							|  |  |  |         SnapshotId=snapshot.id, | 
					
						
							|  |  |  |         Attribute="createVolumePermission", | 
					
						
							|  |  |  |         OperationType="remove", | 
					
						
							|  |  |  |         UserIds=["1234567891", "2345678912"], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     attributes = ec2_client.describe_snapshot_attribute( | 
					
						
							|  |  |  |         SnapshotId=snapshot.id, Attribute="createVolumePermission" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     assert len(attributes["CreateVolumePermissions"]) == 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Idempotency when removing users. | 
					
						
							|  |  |  |     ec2_client.modify_snapshot_attribute( | 
					
						
							|  |  |  |         SnapshotId=snapshot.id, | 
					
						
							|  |  |  |         Attribute="createVolumePermission", | 
					
						
							|  |  |  |         OperationType="remove", | 
					
						
							|  |  |  |         UserIds=["1234567891"], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     attributes = ec2_client.describe_snapshot_attribute( | 
					
						
							|  |  |  |         SnapshotId=snapshot.id, Attribute="createVolumePermission" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     assert len(attributes["CreateVolumePermissions"]) == 0 | 
					
						
							| 
									
										
										
										
											2014-08-26 15:16:58 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2015-11-03 09:37:02 -05:00
										 |  |  | def test_create_volume_from_snapshot(): | 
					
						
							| 
									
										
										
										
											2019-05-25 03:18:16 -07:00
										 |  |  |     conn = boto.ec2.connect_to_region("us-east-1") | 
					
						
							| 
									
										
										
										
											2015-11-03 09:37:02 -05:00
										 |  |  |     volume = conn.create_volume(80, "us-east-1a") | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshot = volume.create_snapshot("a test snapshot") | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         snapshot = volume.create_snapshot("a test snapshot", dry_run=True) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the CreateSnapshot operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2015-11-03 09:37:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshot = volume.create_snapshot("a test snapshot") | 
					
						
							| 
									
										
										
										
											2015-11-03 09:37:02 -05:00
										 |  |  |     snapshot.update() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshot.status.should.equal("completed") | 
					
						
							| 
									
										
										
										
											2015-11-03 09:37:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     new_volume = snapshot.create_volume("us-east-1a") | 
					
						
							| 
									
										
										
										
											2015-11-03 09:37:02 -05:00
										 |  |  |     new_volume.size.should.equal(80) | 
					
						
							|  |  |  |     new_volume.snapshot_id.should.equal(snapshot.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2016-07-04 14:19:29 +01:00
										 |  |  | def test_create_volume_from_encrypted_snapshot(): | 
					
						
							| 
									
										
										
										
											2019-05-25 03:18:16 -07:00
										 |  |  |     conn = boto.ec2.connect_to_region("us-east-1") | 
					
						
							| 
									
										
										
										
											2016-07-04 14:19:29 +01:00
										 |  |  |     volume = conn.create_volume(80, "us-east-1a", encrypted=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshot = volume.create_snapshot("a test snapshot") | 
					
						
							| 
									
										
										
										
											2016-07-04 14:19:29 +01:00
										 |  |  |     snapshot.update() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshot.status.should.equal("completed") | 
					
						
							| 
									
										
										
										
											2016-07-04 14:19:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     new_volume = snapshot.create_volume("us-east-1a") | 
					
						
							| 
									
										
										
										
											2016-07-04 14:19:29 +01:00
										 |  |  |     new_volume.size.should.equal(80) | 
					
						
							|  |  |  |     new_volume.snapshot_id.should.equal(snapshot.id) | 
					
						
							|  |  |  |     new_volume.encrypted.should.be(True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-08-19 23:38:36 -03:00
										 |  |  | 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 13:50:58 -05:00
										 |  |  |     conn = boto.ec2.connect_to_region("us-east-1") | 
					
						
							| 
									
										
										
										
											2014-08-19 23:38:36 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 09:02:11 +00:00
										 |  |  |     reservation = conn.run_instances(EXAMPLE_AMI_ID) | 
					
						
							| 
									
										
										
										
											2014-08-19 23:38:36 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     instance = reservation.instances[0] | 
					
						
							| 
									
										
										
										
											2014-08-20 08:14:13 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         instance.modify_attribute( | 
					
						
							|  |  |  |             "blockDeviceMapping", {"/dev/sda1": True}, dry_run=True | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the ModifyInstanceAttribute operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     instance.modify_attribute("blockDeviceMapping", {"/dev/sda1": True}) | 
					
						
							| 
									
										
										
										
											2014-08-20 08:14:13 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-30 22:46:24 -04:00
										 |  |  |     instance = ec2_backends[conn.region.name].get_instance(instance.id) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07: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 09:25:47 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2015-11-03 09:25:47 -05:00
										 |  |  | def test_volume_tag_escaping(): | 
					
						
							| 
									
										
										
										
											2019-05-25 03:18:16 -07:00
										 |  |  |     conn = boto.ec2.connect_to_region("us-east-1") | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vol = conn.create_volume(10, "us-east-1a") | 
					
						
							|  |  |  |     snapshot = conn.create_snapshot(vol.id, "Desc") | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         snapshot.add_tags({"key": "</closed>"}, dry_run=True) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the CreateTags operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-11-12 11:18:25 +00:00
										 |  |  |     snaps = [snap for snap in conn.get_all_snapshots() if snap.id == snapshot.id] | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     dict(snaps[0].tags).should_not.be.equal({"key": "</closed>"}) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshot.add_tags({"key": "</closed>"}) | 
					
						
							| 
									
										
										
										
											2015-11-03 09:25:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-12 11:18:25 +00:00
										 |  |  |     snaps = [snap for snap in conn.get_all_snapshots() if snap.id == snapshot.id] | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     dict(snaps[0].tags).should.equal({"key": "</closed>"}) | 
					
						
							| 
									
										
										
										
											2018-01-29 13:53:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-07 16:59:03 -05:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_volume_property_hidden_when_no_tags_exist(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2_client = boto3.client("ec2", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2019-03-07 16:59:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     volume_response = ec2_client.create_volume(Size=10, AvailabilityZone="us-east-1a") | 
					
						
							| 
									
										
										
										
											2019-03-07 16:59:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     volume_response.get("Tags").should.equal(None) | 
					
						
							| 
									
										
										
										
											2019-03-07 16:59:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-15 16:01:08 -04:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_copy_snapshot(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2_client = boto3.client("ec2", region_name="eu-west-1") | 
					
						
							|  |  |  |     dest_ec2_client = boto3.client("ec2", region_name="eu-west-2") | 
					
						
							| 
									
										
										
										
											2018-04-15 16:01:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     volume_response = ec2_client.create_volume(AvailabilityZone="eu-west-1a", Size=10) | 
					
						
							| 
									
										
										
										
											2020-10-09 04:33:07 -07:00
										 |  |  |     tag_spec = [ | 
					
						
							|  |  |  |         {"ResourceType": "snapshot", "Tags": [{"Key": "key", "Value": "value"}]} | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2018-04-15 16:01:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     create_snapshot_response = ec2_client.create_snapshot( | 
					
						
							| 
									
										
										
										
											2020-10-09 04:33:07 -07:00
										 |  |  |         VolumeId=volume_response["VolumeId"], TagSpecifications=tag_spec | 
					
						
							| 
									
										
										
										
											2018-04-15 16:01:08 -04:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-03-07 16:59:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-15 16:01:08 -04:00
										 |  |  |     copy_snapshot_response = dest_ec2_client.copy_snapshot( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         SourceSnapshotId=create_snapshot_response["SnapshotId"], | 
					
						
							|  |  |  |         SourceRegion="eu-west-1", | 
					
						
							| 
									
										
										
										
											2020-10-09 04:33:07 -07:00
										 |  |  |         TagSpecifications=tag_spec, | 
					
						
							| 
									
										
										
										
											2018-04-15 16:01:08 -04:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2020-10-09 04:33:07 -07:00
										 |  |  |     copy_snapshot_response["Tags"].should.equal(tag_spec[0]["Tags"]) | 
					
						
							| 
									
										
										
										
											2019-03-07 16:59:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="eu-west-1") | 
					
						
							|  |  |  |     dest_ec2 = boto3.resource("ec2", region_name="eu-west-2") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     source = ec2.Snapshot(create_snapshot_response["SnapshotId"]) | 
					
						
							|  |  |  |     dest = dest_ec2.Snapshot(copy_snapshot_response["SnapshotId"]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     attribs = [ | 
					
						
							|  |  |  |         "data_encryption_key_id", | 
					
						
							|  |  |  |         "encrypted", | 
					
						
							|  |  |  |         "kms_key_id", | 
					
						
							|  |  |  |         "owner_alias", | 
					
						
							|  |  |  |         "owner_id", | 
					
						
							|  |  |  |         "progress", | 
					
						
							|  |  |  |         "state", | 
					
						
							|  |  |  |         "state_message", | 
					
						
							|  |  |  |         "tags", | 
					
						
							|  |  |  |         "volume_id", | 
					
						
							|  |  |  |         "volume_size", | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2019-03-07 16:59:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-15 16:01:08 -04:00
										 |  |  |     for attrib in attribs: | 
					
						
							|  |  |  |         getattr(source, attrib).should.equal(getattr(dest, attrib)) | 
					
						
							| 
									
										
										
										
											2019-03-07 16:59:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-15 16:01:08 -04:00
										 |  |  |     # Copy from non-existent source ID. | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError) as cm: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         create_snapshot_error = ec2_client.create_snapshot(VolumeId="vol-abcd1234") | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |         cm.value.response["Error"]["Code"].should.equal("InvalidVolume.NotFound") | 
					
						
							|  |  |  |         cm.value.response["Error"]["Message"].should.equal( | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |             "The volume 'vol-abcd1234' does not exist." | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |         cm.value.response["ResponseMetadata"]["RequestId"].should_not.be.none | 
					
						
							|  |  |  |         cm.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) | 
					
						
							| 
									
										
										
										
											2018-04-15 16:01:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Copy from non-existent source region. | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError) as cm: | 
					
						
							| 
									
										
										
										
											2018-04-15 16:01:08 -04:00
										 |  |  |         copy_snapshot_response = dest_ec2_client.copy_snapshot( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             SourceSnapshotId=create_snapshot_response["SnapshotId"], | 
					
						
							|  |  |  |             SourceRegion="eu-west-2", | 
					
						
							| 
									
										
										
										
											2018-04-15 16:01:08 -04:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |         cm.value.response["Error"]["Code"].should.equal("InvalidSnapshot.NotFound") | 
					
						
							|  |  |  |         cm.value.response["Error"]["Message"].should.be.none | 
					
						
							|  |  |  |         cm.value.response["ResponseMetadata"]["RequestId"].should_not.be.none | 
					
						
							|  |  |  |         cm.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-15 16:01:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 13:53:44 +00:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_search_for_many_snapshots(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2_client = boto3.client("ec2", region_name="eu-west-1") | 
					
						
							| 
									
										
										
										
											2018-01-29 13:53:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     volume_response = ec2_client.create_volume(AvailabilityZone="eu-west-1a", Size=10) | 
					
						
							| 
									
										
										
										
											2018-01-29 13:53:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     snapshot_ids = [] | 
					
						
							|  |  |  |     for i in range(1, 20): | 
					
						
							|  |  |  |         create_snapshot_response = ec2_client.create_snapshot( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             VolumeId=volume_response["VolumeId"] | 
					
						
							| 
									
										
										
										
											2018-01-29 13:53:44 +00:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         snapshot_ids.append(create_snapshot_response["SnapshotId"]) | 
					
						
							| 
									
										
										
										
											2018-01-29 13:53:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     snapshots_response = ec2_client.describe_snapshots(SnapshotIds=snapshot_ids) | 
					
						
							| 
									
										
										
										
											2018-01-29 13:53:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     assert len(snapshots_response["Snapshots"]) == len(snapshot_ids) | 
					
						
							| 
									
										
										
										
											2020-10-14 07:18:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_create_unencrypted_volume_with_kms_key_fails(): | 
					
						
							|  |  |  |     resource = boto3.resource("ec2", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2020-11-10 18:14:50 +01:00
										 |  |  |     with pytest.raises(ClientError) as ex: | 
					
						
							| 
									
										
										
										
											2020-10-14 07:18:50 -07:00
										 |  |  |         resource.create_volume( | 
					
						
							|  |  |  |             AvailabilityZone="us-east-1a", Encrypted=False, KmsKeyId="key", Size=10 | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-11-10 18:14:50 +01:00
										 |  |  |     ex.value.response["Error"]["Code"].should.equal("InvalidParameterDependency") | 
					
						
							|  |  |  |     ex.value.response["Error"]["Message"].should.contain("KmsKeyId") | 
					
						
							| 
									
										
										
										
											2020-10-14 07:18:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_kms | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_create_encrypted_volume_without_kms_key_should_use_default_key(): | 
					
						
							|  |  |  |     kms = boto3.client("kms", region_name="us-east-1") | 
					
						
							|  |  |  |     # Default master key for EBS does not exist until needed. | 
					
						
							| 
									
										
										
										
											2020-11-10 18:14:50 +01:00
										 |  |  |     with pytest.raises(ClientError) as ex: | 
					
						
							| 
									
										
										
										
											2020-10-14 07:18:50 -07:00
										 |  |  |         kms.describe_key(KeyId="alias/aws/ebs") | 
					
						
							| 
									
										
										
										
											2020-11-10 18:14:50 +01:00
										 |  |  |     ex.value.response["Error"]["Code"].should.equal("NotFoundException") | 
					
						
							| 
									
										
										
										
											2020-10-14 07:18:50 -07:00
										 |  |  |     # Creating an encrypted volume should create (and use) the default key. | 
					
						
							|  |  |  |     resource = boto3.resource("ec2", region_name="us-east-1") | 
					
						
							|  |  |  |     volume = resource.create_volume( | 
					
						
							|  |  |  |         AvailabilityZone="us-east-1a", Encrypted=True, Size=10 | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     default_ebs_key_arn = kms.describe_key(KeyId="alias/aws/ebs")["KeyMetadata"]["Arn"] | 
					
						
							|  |  |  |     volume.kms_key_id.should.equal(default_ebs_key_arn) | 
					
						
							|  |  |  |     volume.encrypted.should.be.true | 
					
						
							|  |  |  |     # Subsequent encrypted volumes should use the now-created default key. | 
					
						
							|  |  |  |     volume = resource.create_volume( | 
					
						
							|  |  |  |         AvailabilityZone="us-east-1a", Encrypted=True, Size=10 | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     volume.kms_key_id.should.equal(default_ebs_key_arn) | 
					
						
							|  |  |  |     volume.encrypted.should.be.true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_create_volume_with_kms_key(): | 
					
						
							|  |  |  |     resource = boto3.resource("ec2", region_name="us-east-1") | 
					
						
							|  |  |  |     volume = resource.create_volume( | 
					
						
							|  |  |  |         AvailabilityZone="us-east-1a", Encrypted=True, KmsKeyId="key", Size=10 | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     volume.kms_key_id.should.equal("key") | 
					
						
							|  |  |  |     volume.encrypted.should.be.true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_kms_key_id_property_hidden_when_volume_not_encrypted(): | 
					
						
							|  |  |  |     client = boto3.client("ec2", region_name="us-east-1") | 
					
						
							|  |  |  |     resp = client.create_volume(AvailabilityZone="us-east-1a", Encrypted=False, Size=10) | 
					
						
							|  |  |  |     resp["Encrypted"].should.be.false | 
					
						
							|  |  |  |     resp.should_not.have.key("KmsKeyId") | 
					
						
							|  |  |  |     resp = client.describe_volumes(VolumeIds=[resp["VolumeId"]]) | 
					
						
							|  |  |  |     resp["Volumes"][0]["Encrypted"].should.be.false | 
					
						
							|  |  |  |     resp["Volumes"][0].should_not.have.key("KmsKeyId") | 
					
						
							|  |  |  |     resource = boto3.resource("ec2", region_name="us-east-1") | 
					
						
							|  |  |  |     volume = resource.create_volume( | 
					
						
							|  |  |  |         AvailabilityZone="us-east-1a", Encrypted=False, Size=10 | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     volume.encrypted.should.be.false | 
					
						
							|  |  |  |     volume.kms_key_id.should.be.none |