Skip to content

Commit 1c3565f

Browse files
committed
Add int tests for VPC subnet with NAT Gateway
1 parent 1340d11 commit 1c3565f

3 files changed

Lines changed: 91 additions & 61 deletions

File tree

‎test/integration/conftest.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from requests.exceptions import ConnectionError, RequestException
1818

1919
from linode_api4 import (
20+
Capability,
2021
Instance,
2122
InterfaceGeneration,
2223
IPAddress,
@@ -25,6 +26,7 @@
2526
LinodeInterfacePublicOptions,
2627
LinodeInterfaceVLANOptions,
2728
LinodeInterfaceVPCOptions,
29+
NATGateway,
2830
PlacementGroupPolicy,
2931
PlacementGroupType,
3032
PostgreSQLDatabase,
@@ -815,3 +817,51 @@ def create_reserved_ip_assigned(test_linode_client, create_linode):
815817
IPAddress.address == reserved_ip.address
816818
):
817819
address[0].delete()
820+
821+
822+
@pytest.fixture
823+
def create_nat_gateway(request, test_linode_client):
824+
client = test_linode_client
825+
label = "nat-gateway-" + get_test_label(length=8)
826+
autoscaling = getattr(request, "param", True)
827+
region = get_region(
828+
client,
829+
{
830+
Capability.firewall,
831+
Capability.linodes,
832+
Capability.linode_interfaces,
833+
Capability.natgateway,
834+
Capability.vpcs,
835+
Capability.vpc_dual_stack,
836+
Capability.vpc_ipv6_stack,
837+
},
838+
)
839+
840+
gateway = client.networking.natgateway_create(
841+
label=label,
842+
region=region,
843+
use_autoscaling=autoscaling,
844+
)
845+
846+
yield gateway
847+
848+
# Delete only if NAT Gateway exists (some tests may delete it earlier)
849+
if client.load(NATGateway, gateway.id):
850+
gateway.delete()
851+
852+
853+
@pytest.fixture
854+
def create_nat_vpc(test_linode_client, create_nat_gateway):
855+
client = test_linode_client
856+
gateway = create_nat_gateway
857+
label = get_test_label(length=10)
858+
859+
vpc = client.vpcs.create(
860+
label=label,
861+
region=gateway.region,
862+
description="test NAT Gateway VPC",
863+
ipv6=[{"range": "auto"}],
864+
)
865+
yield vpc
866+
867+
vpc.delete()

‎test/integration/models/networking/test_networking.py‎

Lines changed: 14 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,6 @@
5252
site_type="core",
5353
)
5454

55-
TEST_NAT_REGION = get_region(
56-
LinodeClient(
57-
token=get_token(),
58-
base_url=get_api_url(),
59-
ca_path=get_api_ca_file(),
60-
),
61-
{
62-
Capability.firewall,
63-
Capability.linodes,
64-
Capability.linode_interfaces,
65-
Capability.natgateway,
66-
Capability.vpcs,
67-
},
68-
site_type="core",
69-
)
70-
7155

7256
def create_linode_func(test_linode_client):
7357
client = test_linode_client
@@ -156,22 +140,6 @@ def create_firewall_with_device(create_linode_without_firewall):
156140
firewall.delete()
157141

158142

159-
@pytest.fixture
160-
def create_nat_vpc(test_linode_client):
161-
client = test_linode_client
162-
label = get_test_label(length=10)
163-
164-
vpc = client.vpcs.create(
165-
label=label,
166-
region=TEST_NAT_REGION,
167-
description="test NAT Gateway VPC",
168-
ipv6=[{"range": "auto"}],
169-
)
170-
yield vpc
171-
172-
vpc.delete()
173-
174-
175143
@pytest.fixture
176144
def create_nat_vpc_with_subnet(test_linode_client, create_nat_vpc):
177145
subnet = create_nat_vpc.subnet_create(
@@ -205,25 +173,6 @@ def create_nat_vpc_with_subnet_and_linode(
205173
instance.delete()
206174

207175

208-
@pytest.fixture
209-
def create_nat_gateway(request, test_linode_client):
210-
client = test_linode_client
211-
label = "nat-gateway-" + get_test_label(length=8)
212-
autoscaling = getattr(request, "param", True)
213-
214-
natgateway = client.networking.natgateway_create(
215-
label=label,
216-
region=TEST_NAT_REGION,
217-
use_autoscaling=autoscaling,
218-
)
219-
220-
yield natgateway
221-
222-
# Delete only if NAT Gateway exists (some tests may delete it earlier)
223-
if client.load(NATGateway, natgateway.id):
224-
natgateway.delete()
225-
226-
227176
def test_get_networking_rule_versions(test_linode_client, test_firewall):
228177
firewall = test_linode_client.load(Firewall, test_firewall.id)
229178

@@ -654,6 +603,14 @@ def test_convert_unassigned_reserved_ip_to_ephemeral(
654603
assert len(reserved_ips_list) == 0
655604

656605

606+
def verify_nat_gateway_ifaces(gateway_ifaces, linode, reserved_ip):
607+
assert len(gateway_ifaces) == 1
608+
assert gateway_ifaces[0].id == linode.linode_interfaces[0].id
609+
assert gateway_ifaces[0].linode.id == linode.id
610+
assert gateway_ifaces[0].addresses[0] == reserved_ip.address
611+
assert gateway_ifaces[0].portsets[0].address == reserved_ip.address
612+
613+
657614
@pytest.mark.smoke
658615
def test_create_nat_gateway(test_linode_client, create_nat_gateway):
659616
client = test_linode_client
@@ -682,7 +639,7 @@ def test_update_nat_gateway_with_ip_address(
682639
test_linode_client, create_nat_gateway, create_reserved_ip
683640
):
684641
client = test_linode_client
685-
gateway = client.load(NATGateway, create_nat_gateway.id)
642+
gateway = create_nat_gateway
686643
reserved_ip = create_reserved_ip
687644

688645
gateway.address_assignment_create(reserved_ip.address)
@@ -701,7 +658,7 @@ def test_get_nat_gateway_linode_ifaces(
701658
create_reserved_ip,
702659
):
703660
client = test_linode_client
704-
gateway = client.load(NATGateway, create_nat_gateway.id)
661+
gateway = create_nat_gateway
705662
vpc, subnet, linode = create_nat_vpc_with_subnet_and_linode
706663
reserved_ip = create_reserved_ip
707664

@@ -732,16 +689,13 @@ def test_get_nat_gateway_linode_ifaces(
732689
),
733690
),
734691
)
692+
linode = client.load(Instance, linode.id)
735693

736694
gateway_ifaces = gateway.interfaces()
737-
linode = client.load(Instance, linode.id)
695+
verify_nat_gateway_ifaces(gateway_ifaces, linode, reserved_ip)
738696

739-
assert len(gateway_ifaces) == 1
740-
assert gateway.vpc_subnet.id == subnet.id
741-
assert gateway_ifaces[0].id == linode.linode_interfaces[0].id
742-
assert gateway_ifaces[0].linode.id == linode.id
743-
assert gateway_ifaces[0].addresses[0] == reserved_ip.address
744-
assert gateway_ifaces[0].portsets[0].address == reserved_ip.address
697+
gateway_ifaces = gateway.address_interfaces(reserved_ip.address)
698+
verify_nat_gateway_ifaces(gateway_ifaces, linode, reserved_ip)
745699

746700

747701
def test_get_nat_gateway_types(test_linode_client):

‎test/integration/models/vpc/test_vpc.py‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
import pytest
44

5-
from linode_api4 import VPC, ApiError, VPCIPv4DefaultRange, VPCSubnet
5+
from linode_api4 import (
6+
VPC,
7+
ApiError,
8+
VPCIPv4DefaultRange,
9+
VPCSubnet,
10+
)
11+
from linode_api4.objects import VPCSubnetNATGatewayOptions
612

713

814
@pytest.mark.smoke
@@ -169,3 +175,23 @@ def test_vpc_with_ipv4(test_linode_client, create_vpc_with_ipv4):
169175

170176
updated_vpc = client.load(VPC, vpc.id)
171177
assert updated_vpc.ipv4[0].range == "192.168.0.0/17"
178+
179+
180+
def test_vpc_subnet_with_nat_gateway(
181+
request, test_linode_client, create_nat_gateway, create_nat_vpc
182+
):
183+
client = test_linode_client
184+
gateway = create_nat_gateway
185+
vpc = create_nat_vpc
186+
187+
subnet = create_nat_vpc.subnet_create(
188+
label="test-nat-subnet",
189+
ipv4="10.0.0.0/24",
190+
ipv6=[{"range": "auto"}],
191+
natgateway=VPCSubnetNATGatewayOptions(id=gateway.id),
192+
)
193+
request.addfinalizer(subnet.delete)
194+
195+
subnet = client.load(VPCSubnet, subnet.id, vpc.id)
196+
197+
assert subnet.natgateway.id == gateway.id

0 commit comments

Comments
 (0)