EC2: validate pagination parameters (#6293)
This commit is contained in:
parent
87d84c12e0
commit
52846c9555
@ -14,6 +14,12 @@ from ._base_response import EC2BaseResponse
|
||||
class InstanceResponse(EC2BaseResponse):
|
||||
def describe_instances(self) -> str:
|
||||
self.error_on_dryrun()
|
||||
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2/client/describe_instances.html
|
||||
# You cannot specify this(MaxResults) parameter and the instance IDs parameter in the same request.
|
||||
if "InstanceId.1" in self.data and "MaxResults" in self.data:
|
||||
raise InvalidParameterCombination(
|
||||
"The parameter instancesSet cannot be used with the parameter maxResults"
|
||||
)
|
||||
filter_dict = self._filters_from_querystring()
|
||||
instance_ids = self._get_multi_param("InstanceId")
|
||||
token = self._get_param("NextToken")
|
||||
|
@ -318,23 +318,46 @@ def test_get_paginated_instances():
|
||||
instances.extend(
|
||||
conn.create_instances(ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1)
|
||||
)
|
||||
instance_ids = [i.id for i in instances]
|
||||
|
||||
resp1 = client.describe_instances(InstanceIds=instance_ids, MaxResults=5)
|
||||
resp1 = client.describe_instances(MaxResults=5)
|
||||
res1 = resp1["Reservations"]
|
||||
res1.should.have.length_of(5)
|
||||
next_token = resp1["NextToken"]
|
||||
|
||||
next_token.should_not.equal(None)
|
||||
|
||||
resp2 = client.describe_instances(InstanceIds=instance_ids, NextToken=next_token)
|
||||
resp2["Reservations"].should.have.length_of(7) # 12 total - 5 from the first call
|
||||
assert "NextToken" not in resp2 # This is it - no more pages
|
||||
resp2 = client.describe_instances(NextToken=next_token)
|
||||
|
||||
# at least 12 total - 5 from the first call but there may be more from servermode tests
|
||||
assert len(resp2["Reservations"]) >= 7
|
||||
|
||||
for i in instances:
|
||||
i.terminate()
|
||||
|
||||
|
||||
@mock_ec2
|
||||
def test_describe_instances_pagination_error():
|
||||
client = boto3.client("ec2", region_name="us-east-1")
|
||||
|
||||
# Call describe_instances with a bad id should raise an error
|
||||
with pytest.raises(ClientError) as ex:
|
||||
paginator = client.get_paginator("describe_instances").paginate(
|
||||
InstanceIds=["i-12345678"],
|
||||
PaginationConfig={
|
||||
"MaxItems": 9999,
|
||||
"PageSize": 100,
|
||||
},
|
||||
)
|
||||
for page in paginator:
|
||||
dir(page)
|
||||
|
||||
assert ex.value.response["Error"]["Code"] == "InvalidParameterCombination"
|
||||
assert (
|
||||
ex.value.response["Error"]["Message"]
|
||||
== "The parameter instancesSet cannot be used with the parameter maxResults"
|
||||
)
|
||||
|
||||
|
||||
@mock_ec2
|
||||
def test_create_with_tags():
|
||||
ec2 = boto3.client("ec2", region_name="us-west-2")
|
||||
|
Loading…
Reference in New Issue
Block a user