2020-10-06 05:54:49 +00:00
|
|
|
import pytest
|
2014-08-25 22:09:38 +00:00
|
|
|
|
2013-02-22 04:13:01 +00:00
|
|
|
import boto
|
2020-11-02 13:30:02 +00:00
|
|
|
import boto3
|
|
|
|
|
2017-02-24 00:43:48 +00:00
|
|
|
from boto.exception import EC2ResponseError
|
2021-09-25 11:13:07 +00:00
|
|
|
from botocore.exceptions import ClientError
|
2014-06-05 04:12:22 +00:00
|
|
|
|
2021-10-18 19:44:29 +00:00
|
|
|
import sure # noqa # pylint: disable=unused-import
|
2013-02-22 04:13:01 +00:00
|
|
|
|
2020-11-02 13:30:02 +00:00
|
|
|
from moto import mock_ec2_deprecated, mock_ec2
|
2021-10-05 17:11:07 +00:00
|
|
|
from uuid import uuid4
|
2013-02-22 04:13:01 +00:00
|
|
|
|
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
VPC_CIDR = "10.0.0.0/16"
|
|
|
|
BAD_VPC = "vpc-deadbeef"
|
|
|
|
BAD_IGW = "igw-deadbeef"
|
|
|
|
|
2014-06-05 04:12:22 +00:00
|
|
|
|
2021-09-25 11:13:07 +00:00
|
|
|
# Has boto3 equivalent
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-06-05 04:12:22 +00:00
|
|
|
def test_igw_create():
|
2021-07-26 14:21:17 +00:00
|
|
|
"""internet gateway create"""
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto.connect_vpc("the_key", "the_secret")
|
2014-06-05 04:12:22 +00:00
|
|
|
|
|
|
|
conn.get_all_internet_gateways().should.have.length_of(0)
|
2016-10-15 13:08:44 +00:00
|
|
|
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(EC2ResponseError) as ex:
|
2016-10-15 13:08:44 +00:00
|
|
|
igw = conn.create_internet_gateway(dry_run=True)
|
2020-10-06 06:04:09 +00:00
|
|
|
ex.value.error_code.should.equal("DryRunOperation")
|
2021-09-07 16:10:01 +00:00
|
|
|
ex.value.status.should.equal(412)
|
2020-10-06 06:04:09 +00:00
|
|
|
ex.value.message.should.equal(
|
2019-10-31 15:44:26 +00:00
|
|
|
"An error occurred (DryRunOperation) when calling the CreateInternetGateway operation: Request would have succeeded, but DryRun flag is set"
|
|
|
|
)
|
2016-10-15 13:08:44 +00:00
|
|
|
|
2014-06-05 04:12:22 +00:00
|
|
|
igw = conn.create_internet_gateway()
|
|
|
|
conn.get_all_internet_gateways().should.have.length_of(1)
|
2019-10-31 15:44:26 +00:00
|
|
|
igw.id.should.match(r"igw-[0-9a-f]+")
|
2014-06-05 04:12:22 +00:00
|
|
|
|
|
|
|
igw = conn.get_all_internet_gateways()[0]
|
|
|
|
igw.attachments.should.have.length_of(0)
|
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
|
2021-09-25 11:13:07 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_igw_create_boto3():
|
|
|
|
""" internet gateway create """
|
|
|
|
ec2 = boto3.resource("ec2", "us-west-1")
|
|
|
|
client = boto3.client("ec2", "us-west-1")
|
|
|
|
|
|
|
|
with pytest.raises(ClientError) as ex:
|
|
|
|
client.create_internet_gateway(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 CreateInternetGateway operation: Request would have succeeded, but DryRun flag is set"
|
|
|
|
)
|
|
|
|
|
|
|
|
igw = ec2.create_internet_gateway()
|
|
|
|
igw.id.should.match(r"igw-[0-9a-f]+")
|
|
|
|
|
2021-10-05 17:11:07 +00:00
|
|
|
igw = client.describe_internet_gateways(InternetGatewayIds=[igw.id])[
|
|
|
|
"InternetGateways"
|
|
|
|
][0]
|
2021-09-25 11:13:07 +00:00
|
|
|
igw["Attachments"].should.have.length_of(0)
|
|
|
|
|
|
|
|
|
|
|
|
# Has boto3 equivalent
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-06-05 04:12:22 +00:00
|
|
|
def test_igw_attach():
|
2021-07-26 14:21:17 +00:00
|
|
|
"""internet gateway attach"""
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto.connect_vpc("the_key", "the_secret")
|
2014-06-05 04:12:22 +00:00
|
|
|
igw = conn.create_internet_gateway()
|
|
|
|
vpc = conn.create_vpc(VPC_CIDR)
|
2016-10-15 13:08:44 +00:00
|
|
|
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(EC2ResponseError) as ex:
|
2016-10-15 13:08:44 +00:00
|
|
|
conn.attach_internet_gateway(igw.id, vpc.id, dry_run=True)
|
2020-10-06 06:04:09 +00:00
|
|
|
ex.value.error_code.should.equal("DryRunOperation")
|
2021-09-07 16:10:01 +00:00
|
|
|
ex.value.status.should.equal(412)
|
2020-10-06 06:04:09 +00:00
|
|
|
ex.value.message.should.equal(
|
2019-10-31 15:44:26 +00:00
|
|
|
"An error occurred (DryRunOperation) when calling the AttachInternetGateway operation: Request would have succeeded, but DryRun flag is set"
|
|
|
|
)
|
2016-10-15 13:08:44 +00:00
|
|
|
|
2014-06-05 04:12:22 +00:00
|
|
|
conn.attach_internet_gateway(igw.id, vpc.id)
|
|
|
|
|
|
|
|
igw = conn.get_all_internet_gateways()[0]
|
|
|
|
igw.attachments[0].vpc_id.should.be.equal(vpc.id)
|
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
|
2021-09-25 11:13:07 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_igw_attach_boto3():
|
|
|
|
""" internet gateway attach """
|
|
|
|
ec2 = boto3.resource("ec2", "us-west-1")
|
|
|
|
client = boto3.client("ec2", "us-west-1")
|
|
|
|
|
|
|
|
igw = ec2.create_internet_gateway()
|
|
|
|
vpc = ec2.create_vpc(CidrBlock=VPC_CIDR)
|
|
|
|
|
|
|
|
with pytest.raises(ClientError) as ex:
|
|
|
|
vpc.attach_internet_gateway(InternetGatewayId=igw.id, 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 AttachInternetGateway operation: Request would have succeeded, but DryRun flag is set"
|
|
|
|
)
|
|
|
|
|
|
|
|
vpc.attach_internet_gateway(InternetGatewayId=igw.id)
|
|
|
|
|
2021-10-05 17:11:07 +00:00
|
|
|
igw = client.describe_internet_gateways(InternetGatewayIds=[igw.id])[
|
|
|
|
"InternetGateways"
|
|
|
|
][0]
|
2021-09-25 11:13:07 +00:00
|
|
|
igw["Attachments"].should.equal([{"State": "available", "VpcId": vpc.id}])
|
|
|
|
|
|
|
|
|
|
|
|
# Has boto3 equivalent
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-06-05 04:12:22 +00:00
|
|
|
def test_igw_attach_bad_vpc():
|
2021-07-26 14:21:17 +00:00
|
|
|
"""internet gateway fail to attach w/ bad vpc"""
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto.connect_vpc("the_key", "the_secret")
|
2014-06-05 04:12:22 +00:00
|
|
|
igw = conn.create_internet_gateway()
|
2014-08-25 17:54:47 +00:00
|
|
|
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(EC2ResponseError) as cm:
|
2014-08-25 17:54:47 +00:00
|
|
|
conn.attach_internet_gateway(igw.id, BAD_VPC)
|
2020-10-06 06:04:09 +00:00
|
|
|
cm.value.code.should.equal("InvalidVpcID.NotFound")
|
|
|
|
cm.value.status.should.equal(400)
|
|
|
|
cm.value.request_id.should_not.be.none
|
2014-06-05 04:12:22 +00:00
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
|
2021-09-25 11:13:07 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_igw_attach_bad_vpc_boto3():
|
|
|
|
""" internet gateway fail to attach w/ bad vpc """
|
|
|
|
ec2 = boto3.resource("ec2", "us-west-1")
|
|
|
|
igw = ec2.create_internet_gateway()
|
|
|
|
|
|
|
|
with pytest.raises(ClientError) as ex:
|
|
|
|
igw.attach_to_vpc(VpcId=BAD_VPC)
|
|
|
|
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
|
|
|
|
ex.value.response["ResponseMetadata"].should.have.key("RequestId")
|
|
|
|
ex.value.response["Error"]["Code"].should.equal("InvalidVpcID.NotFound")
|
|
|
|
|
|
|
|
|
|
|
|
# Has boto3 equivalent
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-06-05 04:12:22 +00:00
|
|
|
def test_igw_attach_twice():
|
2021-07-26 14:21:17 +00:00
|
|
|
"""internet gateway fail to attach twice"""
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto.connect_vpc("the_key", "the_secret")
|
2014-06-05 04:12:22 +00:00
|
|
|
igw = conn.create_internet_gateway()
|
|
|
|
vpc1 = conn.create_vpc(VPC_CIDR)
|
|
|
|
vpc2 = conn.create_vpc(VPC_CIDR)
|
|
|
|
conn.attach_internet_gateway(igw.id, vpc1.id)
|
2014-08-25 17:54:47 +00:00
|
|
|
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(EC2ResponseError) as cm:
|
2014-08-25 17:54:47 +00:00
|
|
|
conn.attach_internet_gateway(igw.id, vpc2.id)
|
2020-10-06 06:04:09 +00:00
|
|
|
cm.value.code.should.equal("Resource.AlreadyAssociated")
|
|
|
|
cm.value.status.should.equal(400)
|
|
|
|
cm.value.request_id.should_not.be.none
|
2014-06-05 04:12:22 +00:00
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
|
2021-09-25 11:13:07 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_igw_attach_twice_boto3():
|
|
|
|
""" internet gateway fail to attach twice """
|
|
|
|
ec2 = boto3.resource("ec2", "us-west-1")
|
|
|
|
client = boto3.client("ec2", region_name="us-west-1")
|
|
|
|
igw = ec2.create_internet_gateway()
|
|
|
|
vpc1 = ec2.create_vpc(CidrBlock=VPC_CIDR)
|
|
|
|
vpc2 = ec2.create_vpc(CidrBlock=VPC_CIDR)
|
|
|
|
client.attach_internet_gateway(InternetGatewayId=igw.id, VpcId=vpc1.id)
|
|
|
|
|
|
|
|
with pytest.raises(ClientError) as ex:
|
|
|
|
client.attach_internet_gateway(InternetGatewayId=igw.id, VpcId=vpc2.id)
|
|
|
|
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
|
|
|
|
ex.value.response["ResponseMetadata"].should.have.key("RequestId")
|
|
|
|
ex.value.response["Error"]["Code"].should.equal("Resource.AlreadyAssociated")
|
|
|
|
|
|
|
|
|
|
|
|
# Has boto3 equivalent
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-06-05 04:12:22 +00:00
|
|
|
def test_igw_detach():
|
2021-07-26 14:21:17 +00:00
|
|
|
"""internet gateway detach"""
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto.connect_vpc("the_key", "the_secret")
|
2014-06-05 04:12:22 +00:00
|
|
|
igw = conn.create_internet_gateway()
|
|
|
|
vpc = conn.create_vpc(VPC_CIDR)
|
|
|
|
conn.attach_internet_gateway(igw.id, vpc.id)
|
2016-10-15 13:08:44 +00:00
|
|
|
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(EC2ResponseError) as ex:
|
2016-10-15 13:08:44 +00:00
|
|
|
conn.detach_internet_gateway(igw.id, vpc.id, dry_run=True)
|
2020-10-06 06:04:09 +00:00
|
|
|
ex.value.error_code.should.equal("DryRunOperation")
|
2021-09-07 16:10:01 +00:00
|
|
|
ex.value.status.should.equal(412)
|
2020-10-06 06:04:09 +00:00
|
|
|
ex.value.message.should.equal(
|
2019-10-31 15:44:26 +00:00
|
|
|
"An error occurred (DryRunOperation) when calling the DetachInternetGateway operation: Request would have succeeded, but DryRun flag is set"
|
|
|
|
)
|
2016-10-15 13:08:44 +00:00
|
|
|
|
2014-06-05 04:12:22 +00:00
|
|
|
conn.detach_internet_gateway(igw.id, vpc.id)
|
|
|
|
igw = conn.get_all_internet_gateways()[0]
|
|
|
|
igw.attachments.should.have.length_of(0)
|
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
|
2021-09-25 11:13:07 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_igw_detach_boto3():
|
|
|
|
""" internet gateway detach"""
|
|
|
|
ec2 = boto3.resource("ec2", "us-west-1")
|
|
|
|
client = boto3.client("ec2", region_name="us-west-1")
|
|
|
|
igw = ec2.create_internet_gateway()
|
|
|
|
vpc = ec2.create_vpc(CidrBlock=VPC_CIDR)
|
|
|
|
client.attach_internet_gateway(InternetGatewayId=igw.id, VpcId=vpc.id)
|
|
|
|
|
|
|
|
with pytest.raises(ClientError) as ex:
|
|
|
|
client.detach_internet_gateway(
|
|
|
|
InternetGatewayId=igw.id, VpcId=vpc.id, DryRun=True
|
|
|
|
)
|
|
|
|
ex.value.response["Error"]["Code"].should.equal("DryRunOperation")
|
|
|
|
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(412)
|
|
|
|
ex.value.response["Error"]["Message"].should.equal(
|
|
|
|
"An error occurred (DryRunOperation) when calling the DetachInternetGateway operation: Request would have succeeded, but DryRun flag is set"
|
|
|
|
)
|
|
|
|
|
|
|
|
client.detach_internet_gateway(InternetGatewayId=igw.id, VpcId=vpc.id)
|
2021-10-05 17:11:07 +00:00
|
|
|
igw = igw = client.describe_internet_gateways(InternetGatewayIds=[igw.id])[
|
|
|
|
"InternetGateways"
|
|
|
|
][0]
|
2021-09-25 11:13:07 +00:00
|
|
|
igw["Attachments"].should.have.length_of(0)
|
|
|
|
|
|
|
|
|
|
|
|
# Has boto3 equivalent
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-08-25 17:54:47 +00:00
|
|
|
def test_igw_detach_wrong_vpc():
|
2021-07-26 14:21:17 +00:00
|
|
|
"""internet gateway fail to detach w/ wrong vpc"""
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto.connect_vpc("the_key", "the_secret")
|
2014-08-25 17:54:47 +00:00
|
|
|
igw = conn.create_internet_gateway()
|
|
|
|
vpc1 = conn.create_vpc(VPC_CIDR)
|
|
|
|
vpc2 = conn.create_vpc(VPC_CIDR)
|
|
|
|
conn.attach_internet_gateway(igw.id, vpc1.id)
|
|
|
|
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(EC2ResponseError) as cm:
|
2014-08-25 17:54:47 +00:00
|
|
|
conn.detach_internet_gateway(igw.id, vpc2.id)
|
2020-10-06 06:04:09 +00:00
|
|
|
cm.value.code.should.equal("Gateway.NotAttached")
|
|
|
|
cm.value.status.should.equal(400)
|
|
|
|
cm.value.request_id.should_not.be.none
|
2014-08-25 17:54:47 +00:00
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
|
2021-09-25 11:13:07 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_igw_detach_wrong_vpc_boto3():
|
|
|
|
""" internet gateway fail to detach w/ wrong vpc """
|
|
|
|
ec2 = boto3.resource("ec2", "us-west-1")
|
|
|
|
client = boto3.client("ec2", region_name="us-west-1")
|
|
|
|
igw = ec2.create_internet_gateway()
|
|
|
|
vpc1 = ec2.create_vpc(CidrBlock=VPC_CIDR)
|
|
|
|
vpc2 = ec2.create_vpc(CidrBlock=VPC_CIDR)
|
|
|
|
client.attach_internet_gateway(InternetGatewayId=igw.id, VpcId=vpc1.id)
|
|
|
|
|
|
|
|
with pytest.raises(ClientError) as ex:
|
|
|
|
client.detach_internet_gateway(InternetGatewayId=igw.id, VpcId=vpc2.id)
|
|
|
|
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
|
|
|
|
ex.value.response["ResponseMetadata"].should.have.key("RequestId")
|
|
|
|
ex.value.response["Error"]["Code"].should.equal("Gateway.NotAttached")
|
|
|
|
|
|
|
|
|
|
|
|
# Has boto3 equivalent
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-08-25 17:54:47 +00:00
|
|
|
def test_igw_detach_invalid_vpc():
|
2021-07-26 14:21:17 +00:00
|
|
|
"""internet gateway fail to detach w/ invalid vpc"""
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto.connect_vpc("the_key", "the_secret")
|
2014-06-05 04:12:22 +00:00
|
|
|
igw = conn.create_internet_gateway()
|
|
|
|
vpc = conn.create_vpc(VPC_CIDR)
|
|
|
|
conn.attach_internet_gateway(igw.id, vpc.id)
|
2014-08-25 17:54:47 +00:00
|
|
|
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(EC2ResponseError) as cm:
|
2014-08-25 17:54:47 +00:00
|
|
|
conn.detach_internet_gateway(igw.id, BAD_VPC)
|
2020-10-06 06:04:09 +00:00
|
|
|
cm.value.code.should.equal("Gateway.NotAttached")
|
|
|
|
cm.value.status.should.equal(400)
|
|
|
|
cm.value.request_id.should_not.be.none
|
2014-06-05 04:12:22 +00:00
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
|
2021-09-25 11:13:07 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_igw_detach_invalid_vpc_boto3():
|
|
|
|
""" internet gateway fail to detach w/ invalid vpc """
|
|
|
|
ec2 = boto3.resource("ec2", "us-west-1")
|
|
|
|
client = boto3.client("ec2", region_name="us-west-1")
|
|
|
|
igw = ec2.create_internet_gateway()
|
|
|
|
vpc = ec2.create_vpc(CidrBlock=VPC_CIDR)
|
|
|
|
client.attach_internet_gateway(InternetGatewayId=igw.id, VpcId=vpc.id)
|
|
|
|
|
|
|
|
with pytest.raises(ClientError) as ex:
|
|
|
|
client.detach_internet_gateway(InternetGatewayId=igw.id, VpcId=BAD_VPC)
|
|
|
|
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
|
|
|
|
ex.value.response["ResponseMetadata"].should.have.key("RequestId")
|
|
|
|
ex.value.response["Error"]["Code"].should.equal("Gateway.NotAttached")
|
|
|
|
|
|
|
|
|
|
|
|
# Has boto3 equivalent
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-06-05 04:12:22 +00:00
|
|
|
def test_igw_detach_unattached():
|
2021-07-26 14:21:17 +00:00
|
|
|
"""internet gateway fail to detach unattached"""
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto.connect_vpc("the_key", "the_secret")
|
2014-06-05 04:12:22 +00:00
|
|
|
igw = conn.create_internet_gateway()
|
2014-08-25 17:54:47 +00:00
|
|
|
vpc = conn.create_vpc(VPC_CIDR)
|
|
|
|
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(EC2ResponseError) as cm:
|
2014-08-25 17:54:47 +00:00
|
|
|
conn.detach_internet_gateway(igw.id, vpc.id)
|
2020-10-06 06:04:09 +00:00
|
|
|
cm.value.code.should.equal("Gateway.NotAttached")
|
|
|
|
cm.value.status.should.equal(400)
|
|
|
|
cm.value.request_id.should_not.be.none
|
2014-06-05 04:12:22 +00:00
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
|
2021-09-25 11:13:07 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_igw_detach_unattached_boto3():
|
|
|
|
""" internet gateway fail to detach unattached """
|
|
|
|
ec2 = boto3.resource("ec2", "us-west-1")
|
|
|
|
client = boto3.client("ec2", region_name="us-west-1")
|
|
|
|
igw = ec2.create_internet_gateway()
|
|
|
|
vpc = ec2.create_vpc(CidrBlock=VPC_CIDR)
|
|
|
|
|
|
|
|
with pytest.raises(ClientError) as ex:
|
|
|
|
client.detach_internet_gateway(InternetGatewayId=igw.id, VpcId=vpc.id)
|
|
|
|
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
|
|
|
|
ex.value.response["ResponseMetadata"].should.have.key("RequestId")
|
|
|
|
ex.value.response["Error"]["Code"].should.equal("Gateway.NotAttached")
|
|
|
|
|
|
|
|
|
|
|
|
# Has boto3 equivalent
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-06-05 04:12:22 +00:00
|
|
|
def test_igw_delete():
|
2021-07-26 14:21:17 +00:00
|
|
|
"""internet gateway delete"""
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto.connect_vpc("the_key", "the_secret")
|
2021-10-18 19:44:29 +00:00
|
|
|
conn.create_vpc(VPC_CIDR)
|
2014-06-05 04:12:22 +00:00
|
|
|
conn.get_all_internet_gateways().should.have.length_of(0)
|
|
|
|
igw = conn.create_internet_gateway()
|
|
|
|
conn.get_all_internet_gateways().should.have.length_of(1)
|
2016-10-15 13:08:44 +00:00
|
|
|
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(EC2ResponseError) as ex:
|
2016-10-15 13:08:44 +00:00
|
|
|
conn.delete_internet_gateway(igw.id, dry_run=True)
|
2020-10-06 06:04:09 +00:00
|
|
|
ex.value.error_code.should.equal("DryRunOperation")
|
2021-09-07 16:10:01 +00:00
|
|
|
ex.value.status.should.equal(412)
|
2020-10-06 06:04:09 +00:00
|
|
|
ex.value.message.should.equal(
|
2019-10-31 15:44:26 +00:00
|
|
|
"An error occurred (DryRunOperation) when calling the DeleteInternetGateway operation: Request would have succeeded, but DryRun flag is set"
|
|
|
|
)
|
2016-10-15 13:08:44 +00:00
|
|
|
|
2014-06-05 04:12:22 +00:00
|
|
|
conn.delete_internet_gateway(igw.id)
|
|
|
|
conn.get_all_internet_gateways().should.have.length_of(0)
|
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
|
2021-09-25 11:13:07 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_igw_delete_boto3():
|
|
|
|
""" internet gateway delete"""
|
|
|
|
ec2 = boto3.resource("ec2", "us-west-1")
|
|
|
|
client = boto3.client("ec2", region_name="us-west-1")
|
|
|
|
ec2.create_vpc(CidrBlock=VPC_CIDR)
|
|
|
|
|
|
|
|
igw = ec2.create_internet_gateway()
|
2021-10-05 17:11:07 +00:00
|
|
|
[i["InternetGatewayId"] for i in (retrieve_all(client))].should.contain(igw.id)
|
2021-09-25 11:13:07 +00:00
|
|
|
|
|
|
|
with pytest.raises(ClientError) as ex:
|
|
|
|
client.delete_internet_gateway(InternetGatewayId=igw.id, DryRun=True)
|
|
|
|
ex.value.response["Error"]["Code"].should.equal("DryRunOperation")
|
|
|
|
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(412)
|
|
|
|
ex.value.response["Error"]["Message"].should.equal(
|
|
|
|
"An error occurred (DryRunOperation) when calling the DeleteInternetGateway operation: Request would have succeeded, but DryRun flag is set"
|
|
|
|
)
|
|
|
|
|
|
|
|
client.delete_internet_gateway(InternetGatewayId=igw.id)
|
2021-10-05 17:11:07 +00:00
|
|
|
[i["InternetGatewayId"] for i in (retrieve_all(client))].shouldnt.contain(igw.id)
|
2021-09-25 11:13:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Has boto3 equivalent
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-06-05 04:12:22 +00:00
|
|
|
def test_igw_delete_attached():
|
2021-07-26 14:21:17 +00:00
|
|
|
"""internet gateway fail to delete attached"""
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto.connect_vpc("the_key", "the_secret")
|
2014-06-05 04:12:22 +00:00
|
|
|
igw = conn.create_internet_gateway()
|
|
|
|
vpc = conn.create_vpc(VPC_CIDR)
|
|
|
|
conn.attach_internet_gateway(igw.id, vpc.id)
|
2014-08-25 17:54:47 +00:00
|
|
|
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(EC2ResponseError) as cm:
|
2014-08-25 17:54:47 +00:00
|
|
|
conn.delete_internet_gateway(igw.id)
|
2020-10-06 06:04:09 +00:00
|
|
|
cm.value.code.should.equal("DependencyViolation")
|
|
|
|
cm.value.status.should.equal(400)
|
|
|
|
cm.value.request_id.should_not.be.none
|
2014-06-05 04:12:22 +00:00
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
|
2021-09-25 11:13:07 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_igw_delete_attached_boto3():
|
|
|
|
""" internet gateway fail to delete attached """
|
|
|
|
ec2 = boto3.resource("ec2", "us-west-1")
|
|
|
|
client = boto3.client("ec2", "us-west-1")
|
|
|
|
igw = ec2.create_internet_gateway()
|
|
|
|
vpc = ec2.create_vpc(CidrBlock=VPC_CIDR)
|
|
|
|
client.attach_internet_gateway(InternetGatewayId=igw.id, VpcId=vpc.id)
|
|
|
|
|
|
|
|
with pytest.raises(ClientError) as ex:
|
|
|
|
client.delete_internet_gateway(InternetGatewayId=igw.id)
|
|
|
|
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
|
|
|
|
ex.value.response["ResponseMetadata"].should.have.key("RequestId")
|
|
|
|
ex.value.response["Error"]["Code"].should.equal("DependencyViolation")
|
|
|
|
|
|
|
|
|
|
|
|
# Has boto3 equivalent
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-06-05 04:12:22 +00:00
|
|
|
def test_igw_desribe():
|
2021-07-26 14:21:17 +00:00
|
|
|
"""internet gateway fetch by id"""
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto.connect_vpc("the_key", "the_secret")
|
2014-06-05 04:12:22 +00:00
|
|
|
igw = conn.create_internet_gateway()
|
|
|
|
igw_by_search = conn.get_all_internet_gateways([igw.id])[0]
|
|
|
|
igw.id.should.equal(igw_by_search.id)
|
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
|
2021-09-25 11:13:07 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_igw_describe_boto3():
|
|
|
|
""" internet gateway fetch by id """
|
|
|
|
ec2 = boto3.resource("ec2", "us-west-1")
|
|
|
|
client = boto3.client("ec2", "us-west-1")
|
|
|
|
igw = ec2.create_internet_gateway()
|
|
|
|
igw_by_search = client.describe_internet_gateways(InternetGatewayIds=[igw.id])[
|
|
|
|
"InternetGateways"
|
|
|
|
][0]
|
|
|
|
igw.id.should.equal(igw_by_search["InternetGatewayId"])
|
|
|
|
|
|
|
|
|
|
|
|
# Has boto3 equivalent
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2018-07-24 22:11:04 +00:00
|
|
|
def test_igw_describe_bad_id():
|
2021-07-26 14:21:17 +00:00
|
|
|
"""internet gateway fail to fetch by bad id"""
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto.connect_vpc("the_key", "the_secret")
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(EC2ResponseError) as cm:
|
2014-08-25 17:54:47 +00:00
|
|
|
conn.get_all_internet_gateways([BAD_IGW])
|
2020-10-06 06:04:09 +00:00
|
|
|
cm.value.code.should.equal("InvalidInternetGatewayID.NotFound")
|
|
|
|
cm.value.status.should.equal(400)
|
|
|
|
cm.value.request_id.should_not.be.none
|
2014-11-05 17:11:56 +00:00
|
|
|
|
|
|
|
|
2021-09-25 11:13:07 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_igw_describe_bad_id_boto3():
|
|
|
|
""" internet gateway fail to fetch by bad id """
|
|
|
|
client = boto3.client("ec2", "us-west-1")
|
|
|
|
with pytest.raises(ClientError) as ex:
|
|
|
|
client.describe_internet_gateways(InternetGatewayIds=[BAD_IGW])
|
|
|
|
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
|
|
|
|
ex.value.response["ResponseMetadata"].should.have.key("RequestId")
|
|
|
|
ex.value.response["Error"]["Code"].should.equal("InvalidInternetGatewayID.NotFound")
|
|
|
|
|
|
|
|
|
|
|
|
# Has boto3 equivalent
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-11-05 17:11:56 +00:00
|
|
|
def test_igw_filter_by_vpc_id():
|
2021-07-26 14:21:17 +00:00
|
|
|
"""internet gateway filter by vpc id"""
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto.connect_vpc("the_key", "the_secret")
|
2014-11-05 17:11:56 +00:00
|
|
|
|
|
|
|
igw1 = conn.create_internet_gateway()
|
2021-10-18 19:44:29 +00:00
|
|
|
conn.create_internet_gateway()
|
2014-11-05 17:11:56 +00:00
|
|
|
vpc = conn.create_vpc(VPC_CIDR)
|
|
|
|
conn.attach_internet_gateway(igw1.id, vpc.id)
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
result = conn.get_all_internet_gateways(filters={"attachment.vpc-id": vpc.id})
|
2014-11-05 17:11:56 +00:00
|
|
|
result.should.have.length_of(1)
|
|
|
|
result[0].id.should.equal(igw1.id)
|
|
|
|
|
|
|
|
|
2021-09-25 11:13:07 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_igw_filter_by_vpc_id_boto3():
|
|
|
|
""" internet gateway filter by vpc id """
|
|
|
|
ec2 = boto3.resource("ec2", "us-west-1")
|
|
|
|
client = boto3.client("ec2", "us-west-1")
|
|
|
|
|
|
|
|
igw1 = ec2.create_internet_gateway()
|
|
|
|
ec2.create_internet_gateway()
|
|
|
|
vpc = ec2.create_vpc(CidrBlock=VPC_CIDR)
|
|
|
|
client.attach_internet_gateway(InternetGatewayId=igw1.id, VpcId=vpc.id)
|
|
|
|
|
|
|
|
result = client.describe_internet_gateways(
|
|
|
|
Filters=[{"Name": "attachment.vpc-id", "Values": [vpc.id]}]
|
|
|
|
)
|
|
|
|
result["InternetGateways"].should.have.length_of(1)
|
|
|
|
result["InternetGateways"][0]["InternetGatewayId"].should.equal(igw1.id)
|
|
|
|
|
|
|
|
|
|
|
|
# Has boto3 equivalent
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-11-05 17:11:56 +00:00
|
|
|
def test_igw_filter_by_tags():
|
2021-07-26 14:21:17 +00:00
|
|
|
"""internet gateway filter by vpc id"""
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto.connect_vpc("the_key", "the_secret")
|
2014-11-05 17:11:56 +00:00
|
|
|
|
|
|
|
igw1 = conn.create_internet_gateway()
|
2021-10-18 19:44:29 +00:00
|
|
|
conn.create_internet_gateway()
|
2014-11-05 17:11:56 +00:00
|
|
|
igw1.add_tag("tests", "yes")
|
|
|
|
|
|
|
|
result = conn.get_all_internet_gateways(filters={"tag:tests": "yes"})
|
|
|
|
result.should.have.length_of(1)
|
|
|
|
result[0].id.should.equal(igw1.id)
|
|
|
|
|
|
|
|
|
2021-09-25 11:13:07 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_igw_filter_by_tags_boto3():
|
|
|
|
""" internet gateway filter by vpc id """
|
|
|
|
ec2 = boto3.resource("ec2", "us-west-1")
|
|
|
|
client = boto3.client("ec2", "us-west-1")
|
|
|
|
|
|
|
|
igw1 = ec2.create_internet_gateway()
|
2021-10-18 19:44:29 +00:00
|
|
|
ec2.create_internet_gateway()
|
2021-10-05 17:11:07 +00:00
|
|
|
tag_value = str(uuid4())
|
|
|
|
igw1.create_tags(Tags=[{"Key": "tests", "Value": tag_value}])
|
2021-09-25 11:13:07 +00:00
|
|
|
|
2021-10-05 17:11:07 +00:00
|
|
|
result = retrieve_all(client, [{"Name": "tag:tests", "Values": [tag_value]}])
|
|
|
|
result.should.have.length_of(1)
|
|
|
|
result[0]["InternetGatewayId"].should.equal(igw1.id)
|
2021-09-25 11:13:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Has boto3 equivalent
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-11-05 17:11:56 +00:00
|
|
|
def test_igw_filter_by_internet_gateway_id():
|
2021-07-26 14:21:17 +00:00
|
|
|
"""internet gateway filter by internet gateway id"""
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto.connect_vpc("the_key", "the_secret")
|
2014-11-05 17:11:56 +00:00
|
|
|
|
|
|
|
igw1 = conn.create_internet_gateway()
|
2021-10-18 19:44:29 +00:00
|
|
|
conn.create_internet_gateway()
|
2014-11-05 17:11:56 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
result = conn.get_all_internet_gateways(filters={"internet-gateway-id": igw1.id})
|
2014-11-05 17:11:56 +00:00
|
|
|
result.should.have.length_of(1)
|
|
|
|
result[0].id.should.equal(igw1.id)
|
|
|
|
|
|
|
|
|
2021-09-25 11:13:07 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_igw_filter_by_internet_gateway_id_boto3():
|
|
|
|
""" internet gateway filter by internet gateway id """
|
|
|
|
ec2 = boto3.resource("ec2", "us-west-1")
|
|
|
|
client = boto3.client("ec2", "us-west-1")
|
|
|
|
|
|
|
|
igw1 = ec2.create_internet_gateway()
|
2021-10-18 19:44:29 +00:00
|
|
|
ec2.create_internet_gateway()
|
2021-09-25 11:13:07 +00:00
|
|
|
|
|
|
|
result = client.describe_internet_gateways(
|
|
|
|
Filters=[{"Name": "internet-gateway-id", "Values": [igw1.id]}]
|
|
|
|
)
|
|
|
|
result["InternetGateways"].should.have.length_of(1)
|
|
|
|
result["InternetGateways"][0]["InternetGatewayId"].should.equal(igw1.id)
|
|
|
|
|
|
|
|
|
|
|
|
# Has boto3 equivalent
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-11-05 17:11:56 +00:00
|
|
|
def test_igw_filter_by_attachment_state():
|
2021-07-26 14:21:17 +00:00
|
|
|
"""internet gateway filter by attachment state"""
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto.connect_vpc("the_key", "the_secret")
|
2014-11-05 17:11:56 +00:00
|
|
|
|
|
|
|
igw1 = conn.create_internet_gateway()
|
2021-10-18 19:44:29 +00:00
|
|
|
conn.create_internet_gateway()
|
2014-11-05 17:11:56 +00:00
|
|
|
vpc = conn.create_vpc(VPC_CIDR)
|
|
|
|
conn.attach_internet_gateway(igw1.id, vpc.id)
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
result = conn.get_all_internet_gateways(filters={"attachment.state": "available"})
|
2014-11-05 17:11:56 +00:00
|
|
|
result.should.have.length_of(1)
|
|
|
|
result[0].id.should.equal(igw1.id)
|
2020-11-02 13:30:02 +00:00
|
|
|
|
|
|
|
|
2021-09-25 11:13:07 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_igw_filter_by_attachment_state_boto3():
|
|
|
|
""" internet gateway filter by attachment state """
|
|
|
|
ec2 = boto3.resource("ec2", "us-west-1")
|
|
|
|
client = boto3.client("ec2", "us-west-1")
|
|
|
|
|
|
|
|
igw1 = ec2.create_internet_gateway()
|
|
|
|
igw2 = ec2.create_internet_gateway()
|
|
|
|
vpc = ec2.create_vpc(CidrBlock=VPC_CIDR)
|
|
|
|
client.attach_internet_gateway(InternetGatewayId=igw1.id, VpcId=vpc.id)
|
|
|
|
|
2021-10-05 17:11:07 +00:00
|
|
|
filters = [{"Name": "attachment.state", "Values": ["available"]}]
|
|
|
|
all_ids = [igw["InternetGatewayId"] for igw in (retrieve_all(client, filters))]
|
|
|
|
all_ids.should.contain(igw1.id)
|
|
|
|
all_ids.shouldnt.contain(igw2.id)
|
2021-09-25 11:13:07 +00:00
|
|
|
|
|
|
|
|
2020-11-02 13:30:02 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_create_internet_gateway_with_tags():
|
|
|
|
ec2 = boto3.resource("ec2", region_name="eu-central-1")
|
|
|
|
|
|
|
|
igw = ec2.create_internet_gateway(
|
|
|
|
TagSpecifications=[
|
|
|
|
{
|
|
|
|
"ResourceType": "internet-gateway",
|
|
|
|
"Tags": [{"Key": "test", "Value": "TestRouteTable"}],
|
|
|
|
}
|
|
|
|
],
|
|
|
|
)
|
|
|
|
igw.tags.should.have.length_of(1)
|
|
|
|
igw.tags.should.equal([{"Key": "test", "Value": "TestRouteTable"}])
|
2021-10-05 17:11:07 +00:00
|
|
|
|
|
|
|
|
2021-10-18 19:44:29 +00:00
|
|
|
def retrieve_all(client, filters=[]): # pylint: disable=W0102
|
2021-10-05 17:11:07 +00:00
|
|
|
resp = client.describe_internet_gateways(Filters=filters)
|
|
|
|
all_igws = resp["InternetGateways"]
|
|
|
|
token = resp.get("NextToken")
|
|
|
|
while token:
|
|
|
|
resp = client.describe_internet_gateways(NextToken=token, Filters=filters)
|
|
|
|
all_igws.extend(resp["InternetGateways"])
|
|
|
|
token = resp.get("NextToken")
|
|
|
|
return all_igws
|