diff --git a/tests/integration/helpers.py b/tests/integration/helpers.py index 94e121d7f..60ec2be92 100644 --- a/tests/integration/helpers.py +++ b/tests/integration/helpers.py @@ -47,6 +47,7 @@ "maintenance", "managed", "monitor", + "natgateways", "networking", "obj", "object-storage", diff --git a/tests/integration/linodes/fixtures.py b/tests/integration/linodes/fixtures.py index d058d0a0c..aa643aae2 100644 --- a/tests/integration/linodes/fixtures.py +++ b/tests/integration/linodes/fixtures.py @@ -114,7 +114,7 @@ def snapshot_of_linode(linode_cloud_firewall): # Configs TEST_REGION = get_random_region_with_caps( - required_capabilities=[Capability.Linodes, Capability.VPCs] + required_capabilities=[Capability.Linodes, Capability.VPCs, Capability.NATGateway] ) @@ -424,7 +424,7 @@ def linode_interface_legacy(linode_cloud_firewall): delete_target_id(target="linodes", id=linode_id) -@pytest.fixture(scope="module") +@pytest.fixture def linode_interface_vpc(linode_cloud_firewall): vpc_output = json.loads( exec_test_command( @@ -435,7 +435,7 @@ def linode_interface_vpc(linode_cloud_firewall): "--label", get_random_text(5) + "-vpc", "--region", - "us-sea", + TEST_REGION, "--subnets.ipv4", "10.0.0.0/24", "--subnets.label", @@ -450,7 +450,7 @@ def linode_interface_vpc(linode_cloud_firewall): linode_id = create_linode( firewall_id=linode_cloud_firewall, - test_region="us-sea", + test_region=TEST_REGION, interface_generation="linode", interfaces='[{"default_route":{"ipv4":true},"firewall_id":' + str(linode_cloud_firewall) @@ -459,7 +459,7 @@ def linode_interface_vpc(linode_cloud_firewall): + "}}]", ) - yield linode_id + yield linode_id, vpc_output[0]["id"], subnet_id delete_target_id(target="linodes", id=linode_id) delete_target_id(target="vpcs", id=str(vpc_output[0]["id"])) diff --git a/tests/integration/linodes/test_linode_interfaces.py b/tests/integration/linodes/test_linode_interfaces.py index ca861750a..f02bf33db 100644 --- a/tests/integration/linodes/test_linode_interfaces.py +++ b/tests/integration/linodes/test_linode_interfaces.py @@ -216,7 +216,7 @@ def test_interface_update(linode_interface_public, monkeypatch: MonkeyPatch): def test_interface_view(linode_interface_vpc, monkeypatch: MonkeyPatch): monkeypatch.setenv("LINODE_CLI_API_VERSION", "v4beta") - linode_id = linode_interface_vpc + linode_id, _, _ = linode_interface_vpc interface_id = get_interface_id(linode_id) data = json.loads( diff --git a/tests/integration/networking/fixtures.py b/tests/integration/networking/fixtures.py index 723694adb..e894bde1b 100644 --- a/tests/integration/networking/fixtures.py +++ b/tests/integration/networking/fixtures.py @@ -1,10 +1,62 @@ +import json + import pytest -from tests.integration.helpers import delete_target_id +from tests.integration.helpers import ( + BASE_CMDS, + COMMAND_JSON_OUTPUT, + delete_target_id, + exec_test_command, +) from tests.integration.linodes.helpers import ( create_linode, create_linode_and_wait, ) +from tests.integration.linodes.fixtures import TEST_REGION # noqa: F401 + + +@pytest.fixture +def create_nat_gateway(request, name_generator): + autoscaling = getattr(request, "param", True) + label = name_generator("nat-gateway") + result = json.loads( + exec_test_command( + BASE_CMDS["natgateways"] + + [ + "create", + "--label", + label, + "--region", + TEST_REGION, + "--default_ports_per_interface", + "4096", + "--use_autoscaling", + str(autoscaling), + ] + + COMMAND_JSON_OUTPUT + ) + )[0] + + yield result, label + + delete_target_id("natgateways", str(result["id"])) + + +@pytest.fixture +def create_nat_reserved_ip(request): + command = [ + "linode-cli", + "networking", + "reserved-ip-add", + "--region", + TEST_REGION, + "--json", + ] + result = json.loads(exec_test_command(command))[0] + + yield result + + delete_target_id("networking", result["address"], "reserved-ip-delete") @pytest.fixture(scope="package") diff --git a/tests/integration/networking/test_networking.py b/tests/integration/networking/test_networking.py index dae38471c..488736858 100644 --- a/tests/integration/networking/test_networking.py +++ b/tests/integration/networking/test_networking.py @@ -7,16 +7,44 @@ from tests.integration.helpers import ( BASE_CMDS, + COMMAND_JSON_OUTPUT, DEFAULT_REGION, assert_headers_in_lines, delete_target_id, exec_test_command, ) +from tests.integration.linodes.fixtures import ( # noqa: F401 + TEST_REGION, + linode_interface_vpc, +) from tests.integration.networking.fixtures import ( # noqa: F401 + create_nat_gateway, + create_nat_reserved_ip, get_linode_id, get_linode_ids_shared_ipv4, ) +NAT_HEADERS = [ + "id", + "label", + "region", + "address_autoscale_max", + "default_ports_per_interface", + "portset_assignments", + "portset_capacity", + # "addresses", + # "vpc_subnet", +] + +NAT_ADDRESS_HEADERS = [ + "address", + "in_use", + "interface_count", + "interface_url", + "portset_assignments", + "portset_capacity" +] + RESERVED_IP_HEADERS = [ "address", "type", @@ -33,6 +61,16 @@ ] +def assign_address_to_nat_gateway(gateway_id, address): + return json.loads( + exec_test_command( + BASE_CMDS["natgateways"] + + ["addresses-add", "--address", address, str(gateway_id)] + + COMMAND_JSON_OUTPUT + ) + )[0] + + def has_shared_ip(linode_id: int, ip: str) -> bool: shared_ips = json.loads( exec_test_command( @@ -51,6 +89,48 @@ def has_shared_ip(linode_id: int, ip: str) -> bool: return False +def verify_nat_gateway_vals(result, label): + assert result["label"] == label + assert result["region"] == TEST_REGION + assert result["default_ports_per_interface"] == 4096 + assert result["address_autoscale_max"] >= 0 + assert result["portset_assignments"] >= 0 + assert result["portset_capacity"] >= 0 + + if result["addresses"]: + assert all(isinstance( + ipaddress.ip_address(addr["address"]), ipaddress.IPv4Address + ) for addr in result["addresses"]) + else: + assert result["addresses"] == [] + + # assert result["vpc_subnet"] is None + + +def verify_nat_gateway_address_vals(result, address): + interface_url = f"/addresses/{address}/interfaces" + + assert result["address"] == address + assert result["in_use"] == False + assert result["interface_count"] == 0 + # assert result["interface_url"].endswith(interface_url) + assert result["portset_assignments"] >= 0 + assert result["portset_capacity"] > 0 + + +def verify_nat_gateway_view_json(gateway_id, label): + result = json.loads( + exec_test_command( + BASE_CMDS["natgateways"] + + ["view", str(gateway_id)] + + COMMAND_JSON_OUTPUT + ) + )[0] + assert_headers_in_lines(NAT_HEADERS, [list(result.keys())]) + assert result["id"] == gateway_id + verify_nat_gateway_vals(result, label) + + def verify_reserved_ip(result): assert isinstance( ipaddress.ip_address(result["address"]), ipaddress.IPv4Address @@ -402,3 +482,223 @@ def test_share_ipv4_address( ) assert not has_shared_ip(target_linode, ip_address) + + +@pytest.mark.smoke +def test_nat_gateway_create(create_nat_gateway): + gateway, label = create_nat_gateway + assert_headers_in_lines(NAT_HEADERS, [list(gateway.keys())]) + verify_nat_gateway_view_json(gateway["id"], label) + + +def test_nat_gateway_list(create_nat_gateway): + gateway, label = create_nat_gateway + result = exec_test_command( + BASE_CMDS["natgateways"] + + ["list", "--text", "--delimiter", ","] + ).splitlines() + assert_headers_in_lines(NAT_HEADERS, result) + + result = json.loads( + exec_test_command( + BASE_CMDS["natgateways"] + + ["list"] + + COMMAND_JSON_OUTPUT + ) + ) + matching_gateway = [ + item for item in result if item["id"] == gateway["id"] + ][0] + assert_headers_in_lines(NAT_HEADERS, [list(matching_gateway.keys())]) + verify_nat_gateway_vals(matching_gateway, label) + + +def test_nat_gateway_view(create_nat_gateway): + gateway, label = create_nat_gateway + result = exec_test_command( + BASE_CMDS["natgateways"] + + ["view", str(gateway["id"]), "--text", "--delimiter", ","] + ).splitlines() + assert_headers_in_lines(NAT_HEADERS, result) + verify_nat_gateway_view_json(gateway["id"], label) + + +def test_nat_gateway_update(create_nat_gateway): + gateway, label = create_nat_gateway + label_new = label + "-updated" + + result = json.loads( + exec_test_command( + BASE_CMDS["natgateways"] + + ["update", "--label", label_new, str(gateway["id"])] + + COMMAND_JSON_OUTPUT + ) + )[0] + assert_headers_in_lines(NAT_HEADERS, [list(result.keys())]) + assert result["id"] == gateway["id"] + verify_nat_gateway_view_json(gateway["id"], label_new) + + +@pytest.mark.parametrize("create_nat_gateway", [False], indirect=True) +def test_nat_gateway_address(create_nat_gateway, create_nat_reserved_ip): + gateway, label = create_nat_gateway + address = create_nat_reserved_ip["address"] + + result = assign_address_to_nat_gateway(gateway['id'], address) + assert_headers_in_lines(NAT_ADDRESS_HEADERS, [list(result.keys())]) + verify_nat_gateway_address_vals(result, address) + + exec_test_command( + BASE_CMDS["natgateways"] + + ["address-delete", str(gateway["id"]), address] + ) + verify_nat_gateway_view_json(gateway["id"], label) + + +@pytest.mark.parametrize("create_nat_gateway", [False], indirect=True) +def test_nat_gateway_addresses_list(create_nat_gateway, create_nat_reserved_ip): + gateway, label = create_nat_gateway + address = create_nat_reserved_ip["address"] + assign_address_to_nat_gateway(gateway['id'], address) + + result = exec_test_command( + BASE_CMDS["natgateways"] + + ["addresses-list", str(gateway["id"]), "--text", "--delimiter", ","] + ).splitlines() + assert_headers_in_lines(NAT_ADDRESS_HEADERS, result) + + result = json.loads( + exec_test_command( + BASE_CMDS["natgateways"] + + ["addresses-list", str(gateway["id"])] + + COMMAND_JSON_OUTPUT + ) + ) + matching_address = [ + item for item in result if item["address"] == address + ][0] + assert_headers_in_lines(NAT_ADDRESS_HEADERS, [list(matching_address.keys())]) + verify_nat_gateway_address_vals(matching_address, address) + + +@pytest.mark.parametrize("create_nat_gateway", [False], indirect=True) +def test_nat_gateway_address_view(create_nat_gateway, create_nat_reserved_ip): + gateway, label = create_nat_gateway + address = create_nat_reserved_ip["address"] + assign_address_to_nat_gateway(gateway['id'], address) + + result = exec_test_command( + BASE_CMDS["natgateways"] + + ["address-view", str(gateway["id"]), address, "--text", "--delimiter", ","] + ).splitlines() + assert_headers_in_lines(NAT_ADDRESS_HEADERS, result) + + result = json.loads( + exec_test_command( + BASE_CMDS["natgateways"] + + ["address-view", str(gateway["id"]), address] + + COMMAND_JSON_OUTPUT + ) + )[0] + assert_headers_in_lines(NAT_ADDRESS_HEADERS, [list(result.keys())]) + verify_nat_gateway_address_vals(result, address) + verify_nat_gateway_view_json(gateway["id"], label) + + +@pytest.mark.parametrize("create_nat_gateway", [False], indirect=True) +def test_nat_gateway_interfaces_list( + create_nat_gateway, + create_nat_reserved_ip, + linode_interface_vpc +): + headers = ["id", "linode", "addresses"] + gateway, label = create_nat_gateway + address = create_nat_reserved_ip["address"] + linode_id, vpc_id, subnet_id = linode_interface_vpc + + assign_address_to_nat_gateway(gateway['id'], address) + + # Link the NAT Gateway to the VPC Subnet + exec_test_command( + BASE_CMDS["vpcs"] + + [ + "subnet-update", + "--natgateway", + str(gateway["id"]), + str(vpc_id), + str(subnet_id) + ] + ) + + commands = [ + ["interfaces-list", str(gateway["id"])], + [ + "address-interfaces-list", + str(gateway["id"]), + address, + ], + ] + + for args in commands: + command = BASE_CMDS["natgateways"] + args + result = exec_test_command( + command + ["--text", "--delimiter", ","] + ).splitlines() + assert_headers_in_lines(headers, result) + + result = json.loads( + exec_test_command(command + COMMAND_JSON_OUTPUT) + )[0] + assert result["linode"]["id"] == int(linode_id) + assert result["addresses"][0] == address + assert isinstance(result["portsets"], list) + + +def test_nat_gateway_types(create_nat_gateway): + headers = ["id", "label", "price"] + result = exec_test_command( + BASE_CMDS["natgateways"] + + ["types-list", "--text", "--delimiter", ","] + ).splitlines() + assert_headers_in_lines(headers, result) + + result = json.loads( + exec_test_command( + BASE_CMDS["natgateways"] + + ["types-list"] + + COMMAND_JSON_OUTPUT + ) + )[0] + assert_headers_in_lines(headers, [list(result.keys())]) + prices = [result["price"].get("hourly"), result["price"].get("monthly")] + assert result["id"] == "g1-natgateway" + assert result["label"] == "NAT Gateway" + assert any(price != 0 for price in prices) + + +def test_nat_gateway_settings(): + headers = [ + "allowed_ports_per_interface", + "maximum_autoscaling_addresses_per_natgateway", + "maximum_reserved_addresses_per_natgateway" + ] + result = exec_test_command( + BASE_CMDS["natgateways"] + + ["settings-view", "--text", "--delimiter", ","] + ).splitlines() + assert_headers_in_lines(headers, result) + + result = json.loads( + exec_test_command( + BASE_CMDS["natgateways"] + + ["settings-view"] + + COMMAND_JSON_OUTPUT + ) + )[0] + assert_headers_in_lines(headers, [list(result.keys())]) + assert all( + port in [4096, 8192, 16384] + for port in result["allowed_ports_per_interface"] + ) + assert result["maximum_autoscaling_addresses_per_natgateway"] > 0 + assert result["maximum_reserved_addresses_per_natgateway"] > 0