2023-11-30 15:55:51 +00:00
|
|
|
from unittest import SkipTest
|
|
|
|
|
2021-09-15 21:07:04 +00:00
|
|
|
import boto3
|
|
|
|
import pytest
|
|
|
|
from botocore.exceptions import ClientError
|
2023-11-30 15:55:51 +00:00
|
|
|
|
2021-10-05 17:11:07 +00:00
|
|
|
from moto import mock_ec2, settings
|
2022-08-13 09:49:43 +00:00
|
|
|
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
|
2021-09-15 21:07:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_describe_carrier_gateways_none():
|
2021-10-05 17:11:07 +00:00
|
|
|
if settings.TEST_SERVER_MODE:
|
|
|
|
raise SkipTest("ServerMode is not guaranteed to be empty")
|
2021-09-15 21:07:04 +00:00
|
|
|
ec2 = boto3.client("ec2", region_name="us-east-1")
|
2023-07-17 09:31:05 +00:00
|
|
|
assert ec2.describe_carrier_gateways()["CarrierGateways"] == []
|
2021-09-15 21:07:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_describe_carrier_gateways_multiple():
|
|
|
|
client = boto3.client("ec2", region_name="us-east-1")
|
|
|
|
ec2 = boto3.resource("ec2", region_name="us-east-1")
|
|
|
|
vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
|
|
|
|
cg1 = client.create_carrier_gateway(VpcId=vpc.id)["CarrierGateway"]
|
|
|
|
cg2 = client.create_carrier_gateway(VpcId=vpc.id)["CarrierGateway"]
|
|
|
|
|
2021-10-05 17:11:07 +00:00
|
|
|
gateways = client.describe_carrier_gateways()["CarrierGateways"]
|
|
|
|
gateway_ids = [g["CarrierGatewayId"] for g in gateways]
|
|
|
|
|
2023-07-17 09:31:05 +00:00
|
|
|
assert cg1["CarrierGatewayId"] in gateway_ids
|
|
|
|
assert cg2["CarrierGatewayId"] in gateway_ids
|
2021-09-15 21:07:04 +00:00
|
|
|
|
|
|
|
find_one = client.describe_carrier_gateways(
|
|
|
|
CarrierGatewayIds=[cg1["CarrierGatewayId"]]
|
|
|
|
)["CarrierGateways"]
|
2023-07-17 09:31:05 +00:00
|
|
|
assert len(find_one) == 1
|
|
|
|
assert find_one[0]["CarrierGatewayId"] == cg1["CarrierGatewayId"]
|
2021-09-15 21:07:04 +00:00
|
|
|
|
|
|
|
find_one = client.describe_carrier_gateways(
|
|
|
|
CarrierGatewayIds=[cg2["CarrierGatewayId"], "non-existant"]
|
|
|
|
)["CarrierGateways"]
|
2023-07-17 09:31:05 +00:00
|
|
|
assert len(find_one) == 1
|
|
|
|
assert find_one[0]["CarrierGatewayId"] == cg2["CarrierGatewayId"]
|
2021-09-15 21:07:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_create_carrier_gateways_without_tags():
|
|
|
|
client = boto3.client("ec2", region_name="us-east-1")
|
|
|
|
ec2 = boto3.resource("ec2", region_name="us-east-1")
|
|
|
|
vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
|
|
|
|
cg = client.create_carrier_gateway(VpcId=vpc.id)["CarrierGateway"]
|
|
|
|
|
2023-07-17 09:31:05 +00:00
|
|
|
assert cg["CarrierGatewayId"].startswith("cagw-")
|
|
|
|
assert cg["VpcId"] == vpc.id
|
|
|
|
assert cg["State"] == "available"
|
|
|
|
assert cg["OwnerId"] == ACCOUNT_ID
|
|
|
|
assert cg["Tags"] == []
|
2021-09-15 21:07:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_create_carrier_gateways_with_tags():
|
|
|
|
client = boto3.client("ec2", region_name="us-east-1")
|
|
|
|
ec2 = boto3.resource("ec2", region_name="us-east-1")
|
|
|
|
vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
|
|
|
|
cg = client.create_carrier_gateway(
|
|
|
|
VpcId=vpc.id,
|
|
|
|
TagSpecifications=[
|
|
|
|
{"ResourceType": "CarrierGateway", "Tags": [{"Key": "tk", "Value": "tv"}]}
|
|
|
|
],
|
|
|
|
)["CarrierGateway"]
|
|
|
|
|
2023-07-17 09:31:05 +00:00
|
|
|
assert cg["CarrierGatewayId"].startswith("cagw-")
|
|
|
|
assert cg["VpcId"] == vpc.id
|
|
|
|
assert cg["State"] == "available"
|
|
|
|
assert cg["OwnerId"] == ACCOUNT_ID
|
|
|
|
assert cg["Tags"] == [{"Key": "tk", "Value": "tv"}]
|
2021-09-15 21:07:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_create_carrier_gateways_invalid_vpc():
|
|
|
|
ec2 = boto3.client("ec2", region_name="us-east-1")
|
|
|
|
with pytest.raises(ClientError) as exc:
|
|
|
|
ec2.create_carrier_gateway(VpcId="vpc-asdf")
|
|
|
|
err = exc.value.response["Error"]
|
2023-07-17 09:31:05 +00:00
|
|
|
assert err["Code"] == "InvalidVpcID.NotFound"
|
|
|
|
assert err["Message"] == "VpcID vpc-asdf does not exist."
|
2021-09-15 21:07:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_delete_carrier_gateways():
|
|
|
|
client = boto3.client("ec2", region_name="us-east-1")
|
|
|
|
ec2 = boto3.resource("ec2", region_name="us-east-1")
|
|
|
|
vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
|
|
|
|
cg = client.create_carrier_gateway(VpcId=vpc.id)["CarrierGateway"]
|
|
|
|
client.delete_carrier_gateway(CarrierGatewayId=cg["CarrierGatewayId"])
|
|
|
|
|
2021-10-05 17:11:07 +00:00
|
|
|
gateways = client.describe_carrier_gateways()["CarrierGateways"]
|
|
|
|
gateway_ids = [g["CarrierGatewayId"] for g in gateways]
|
|
|
|
|
2023-07-17 09:31:05 +00:00
|
|
|
assert cg["CarrierGatewayId"] not in gateway_ids
|