Merge pull request #3508 from bblommers/feature/ec2-describe-instance-type-offerings

EC2: describe instance type offerings
This commit is contained in:
Steve Pulec 2020-12-03 18:06:06 -06:00 committed by GitHub
commit 51928f2410
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
67 changed files with 346 additions and 9 deletions

View file

@ -0,0 +1,85 @@
from __future__ import unicode_literals
import boto3
import sure # noqa
from moto import mock_ec2
@mock_ec2
def test_describe_instance_type_offerings():
client = boto3.client("ec2", "us-east-1")
offerings = client.describe_instance_type_offerings()
offerings.should.have.key("InstanceTypeOfferings")
offerings["InstanceTypeOfferings"].should_not.be.empty
offerings["InstanceTypeOfferings"][0].should.have.key("InstanceType")
offerings["InstanceTypeOfferings"][0].should.have.key("Location")
offerings["InstanceTypeOfferings"][0].should.have.key("LocationType")
@mock_ec2
def test_describe_instance_type_offering_filter_by_type():
client = boto3.client("ec2", "us-east-1")
# Verify offerings of a specific instance type
offerings = client.describe_instance_type_offerings(
Filters=[{"Name": "instance-type", "Values": ["t2.nano"]}]
)
offerings.should.have.key("InstanceTypeOfferings")
offerings["InstanceTypeOfferings"].should_not.be.empty
offerings = offerings["InstanceTypeOfferings"]
offerings.should.have.length_of(1)
offerings[0]["InstanceType"].should.equal("t2.nano")
offerings[0]["Location"].should.equal("us-east-1")
# Verify offerings of that instance type per availibility zone
offerings = client.describe_instance_type_offerings(
LocationType="availability-zone",
Filters=[{"Name": "instance-type", "Values": ["t2.nano"]}],
)
offerings.should.have.key("InstanceTypeOfferings")
offerings = offerings["InstanceTypeOfferings"]
offerings.should.have.length_of(6)
for offering in offerings:
offering["InstanceType"].should.equal("t2.nano")
offering["LocationType"].should.equal("availability-zone")
offering["Location"].should.match("us-east-1[a-f]")
@mock_ec2
def test_describe_instance_type_offering_filter_by_zone():
client = boto3.client("ec2", "us-east-1")
offerings = client.describe_instance_type_offerings(
LocationType="availability-zone",
Filters=[{"Name": "location", "Values": ["us-east-1c"]}],
)
offerings.should.have.key("InstanceTypeOfferings")
offerings = offerings["InstanceTypeOfferings"]
offerings.should_not.be.empty
offerings.should.have.length_of(353)
assert all([o["LocationType"] == "availability-zone" for o in offerings])
assert all([o["Location"] == "us-east-1c" for o in offerings])
assert any([o["InstanceType"] == "a1.2xlarge" for o in offerings])
@mock_ec2
def test_describe_instance_type_offering_filter_by_zone_id():
client = boto3.client("ec2", "ca-central-1")
offerings = client.describe_instance_type_offerings(
LocationType="availability-zone-id",
Filters=[
{"Name": "location", "Values": ["cac1-az1"]},
{"Name": "instance-type", "Values": ["c5.9xlarge"]},
],
)
offerings.should.have.key("InstanceTypeOfferings")
offerings = offerings["InstanceTypeOfferings"]
offerings.should_not.be.empty
offerings.should.have.length_of(1)
offerings[0]["LocationType"].should.equal("availability-zone-id")
offerings[0]["InstanceType"].should.equal("c5.9xlarge")
offerings[0]["Location"].should.equal("cac1-az1")

View file

@ -2,6 +2,9 @@ from __future__ import unicode_literals
import boto3
import sure # noqa
import pytest
from botocore.exceptions import ClientError
from moto import mock_ec2
@ -16,3 +19,34 @@ def test_describe_instance_types():
instance_types["InstanceTypes"][0].should.have.key("InstanceType")
instance_types["InstanceTypes"][0].should.have.key("MemoryInfo")
instance_types["InstanceTypes"][0]["MemoryInfo"].should.have.key("SizeInMiB")
@mock_ec2
def test_describe_instance_types_filter_by_type():
client = boto3.client("ec2", "us-east-1")
instance_types = client.describe_instance_types(
InstanceTypes=["t1.micro", "t2.nano"]
)
instance_types.should.have.key("InstanceTypes")
instance_types["InstanceTypes"].should_not.be.empty
instance_types["InstanceTypes"].should.have.length_of(2)
instance_types["InstanceTypes"][0]["InstanceType"].should.be.within(
["t1.micro", "t2.nano"]
)
instance_types["InstanceTypes"][1]["InstanceType"].should.be.within(
["t1.micro", "t2.nano"]
)
@mock_ec2
def test_describe_instance_types_unknown_type():
client = boto3.client("ec2", "us-east-1")
with pytest.raises(ClientError) as err:
client.describe_instance_types(InstanceTypes=["t1.non_existent"])
err.response["Error"]["Code"].should.equal("ValidationException")
err.response["Error"]["Message"].split(":")[0].should.look_like(
"The instance type '{'t1.non_existent'}' does not exist"
)
err.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)