moto/tests/test_ec2/test_customer_gateways.py

143 lines
4.9 KiB
Python
Raw Normal View History

2013-02-22 04:13:01 +00:00
import boto
2021-10-18 19:44:29 +00:00
import sure # noqa # pylint: disable=unused-import
2021-09-25 11:13:07 +00:00
import boto3
import pytest
from boto.exception import EC2ResponseError
2021-09-25 11:13:07 +00:00
from botocore.exceptions import ClientError
2013-02-22 04:13:01 +00:00
2021-09-25 11:13:07 +00:00
from moto import mock_ec2_deprecated, mock_ec2
2013-02-22 04:13:01 +00:00
2021-09-25 11:13:07 +00:00
# Has boto3 equivalent
2017-02-16 03:35:45 +00:00
@mock_ec2_deprecated
def test_create_customer_gateways():
2019-10-31 15:44:26 +00:00
conn = boto.connect_vpc("the_key", "the_secret")
2019-10-31 15:44:26 +00:00
customer_gateway = conn.create_customer_gateway("ipsec.1", "205.251.242.54", 65534)
customer_gateway.should_not.be.none
2019-10-31 15:44:26 +00:00
customer_gateway.id.should.match(r"cgw-\w+")
customer_gateway.type.should.equal("ipsec.1")
customer_gateway.bgp_asn.should.equal(65534)
2019-10-31 15:44:26 +00:00
customer_gateway.ip_address.should.equal("205.251.242.54")
2017-02-24 02:37:43 +00:00
2021-09-25 11:13:07 +00:00
@mock_ec2
def test_create_customer_gateways_boto3():
ec2 = boto3.client("ec2", region_name="us-east-1")
customer_gateway = create_customer_gateway(ec2)
customer_gateway.should.have.key("CustomerGatewayId").match(r"cgw-\w+")
customer_gateway.should.have.key("Type").equal("ipsec.1")
customer_gateway.should.have.key("BgpAsn").equal("65534")
customer_gateway.should.have.key("IpAddress").equal("205.251.242.54")
# Has boto3 equivalent
2017-02-16 03:35:45 +00:00
@mock_ec2_deprecated
def test_describe_customer_gateways():
2019-10-31 15:44:26 +00:00
conn = boto.connect_vpc("the_key", "the_secret")
customer_gateway = conn.create_customer_gateway("ipsec.1", "205.251.242.54", 65534)
cgws = conn.get_all_customer_gateways()
cgws.should.have.length_of(1)
cgws[0].id.should.match(customer_gateway.id)
2017-02-24 02:37:43 +00:00
@mock_ec2
def test_describe_customer_gateways_dryrun():
client = boto3.client("ec2", region_name="us-east-1")
with pytest.raises(ClientError) as ex:
client.describe_customer_gateways(DryRun=True)
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(412)
ex.value.response["Error"]["Code"].should.equal("DryRunOperation")
ex.value.response["Error"]["Message"].should.equal(
"An error occurred (DryRunOperation) when calling the DescribeCustomerGateways operation: Request would have succeeded, but DryRun flag is set"
)
2021-09-25 11:13:07 +00:00
@mock_ec2
def test_describe_customer_gateways_boto3():
ec2 = boto3.client("ec2", region_name="us-east-1")
customer_gateway = create_customer_gateway(ec2)
2021-10-05 17:11:07 +00:00
cg_id = customer_gateway["CustomerGatewayId"]
2021-09-25 11:13:07 +00:00
cgws = ec2.describe_customer_gateways()["CustomerGateways"]
2021-10-05 17:11:07 +00:00
cg_ids = [cg["CustomerGatewayId"] for cg in cgws]
cg_ids.should.contain(cg_id)
cgw = ec2.describe_customer_gateways(CustomerGatewayIds=[cg_id])[
"CustomerGateways"
][0]
cgw.should.have.key("BgpAsn")
cgw.should.have.key("CustomerGatewayId").equal(cg_id)
cgw.should.have.key("IpAddress")
cgw.should.have.key("State").equal("available")
cgw.should.have.key("Type").equal("ipsec.1")
all_cgws = ec2.describe_customer_gateways()["CustomerGateways"]
assert (
len(all_cgws) >= 1
), "Should have at least the one CustomerGateway we just created"
2021-09-25 11:13:07 +00:00
# Has boto3 equivalent
2017-02-16 03:35:45 +00:00
@mock_ec2_deprecated
def test_delete_customer_gateways():
2019-10-31 15:44:26 +00:00
conn = boto.connect_vpc("the_key", "the_secret")
2019-10-31 15:44:26 +00:00
customer_gateway = conn.create_customer_gateway("ipsec.1", "205.251.242.54", 65534)
customer_gateway.should_not.be.none
cgws = conn.get_all_customer_gateways()
cgws[0].id.should.match(customer_gateway.id)
2021-10-18 19:44:29 +00:00
conn.delete_customer_gateway(customer_gateway.id)
cgws = conn.get_all_customer_gateways()
Merge LocalStack changes into upstream moto (#4082) * fix OPTIONS requests on non-existing API GW integrations * add cloudformation models for API Gateway deployments * bump version * add backdoor to return CloudWatch metrics * Updating implementation coverage * Updating implementation coverage * add cloudformation models for API Gateway deployments * Updating implementation coverage * Updating implementation coverage * Implemented get-caller-identity returning real data depending on the access key used. * bump version * minor fixes * fix Number data_type for SQS message attribute * fix handling of encoding errors * bump version * make CF stack queryable before starting to initialize its resources * bump version * fix integration_method for API GW method integrations * fix undefined status in CF FakeStack * Fix apigateway issues with terraform v0.12.21 * resource_methods -> add handle for "DELETE" method * integrations -> fix issue that "httpMethod" wasn't included in body request (this value was set as the value from refer method resource) * bump version * Fix setting http method for API gateway integrations (#6) * bump version * remove duplicate methods * add storage class to S3 Key when completing multipart upload (#7) * fix SQS performance issues; bump version * add pagination to SecretsManager list-secrets (#9) * fix default parameter groups in RDS * fix adding S3 metadata headers with names containing dots (#13) * Updating implementation coverage * Updating implementation coverage * add cloudformation models for API Gateway deployments * Updating implementation coverage * Updating implementation coverage * Implemented get-caller-identity returning real data depending on the access key used. * make CF stack queryable before starting to initialize its resources * bump version * remove duplicate methods * fix adding S3 metadata headers with names containing dots (#13) * Update amis.json to support EKS AMI mocks (#15) * fix PascalCase for boolean value in ListMultipartUploads response (#17); fix _get_multi_param to parse nested list/dict query params * determine non-zero container exit code in Batch API * support filtering by dimensions in CW get_metric_statistics * fix storing attributes for ELBv2 Route entities; API GW refactorings for TF tests * add missing fields for API GW resources * fix error messages for Route53 (TF-compat) * various fixes for IAM resources (tf-compat) * minor fixes for API GW models (tf-compat) * minor fixes for API GW responses (tf-compat) * add s3 exception for bucket notification filter rule validation * change the way RESTErrors generate the response body and content-type header * fix lint errors and disable "black" syntax enforcement * remove return type hint in RESTError.get_body * add RESTError XML template for IAM exceptions * add support for API GW minimumCompressionSize * fix casing getting PrivateDnsEnabled API GW attribute * minor fixes for error responses * fix escaping special chars for IAM role descriptions (tf-compat) * minor fixes and tagging support for API GW and ELB v2 (tf-compat) * Merge branch 'master' into localstack * add "AlarmRule" attribute to enable support for composite CloudWatch metrics * fix recursive parsing of complex/nested query params * bump version * add API to delete S3 website configurations (#18) * use dict copy to allow parallelism and avoid concurrent modification exceptions in S3 * fix precondition check for etags in S3 (#19) * minor fix for user filtering in Cognito * fix API Gateway error response; avoid returning empty response templates (tf-compat) * support tags and tracingEnabled attribute for API GW stages * fix boolean value in S3 encryption response (#20) * fix connection arn structure * fix api destination arn structure * black format * release 2.0.3.37 * fix s3 exception tests see botocore/parsers.py:1002 where RequestId is removed from parsed * remove python 2 from build action * add test failure annotations in build action * fix events test arn comparisons * fix s3 encryption response test * return default value "0" if EC2 availableIpAddressCount is empty * fix extracting SecurityGroupIds for EC2 VPC endpoints * support deleting/updating API Gateway DomainNames * fix(events): Return empty string instead of null when no pattern is specified in EventPattern (tf-compat) (#22) * fix logic and revert CF changes to get tests running again (#21) * add support for EC2 customer gateway API (#25) * add support for EC2 Transit Gateway APIs (#24) * feat(logs): add `kmsKeyId` into `LogGroup` entity (#23) * minor change in ELBv2 logic to fix tests * feat(events): add APIs to describe and delete CloudWatch Events connections (#26) * add support for EC2 transit gateway route tables (#27) * pass transit gateway route table ID in Describe API, minor refactoring (#29) * add support for EC2 Transit Gateway Routes (#28) * fix region on ACM certificate import (#31) * add support for EC2 transit gateway attachments (#30) * add support for EC2 Transit Gateway VPN attachments (#32) * fix account ID for logs API * add support for DeleteOrganization API * feat(events): store raw filter representation for CloudWatch events patterns (tf-compat) (#36) * feat(events): add support to describe/update/delete CloudWatch API destinations (#35) * add Cognito UpdateIdentityPool, CW Logs PutResourcePolicy * feat(events): add support for tags in EventBus API (#38) * fix parameter validation for Batch compute environments (tf-compat) * revert merge conflicts in IMPLEMENTATION_COVERAGE.md * format code using black * restore original README; re-enable and fix CloudFormation tests * restore tests and old logic for CF stack parameters from SSM * parameterize RequestId/RequestID in response messages and revert related test changes * undo LocalStack-specific adaptations * minor fix * Update CodeCov config to reflect removal of Py2 * undo change related to CW metric filtering; add additional test for CW metric statistics with dimensions * Terraform - Extend whitelist of running tests Co-authored-by: acsbendi <acsbendi28@gmail.com> Co-authored-by: Phan Duong <duongpv@outlook.com> Co-authored-by: Thomas Rausch <thomas@thrau.at> Co-authored-by: Macwan Nevil <macnev2013@gmail.com> Co-authored-by: Dominik Schubert <dominik.schubert91@gmail.com> Co-authored-by: Gonzalo Saad <saad.gonzalo.ale@gmail.com> Co-authored-by: Mohit Alonja <monty16597@users.noreply.github.com> Co-authored-by: Miguel Gagliardo <migag9@gmail.com> Co-authored-by: Bert Blommers <info@bertblommers.nl>
2021-07-26 14:21:17 +00:00
cgws[0].state.should.equal("deleted")
cgws.should.have.length_of(1)
2017-02-24 02:37:43 +00:00
2021-09-25 11:13:07 +00:00
@mock_ec2
def test_delete_customer_gateways_boto3():
ec2 = boto3.client("ec2", region_name="us-east-1")
customer_gateway = create_customer_gateway(ec2)
2021-10-05 17:11:07 +00:00
cg_id = customer_gateway["CustomerGatewayId"]
2021-09-25 11:13:07 +00:00
2021-10-05 17:11:07 +00:00
cgws = ec2.describe_customer_gateways(CustomerGatewayIds=[cg_id])[
"CustomerGateways"
]
2021-09-25 11:13:07 +00:00
cgws.should.have.length_of(1)
2021-10-05 17:11:07 +00:00
cgws[0].should.have.key("State").equal("available")
2021-09-25 11:13:07 +00:00
ec2.delete_customer_gateway(CustomerGatewayId=customer_gateway["CustomerGatewayId"])
2021-10-05 17:11:07 +00:00
cgws = ec2.describe_customer_gateways(CustomerGatewayIds=[cg_id])[
"CustomerGateways"
]
2021-09-25 11:13:07 +00:00
cgws.should.have.length_of(1)
cgws[0].should.have.key("State").equal("deleted")
# Has boto3 equivalent
2017-02-16 03:35:45 +00:00
@mock_ec2_deprecated
def test_delete_customer_gateways_bad_id():
2019-10-31 15:44:26 +00:00
conn = boto.connect_vpc("the_key", "the_secret")
2021-10-18 19:44:29 +00:00
with pytest.raises(EC2ResponseError):
2019-10-31 15:44:26 +00:00
conn.delete_customer_gateway("cgw-0123abcd")
2021-09-25 11:13:07 +00:00
@mock_ec2
def test_delete_customer_gateways_bad_id_boto3():
ec2 = boto3.client("ec2", region_name="us-east-1")
with pytest.raises(ClientError) as ex:
ec2.delete_customer_gateway(CustomerGatewayId="cgw-0123abcd")
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
ex.value.response["ResponseMetadata"].should.have.key("RequestId")
ex.value.response["Error"]["Code"].should.equal("InvalidCustomerGatewayID.NotFound")
def create_customer_gateway(ec2):
return ec2.create_customer_gateway(
Type="ipsec.1", PublicIp="205.251.242.54", BgpAsn=65534
)["CustomerGateway"]