From f3debf8f6ff57c023231dedd2ee97e7e34774b85 Mon Sep 17 00:00:00 2001 From: grahamlyons Date: Mon, 29 Jan 2018 13:53:44 +0000 Subject: [PATCH] Test and fix bug for snapshot searching The logic which contructed a list of values for parameters with multiple values was flawed in that e.g. `Subnet.1` and `Subnet.10` would be have their values counted against `Subnet.1` because they share a prefix. This now checks for a starting `.` before counting that name as having the requested prefix. --- moto/core/responses.py | 4 ++++ tests/test_ec2/test_elastic_block_store.py | 25 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/moto/core/responses.py b/moto/core/responses.py index d254d1f85..278a24dc4 100644 --- a/moto/core/responses.py +++ b/moto/core/responses.py @@ -345,6 +345,10 @@ class BaseResponse(_TemplateEnvironmentMixin): if is_tracked(name) or not name.startswith(param_prefix): continue + if len(name) > len(param_prefix) and \ + not name[len(param_prefix):].startswith('.'): + continue + match = self.param_list_regex.search(name[len(param_prefix):]) if len(name) > len(param_prefix) else None if match: prefix = param_prefix + match.group(1) diff --git a/tests/test_ec2/test_elastic_block_store.py b/tests/test_ec2/test_elastic_block_store.py index 9c07f38d6..fc0677cfe 100644 --- a/tests/test_ec2/test_elastic_block_store.py +++ b/tests/test_ec2/test_elastic_block_store.py @@ -5,10 +5,11 @@ from nose.tools import assert_raises from moto.ec2 import ec2_backends import boto +import boto3 from boto.exception import EC2ResponseError import sure # noqa -from moto import mock_ec2_deprecated +from moto import mock_ec2_deprecated, mock_ec2 @mock_ec2_deprecated @@ -579,3 +580,25 @@ def test_volume_tag_escaping(): snaps = [snap for snap in conn.get_all_snapshots() if snap.id == snapshot.id] dict(snaps[0].tags).should.equal({'key': ''}) + + +@mock_ec2 +def test_search_for_many_snapshots(): + ec2_client = boto3.client('ec2', region_name='eu-west-1') + + volume_response = ec2_client.create_volume( + AvailabilityZone='eu-west-1a', Size=10 + ) + + snapshot_ids = [] + for i in range(1, 20): + create_snapshot_response = ec2_client.create_snapshot( + VolumeId=volume_response['VolumeId'] + ) + snapshot_ids.append(create_snapshot_response['SnapshotId']) + + snapshots_response = ec2_client.describe_snapshots( + SnapshotIds=snapshot_ids + ) + + assert len(snapshots_response['Snapshots']) == len(snapshot_ids)