File: test_virtual_private_gateways.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (300 lines) | stat: -rw-r--r-- 10,091 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import boto3
import pytest
from botocore.exceptions import ClientError

from moto import mock_aws

from .test_tags import retrieve_all_tagged


@mock_aws
def test_attach_unknown_vpn_gateway():
    """describe_vpn_gateways attachment.vpc-id filter"""

    ec2 = boto3.client("ec2", region_name="us-east-1")

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")["Vpc"]

    with pytest.raises(ClientError) as ex:
        ec2.attach_vpn_gateway(VpcId=vpc["VpcId"], VpnGatewayId="?")
    err = ex.value.response["Error"]
    assert err["Message"] == "The virtual private gateway ID '?' does not exist"
    assert err["Code"] == "InvalidVpnGatewayID.NotFound"


@mock_aws
def test_delete_unknown_vpn_gateway():
    """describe_vpn_gateways attachment.vpc-id filter"""

    ec2 = boto3.client("ec2", region_name="us-east-1")

    with pytest.raises(ClientError) as ex:
        ec2.delete_vpn_gateway(VpnGatewayId="?")
    err = ex.value.response["Error"]
    assert err["Message"] == "The virtual private gateway ID '?' does not exist"
    assert err["Code"] == "InvalidVpnGatewayID.NotFound"


@mock_aws
def test_detach_unknown_vpn_gateway():
    """describe_vpn_gateways attachment.vpc-id filter"""

    ec2 = boto3.client("ec2", region_name="us-east-1")

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")["Vpc"]

    with pytest.raises(ClientError) as ex:
        ec2.detach_vpn_gateway(VpcId=vpc["VpcId"], VpnGatewayId="?")
    err = ex.value.response["Error"]
    assert err["Message"] == "The virtual private gateway ID '?' does not exist"
    assert err["Code"] == "InvalidVpnGatewayID.NotFound"


@mock_aws
def test_describe_vpn_connections_attachment_vpc_id_filter():
    """describe_vpn_gateways attachment.vpc-id filter"""

    ec2 = boto3.client("ec2", region_name="us-east-1")

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    vpc_id = vpc["Vpc"]["VpcId"]
    gateway = ec2.create_vpn_gateway(AvailabilityZone="us-east-1a", Type="ipsec.1")
    gateway_id = gateway["VpnGateway"]["VpnGatewayId"]

    ec2.attach_vpn_gateway(VpcId=vpc_id, VpnGatewayId=gateway_id)

    gateways = ec2.describe_vpn_gateways(
        Filters=[{"Name": "attachment.vpc-id", "Values": [vpc_id]}]
    )

    assert len(gateways["VpnGateways"]) == 1
    assert gateways["VpnGateways"][0]["VpnGatewayId"] == gateway_id
    assert {"State": "attached", "VpcId": vpc_id} in gateways["VpnGateways"][0][
        "VpcAttachments"
    ]


@mock_aws
def test_describe_vpn_connections_state_filter_attached():
    """describe_vpn_gateways attachment.state filter - match attached"""

    ec2 = boto3.client("ec2", region_name="us-east-1")

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    vpc_id = vpc["Vpc"]["VpcId"]
    gateway = ec2.create_vpn_gateway(AvailabilityZone="us-east-1a", Type="ipsec.1")
    gateway_id = gateway["VpnGateway"]["VpnGatewayId"]

    ec2.attach_vpn_gateway(VpcId=vpc_id, VpnGatewayId=gateway_id)

    all_gateways = retrieve_all(
        ec2, [{"Name": "attachment.state", "Values": ["attached"]}]
    )

    assert gateway_id in [gw["VpnGatewayId"] for gw in all_gateways]
    my_gateway = [gw for gw in all_gateways if gw["VpnGatewayId"] == gateway_id][0]
    assert {"State": "attached", "VpcId": vpc_id} in my_gateway["VpcAttachments"]


@mock_aws
def test_virtual_private_gateways():
    client = boto3.client("ec2", region_name="us-west-1")

    vpn_gateway = client.create_vpn_gateway(
        Type="ipsec.1", AvailabilityZone="us-east-1a"
    )["VpnGateway"]

    assert vpn_gateway["VpnGatewayId"].startswith("vgw-")
    assert vpn_gateway["Type"] == "ipsec.1"
    assert vpn_gateway["State"] == "available"
    assert vpn_gateway["AvailabilityZone"] == "us-east-1a"


@mock_aws
def test_describe_vpn_gateway():
    client = boto3.client("ec2", region_name="us-west-1")
    vpn_gateway = client.create_vpn_gateway(
        Type="ipsec.1", AvailabilityZone="us-east-1a"
    )["VpnGateway"]

    vgws = client.describe_vpn_gateways(VpnGatewayIds=[vpn_gateway["VpnGatewayId"]])[
        "VpnGateways"
    ]
    assert len(vgws) == 1

    gateway = vgws[0]
    assert gateway["VpnGatewayId"].startswith("vgw-")
    assert gateway["VpnGatewayId"] == vpn_gateway["VpnGatewayId"]
    # TODO: fixme. This currently returns the ID
    # assert gateway["Type"] == "ipsec.1"
    assert gateway["State"] == "available"
    assert gateway["AvailabilityZone"] == "us-east-1a"


@mock_aws
def test_describe_vpn_connections_state_filter_deatched():
    """describe_vpn_gateways attachment.state filter - don't match detatched"""

    ec2 = boto3.client("ec2", region_name="us-east-1")

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    vpc_id = vpc["Vpc"]["VpcId"]
    gateway = ec2.create_vpn_gateway(AvailabilityZone="us-east-1a", Type="ipsec.1")
    gateway_id = gateway["VpnGateway"]["VpnGatewayId"]

    ec2.attach_vpn_gateway(VpcId=vpc_id, VpnGatewayId=gateway_id)

    gateways = ec2.describe_vpn_gateways(
        Filters=[{"Name": "attachment.state", "Values": ["detached"]}]
    )

    assert len(gateways["VpnGateways"]) == 0


@mock_aws
def test_describe_vpn_connections_id_filter_match():
    """describe_vpn_gateways vpn-gateway-id filter - match correct id"""

    ec2 = boto3.client("ec2", region_name="us-east-1")

    gateway = ec2.create_vpn_gateway(AvailabilityZone="us-east-1a", Type="ipsec.1")
    gateway_id = gateway["VpnGateway"]["VpnGatewayId"]

    gateways = ec2.describe_vpn_gateways(
        Filters=[{"Name": "vpn-gateway-id", "Values": [gateway_id]}]
    )

    assert len(gateways["VpnGateways"]) == 1
    assert gateways["VpnGateways"][0]["VpnGatewayId"] == gateway_id


@mock_aws
def test_describe_vpn_connections_id_filter_miss():
    """describe_vpn_gateways vpn-gateway-id filter - don't match"""

    ec2 = boto3.client("ec2", region_name="us-east-1")

    ec2.create_vpn_gateway(AvailabilityZone="us-east-1a", Type="ipsec.1")

    gateways = ec2.describe_vpn_gateways(
        Filters=[{"Name": "vpn-gateway-id", "Values": ["unknown_gateway_id"]}]
    )

    assert len(gateways["VpnGateways"]) == 0


@mock_aws
def test_describe_vpn_connections_type_filter_match():
    """describe_vpn_gateways type filter - match"""

    ec2 = boto3.client("ec2", region_name="us-east-1")

    gateway = ec2.create_vpn_gateway(AvailabilityZone="us-east-1a", Type="ipsec.1")
    gateway_id = gateway["VpnGateway"]["VpnGatewayId"]

    my_gateways = retrieve_all(ec2, [{"Name": "type", "Values": ["ipsec.1"]}])

    assert gateway_id in [gw["VpnGatewayId"] for gw in my_gateways]


@mock_aws
def test_describe_vpn_connections_type_filter_miss():
    """describe_vpn_gateways type filter - don't match"""

    ec2 = boto3.client("ec2", region_name="us-east-1")

    ec2.create_vpn_gateway(AvailabilityZone="us-east-1a", Type="ipsec.1")

    gateways = ec2.describe_vpn_gateways(
        Filters=[{"Name": "type", "Values": ["unknown_type"]}]
    )

    assert len(gateways["VpnGateways"]) == 0


@mock_aws
def test_vpn_gateway_vpc_attachment():
    ec2 = boto3.resource("ec2", region_name="us-west-1")
    client = boto3.client("ec2", region_name="us-west-1")
    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    vpn_gateway = client.create_vpn_gateway(
        Type="ipsec.1", AvailabilityZone="us-east-1a"
    )["VpnGateway"]
    vpng_id = vpn_gateway["VpnGatewayId"]

    client.attach_vpn_gateway(VpnGatewayId=vpng_id, VpcId=vpc.id)

    gateway = client.describe_vpn_gateways(VpnGatewayIds=[vpng_id])["VpnGateways"][0]
    attachments = gateway["VpcAttachments"]
    assert attachments == [{"State": "attached", "VpcId": vpc.id}]


@mock_aws
def test_delete_vpn_gateway():
    client = boto3.client("ec2", region_name="us-west-1")
    vpn_gateway = client.create_vpn_gateway(
        Type="ipsec.1", AvailabilityZone="us-east-1a"
    )["VpnGateway"]
    vpng_id = vpn_gateway["VpnGatewayId"]

    client.delete_vpn_gateway(VpnGatewayId=vpng_id)
    gateways = client.describe_vpn_gateways(VpnGatewayIds=[vpng_id])["VpnGateways"]
    assert len(gateways) == 1
    assert gateways[0]["State"] == "deleted"


@mock_aws
def test_vpn_gateway_tagging():
    client = boto3.client("ec2", region_name="us-west-1")
    vpn_gateway = client.create_vpn_gateway(
        Type="ipsec.1", AvailabilityZone="us-east-1a"
    )["VpnGateway"]
    client.create_tags(
        Resources=[vpn_gateway["VpnGatewayId"]],
        Tags=[{"Key": "a key", "Value": "some value"}],
    )

    all_tags = retrieve_all_tagged(client)
    ours = [a for a in all_tags if a["ResourceId"] == vpn_gateway["VpnGatewayId"]][0]
    assert ours["Key"] == "a key"
    assert ours["Value"] == "some value"

    vpn_gateway = client.describe_vpn_gateways()["VpnGateways"][0]
    # TODO: Fixme: Tags is currently empty
    # assert vpn_gateway["Tags"] == [{'Key': 'a key', 'Value': 'some value'}]


@mock_aws
def test_detach_vpn_gateway():
    ec2 = boto3.resource("ec2", region_name="us-west-1")
    client = boto3.client("ec2", region_name="us-west-1")

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    vpn_gateway = client.create_vpn_gateway(
        Type="ipsec.1", AvailabilityZone="us-east-1a"
    )
    vpn_gateway = vpn_gateway["VpnGateway"]
    vpng_id = vpn_gateway["VpnGatewayId"]

    client.attach_vpn_gateway(VpnGatewayId=vpng_id, VpcId=vpc.id)

    gateway = client.describe_vpn_gateways(VpnGatewayIds=[vpng_id])["VpnGateways"][0]
    attachments = gateway["VpcAttachments"]
    assert attachments == [{"State": "attached", "VpcId": vpc.id}]

    client.detach_vpn_gateway(VpnGatewayId=vpng_id, VpcId=vpc.id)

    gateway = client.describe_vpn_gateways(VpnGatewayIds=[vpng_id])["VpnGateways"][0]
    attachments = gateway["VpcAttachments"]
    assert attachments == [{"State": "detached", "VpcId": vpc.id}]


def retrieve_all(client, filters=None):
    resp = client.describe_vpn_gateways(Filters=filters or [])
    all_gateways = resp["VpnGateways"]
    token = resp.get("NextToken")
    while token:
        resp = client.describe_vpn_gateways(Filters=filters or [])
        all_gateways.extend(resp["VpnGateways"])
        token = resp.get("NextToken")
    return all_gateways