From 7b236c4dedac874486ed9947c94847375404212a Mon Sep 17 00:00:00 2001 From: Tay Frost Date: Thu, 7 Mar 2019 17:07:15 -0500 Subject: [PATCH] bugfix ebs volume tag behaviour This commit modifies the response format of the ec2 calls `create_volume` and `describe_volumes`. Previously, these calls would always include a `Tags` key in the response, even when a volume has no tags. Now, the `Tags` key will not be included in the response if the volume has no tags. When an EBS volume has no tags, calls to the aws ec2 endpoints `create_volume` and `describe_volumes` do not include the `Tags` key in the `response.Volumes[]` object. However, moto does include the `Tags` key in this case. This discrepancy in behaviour can result in code passing a moto test but failing in production. Sample snippets that trigger this condition: ``` def create_volume_and_then_get_tags_from_response(): client = boto3.client('ec2', region_name='us-east-1') volume_response = client.create_volume( Size=10, AvailabilityZone='us-east-1a' ) keys = volume_response['Keys'] ``` ``` def create_volume_and_then_get_tags_from_describe_volumes(): client = boto3.client('ec2', region_name='us-east-1') volume_response = client.create_volume( Size=10, AvailabilityZone='us-east-1a' ) volume_describe_response = client.describe_volumes() keys = volume_describe_response['Volumes'][0]['Keys'] ``` Both sample snippets will succeed in a moto test, but fail with a `KeyError` when using the aws api. --- moto/ec2/responses/elastic_block_store.py | 44 ++++++++++++----------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/moto/ec2/responses/elastic_block_store.py b/moto/ec2/responses/elastic_block_store.py index aa0d7f73b..acd37b283 100644 --- a/moto/ec2/responses/elastic_block_store.py +++ b/moto/ec2/responses/elastic_block_store.py @@ -150,16 +150,18 @@ CREATE_VOLUME_RESPONSE = """