From abf3db8d8a4287946a7dc0c5dfaa233139e3e71f Mon Sep 17 00:00:00 2001 From: Don Kuntz Date: Mon, 22 Jul 2019 21:57:15 -0500 Subject: [PATCH] Add a test to ensure that ec2.copy_image sets the proper owner id This test is useful because before the last commit using copy_image would not set the owner_id to the same one used when calling describe_images. For example, this code conn = boto3.client("ec2") copy_resp = conn.copy_image( SourceImageId="ami-whatever", ... ) describe_resp = conn.describe_images( Owners=["self"] ) Would result in describe_resp being empty, when it should contain the image from the copy_resp before it. By ensuring the owner ids are the same (see ce4059f6) the code example now works as expected. --- tests/test_ec2/test_amis.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/test_ec2/test_amis.py b/tests/test_ec2/test_amis.py index fd7234511..feff4a16c 100644 --- a/tests/test_ec2/test_amis.py +++ b/tests/test_ec2/test_amis.py @@ -10,7 +10,7 @@ from nose.tools import assert_raises import sure # noqa from moto import mock_ec2_deprecated, mock_ec2 -from moto.ec2.models import AMIS +from moto.ec2.models import AMIS, OWNER_ID from tests.helpers import requires_boto_gte @@ -152,6 +152,29 @@ def test_ami_copy(): cm.exception.request_id.should_not.be.none +@mock_ec2 +def test_copy_image_changes_owner_id(): + conn = boto3.client('ec2', region_name='us-east-1') + + # this source AMI ID is from moto/ec2/resources/amis.json + source_ami_id = "ami-03cf127a" + + # confirm the source ami owner id is different from the default owner id. + # if they're ever the same it means this test is invalid. + check_resp = conn.describe_images(ImageIds=[source_ami_id]) + check_resp["Images"][0]["OwnerId"].should_not.equal(OWNER_ID) + + copy_resp = conn.copy_image( + SourceImageId=source_ami_id, + Name="new-image", + Description="a copy of an image", + SourceRegion="us-east-1") + + describe_resp = conn.describe_images(Owners=["self"]) + describe_resp["Images"][0]["OwnerId"].should.equal(OWNER_ID) + describe_resp["Images"][0]["ImageId"].should.equal(copy_resp["ImageId"]) + + @mock_ec2_deprecated def test_ami_tagging(): conn = boto.connect_vpc('the_key', 'the_secret')