Skip to content

Commit bbbc4b5

Browse files
committed
Refactor NAT Gateway integration tests
1 parent 1776a6c commit bbbc4b5

2 files changed

Lines changed: 58 additions & 22 deletions

File tree

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

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -603,22 +603,47 @@ def test_convert_unassigned_reserved_ip_to_ephemeral(
603603
assert len(reserved_ips_list) == 0
604604

605605

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
606+
def verify_nat_gateway(nat_gateway, nat_gateway_read):
607+
assert nat_gateway_read.id == nat_gateway.id
608+
assert nat_gateway_read.region.id == nat_gateway.region.id
609+
assert nat_gateway_read.label == nat_gateway.label
610+
assert len(nat_gateway_read.addresses) == 0
611+
assert nat_gateway_read.address_autoscale_max > 0
612+
assert nat_gateway_read.default_ports_per_interface == 4096
613+
assert nat_gateway_read.portset_assignments == 0
614+
assert nat_gateway_read.portset_capacity > 0
615+
616+
617+
def verify_nat_gateway_address(nat_address, reserved_ip_address):
618+
assert nat_address.address == reserved_ip_address
619+
assert nat_address.in_use == False
620+
assert nat_address.interface_count == 0
621+
assert nat_address.interface_url.endswith(
622+
f"/{reserved_ip_address}/interfaces"
623+
)
624+
assert nat_address.portset_assignments == 0
625+
assert nat_address.portset_capacity > 0
626+
627+
628+
def verify_nat_gateway_interface(interface, linode, reserved_ip):
629+
assert interface.id == linode.linode_interfaces[0].id
630+
assert interface.linode.id == linode.id
631+
assert interface.addresses[0] == reserved_ip.address
632+
assert interface.portsets[0].address == reserved_ip.address
612633

613634

614635
@pytest.mark.smoke
615636
def test_create_nat_gateway(test_linode_client, create_nat_gateway):
616637
client = test_linode_client
638+
617639
gateway = client.load(NATGateway, create_nat_gateway.id)
640+
verify_nat_gateway(create_nat_gateway, gateway)
618641

619-
assert gateway.id == create_nat_gateway.id
620-
assert gateway.region.id == create_nat_gateway.region.id
621-
assert gateway.label == create_nat_gateway.label
642+
gateways = client.networking.natgateways(
643+
NATGateway.label == create_nat_gateway.label
644+
)
645+
assert len(gateways) == 1
646+
verify_nat_gateway(create_nat_gateway, gateways[0])
622647

623648

624649
def test_update_nat_gateway(test_linode_client, create_nat_gateway):
@@ -630,23 +655,28 @@ def test_update_nat_gateway(test_linode_client, create_nat_gateway):
630655
gateway.save()
631656

632657
gateway_updated = client.load(NATGateway, gateway.id)
633-
634658
assert gateway_updated.label == new_label
635659

636660

637661
@pytest.mark.parametrize("create_nat_gateway", [False], indirect=True)
638-
def test_update_nat_gateway_with_ip_address(
662+
def test_assign_nat_gateway_ip_address(
639663
test_linode_client, create_nat_gateway, create_reserved_ip
640664
):
641-
client = test_linode_client
642665
gateway = create_nat_gateway
643666
reserved_ip = create_reserved_ip
644667

645668
gateway.address_assignment_create(reserved_ip.address)
646-
gateway = client.load(NATGateway, gateway.id)
647669

648-
assert len(gateway.addresses) == 1
649-
assert gateway.addresses[0].address == reserved_ip.address
670+
addresses = gateway.address_assignments()
671+
assert len(addresses) == 1
672+
verify_nat_gateway_address(addresses[0], reserved_ip.address)
673+
674+
address = gateway.address_assignment_view(reserved_ip.address)
675+
verify_nat_gateway_address(address, reserved_ip.address)
676+
677+
gateway.address_assignment_delete(reserved_ip.address)
678+
addresses = gateway.address_assignments()
679+
assert len(addresses) == 0
650680

651681

652682
@pytest.mark.parametrize("create_nat_gateway", [False], indirect=True)
@@ -663,6 +693,7 @@ def test_get_nat_gateway_linode_ifaces(
663693
reserved_ip = create_reserved_ip
664694

665695
gateway.address_assignment_create(reserved_ip.address)
696+
# Link the NAT Gateway to the VPC Subnet
666697
subnet.natgateway = VPCSubnetNATGatewayOptions(id=gateway.id)
667698
subnet.save()
668699

@@ -692,32 +723,38 @@ def test_get_nat_gateway_linode_ifaces(
692723
linode = client.load(Instance, linode.id)
693724

694725
gateway_ifaces = gateway.interfaces()
695-
verify_nat_gateway_ifaces(gateway_ifaces, linode, reserved_ip)
726+
assert len(gateway_ifaces) == 1
727+
verify_nat_gateway_interface(gateway_ifaces[0], linode, reserved_ip)
696728

697729
gateway_ifaces = gateway.address_interfaces(reserved_ip.address)
698-
verify_nat_gateway_ifaces(gateway_ifaces, linode, reserved_ip)
730+
verify_nat_gateway_interface(gateway_ifaces[0], linode, reserved_ip)
731+
732+
address = gateway.address_assignment_view(reserved_ip.address)
733+
assert address.in_use == True
734+
assert address.interface_count == 1
735+
assert address.portset_assignments == 1
699736

700737

701738
def test_get_nat_gateway_types(test_linode_client):
702739
client = test_linode_client
703-
gateway_types = client.networking.natgateway_types()
704740

741+
gateway_types = client.networking.natgateway_types()
705742
assert len(gateway_types) > 0
706743
assert gateway_types[0].id == "g1-natgateway"
707744
assert gateway_types[0].label == "NAT Gateway"
708745
assert (
709746
gateway_types[0].price.hourly >= 0
710747
or gateway_types[0].price.monthly >= 0
711748
)
712-
# region_prices and transfer fields are not in use at the moment
749+
# TODO region_prices and transfer fields are not in use at the moment
713750
# assert isinstance(gateway_types[0].region_prices, list)
714751
# assert isinstance(gateway_types[0].transfer, int)
715752

716753

717754
def test_get_nat_gateway_settings(test_linode_client):
718755
client = test_linode_client
719-
gateway_settings = client.networking.natgateway_settings()
720756

757+
gateway_settings = client.networking.natgateway_settings()
721758
assert all(
722759
port in [4096, 8192, 16384]
723760
for port in gateway_settings.allowed_ports_per_interface

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def test_vpc_subnet_with_nat_gateway(
184184
gateway = create_nat_gateway
185185
vpc = create_nat_vpc
186186

187-
subnet = create_nat_vpc.subnet_create(
187+
subnet = vpc.subnet_create(
188188
label="test-nat-subnet",
189189
ipv4="10.0.0.0/24",
190190
ipv6=[{"range": "auto"}],
@@ -193,5 +193,4 @@ def test_vpc_subnet_with_nat_gateway(
193193
request.addfinalizer(subnet.delete)
194194

195195
subnet = client.load(VPCSubnet, subnet.id, vpc.id)
196-
197196
assert subnet.natgateway.id == gateway.id

0 commit comments

Comments
 (0)