diff --git a/tests/integration/account/test_account.py b/tests/integration/account/test_account.py index fb173d2a2..f1fae5dca 100644 --- a/tests/integration/account/test_account.py +++ b/tests/integration/account/test_account.py @@ -1,15 +1,15 @@ import pytest -from tests.integration.helpers import assert_headers_in_lines, exec_test_command - -BASE_CMD = ["linode-cli", "account"] +from tests.integration.helpers import ( + BASE_CMDS, + assert_headers_in_lines, + exec_test_command, +) def test_account_transfer(): - res = ( - exec_test_command(BASE_CMD + ["transfer", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["account"] + ["transfer", "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -18,12 +18,8 @@ def test_account_transfer(): def test_available_service(): - res = ( - exec_test_command( - BASE_CMD + ["get-availability", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["account"] + ["get-availability", "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -32,13 +28,9 @@ def test_available_service(): def test_region_availability(): - res = ( - exec_test_command( - BASE_CMD - + ["get-account-availability", "us-east", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["account"] + + ["get-account-availability", "us-east", "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -47,12 +39,8 @@ def test_region_availability(): def test_event_list(): - res = ( - exec_test_command( - ["linode-cli", "events", "list", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + ["linode-cli", "events", "list", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["entity.label", "username"] @@ -61,43 +49,34 @@ def test_event_list(): @pytest.fixture def get_event_id(): - event_id = ( - exec_test_command( - [ - "linode-cli", - "events", - "list", - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) + event_id = exec_test_command( + [ + "linode-cli", + "events", + "list", + "--text", + "--no-headers", + "--delimiter", + ",", + "--format", + "id", + ] + ).splitlines() first_id = event_id[0].split(",")[0] yield first_id def test_event_view(get_event_id): event_id = get_event_id - res = ( - exec_test_command( - [ - "linode-cli", - "events", - "view", - event_id, - "--text", - "--delimiter=,", - ] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + [ + "linode-cli", + "events", + "view", + event_id, + "--text", + "--delimiter=,", + ] ) lines = res.splitlines() @@ -117,7 +96,6 @@ def test_event_read(get_event_id): "--delimiter=,", ] ) - assert process.returncode == 0 def test_event_seen(get_event_id): @@ -132,16 +110,11 @@ def test_event_seen(get_event_id): "--delimiter=,", ] ) - assert process.returncode == 0 def test_account_invoice_list(): - res = ( - exec_test_command( - BASE_CMD + ["invoices-list", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["account"] + ["invoices-list", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["billing_source", "tax", "subtotal"] @@ -150,35 +123,27 @@ def test_account_invoice_list(): @pytest.fixture def get_invoice_id(): - invoice_id = ( - exec_test_command( - BASE_CMD - + [ - "invoices-list", - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) + invoice_id = exec_test_command( + BASE_CMDS["account"] + + [ + "invoices-list", + "--text", + "--no-headers", + "--delimiter", + ",", + "--format", + "id", + ] + ).splitlines() first_id = invoice_id[0] yield first_id def test_account_invoice_view(get_invoice_id): invoice_id = get_invoice_id - res = ( - exec_test_command( - BASE_CMD + ["invoice-view", invoice_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["account"] + + ["invoice-view", invoice_id, "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -188,12 +153,9 @@ def test_account_invoice_view(get_invoice_id): def test_account_invoice_items(get_invoice_id): invoice_id = get_invoice_id - res = ( - exec_test_command( - BASE_CMD + ["invoice-items", invoice_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["account"] + + ["invoice-items", invoice_id, "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -202,10 +164,8 @@ def test_account_invoice_items(get_invoice_id): def test_account_logins_list(): - res = ( - exec_test_command(BASE_CMD + ["logins-list", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["account"] + ["logins-list", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["ip", "username", "status"] @@ -214,35 +174,27 @@ def test_account_logins_list(): @pytest.fixture def get_login_id(): - login_id = ( - exec_test_command( - BASE_CMD - + [ - "logins-list", - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) + login_id = exec_test_command( + BASE_CMDS["account"] + + [ + "logins-list", + "--text", + "--no-headers", + "--delimiter", + ",", + "--format", + "id", + ] + ).splitlines() first_id = login_id[0] yield first_id def test_account_login_view(get_login_id): login_id = get_login_id - res = ( - exec_test_command( - BASE_CMD + ["login-view", login_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["account"] + + ["login-view", login_id, "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -251,10 +203,8 @@ def test_account_login_view(get_login_id): def test_account_setting_view(): - res = ( - exec_test_command(BASE_CMD + ["settings", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["account"] + ["settings", "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -263,12 +213,8 @@ def test_account_setting_view(): def test_user_list(): - res = ( - exec_test_command( - ["linode-cli", "users", "list", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + ["linode-cli", "users", "list", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["email", "username"] @@ -277,35 +223,26 @@ def test_user_list(): @pytest.fixture def username(): - user_id = ( - exec_test_command( - [ - "linode-cli", - "users", - "list", - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "username", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) + user_id = exec_test_command( + [ + "linode-cli", + "users", + "list", + "--text", + "--no-headers", + "--delimiter", + ",", + "--format", + "username", + ] + ).splitlines() first_id = user_id[0].split(",")[0] yield first_id def test_user_view(username: str): - res = ( - exec_test_command( - ["linode-cli", "users", "view", username, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + ["linode-cli", "users", "view", username, "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -314,12 +251,8 @@ def test_user_view(username: str): def test_payment_method_list(): - res = ( - exec_test_command( - ["linode-cli", "payment-methods", "list", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + ["linode-cli", "payment-methods", "list", "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -328,12 +261,8 @@ def test_payment_method_list(): def test_payment_list(): - res = ( - exec_test_command( - BASE_CMD + ["payments-list", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["account"] + ["payments-list", "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -342,18 +271,14 @@ def test_payment_list(): def test_service_transfers(): - res = ( - exec_test_command( - [ - "linode-cli", - "service-transfers", - "list", - "--text", - "--delimiter=,", - ] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + [ + "linode-cli", + "service-transfers", + "list", + "--text", + "--delimiter=,", + ] ) lines = res.splitlines() @@ -362,12 +287,8 @@ def test_service_transfers(): def test_maintenance_list(): - res = ( - exec_test_command( - BASE_CMD + ["maintenance-list", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["account"] + ["maintenance-list", "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -376,12 +297,8 @@ def test_maintenance_list(): def test_notifications_list(): - res = ( - exec_test_command( - BASE_CMD + ["notifications-list", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["account"] + ["notifications-list", "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -390,12 +307,8 @@ def test_notifications_list(): def test_clients_list(): - res = ( - exec_test_command( - BASE_CMD + ["clients-list", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["account"] + ["clients-list", "--text", "--delimiter=,"] ) lines = res.splitlines() diff --git a/tests/integration/beta/helpers.py b/tests/integration/beta/helpers.py new file mode 100644 index 000000000..249289f1d --- /dev/null +++ b/tests/integration/beta/helpers.py @@ -0,0 +1,25 @@ +import pytest + +from tests.integration.helpers import ( + BASE_CMDS, + exec_test_command, +) + + +def get_beta_id(): + beta_ids = exec_test_command( + BASE_CMDS["betas"] + + [ + "list", + "--text", + "--no-headers", + "--delimiter", + ",", + "--format", + "id", + ] + ).splitlines() + if not beta_ids or beta_ids == [""]: + pytest.skip("No betas available to test.") + + return beta_ids[0] if beta_ids else None diff --git a/tests/integration/beta/test_beta_program.py b/tests/integration/beta/test_beta_program.py index 240272dff..5dbe7da08 100644 --- a/tests/integration/beta/test_beta_program.py +++ b/tests/integration/beta/test_beta_program.py @@ -1,15 +1,16 @@ import pytest -from tests.integration.helpers import assert_headers_in_lines, exec_test_command - -BASE_CMD = ["linode-cli", "betas"] +from tests.integration.beta.helpers import get_beta_id +from tests.integration.helpers import ( + BASE_CMDS, + assert_headers_in_lines, + exec_test_command, +) def test_beta_list(): - res = ( - exec_test_command(BASE_CMD + ["list", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["betas"] + ["list", "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -20,43 +21,13 @@ def test_beta_list(): assert_headers_in_lines(headers, lines) -@pytest.fixture -def get_beta_id(): - beta_ids = ( - exec_test_command( - BASE_CMD - + [ - "list", - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) - if not beta_ids or beta_ids == [""]: - pytest.skip("No betas available to test.") - - first_id = beta_ids[0] - yield first_id - - -def test_beta_view(get_beta_id): - beta_id = get_beta_id +def test_beta_view(): + beta_id = get_beta_id() if beta_id is None: pytest.skip("No beta program available to test") else: - res = ( - exec_test_command( - BASE_CMD + ["view", beta_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["betas"] + ["view", beta_id, "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["label", "description"] @@ -64,10 +35,8 @@ def test_beta_view(get_beta_id): def test_beta_enrolled(): - res = ( - exec_test_command(BASE_CMD + ["enrolled", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["betas"] + ["enrolled", "--text", "--delimiter=,"] ) lines = res.splitlines() diff --git a/tests/integration/cli/test_help.py b/tests/integration/cli/test_help.py index 3e08bd7d9..50b266f19 100644 --- a/tests/integration/cli/test_help.py +++ b/tests/integration/cli/test_help.py @@ -11,8 +11,7 @@ @pytest.mark.smoke def test_help_page_for_non_aliased_actions(): - process = exec_test_command(["linode-cli", "linodes", "list", "--help"]) - output = process.stdout.decode() + output = exec_test_command(["linode-cli", "linodes", "list", "--help"]) wrapped_output = textwrap.fill(output, width=180).replace("\n", "") assert contains_at_least_one_of( @@ -22,8 +21,8 @@ def test_help_page_for_non_aliased_actions(): assert contains_at_least_one_of( wrapped_output, [ - "API Documentation: https://techdocs.akamai.com/linode-api/reference/get-linode", - "API Documentation: https://techdocs.akamai.com/linode-api/reference/get-linode-instances", + "API Documentation:", + "https://techdocs.akamai.com/linode-api/reference/", ], ) @@ -33,8 +32,7 @@ def test_help_page_for_non_aliased_actions(): @pytest.mark.smoke def test_help_page_for_aliased_actions(): - process = exec_test_command(["linode-cli", "linodes", "ls", "--help"]) - output = process.stdout.decode() + output = exec_test_command(["linode-cli", "linodes", "ls", "--help"]) wrapped_output = textwrap.fill(output, width=180).replace("\n", "") assert contains_at_least_one_of( @@ -44,8 +42,8 @@ def test_help_page_for_aliased_actions(): assert contains_at_least_one_of( wrapped_output, [ - "API Documentation: https://techdocs.akamai.com/linode-api/reference/get-linode", - "API Documentation: https://techdocs.akamai.com/linode-api/reference/get-linode-instances", + "API Documentation:", + "https://techdocs.akamai.com/linode-api/reference/", ], ) @@ -72,7 +70,7 @@ def test_debug_output_contains_request_url(monkeypatch: pytest.MonkeyPatch): "12345", "--debug", ] - ).stderr.decode() + ) wrapped_output = textwrap.fill(output, width=180).replace("\n", "") assert ( diff --git a/tests/integration/cli/test_host_overrides.py b/tests/integration/cli/test_host_overrides.py index 9ccbf41da..421dcc192 100644 --- a/tests/integration/cli/test_host_overrides.py +++ b/tests/integration/cli/test_host_overrides.py @@ -9,10 +9,9 @@ def test_cli_command_fails_to_access_invalid_host(monkeypatch: MonkeyPatch): monkeypatch.setenv("LINODE_CLI_API_HOST", INVALID_HOST) - process = exec_failing_test_command( + output = exec_failing_test_command( ["linode-cli", "linodes", "ls"], ExitCodes.UNRECOGNIZED_COMMAND ) - output = process.stderr.decode() expected_output = ["Max retries exceeded with url:", "wrongapi.linode.com"] @@ -32,9 +31,8 @@ def test_cli_command_fails_to_access_invalid_api_scheme( monkeypatch: MonkeyPatch, ): monkeypatch.setenv("LINODE_CLI_API_SCHEME", "ssh") - process = exec_failing_test_command( + output = exec_failing_test_command( ["linode-cli", "linodes", "ls"], ExitCodes.UNRECOGNIZED_COMMAND ) - output = process.stderr.decode() assert "ssh://" in output diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 5e5ab8a96..389007f41 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -23,17 +23,6 @@ get_random_region_with_caps, get_random_text, ) -from tests.integration.linodes.helpers_linodes import ( - DEFAULT_LINODE_TYPE, - DEFAULT_RANDOM_PASS, - DEFAULT_REGION, - DEFAULT_TEST_IMAGE, - create_linode_and_wait, -) - -DOMAIN_BASE_CMD = ["linode-cli", "domains"] -LINODE_BASE_CMD = ["linode-cli", "linodes"] -NODEBALANCER_BASE_CMD = ["linode-cli", "nodebalancers"] @pytest.fixture(autouse=True, scope="session") @@ -106,7 +95,7 @@ def create_inbound_rule(ipv4_address, ipv6_address): if is_valid_ipv4(ipv4_address) or is_valid_ipv6(ipv6_address): command.extend(["--rules.inbound", inbound_rule]) - firewall_id = exec_test_command(command).stdout.decode().rstrip() + firewall_id = exec_test_command(command) yield firewall_id @@ -201,271 +190,6 @@ def _generate_test_files( return _generate_test_files -# test helper specific to Domains test suite -@pytest.fixture -def master_domain(): - timestamp = str(time.time_ns()) - - domain_id = ( - exec_test_command( - DOMAIN_BASE_CMD - + [ - "create", - "--type", - "master", - "--domain", - timestamp + "example.com", - "--soa_email", - "pthiel_test@linode.com", - "--text", - "--no-header", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield domain_id - - delete_target_id("domains", id=domain_id) - - -@pytest.fixture -def slave_domain(): - timestamp = str(time.time_ns()) - - domain_id = ( - exec_test_command( - DOMAIN_BASE_CMD - + [ - "create", - "--type", - "slave", - "--domain", - timestamp + "-example.com", - "--master_ips", - "1.1.1.1", - "--text", - "--no-header", - "--delimiter", - ",", - "--format=id", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield domain_id - - delete_target_id("domains", domain_id) - - -# Test helpers specific to Linodes test suite -@pytest.fixture -def linode_with_label(linode_cloud_firewall): - timestamp = str(time.time_ns()) - label = "cli" + timestamp - result = ( - exec_test_command( - LINODE_BASE_CMD - + [ - "create", - "--type", - "g6-nanode-1", - "--region", - "us-ord", - "--image", - DEFAULT_TEST_IMAGE, - "--label", - label, - "--root_pass", - DEFAULT_RANDOM_PASS, - "--firewall_id", - linode_cloud_firewall, - "--text", - "--delimiter", - ",", - "--no-headers", - "--format", - "label,region,type,image,id", - "--no-defaults", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield result - res_arr = result.split(",") - linode_id = res_arr[4] - delete_target_id(target="linodes", id=linode_id) - - -@pytest.fixture -def linode_min_req(linode_cloud_firewall): - result = ( - exec_test_command( - LINODE_BASE_CMD - + [ - "create", - "--type", - "g6-nanode-1", - "--region", - "us-ord", - "--root_pass", - DEFAULT_RANDOM_PASS, - "--firewall_id", - linode_cloud_firewall, - "--no-defaults", - "--text", - "--delimiter", - ",", - "--no-headers", - "--format", - "id,region,type", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield result - - res_arr = result.split(",") - linode_id = res_arr[0] - delete_target_id(target="linodes", id=linode_id) - - -@pytest.fixture -def linode_wo_image(linode_cloud_firewall): - label = "cli" + str(int(time.time()) + randint(10, 1000)) - linode_id = ( - exec_test_command( - LINODE_BASE_CMD - + [ - "create", - "--no-defaults", - "--label", - label, - "--type", - DEFAULT_LINODE_TYPE, - "--region", - DEFAULT_REGION, - "--root_pass", - DEFAULT_RANDOM_PASS, - "--firewall_id", - linode_cloud_firewall, - "--format", - "id", - "--no-headers", - "--text", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield linode_id - - delete_target_id(target="linodes", id=linode_id) - - -@pytest.fixture -def linode_backup_enabled(linode_cloud_firewall): - # create linode with backups enabled - linode_id = ( - exec_test_command( - [ - "linode-cli", - "linodes", - "create", - "--backups_enabled", - "true", - "--type", - DEFAULT_LINODE_TYPE, - "--region", - DEFAULT_REGION, - "--image", - DEFAULT_TEST_IMAGE, - "--root_pass", - DEFAULT_RANDOM_PASS, - "--firewall_id", - linode_cloud_firewall, - "--text", - "--no-headers", - "--format=id", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield linode_id - - delete_target_id("linodes", linode_id) - - -@pytest.fixture -def snapshot_of_linode(): - timestamp = str(time.time_ns()) - # get linode id after creation and wait for "running" status - linode_id = create_linode_and_wait() - new_snapshot_label = "test_snapshot" + timestamp - - result = exec_test_command( - LINODE_BASE_CMD - + [ - "snapshot", - linode_id, - "--label", - new_snapshot_label, - "--text", - "--delimiter", - ",", - "--no-headers", - ] - ).stdout.decode() - - yield linode_id, new_snapshot_label - - delete_target_id("linodes", linode_id) - - -# Test helpers specific to Nodebalancers test suite -@pytest.fixture -def nodebalancer_with_default_conf(linode_cloud_firewall): - result = ( - exec_test_command( - NODEBALANCER_BASE_CMD - + [ - "create", - "--region", - "us-ord", - "--firewall_id", - linode_cloud_firewall, - "--text", - "--delimiter", - ",", - "--format", - "id,label,region,hostname,client_conn_throttle", - "--suppress-warnings", - "--no-headers", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield result - - res_arr = result.split(",") - nodebalancer_id = res_arr[0] - delete_target_id(target="nodebalancers", id=nodebalancer_id) - - def create_vpc_w_subnet(): """ Creates and returns a VPC and a corresponding subnet. @@ -478,8 +202,8 @@ def create_vpc_w_subnet(): """ region = get_random_region_with_caps(required_capabilities=["VPCs"]) - vpc_label = str(time.time_ns()) + "label" - subnet_label = str(time.time_ns()) + "label" + vpc_label = get_random_text(5) + "label" + subnet_label = get_random_text(5) + "label" vpc_json = json.loads( exec_test_command( @@ -499,8 +223,6 @@ def create_vpc_w_subnet(): "--suppress-warnings", ] ) - .stdout.decode() - .rstrip() )[0] return vpc_json @@ -511,45 +233,3 @@ def pytest_configure(config): config.addinivalue_line( "markers", "smoke: mark test as part of smoke test suite" ) - - -@pytest.fixture -def support_test_linode_id(linode_cloud_firewall): - timestamp = str(time.time_ns()) - label = "cli" + timestamp - - res = ( - exec_test_command( - LINODE_BASE_CMD - + [ - "create", - "--type", - "g6-nanode-1", - "--region", - "us-mia", - "--image", - DEFAULT_TEST_IMAGE, - "--label", - label, - "--root_pass", - DEFAULT_RANDOM_PASS, - "--firewall_id", - linode_cloud_firewall, - "--text", - "--delimiter", - ",", - "--no-headers", - "--format", - "id", - "--no-defaults", - ] - ) - .stdout.decode() - .rstrip() - ) - - linode_id = res - - yield linode_id - - delete_target_id(target="linodes", id=linode_id) diff --git a/tests/integration/database/fixtures.py b/tests/integration/database/fixtures.py new file mode 100644 index 000000000..40212682d --- /dev/null +++ b/tests/integration/database/fixtures.py @@ -0,0 +1,78 @@ +import pytest + +from tests.integration.helpers import ( + BASE_CMDS, + delete_target_id, + exec_test_command, + get_random_text, +) + + +@pytest.fixture(scope="module") +def postgresql_cluster(): + postgresql_database_label = get_random_text(5) + "_postgresql" + + database_id = exec_test_command( + BASE_CMDS["databases"] + + [ + "postgresql-create", + "--type", + "g6-nanode-1", + "--region", + "us-ord", + "--label", + postgresql_database_label, + "--engine", + "postgresql/16", + "--text", + "--delimiter", + ",", + "--no-headers", + "--format", + "id", + "--no-defaults", + "--format", + "id", + ] + ) + + yield database_id + + delete_target_id( + target="databases", delete_command="postgresql-delete", id=database_id + ) + + +@pytest.fixture(scope="module") +def mysql_cluster(): + mysql_database_label = get_random_text(5) + "_mysql" + + database_id = exec_test_command( + BASE_CMDS["databases"] + + [ + "mysql-create", + "--type", + "g6-nanode-1", + "--region", + "us-ord", + "--label", + mysql_database_label, + "--engine", + "mysql/8", + "--text", + "--delimiter", + ",", + "--no-headers", + "--format", + "id", + "--no-defaults", + "--format", + "id", + ] + ) + + yield database_id + + delete_target_id( + target="databases", delete_command="mysql-delete", id=database_id + ) diff --git a/tests/integration/database/helpers.py b/tests/integration/database/helpers.py new file mode 100644 index 000000000..4ea37e2aa --- /dev/null +++ b/tests/integration/database/helpers.py @@ -0,0 +1,80 @@ +from tests.integration.helpers import ( + BASE_CMDS, + exec_test_command, +) + + +def get_db_type_id(): + db_type_ids = exec_test_command( + BASE_CMDS["databases"] + + [ + "types", + "--text", + "--no-headers", + "--delimiter", + ",", + "--format", + "id", + ] + ).splitlines() + return db_type_ids[0] if db_type_ids else None + + +def get_engine_id(): + engine_ids = exec_test_command( + BASE_CMDS["databases"] + + [ + "engines", + "--text", + "--no-headers", + "--delimiter", + ",", + "--format", + "id", + ] + ).splitlines() + return engine_ids[0] if engine_ids else None + + +def get_expected_keys_pg_engine_config(): + # Basic checks for pg config keys + return [ + "autovacuum_analyze_scale_factor", + "autovacuum_analyze_threshold", + "autovacuum_max_workers", + "autovacuum_naptime", + "autovacuum_vacuum_cost_delay", + "autovacuum_vacuum_cost_limit", + "autovacuum_vacuum_scale_factor", + "autovacuum_vacuum_threshold", + "bgwriter_delay", + "bgwriter_flush_after", + "bgwriter_lru_maxpages", + "bgwriter_lru_multiplier", + "deadlock_timeout", + "default_toast_compression", + "idle_in_transaction_session_timeout", + "jit", + "max_files_per_process", + "max_locks_per_transaction", + "max_logical_replication_workers", + "max_parallel_workers", + "max_parallel_workers_per_gather", + "max_pred_locks_per_transaction", + "max_replication_slots", + "max_slot_wal_keep_size", + "max_stack_depth", + "max_standby_archive_delay", + "max_standby_streaming_delay", + "max_wal_senders", + "max_worker_processes", + "password_encryption", + "temp_file_limit", + "timezone", + "track_activity_query_size", + "track_commit_timestamp", + "track_functions", + "track_io_timing", + "wal_sender_timeout", + "wal_writer_delay", + ] diff --git a/tests/integration/database/test_database.py b/tests/integration/database/test_database.py index 3fc9843bb..a80a4fe05 100644 --- a/tests/integration/database/test_database.py +++ b/tests/integration/database/test_database.py @@ -1,165 +1,59 @@ -import time - -import pytest - +from tests.integration.database.fixtures import ( # noqa: F401 + mysql_cluster, + postgresql_cluster, +) +from tests.integration.database.helpers import get_db_type_id, get_engine_id from tests.integration.helpers import ( + BASE_CMDS, assert_headers_in_lines, - delete_target_id, exec_test_command, ) -from tests.integration.linodes.helpers_linodes import DEFAULT_LABEL - -BASE_CMD = ["linode-cli", "databases"] def test_engines_list(): - res = ( - exec_test_command(BASE_CMD + ["engines", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["databases"] + ["engines", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["id", "engine", "version"] assert_headers_in_lines(headers, lines) -timestamp = str(time.time_ns()) -mysql_database_label = DEFAULT_LABEL + "-mysql-" + timestamp -postgresql_database_label = DEFAULT_LABEL + "-postgresql-" + timestamp - - -@pytest.fixture(scope="package", autouse=True) -def test_mysql_cluster(): - database_id = ( - exec_test_command( - BASE_CMD - + [ - "mysql-create", - "--type", - "g6-nanode-1", - "--region", - "us-ord", - "--label", - mysql_database_label, - "--engine", - "mysql/8", - "--text", - "--delimiter", - ",", - "--no-headers", - "--format", - "id", - "--no-defaults", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield database_id - - delete_target_id( - target="databases", delete_command="mysql-delete", id=database_id - ) - - -def test_mysql_suspend_resume(test_mysql_cluster): - database_id = test_mysql_cluster +def test_mysql_suspend_resume(mysql_cluster): + database_id = mysql_cluster res = exec_test_command( - BASE_CMD + ["mysql-suspend", database_id, "--text", "--delimiter=,"] - ).stdout.decode() + BASE_CMDS["databases"] + + ["mysql-suspend", database_id, "--text", "--delimiter=,"] + ) assert "Request failed: 400" not in res res = exec_test_command( - BASE_CMD + ["mysql-resume", database_id, "--text", "--delimiter=,"] - ).stdout.decode() - assert "Request failed: 400" not in res - - -@pytest.fixture(scope="package", autouse=True) -def test_postgresql_cluster(): - database_id = ( - exec_test_command( - BASE_CMD - + [ - "postgresql-create", - "--type", - "g6-nanode-1", - "--region", - "us-ord", - "--label", - postgresql_database_label, - "--engine", - "postgresql/16", - "--text", - "--delimiter", - ",", - "--no-headers", - "--format", - "id", - "--no-defaults", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield database_id - - delete_target_id( - target="databases", delete_command="postgresql-delete", id=database_id + BASE_CMDS["databases"] + + ["mysql-resume", database_id, "--text", "--delimiter=,"] ) + assert "Request failed: 400" not in res -def test_postgresql_suspend_resume(test_postgresql_cluster): - database_id = test_postgresql_cluster +def test_postgresql_suspend_resume(postgresql_cluster): + database_id = postgresql_cluster res = exec_test_command( - BASE_CMD + BASE_CMDS["databases"] + ["postgresql-suspend", database_id, "--text", "--delimiter=,"] - ).stdout.decode() + ) assert "Request failed: 400" not in res res = exec_test_command( - BASE_CMD + ["postgresql-resume", database_id, "--text", "--delimiter=,"] - ).stdout.decode() - assert "Request failed: 400" not in res - - -@pytest.fixture -def get_engine_id(): - engine_id = ( - exec_test_command( - BASE_CMD - + [ - "engines", - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() + BASE_CMDS["databases"] + + ["postgresql-resume", database_id, "--text", "--delimiter=,"] ) - first_id = engine_id[0] - yield first_id + assert "Request failed: 400" not in res -def test_engines_view(get_engine_id): - engine_id = get_engine_id - res = ( - exec_test_command( - BASE_CMD + ["engine-view", engine_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() +def test_engines_view(): + engine_id = get_engine_id() + res = exec_test_command( + BASE_CMDS["databases"] + + ["engine-view", engine_id, "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -169,10 +63,8 @@ def test_engines_view(get_engine_id): def test_databases_list(): - res = ( - exec_test_command(BASE_CMD + ["list", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["databases"] + ["list", "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -182,10 +74,8 @@ def test_databases_list(): def test_mysql_list(): - res = ( - exec_test_command(BASE_CMD + ["mysql-list", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["databases"] + ["mysql-list", "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -196,12 +86,8 @@ def test_mysql_list(): def test_postgresql_list(): - res = ( - exec_test_command( - BASE_CMD + ["postgresql-list", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["databases"] + ["postgresql-list", "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -212,47 +98,19 @@ def test_postgresql_list(): def test_databases_types(): - res = ( - exec_test_command(BASE_CMD + ["types", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["databases"] + ["types", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["id", "label", "_split"] assert_headers_in_lines(headers, lines) -@pytest.fixture -def get_node_id(): - node_id = ( - exec_test_command( - BASE_CMD - + [ - "types", - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) - first_id = node_id[0] - yield first_id - - -def test_databases_type_view(get_node_id): - node_id = get_node_id - res = ( - exec_test_command( - BASE_CMD + ["type-view", node_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() +def test_databases_type_view(): + db_type_id = get_db_type_id() + res = exec_test_command( + BASE_CMDS["databases"] + + ["type-view", db_type_id, "--text", "--delimiter=,"] ) lines = res.splitlines() diff --git a/tests/integration/database/test_database_engine_config.py b/tests/integration/database/test_database_engine_config.py index 3694e3726..973d23b4c 100644 --- a/tests/integration/database/test_database_engine_config.py +++ b/tests/integration/database/test_database_engine_config.py @@ -2,65 +2,23 @@ import pytest +from tests.integration.database.helpers import ( + get_expected_keys_pg_engine_config, +) from tests.integration.helpers import ( + BASE_CMDS, delete_target_id, exec_test_command, get_random_text, ) -BASE_CMD = ["linode-cli", "databases"] - - -def get_expected_keys_pg_engine_config(): - # Basic checks for pg config keys - return [ - "autovacuum_analyze_scale_factor", - "autovacuum_analyze_threshold", - "autovacuum_max_workers", - "autovacuum_naptime", - "autovacuum_vacuum_cost_delay", - "autovacuum_vacuum_cost_limit", - "autovacuum_vacuum_scale_factor", - "autovacuum_vacuum_threshold", - "bgwriter_delay", - "bgwriter_flush_after", - "bgwriter_lru_maxpages", - "bgwriter_lru_multiplier", - "deadlock_timeout", - "default_toast_compression", - "idle_in_transaction_session_timeout", - "jit", - "max_files_per_process", - "max_locks_per_transaction", - "max_logical_replication_workers", - "max_parallel_workers", - "max_parallel_workers_per_gather", - "max_pred_locks_per_transaction", - "max_replication_slots", - "max_slot_wal_keep_size", - "max_stack_depth", - "max_standby_archive_delay", - "max_standby_streaming_delay", - "max_wal_senders", - "max_worker_processes", - "password_encryption", - "temp_file_limit", - "timezone", - "track_activity_query_size", - "track_commit_timestamp", - "track_functions", - "track_io_timing", - "wal_sender_timeout", - "wal_writer_delay", - ] - @pytest.fixture(scope="session") def postgresql_db_engine_config(linode_cloud_firewall): label = get_random_text(5) + "-postgresql-db" database = json.loads( exec_test_command( - BASE_CMD + BASE_CMDS["databases"] + [ "postgresql-create", "--engine", @@ -170,8 +128,6 @@ def postgresql_db_engine_config(linode_cloud_firewall): "--json", ] ) - .stdout.decode() - .rstrip() )[0] yield database["id"] @@ -183,14 +139,12 @@ def postgresql_db_engine_config(linode_cloud_firewall): def test_postgresql_engine_config_view(): pg_configs = json.loads( exec_test_command( - BASE_CMD + BASE_CMDS["databases"] + [ "postgres-config-view", "--json", ] ) - .stdout.decode() - .rstrip() ) assert "pg" in pg_configs[0] @@ -263,14 +217,12 @@ def test_postgresql_list_with_engine_config(postgresql_db_engine_config): postgres_dbs = json.loads( exec_test_command( - BASE_CMD + BASE_CMDS["databases"] + [ "postgresql-list", "--json", ] ) - .stdout.decode() - .rstrip() ) # Find the DB with the matching ID @@ -303,15 +255,13 @@ def test_postgresql_db_engine_config_view(postgresql_db_engine_config): postgres_db_id = postgresql_db_engine_config postgres_db = json.loads( exec_test_command( - BASE_CMD + BASE_CMDS["databases"] + [ "postgresql-view", str(postgres_db_id), "--json", ] ) - .stdout.decode() - .rstrip() ) db = postgres_db[0] @@ -330,7 +280,7 @@ def test_postgresql_db_engine_config_update(postgresql_db_engine_config): postgres_db_id = postgresql_db_engine_config updated_db = json.loads( exec_test_command( - BASE_CMD + BASE_CMDS["databases"] + [ "postgresql-update", str(postgres_db_id), @@ -427,8 +377,6 @@ def test_postgresql_db_engine_config_update(postgresql_db_engine_config): "--json", ] ) - .stdout.decode() - .rstrip() ) db = updated_db[0] @@ -452,7 +400,7 @@ def mysql_db_engine_config(linode_cloud_firewall): label = get_random_text(5) + "-mysql-db" database = json.loads( exec_test_command( - BASE_CMD + BASE_CMDS["databases"] + [ "mysql-create", "--engine", @@ -522,8 +470,6 @@ def mysql_db_engine_config(linode_cloud_firewall): "--json", ] ) - .stdout.decode() - .rstrip() )[0] yield database["id"] @@ -537,14 +483,12 @@ def mysql_db_engine_config(linode_cloud_firewall): def test_mysql_engine_config_view(): mysql_config = json.loads( exec_test_command( - BASE_CMD + BASE_CMDS["databases"] + [ "mysql-config-view", "--json", ] ) - .stdout.decode() - .rstrip() ) assert "mysql" in mysql_config[0] @@ -576,14 +520,12 @@ def test_mysql_list_with_engine_config(mysql_db_engine_config): mysql_dbs = json.loads( exec_test_command( - BASE_CMD + BASE_CMDS["databases"] + [ "mysql-list", "--json", ] ) - .stdout.decode() - .rstrip() ) # Find the DB with the matching ID @@ -631,15 +573,13 @@ def test_mysql_db_view_with_engine_config(mysql_db_engine_config): mysql_db = json.loads( exec_test_command( - BASE_CMD + BASE_CMDS["databases"] + [ "mysql-view", str(mysql_db_id), "--json", ] ) - .stdout.decode() - .rstrip() ) db = mysql_db[0] @@ -667,7 +607,7 @@ def test_mysql_db_engine_config_update(mysql_db_engine_config): mysql_db = json.loads( exec_test_command( - BASE_CMD + BASE_CMDS["databases"] + [ "mysql-update", str(mysql_db_id), @@ -728,8 +668,6 @@ def test_mysql_db_engine_config_update(mysql_db_engine_config): "--json", ] ) - .stdout.decode() - .rstrip() )[0] # Assertions for updated values diff --git a/tests/integration/domains/fixtures.py b/tests/integration/domains/fixtures.py new file mode 100644 index 000000000..476e38042 --- /dev/null +++ b/tests/integration/domains/fixtures.py @@ -0,0 +1,100 @@ +import pytest + +from tests.integration.helpers import ( + BASE_CMDS, + delete_target_id, + exec_test_command, + get_random_text, +) + + +@pytest.fixture(scope="function") +def master_domain(): + domain_id = exec_test_command( + BASE_CMDS["domains"] + + [ + "create", + "--type", + "master", + "--domain", + get_random_text(5) + "-example.com", + "--soa_email", + "pthiel_test@linode.com", + "--text", + "--no-header", + "--format", + "id", + ] + ) + + yield domain_id + + delete_target_id("domains", id=domain_id) + + +@pytest.fixture(scope="function") +def slave_domain(): + domain_id = exec_test_command( + BASE_CMDS["domains"] + + [ + "create", + "--type", + "slave", + "--domain", + get_random_text(5) + "-example.com", + "--master_ips", + "1.1.1.1", + "--text", + "--no-header", + "--delimiter", + ",", + "--format=id", + ] + ) + + yield domain_id + + delete_target_id("domains", domain_id) + + +@pytest.fixture(scope="function") +def domain_and_record(): + # Create domain + domain_id = exec_test_command( + BASE_CMDS["domains"] + + [ + "create", + "--type", + "master", + "--domain", + get_random_text(5) + "-example.com", + "--soa_email=pthiel@linode.com", + "--text", + "--no-header", + "--format=id", + ] + ) + + # Create record + record_id = exec_test_command( + BASE_CMDS["domains"] + + [ + "records-create", + "--protocol=tcp", + "--type=SRV", + "--port=23", + "--priority=4", + "--service=telnet", + "--target=record-setup", + "--weight=4", + "--text", + "--no-header", + "--delimiter=,", + "--format=id", + domain_id, + ] + ) + + yield domain_id, record_id + + delete_target_id(target="domains", id=domain_id) diff --git a/tests/integration/domains/test_master_domains.py b/tests/integration/domains/test_domain_master.py similarity index 64% rename from tests/integration/domains/test_master_domains.py rename to tests/integration/domains/test_domain_master.py index ee990b1ab..67649d528 100644 --- a/tests/integration/domains/test_master_domains.py +++ b/tests/integration/domains/test_domain_master.py @@ -4,44 +4,15 @@ import pytest from linodecli.exit_codes import ExitCodes +from tests.integration.domains.fixtures import ( # noqa: F401 + master_domain, +) from tests.integration.helpers import ( - delete_target_id, + BASE_CMDS, exec_failing_test_command, exec_test_command, ) -BASE_CMD = ["linode-cli", "domains"] - - -@pytest.fixture -def master_test_domain(): - timestamp = str(time.time_ns()) - # Create domain - master_domain_id = ( - exec_test_command( - BASE_CMD - + [ - "create", - "--type", - "master", - "--domain", - "BC" + timestamp + "-example.com", - "--soa_email=pthiel" + timestamp + "@linode.com", - "--text", - "--no-header", - "--delimiter", - ",", - "--format=id", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield master_domain_id - - delete_target_id(target="domains", id=master_domain_id) - def test_create_domain_fails_without_spcified_type(): timestamp = str(time.time_ns()) @@ -50,7 +21,7 @@ def test_create_domain_fails_without_spcified_type(): # not all output from the linode-cli goes to stdout, stderr result = exec_failing_test_command( - BASE_CMD + BASE_CMDS["domains"] + [ "create", "--domain", @@ -61,7 +32,7 @@ def test_create_domain_fails_without_spcified_type(): "--no-headers", ], expected_code=ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) assert "Request failed: 400" in result assert "type is required" in result @@ -70,7 +41,7 @@ def test_create_domain_fails_without_spcified_type(): def test_create_master_domain_fails_without_soa_email(): timestamp = str(time.time_ns()) result = exec_failing_test_command( - BASE_CMD + BASE_CMDS["domains"] + [ "create", "--type", @@ -81,7 +52,7 @@ def test_create_master_domain_fails_without_soa_email(): "--no-headers", ], expected_code=ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) assert "Request failed: 400" in result assert "soa_email soa_email required when type=master" in result @@ -93,15 +64,15 @@ def test_create_master_domain(master_domain): assert re.search("[0-9]+", domain_id) -def test_update_master_domain_soa_email(master_test_domain): +def test_update_master_domain_soa_email(master_domain): # Remove --master_ips param when 872 is resolved timestamp = str(time.time_ns()) new_soa_email = "pthiel_new@linode.com" - domain_id = master_test_domain + domain_id = master_domain result = exec_test_command( - BASE_CMD + BASE_CMDS["domains"] + [ "update", domain_id, @@ -115,14 +86,14 @@ def test_update_master_domain_soa_email(master_test_domain): "--text", "--no-header", ] - ).stdout.decode() + ) assert new_soa_email in result -def test_list_master_domain(master_test_domain): +def test_list_master_domain(master_domain): result = exec_test_command( - BASE_CMD + BASE_CMDS["domains"] + [ "list", "--format=id,domain,type,status", @@ -131,14 +102,14 @@ def test_list_master_domain(master_test_domain): "--delimiter", ",", ] - ).stdout.decode() + ) - assert re.search("[0-9]+,BC[0-9]+-example.com,master,active", result) + assert re.search("master,active", result) -def test_show_domain_detail(master_test_domain): +def test_show_domain_detail(master_domain): result = exec_test_command( - BASE_CMD + BASE_CMDS["domains"] + [ "list", "--format=id,domain,type,status", @@ -147,6 +118,6 @@ def test_show_domain_detail(master_test_domain): "--delimiter", ",", ] - ).stdout.decode() + ) - assert re.search("[0-9]+,BC[0-9]+-example.com,master,active", result) + assert re.search("master,active", result) diff --git a/tests/integration/domains/test_domain_records.py b/tests/integration/domains/test_domain_records.py index 74eb1e9ff..59e307918 100644 --- a/tests/integration/domains/test_domain_records.py +++ b/tests/integration/domains/test_domain_records.py @@ -3,116 +3,63 @@ import pytest +from tests.integration.domains.fixtures import ( # noqa: F401 + domain_and_record, + master_domain, + slave_domain, +) from tests.integration.helpers import ( - SUCCESS_STATUS_CODE, + BASE_CMDS, contains_at_least_one_of, - delete_target_id, exec_test_command, ) -BASE_CMD = ["linode-cli", "domains"] - - -@pytest.fixture -def test_domain_and_record(): - timestamp = str(time.time_ns()) - # Create domain - domain_id = ( - exec_test_command( - BASE_CMD - + [ - "create", - "--type", - "master", - "--domain", - timestamp + "example.com", - "--soa_email=pthiel@linode.com", - "--text", - "--no-header", - "--format=id", - ] - ) - .stdout.decode() - .rstrip() - ) - - # Create record - record_id = ( - exec_test_command( - BASE_CMD - + [ - "records-create", - "--protocol=tcp", - "--type=SRV", - "--port=23", - "--priority=4", - "--service=telnet", - "--target=record-setup", - "--weight=4", - "--text", - "--no-header", - "--delimiter=,", - "--format=id", - domain_id, - ] - ) - .stdout.decode() - .rstrip() - ) - - yield domain_id, record_id - - delete_target_id(target="domains", id=domain_id) - @pytest.mark.smoke def test_create_a_domain(master_domain): # Current domain list - process = exec_test_command( - BASE_CMD + ["list", '--format="id"', "--text", "--no-header"] + domain_list_before = exec_test_command( + BASE_CMDS["domains"] + + ["list", '--format="id"', "--text", "--no-header"] ) - output_current = process.stdout.decode() timestamp = str(time.time_ns()) # Create domain - another_domain = ( - exec_test_command( - [ - "linode-cli", - "domains", - "create", - "--type", - "master", - "--domain", - timestamp + "example.com", - "--soa_email", - "pthiel_test@linode.com", - "--text", - "--no-header", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() + another_domain = exec_test_command( + [ + "linode-cli", + "domains", + "create", + "--type", + "master", + "--domain", + timestamp + "example.com", + "--soa_email", + "pthiel_test@linode.com", + "--text", + "--no-header", + "--format", + "id", + ] ) - process = exec_test_command( - BASE_CMD + ["list", "--format=id", "--text", "--no-header"] + domain_list_after = exec_test_command( + BASE_CMDS["domains"] + ["list", "--format=id", "--text", "--no-header"] ) - output_after = process.stdout.decode() - # Check if list is bigger than previous list - assert len(output_after.splitlines()) > len(output_current.splitlines()) + assert len(domain_list_after.splitlines()) > len( + domain_list_before.splitlines() + ) + assert another_domain in domain_list_after @pytest.mark.smoke -def test_create_domain_srv_record(test_domain_and_record): - domain_id = test_domain_and_record[0] +def test_create_domain_srv_record(domain_and_record): + domain_id = domain_and_record[0] - process = exec_test_command( - BASE_CMD + output = exec_test_command( + BASE_CMDS["domains"] + [ "records-create", "--protocol=tcp", @@ -129,18 +76,16 @@ def test_create_domain_srv_record(test_domain_and_record): ] ) - output = process.stdout.decode() - assert re.search( - r"[0-9]+,SRV,_telnet\._tcp,target-test-record\.\d+example\.com,0,4,4\n", + r"[0-9]+,SRV,_telnet\._tcp,target-test-record\.\w+-example\.com,0,4,4", str(output), ) -def test_list_srv_record(test_domain_and_record): - domain_id = test_domain_and_record[0] - process = exec_test_command( - BASE_CMD +def test_list_srv_record(domain_and_record): + domain_id = domain_and_record[0] + output = exec_test_command( + BASE_CMDS["domains"] + [ "records-list", domain_id, @@ -149,21 +94,20 @@ def test_list_srv_record(test_domain_and_record): "--delimiter=,", ] ) - output = process.stdout.decode() assert re.search( - r"[0-9]+,SRV,_telnet\._tcp,record-setup\.\d+example\.com,0,4,4\n", + r"[0-9]+,SRV,_telnet\._tcp,record-setup\.\w+-example\.com,0,4,4", str(output), ) @pytest.mark.smoke -def test_view_domain_record(test_domain_and_record): - domain_id = test_domain_and_record[0] - record_id = test_domain_and_record[1] +def test_view_domain_record(domain_and_record): + domain_id = domain_and_record[0] + record_id = domain_and_record[1] - process = exec_test_command( - BASE_CMD + output = exec_test_command( + BASE_CMDS["domains"] + [ "records-view", domain_id, @@ -173,20 +117,19 @@ def test_view_domain_record(test_domain_and_record): "--delimiter=,", ] ) - output = process.stdout.decode() assert re.search( - r"[0-9]+,SRV,_telnet\._tcp,record-setup\.\d+example\.com,0,4,4\n", + r"[0-9]+,SRV,_telnet\._tcp,record-setup\.\w+-example\.com,0,4,4", output, ) -def test_update_domain_record(test_domain_and_record): - domain_id = test_domain_and_record[0] - record_id = test_domain_and_record[1] +def test_update_domain_record(domain_and_record): + domain_id = domain_and_record[0] + record_id = domain_and_record[1] - process = exec_test_command( - BASE_CMD + output = exec_test_command( + BASE_CMDS["domains"] + [ "records-update", domain_id, @@ -197,35 +140,30 @@ def test_update_domain_record(test_domain_and_record): "--delimiter=,", ] ) - output = process.stdout.decode() assert re.search( - r"[0-9]+,SRV,_telnet\._tcp,record-setup-update\.\d+example\.com,0,4,4\n", + r"[0-9]+,SRV,_telnet\._tcp,record-setup-update\.\w+-example\.com,0,4,4", str(output), ) -def test_delete_a_domain_record(test_domain_and_record): - domain_id = test_domain_and_record[0] - record_id = test_domain_and_record[1] +def test_delete_a_domain_record(domain_and_record): + domain_id = domain_and_record[0] + record_id = domain_and_record[1] - process = exec_test_command( - BASE_CMD + ["records-delete", domain_id, record_id] + exec_test_command( + BASE_CMDS["domains"] + ["records-delete", domain_id, record_id] ) - # Assert on status code returned from deleting domain - assert process.returncode == SUCCESS_STATUS_CODE - -def test_help_records_list(test_domain_and_record): - process = exec_test_command( - BASE_CMD +def test_help_records_list(domain_and_record): + output = exec_test_command( + BASE_CMDS["domains"] + [ "records-list", "--help", ] ) - output = process.stdout.decode() assert contains_at_least_one_of( output, ["List domain records", "Domain Records List"] diff --git a/tests/integration/domains/test_domain_slave.py b/tests/integration/domains/test_domain_slave.py new file mode 100644 index 000000000..05da7e16e --- /dev/null +++ b/tests/integration/domains/test_domain_slave.py @@ -0,0 +1,62 @@ +import os +import re + +import pytest + +from tests.integration.domains.fixtures import ( # noqa: F401 + slave_domain, +) +from tests.integration.helpers import ( + BASE_CMDS, + exec_test_command, + get_random_text, +) + + +def test_create_slave_domain_fails_without_master_dns_server(): + os.system( + 'linode-cli domains create --type slave --domain "' + + get_random_text(5) + + '-example.com" --text --no-header 2>&1 | tee /tmp/test.txt' + ) + result = exec_test_command(["cat", "/tmp/test.txt"]) + + assert "Request failed: 400" in result + assert ( + "master_ips You need at least one master DNS server IP address for this zone." + in result + ) + + +@pytest.mark.smoke +def test_create_slave_domain(slave_domain): + domain_id = slave_domain + assert re.search("[0-9]+", domain_id) + + +def test_list_slave_domain(slave_domain): + result = exec_test_command( + BASE_CMDS["domains"] + ["list", "--text", "--no-header"] + ) + assert "-example.com" in result + + +def test_update_slave_domain(slave_domain): + domain_id = slave_domain + output = exec_test_command( + BASE_CMDS["domains"] + + [ + "update", + "--type", + "slave", + "--master_ips", + "8.8.8.8", + domain_id, + "--text", + "--no-header", + "--delimiter=,", + ] + ) + + assert "slave,active" in output + assert domain_id in output diff --git a/tests/integration/domains/test_domains_tags.py b/tests/integration/domains/test_domain_tags.py similarity index 54% rename from tests/integration/domains/test_domains_tags.py rename to tests/integration/domains/test_domain_tags.py index 6cdfc8938..22dde78fc 100644 --- a/tests/integration/domains/test_domains_tags.py +++ b/tests/integration/domains/test_domain_tags.py @@ -1,35 +1,31 @@ import re -import time import pytest from linodecli.exit_codes import ExitCodes from tests.integration.helpers import ( - delete_tag, + BASE_CMDS, delete_target_id, exec_failing_test_command, exec_test_command, + get_random_text, ) -BASE_CMD = ["linode-cli", "domains"] - -# @pytest.mark.skip(reason="BUG 943") def test_fail_to_create_master_domain_with_invalid_tags(): - timestamp = str(time.time_ns()) bad_tag = ( "a" * 300 ) # Tag validation rules changed — '*' is no longer rejected exec_failing_test_command( - BASE_CMD + BASE_CMDS["domains"] + [ "create", "--type", "master", "--domain", - timestamp + "example.com", - "--soa_email=" + timestamp + "pthiel@linode.com", + get_random_text(5) + "example.com", + "--soa_email=" + get_random_text(5) + "pthiel@linode.com", "--text", "--no-header", "--format=id", @@ -40,20 +36,18 @@ def test_fail_to_create_master_domain_with_invalid_tags(): ) -# @pytest.mark.skip(reason="BUG 943") def test_fail_to_create_slave_domain_with_invalid_tags(): - timestamp = str(time.time_ns()) bad_tag = "*" exec_failing_test_command( - BASE_CMD + BASE_CMDS["domains"] + [ "create", "--type", "slave", "--domain", - timestamp + "example.com", - "--soa_email=" + timestamp + "pthiel@linode.com", + get_random_text(5) + "example.com", + "--soa_email=" + get_random_text(5) + "pthiel@linode.com", "--text", "--no-header", "--format=id", @@ -66,18 +60,17 @@ def test_fail_to_create_slave_domain_with_invalid_tags(): @pytest.mark.smoke def test_create_master_domain_with_tags(): - timestamp = str(time.time_ns()) tag = "foo" - process = exec_test_command( - BASE_CMD + output = exec_test_command( + BASE_CMDS["domains"] + [ "create", "--type", "master", "--domain", - timestamp + "-example.com", - "--soa_email=" + timestamp + "pthiel@linode.com", + get_random_text(5) + "-example.com", + "--soa_email=" + get_random_text(5) + "pthiel@linode.com", "--text", "--no-header", "--delimiter=,", @@ -86,43 +79,39 @@ def test_create_master_domain_with_tags(): tag, ] ) - output = process.stdout.decode().rstrip() - assert re.search("[0-9]+,[0-9]+-example.com,master,active," + tag, output) + + assert re.search( + r"\d+,[^,]*-example\.com,master,active," + re.escape(tag), output + ) res_arr = output.split(",") domain_id = res_arr[0] delete_target_id(target="domains", id=domain_id) -# @pytest.mark.skip(reason="BUG 943") def test_delete_domain_and_tag(): - timestamp = str(int(time.time())) tag = "zoo" - domain_id = ( - exec_test_command( - BASE_CMD - + [ - "create", - "--type", - "master", - "--domain", - timestamp + "-example.com", - "--soa_email=" + timestamp + "pthiel@linode.com", - "--text", - "--no-header", - "--delimiter=,", - "--format=id", - "--tag", - tag, - ] - ) - .stdout.decode() - .rstrip() + domain_id = exec_test_command( + BASE_CMDS["domains"] + + [ + "create", + "--type", + "master", + "--domain", + get_random_text(5) + "-example.com", + "--soa_email=" + get_random_text(5) + "pthiel@linode.com", + "--text", + "--no-header", + "--delimiter=,", + "--format=id", + "--tag", + tag, + ] ) # need to check if tag foo is still present while running this test - result = exec_test_command(["linode-cli", "tags", "list"]).stdout.decode() + result = exec_test_command(["linode-cli", "tags", "list"]) if "zoo" in result: - delete_tag("zoo") + delete_target_id(target="tags", id="zoo") delete_target_id(target="domains", id=domain_id) diff --git a/tests/integration/domains/test_slave_domains.py b/tests/integration/domains/test_slave_domains.py deleted file mode 100644 index 15fbeed31..000000000 --- a/tests/integration/domains/test_slave_domains.py +++ /dev/null @@ -1,105 +0,0 @@ -import os -import re -import time - -import pytest - -from tests.integration.helpers import ( - FAILED_STATUS_CODE, - SUCCESS_STATUS_CODE, - delete_target_id, - exec_test_command, -) - -BASE_CMD = ["linode-cli", "domains"] -timestamp = str(time.time_ns()) - - -@pytest.fixture -def slave_domain_setup(): - # Create domain - slave_domain_id = ( - exec_test_command( - BASE_CMD - + [ - "create", - "--type", - "slave", - "--domain", - timestamp + "-example.com", - "--master_ips", - "1.1.1.1", - "--text", - "--no-header", - "--delimiter", - ",", - "--format=id", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield slave_domain_id - - delete_target_id(target="domains", id=slave_domain_id) - - -def test_create_slave_domain_fails_without_master_dns_server(): - os.system( - 'linode-cli domains create --type slave --domain "' - + timestamp - + '-example.com" --text --no-header 2>&1 | tee /tmp/test.txt' - ) - result = exec_test_command(["cat", "/tmp/test.txt"]).stdout.decode() - - assert "Request failed: 400" in result - assert ( - "master_ips You need at least one master DNS server IP address for this zone." - in result - ) - - -@pytest.mark.smoke -def test_create_slave_domain(slave_domain): - domain_id = slave_domain - assert re.search("[0-9]+", domain_id) - - -def test_list_slave_domain(slave_domain): - result = exec_test_command( - BASE_CMD + ["list", "--text", "--no-header"] - ).stdout.decode() - assert "-example.com" in result - - -@pytest.mark.skip(reason="BUG 872") -def test_update_domain_fails_without_type(slave_domain_setup): - domain_id = slave_domain_setup - - result = os.system( - "linode-cli domains update " - + domain_id - + ' --master_ips 8.8.8.8 --text --no-header --deleteimiter "," --format "id,domain,type,status"' - ) - - assert result == FAILED_STATUS_CODE - - -def test_update_slave_domain(slave_domain_setup): - domain_id = slave_domain_setup - result = exec_test_command( - BASE_CMD - + [ - "update", - "--type", - "slave", - "--master_ips", - "8.8.8.8", - domain_id, - "--text", - "--no-header", - ] - ) - - assert result.returncode == SUCCESS_STATUS_CODE diff --git a/tests/integration/events/fixtures.py b/tests/integration/events/fixtures.py new file mode 100644 index 000000000..0c9b76478 --- /dev/null +++ b/tests/integration/events/fixtures.py @@ -0,0 +1,32 @@ +import pytest + +from tests.integration.helpers import ( + BASE_CMDS, + delete_target_id, + exec_test_command, + get_random_text, +) + + +@pytest.fixture(scope="function") +def events_create_domain(): + # Create domain + domain_id = exec_test_command( + BASE_CMDS["domains"] + + [ + "create", + "--type", + "master", + "--domain", + "A" + get_random_text(5) + "example.com", + "--soa_email=developer-test@linode.com", + "--text", + "--no-header", + "--format", + "id", + ] + ) + + yield domain_id + + delete_target_id(target="domains", id=domain_id) diff --git a/tests/integration/events/test_events.py b/tests/integration/events/test_events.py index 066f272f4..67a1d3de7 100644 --- a/tests/integration/events/test_events.py +++ b/tests/integration/events/test_events.py @@ -1,46 +1,15 @@ import re -import time import pytest -from tests.integration.helpers import delete_target_id, exec_test_command - -BASE_CMD = ["linode-cli", "events"] - - -@pytest.fixture -def events_test_domain_id(): - timestamp = str(time.time_ns()) - # Create domain - domain_id = ( - exec_test_command( - [ - "linode-cli", - "domains", - "create", - "--type", - "master", - "--domain", - "A" + timestamp + "example.com", - "--soa_email=developer-test@linode.com", - "--text", - "--no-header", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield domain_id - - delete_target_id(target="domains", id=domain_id) +from tests.integration.events.fixtures import ( # noqa: F401 + events_create_domain, +) +from tests.integration.helpers import BASE_CMDS, exec_test_command def test_print_events_usage_information(): - process = exec_test_command(BASE_CMD) - output = process.stdout.decode() + output = exec_test_command(BASE_CMDS["events"]) assert "linode-cli events [ACTION]" in output @@ -56,124 +25,102 @@ def test_print_events_usage_information(): @pytest.mark.smoke def test_list_events(): - process = exec_test_command( - BASE_CMD + ["list", "--text", "--no-headers", "--delimiter", ","] + output = exec_test_command( + BASE_CMDS["events"] + + ["list", "--text", "--no-headers", "--delimiter", ","] ) - output = process.stdout.decode() assert re.search("[0-9]+,.*,.*,[0-9]+-[0-9][0-9]-.*,.*,[a-z]+.*", output) def test_view_events(): - event_id = ( - exec_test_command( - [ - "linode-cli", - "events", - "list", - "--format", - "id", - "--no-headers", - "--text", - ] - ) - .stdout.decode() - .rstrip() - .split()[0] - ) + event_id = exec_test_command( + BASE_CMDS["events"] + + [ + "list", + "--format", + "id", + "--no-headers", + "--text", + ] + ).split()[0] result = exec_test_command( - BASE_CMD + BASE_CMDS["events"] + ["view", event_id, "--text", "--no-headers", "--delimiter", ","] - ).stdout.decode() + ) assert re.search("[0-9]+,.*,.*,[0-9]+-[0-9][0-9]-.*,.*,[a-z]+.*", result) def test_mark_event_seen(): - event_id = ( - exec_test_command( - [ - "linode-cli", - "events", - "list", - "--format", - "id", - "--no-headers", - "--text", - ] - ) - .stdout.decode() - .rstrip() - .split()[0] - ) + event_id = exec_test_command( + BASE_CMDS["events"] + + [ + "list", + "--format", + "id", + "--no-headers", + "--text", + ] + ).split()[0] # mark event as seen exec_test_command( - BASE_CMD + BASE_CMDS["events"] + ["mark-seen", event_id, "--text", "--no-headers", "--delimiter", ","] - ).stdout.decode() + ) # view event result = exec_test_command( - BASE_CMD + BASE_CMDS["events"] + ["view", event_id, "--text", "--no-headers", "--delimiter", ","] - ).stdout.decode() + ) assert re.search("[0-9]+,.*,.*,[0-9]+-[0-9][0-9]-.*,.*,[a-z]+.*", result) @pytest.mark.smoke def test_mark_event_read(): - event_id = ( - exec_test_command( - [ - "linode-cli", - "events", - "list", - "--format", - "id", - "--no-headers", - "--text", - ] - ) - .stdout.decode() - .rstrip() - .split()[0] - ) + event_id = exec_test_command( + [ + "linode-cli", + "events", + "list", + "--format", + "id", + "--no-headers", + "--text", + ] + ).split()[0] # mark event as read exec_test_command( - BASE_CMD + BASE_CMDS["events"] + ["mark-read", event_id, "--text", "--no-headers", "--delimiter", ","] - ).stdout.decode() + ) # view event result = exec_test_command( - BASE_CMD + BASE_CMDS["events"] + ["view", event_id, "--text", "--no-headers", "--delimiter", ","] - ).stdout.decode() + ) assert re.search("[0-9]+,.*,.*,[0-9]+-[0-9][0-9]-.*,.*,[a-z]+.*", result) def test_filter_events_by_entity_id(): - event_id = ( - exec_test_command( - [ - "linode-cli", - "events", - "list", - "--format", - "id", - "--no-headers", - "--text", - ] - ) - .stdout.decode() - .rstrip() - .split()[0] - ) + event_id = exec_test_command( + [ + "linode-cli", + "events", + "list", + "--format", + "id", + "--no-headers", + "--text", + ] + ).split()[0] result = exec_test_command( - BASE_CMD + BASE_CMDS["events"] + [ "list", "--id", @@ -183,17 +130,16 @@ def test_filter_events_by_entity_id(): "--delimiter", ",", ] - ).stdout.decode() + ) assert re.search( event_id + ",.*,.*,[0-9]+-[0-9][0-9]-.*,.*,[a-z]+.*", result ) -@pytest.mark.skip(reason="https://github.com/linode/linode-cli/issues/500") -def test_create_domain_and_filter_domain_events(events_test_domain_id): - domain_id = events_test_domain_id - result = exec_test_command( - BASE_CMD +def test_create_domain_and_filter_domain_events(events_create_domain): + domain_id = events_create_domain + output = exec_test_command( + BASE_CMDS["events"] + [ "list", "--entity.id", @@ -205,8 +151,6 @@ def test_create_domain_and_filter_domain_events(events_test_domain_id): "--delimiter", ",", ] - ).stdout.decode() - - assert re.search( - "[0-9]+,.*,domain_create,A[0-9]+.*,[0-9]+-[0-9][0-9]-.*,.*", result ) + + assert "domain_create" in output diff --git a/tests/integration/firewalls/conftest.py b/tests/integration/firewalls/conftest.py index bd705fff7..3d8fc74a1 100644 --- a/tests/integration/firewalls/conftest.py +++ b/tests/integration/firewalls/conftest.py @@ -3,13 +3,12 @@ import pytest from tests.integration.helpers import ( + BASE_CMDS, delete_target_id, exec_test_command, get_random_text, ) -BASE_CMD = ["linode-cli", "firewalls"] - @pytest.fixture(scope="function") def _firewall_id_and_label(): @@ -17,7 +16,7 @@ def _firewall_id_and_label(): label = "fw-" + get_random_text(5) # create it and capture the ID result = exec_test_command( - BASE_CMD + BASE_CMDS["firewalls"] + [ "create", "--label", @@ -32,7 +31,7 @@ def _firewall_id_and_label(): "id", ] ) - fw_id = result.stdout.decode().strip() + fw_id = result yield fw_id, label # cleanup delete_target_id(target="firewalls", id=fw_id) @@ -53,8 +52,10 @@ def test_firewall_label(_firewall_id_and_label): @pytest.fixture def restore_firewall_defaults(): # Fetch and store current default firewall settings - result = exec_test_command(BASE_CMD + ["firewall-settings-list", "--json"]) - settings = json.loads(result.stdout.decode()) + result = exec_test_command( + BASE_CMDS["firewalls"] + ["firewall-settings-list", "--json"] + ) + settings = json.loads(result) original_defaults = settings[0]["default_firewall_ids"] yield original_defaults @@ -66,4 +67,6 @@ def restore_firewall_defaults(): args.extend([f"--default_firewall_ids.{key}", str(val)]) if args: - exec_test_command(BASE_CMD + ["firewall-settings-update"] + args) + exec_test_command( + BASE_CMDS["firewalls"] + ["firewall-settings-update"] + args + ) diff --git a/tests/integration/firewalls/fixtures.py b/tests/integration/firewalls/fixtures.py new file mode 100644 index 000000000..72559555f --- /dev/null +++ b/tests/integration/firewalls/fixtures.py @@ -0,0 +1,34 @@ +import pytest + +from tests.integration.helpers import ( + BASE_CMDS, + delete_target_id, + exec_test_command, + get_random_text, +) + +FIREWALL_LABEL = "label-fw-test-" + get_random_text(5) + + +@pytest.fixture(scope="session") +def firewall_id(): + firewall_id = exec_test_command( + BASE_CMDS["firewalls"] + + [ + "create", + "--label", + FIREWALL_LABEL, + "--rules.outbound_policy", + "ACCEPT", + "--rules.inbound_policy", + "DROP", + "--text", + "--no-headers", + "--format", + "id", + ] + ) + + yield firewall_id + + delete_target_id(target="firewalls", id=firewall_id) diff --git a/tests/integration/firewalls/test_firewall_settings.py b/tests/integration/firewalls/test_firewall_settings.py index 98ef6ba35..c89ecab4b 100644 --- a/tests/integration/firewalls/test_firewall_settings.py +++ b/tests/integration/firewalls/test_firewall_settings.py @@ -3,20 +3,16 @@ import pytest from tests.integration.helpers import ( + BASE_CMDS, exec_test_command, ) -BASE_CMD = ["linode-cli", "firewalls"] - def test_firewall_settings_defaults(test_firewall_id, test_firewall_label): # list all firewalls and extract the IDs - list_result = ( - exec_test_command( - BASE_CMD + ["list", "--no-headers", "--text", "--delimiter", ","] - ) - .stdout.decode() - .strip() + list_result = exec_test_command( + BASE_CMDS["firewalls"] + + ["list", "--no-headers", "--text", "--delimiter", ","] ) firewall_lines = list_result.splitlines() @@ -30,8 +26,8 @@ def test_firewall_settings_defaults(test_firewall_id, test_firewall_label): # get the default firewall settings settings_result = exec_test_command( - BASE_CMD + ["firewall-settings-list", "--json"] - ).stdout.decode() + BASE_CMDS["firewalls"] + ["firewall-settings-list", "--json"] + ) settings = json.loads(settings_result) assert ( @@ -65,8 +61,8 @@ def test_update_firewall_defaults(test_firewall_id, restore_firewall_defaults): # Fetch current default firewall settings settings = json.loads( exec_test_command( - BASE_CMD + ["firewall-settings-list", "--json"] - ).stdout.decode() + BASE_CMDS["firewalls"] + ["firewall-settings-list", "--json"] + ) ) default_ids_before = settings[0]["default_firewall_ids"] @@ -81,12 +77,9 @@ def test_update_firewall_defaults(test_firewall_id, restore_firewall_defaults): ) # List all firewalls - firewall_list = ( - exec_test_command( - BASE_CMD + ["list", "--no-headers", "--text", "--delimiter", ","] - ) - .stdout.decode() - .strip() + firewall_list = exec_test_command( + BASE_CMDS["firewalls"] + + ["list", "--no-headers", "--text", "--delimiter", ","] ) firewall_ids = [ @@ -107,7 +100,7 @@ def test_update_firewall_defaults(test_firewall_id, restore_firewall_defaults): # Update all default firewall IDs to the new one exec_test_command( - BASE_CMD + BASE_CMDS["firewalls"] + [ "firewall-settings-update", "--default_firewall_ids.linode", @@ -125,8 +118,8 @@ def test_update_firewall_defaults(test_firewall_id, restore_firewall_defaults): # Verify update updated_settings = json.loads( exec_test_command( - BASE_CMD + ["firewall-settings-list", "--json"] - ).stdout.decode() + BASE_CMDS["firewalls"] + ["firewall-settings-list", "--json"] + ) )[0]["default_firewall_ids"] for key, val in updated_settings.items(): diff --git a/tests/integration/firewalls/test_firewalls.py b/tests/integration/firewalls/test_firewalls.py index 15740f413..1cd5b9c44 100644 --- a/tests/integration/firewalls/test_firewalls.py +++ b/tests/integration/firewalls/test_firewalls.py @@ -1,81 +1,71 @@ import json import re -import time import pytest from pytest import MonkeyPatch from linodecli.exit_codes import ExitCodes +from tests.integration.firewalls.fixtures import ( # noqa: F401 + FIREWALL_LABEL, + firewall_id, +) from tests.integration.helpers import ( + BASE_CMDS, delete_target_id, exec_failing_test_command, exec_test_command, + get_random_text, ) -BASE_CMD = ["linode-cli", "firewalls"] - @pytest.mark.smoke -def test_view_firewall(test_firewall_id, test_firewall_label): - fw_id = test_firewall_id - fw_label = test_firewall_label - - result = ( - exec_test_command( - BASE_CMD - + [ - "view", - fw_id, - "--no-headers", - "--text", - "--delimiter", - ",", - ] - ) - .stdout.decode() - .rstrip() +def test_view_firewall(firewall_id): + firewall_id = firewall_id + + result = exec_test_command( + BASE_CMDS["firewalls"] + + [ + "view", + firewall_id, + "--no-headers", + "--text", + "--delimiter", + ",", + ] ) - assert f"{fw_id},{fw_label},enabled" in result + assert re.search(firewall_id + "," + FIREWALL_LABEL + ",enabled", result) -def test_list_firewall(test_firewall_id, test_firewall_label): - fw_id = test_firewall_id - fw_label = test_firewall_label - result = ( - exec_test_command( - BASE_CMD + ["list", "--no-headers", "--text", "--delimiter", ","] - ) - .stdout.decode() - .rstrip() +def test_list_firewall(firewall_id): + firewall_id = firewall_id + + result = exec_test_command( + BASE_CMDS["firewalls"] + + ["list", "--no-headers", "--text", "--delimiter", ","] ) - assert f"{fw_id},{fw_label},enabled" in result + assert re.search(firewall_id + "," + FIREWALL_LABEL + ",enabled", result) @pytest.mark.smoke def test_create_firewall_with_minimum_required_args(): - timestamp = str(time.time_ns()) - firewall_label = "label-fw-test" + timestamp - result = ( - exec_test_command( - BASE_CMD - + [ - "create", - "--label", - firewall_label, - "--rules.outbound_policy", - "ACCEPT", - "--rules.inbound_policy", - "DROP", - "--text", - "--no-headers", - "--delimiter", - ",", - ] - ) - .stdout.decode() - .rstrip() + firewall_label = "label-fw-test" + get_random_text(5) + result = exec_test_command( + BASE_CMDS["firewalls"] + + [ + "create", + "--label", + firewall_label, + "--rules.outbound_policy", + "ACCEPT", + "--rules.inbound_policy", + "DROP", + "--text", + "--no-headers", + "--delimiter", + ",", + ] ) assert re.search("[0-9]+," + firewall_label + ",enabled", result) @@ -86,110 +76,90 @@ def test_create_firewall_with_minimum_required_args(): def test_fails_to_create_firewall_without_inbound_policy(): - timestamp = str(time.time_ns()) - firewall_label = "fw_label" + timestamp - result = ( - exec_failing_test_command( - BASE_CMD - + [ - "create", - "--label", - firewall_label, - "--rules.outbound_policy", - "ACCEPT", - "--text", - "--no-headers", - "--format", - "id", - ], - ExitCodes.REQUEST_FAILED, - ) - .stderr.decode() - .rstrip() + firewall_label = "fw_label" + get_random_text(5) + result = exec_failing_test_command( + BASE_CMDS["firewalls"] + + [ + "create", + "--label", + firewall_label, + "--rules.outbound_policy", + "ACCEPT", + "--text", + "--no-headers", + "--format", + "id", + ], + ExitCodes.REQUEST_FAILED, ) assert "inbound_policy is required" in result def test_fails_to_create_firewall_without_outbound_policy(): - timestamp = str(time.time_ns()) - firewall_label = "fw_label" + timestamp - result = ( - exec_failing_test_command( - BASE_CMD - + [ - "create", - "--label", - firewall_label, - "--rules.inbound_policy", - "DROP", - "--text", - "--no-headers", - "--format", - "id", - ], - ExitCodes.REQUEST_FAILED, - ) - .stderr.decode() - .rstrip() + firewall_label = "fw_label" + get_random_text(5) + result = exec_failing_test_command( + BASE_CMDS["firewalls"] + + [ + "create", + "--label", + firewall_label, + "--rules.inbound_policy", + "DROP", + "--text", + "--no-headers", + "--format", + "id", + ], + ExitCodes.REQUEST_FAILED, ) assert "outbound_policy is required" in result -def test_firewall_label_must_be_unique_upon_creation(test_firewall_label): - fw_label = test_firewall_label - result = ( - exec_failing_test_command( - BASE_CMD - + [ - "create", - "--label", - fw_label, - "--rules.outbound_policy", - "ACCEPT", - "--rules.inbound_policy", - "DROP", - "--text", - "--no-headers", - "--format", - "id", - ], - ExitCodes.REQUEST_FAILED, - ) - .stderr.decode() - .rstrip() +def test_firewall_label_must_be_unique_upon_creation(firewall_id): + result = exec_failing_test_command( + BASE_CMDS["firewalls"] + + [ + "create", + "--label", + FIREWALL_LABEL, + "--rules.outbound_policy", + "ACCEPT", + "--rules.inbound_policy", + "DROP", + "--text", + "--no-headers", + "--format", + "id", + ], + ExitCodes.REQUEST_FAILED, ) assert "Label must be unique among your Cloud Firewalls" in result def test_create_firewall_with_inbound_and_outbound_args(): - timestamp = str(time.time_ns()) - firewall_label = "label-fw-test" + timestamp - result = ( - exec_test_command( - BASE_CMD - + [ - "create", - "--label", - firewall_label, - "--rules.outbound_policy", - "ACCEPT", - "--rules.inbound_policy", - "DROP", - "--rules.inbound", - '[{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.2/32"]}, "action": "ACCEPT", "label": "accept-inbound-SSH"}]', - "--rules.outbound", - '[{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.1/32"]}, "action": "ACCEPT", "label": "accept-outbound-SSH"}]', - "--text", - "--no-headers", - "--delimiter", - ",", - ] - ) - .stdout.decode() - .rstrip() + firewall_label = "label-fw-test" + get_random_text(5) + result = exec_test_command( + BASE_CMDS["firewalls"] + + [ + "create", + "--label", + firewall_label, + "--rules.outbound_policy", + "ACCEPT", + "--rules.inbound_policy", + "DROP", + "--rules.inbound", + '[{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.2/32"]}, "action": "ACCEPT", "label": "accept-inbound-SSH"}]', + "--rules.outbound", + '[{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.1/32"]}, "action": "ACCEPT", "label": "accept-outbound-SSH"}]', + "--text", + "--no-headers", + "--delimiter", + ",", + ] ) assert re.search("[0-9]+," + firewall_label + ",enabled", result) @@ -199,43 +169,69 @@ def test_create_firewall_with_inbound_and_outbound_args(): delete_target_id(target="firewalls", id=firewall_id) -def test_update_firewall(test_firewall_id): - timestamp = str(time.time_ns()) - firewall_id = test_firewall_id - updated_tag = "updated-tag" + timestamp - updated_label = "updated-" + timestamp - - result = ( - exec_test_command( - BASE_CMD - + [ - "update", - firewall_id, - "--tags", - updated_tag, - "--label", - updated_label, - "--status", - "enabled", - "--no-headers", - "--text", - "--delimiter", - ",", - ] - ) - .stdout.decode() - .rstrip() +def test_update_firewall(firewall_id): + firewall_id = firewall_id + updated_tag = "updated-tag" + get_random_text(5) + updated_label = "updated-" + get_random_text(5) + + result = exec_test_command( + BASE_CMDS["firewalls"] + + [ + "update", + firewall_id, + "--tags", + updated_tag, + "--label", + updated_label, + "--status", + "enabled", + "--no-headers", + "--text", + "--delimiter", + ",", + ] ) assert re.search(firewall_id + "," + updated_label + ",enabled", result) +@pytest.mark.skip("skip until there is a way to delete default firewall") +def test_firewall_settings_update_and_list(test_firewall_id): + for cmd in [ + BASE_CMDS["firewalls"] + + [ + "firewall-settings-update", + "--default_firewall_ids.vpc_interfac", + test_firewall_id, + "--default_firewall_ids.public_interface", + test_firewall_id, + "--default_firewall_ids.nodebalancer", + test_firewall_id, + "--default_firewall_ids.linode", + test_firewall_id, + "--json", + ], + BASE_CMDS["firewalls"] + + [ + "firewall-settings-list", + "--json", + ], + ]: + data = json.loads(exec_test_command(cmd).stdout.decode().rstrip()) + firewall_ids = data[0]["default_firewall_ids"] + for key in [ + "linode", + "nodebalancer", + "public_interface", + "vpc_interface", + ]: + assert firewall_ids[key] == int(test_firewall_id) + + def test_firewall_templates_list(monkeypatch: MonkeyPatch): monkeypatch.setenv("LINODE_CLI_API_VERSION", "v4beta") data = json.loads( - exec_test_command(BASE_CMD + ["templates-list", "--json"]) - .stdout.decode() - .rstrip() + exec_test_command(BASE_CMDS["firewalls"] + ["templates-list", "--json"]) ) slugs = {template["slug"] for template in data} @@ -263,9 +259,9 @@ def test_firewall_template_view(monkeypatch: MonkeyPatch): monkeypatch.setenv("LINODE_CLI_API_VERSION", "v4beta") for slug in ["akamai-non-prod", "vpc", "public"]: data = json.loads( - exec_test_command(BASE_CMD + ["template-view", slug, "--json"]) - .stdout.decode() - .rstrip() + exec_test_command( + BASE_CMDS["firewalls"] + ["template-view", slug, "--json"] + ) ) template = data[0] diff --git a/tests/integration/firewalls/test_firewalls_rules.py b/tests/integration/firewalls/test_firewalls_rules.py index a90540213..781ca7725 100644 --- a/tests/integration/firewalls/test_firewalls_rules.py +++ b/tests/integration/firewalls/test_firewalls_rules.py @@ -1,23 +1,26 @@ import json import re +from tests.integration.firewalls.fixtures import ( # noqa: F401 + firewall_id, +) from tests.integration.helpers import ( + BASE_CMDS, delete_target_id, exec_test_command, get_random_text, ) -BASE_CMD = ["linode-cli", "firewalls", "rules-update"] - -def test_add_rule_to_existing_firewall(test_firewall_id): - firewall_id = test_firewall_id +def test_add_rule_to_existing_firewall(firewall_id): + firewall_id = firewall_id inbound_rule = '[{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.1/32"]}, "action": "ACCEPT", "label": "accept-inbound-SSH"}]' outbound_rule = '[{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.2/32"]}, "action": "ACCEPT", "label": "accept-outbound-SSH"}]' result = json.loads( exec_test_command( - BASE_CMD + BASE_CMDS["firewalls"] + [ + "rules-update", firewall_id, "--inbound", inbound_rule, @@ -26,25 +29,28 @@ def test_add_rule_to_existing_firewall(test_firewall_id): "--json", ] ) - .stdout.decode() - .rstrip() ) assert result[0]["inbound"][0] == json.loads(inbound_rule)[0] assert result[0]["outbound"][0] == json.loads(outbound_rule)[0] -def test_add_multiple_rules(test_firewall_id): - firewall_id = test_firewall_id +def test_add_multiple_rules(firewall_id): + firewall_id = firewall_id inbound_rule_1 = '{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.1/32"]}, "action": "ACCEPT", "label": "accept-inbound-SSH"}' inbound_rule_2 = '{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.2/32"]}, "action": "ACCEPT", "label": "accept-inbound-SSH-2"}' inbound_rules = "[" + inbound_rule_1 + "," + inbound_rule_2 + "]" result = json.loads( exec_test_command( - BASE_CMD + [firewall_id, "--inbound", inbound_rules, "--json"] + BASE_CMDS["firewalls"] + + [ + "rules-update", + firewall_id, + "--inbound", + inbound_rules, + "--json", + ] ) - .stdout.decode() - .rstrip() ) assert result[0]["inbound"][0] == json.loads(inbound_rule_1) @@ -52,130 +58,111 @@ def test_add_multiple_rules(test_firewall_id): def test_swap_rules(): - firewall_label = "fw-" + get_random_text(5) - inbound_rules_obj = [ - { - "ports": "22", - "protocol": "TCP", - "addresses": {"ipv4": ["198.0.0.1/32"]}, - "action": "ACCEPT", - "label": "swap_rule_1", - }, - { - "ports": "22", - "protocol": "TCP", - "addresses": {"ipv4": ["198.0.0.2/32"]}, - "action": "ACCEPT", - "label": "swap_rule_2", - }, - ] - inbound_rules = json.dumps(inbound_rules_obj) - - firewall_id = ( - exec_test_command( - [ - "linode-cli", - "firewalls", - "create", - "--label", - firewall_label, - "--rules.outbound_policy", - "ACCEPT", - "--rules.inbound_policy", - "DROP", - "--rules.inbound", - inbound_rules, - "--text", - "--no-headers", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() + firewall_label = "label-fw-test" + get_random_text(5) + inbound_rule_1 = '{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.1/32"]}, "action": "ACCEPT", "label": "swap_rule_1"}' + inbound_rule_2 = '{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.2/32"]}, "action": "ACCEPT", "label": "swap_rule_2"}' + inbound_rules = "[" + inbound_rule_1 + "," + inbound_rule_2 + "]" + firewall_id = exec_test_command( + BASE_CMDS["firewalls"] + + [ + "create", + "--label", + firewall_label, + "--rules.outbound_policy", + "ACCEPT", + "--rules.inbound_policy", + "DROP", + "--rules.inbound", + inbound_rules, + "--text", + "--no-headers", + "--format", + "id", + ] ) - swapped_rules_obj = list(reversed(inbound_rules_obj)) - swapped_rules = json.dumps(swapped_rules_obj) + swapped_rules = "[" + inbound_rule_2 + "," + inbound_rule_1 + "]" result = json.loads( exec_test_command( - BASE_CMD + [firewall_id, "--inbound", swapped_rules, "--json"] + BASE_CMDS["firewalls"] + + [ + "rules-update", + firewall_id, + "--inbound", + swapped_rules, + "--json", + ] ) - .stdout.decode() - .rstrip() ) - assert result[0]["inbound"][0] == swapped_rules_obj[0] - assert result[0]["inbound"][1] == swapped_rules_obj[1] + assert result[0]["inbound"][0] == json.loads(inbound_rule_2) + assert result[0]["inbound"][1] == json.loads(inbound_rule_1) delete_target_id(target="firewalls", id=firewall_id) -def test_update_inbound_and_outbound_policy(test_firewall_id): - firewall_id = test_firewall_id +def test_update_inbound_and_outbound_policy(firewall_id): + firewall_id = firewall_id outbound_policy = "DROP" inbound_policy = "ACCEPT" - result = ( - exec_test_command( - BASE_CMD - + [ - firewall_id, - "--inbound_policy", - inbound_policy, - "--outbound_policy", - outbound_policy, - "--text", - "--no-headers", - "--delimiter", - ",", - ] - ) - .stdout.decode() - .rstrip() + result = exec_test_command( + BASE_CMDS["firewalls"] + + [ + "rules-update", + firewall_id, + "--inbound_policy", + inbound_policy, + "--outbound_policy", + outbound_policy, + "--text", + "--no-headers", + "--delimiter", + ",", + ] ) assert re.search(inbound_policy + "," + outbound_policy, result) def test_remove_one_rule_via_rules_update(): - firewall_label = "fw-" + get_random_text(5) + firewall_label = "label-fw-test" + get_random_text(5) inbound_rule_1 = '{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.1/32"]}, "action": "ACCEPT", "label": "test_rule_1"}' inbound_rule_2 = '{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.2/32"]}, "action": "ACCEPT", "label": "rule_to_delete"}' inbound_rules = "[" + inbound_rule_1 + "," + inbound_rule_2 + "]" - firewall_id = ( - exec_test_command( - [ - "linode-cli", - "firewalls", - "create", - "--label", - firewall_label, - "--rules.outbound_policy", - "ACCEPT", - "--rules.inbound_policy", - "DROP", - "--rules.inbound", - inbound_rules, - "--text", - "--no-headers", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() + firewall_id = exec_test_command( + BASE_CMDS["firewalls"] + + [ + "create", + "--label", + firewall_label, + "--rules.outbound_policy", + "ACCEPT", + "--rules.inbound_policy", + "DROP", + "--rules.inbound", + inbound_rules, + "--text", + "--no-headers", + "--format", + "id", + ] ) new_rule = "[" + inbound_rule_1 + "]" # swapping rules result = json.loads( exec_test_command( - BASE_CMD - + [firewall_id, "--inbound", new_rule, "--json", "--no-headers"] + BASE_CMDS["firewalls"] + + [ + "rules-update", + firewall_id, + "--inbound", + new_rule, + "--json", + "--no-headers", + ] ) - .stdout.decode() - .rstrip() ) rule_labels = [v["label"] for v in result[0]["inbound"]] @@ -185,8 +172,8 @@ def test_remove_one_rule_via_rules_update(): delete_target_id(target="firewalls", id=firewall_id) -def test_list_rules(test_firewall_id): - firewall_id = test_firewall_id +def test_list_rules(firewall_id): + firewall_id = firewall_id new_label = '"rules-list-test"' inbound_rule = ( '[{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.1/32"]}, "action": "ACCEPT", "label": ' @@ -195,8 +182,9 @@ def test_list_rules(test_firewall_id): ) # adding a rule exec_test_command( - BASE_CMD + BASE_CMDS["firewalls"] + [ + "rules-update", firewall_id, "--inbound", inbound_rule, @@ -205,27 +193,22 @@ def test_list_rules(test_firewall_id): "--delimiter", ",", ] - ).stdout.decode().rstrip() - result = ( - exec_test_command( - [ - "linode-cli", - "firewalls", - "rules-list", - firewall_id, - "--text", - "--no-headers", - ] - ) - .stdout.decode() - .rstrip() + ) + result = exec_test_command( + BASE_CMDS["firewalls"] + + [ + "rules-list", + firewall_id, + "--text", + "--no-headers", + ] ) assert new_label.replace('"', "") in result -def test_list_rules_json(test_firewall_id): - firewall_id = test_firewall_id +def test_list_rules_json(firewall_id): + firewall_id = firewall_id new_label = '"rules-list-test"' inbound_rule = ( '[{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.1/32"]}, "action": "ACCEPT", "label": ' @@ -234,8 +217,9 @@ def test_list_rules_json(test_firewall_id): ) # adding a rule exec_test_command( - BASE_CMD + BASE_CMDS["firewalls"] + [ + "rules-update", firewall_id, "--inbound", inbound_rule, @@ -244,19 +228,16 @@ def test_list_rules_json(test_firewall_id): "--delimiter", ",", ] - ).stdout.decode().rstrip() + ) result = json.loads( exec_test_command( - [ - "linode-cli", - "firewalls", + BASE_CMDS["firewalls"] + + [ "rules-list", firewall_id, "--json", ] ) - .stdout.decode() - .rstrip() ) assert result[0]["inbound"][0]["action"] == "ACCEPT" @@ -264,8 +245,8 @@ def test_list_rules_json(test_firewall_id): assert result[0]["inbound"][0]["addresses"]["ipv4"] == ["198.0.0.1/32"] -def test_list_rules_json_format(test_firewall_id): - firewall_id = test_firewall_id +def test_list_rules_json_format(firewall_id): + firewall_id = firewall_id new_label = '"rules-list-test"' inbound_rule = ( '[{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.1/32"]}, "action": "ACCEPT", "label": ' @@ -274,8 +255,9 @@ def test_list_rules_json_format(test_firewall_id): ) # adding a rule exec_test_command( - BASE_CMD + BASE_CMDS["firewalls"] + [ + "rules-update", firewall_id, "--inbound", inbound_rule, @@ -284,12 +266,11 @@ def test_list_rules_json_format(test_firewall_id): "--delimiter", ",", ] - ).stdout.decode().rstrip() + ) result = json.loads( exec_test_command( - [ - "linode-cli", - "firewalls", + BASE_CMDS["firewalls"] + + [ "rules-list", firewall_id, "--json", @@ -297,7 +278,5 @@ def test_list_rules_json_format(test_firewall_id): "label", ] ) - .stdout.decode() - .rstrip() ) assert result[0]["inbound"][0]["label"] == "rules-list-test" diff --git a/tests/integration/fixture_types.py b/tests/integration/fixture_types.py deleted file mode 100644 index 273dd7571..000000000 --- a/tests/integration/fixture_types.py +++ /dev/null @@ -1,9 +0,0 @@ -""" -Complicated type alias for fixtures and other stuff. -""" - -from pathlib import Path -from typing import Callable, List, Optional - -GetTestFilesType = Callable[[Optional[int], Optional[str]], List[Path]] -GetTestFileType = Callable[[Optional[str], Optional[str], Optional[int]], Path] diff --git a/tests/integration/helpers.py b/tests/integration/helpers.py index 474b5bead..0408da723 100644 --- a/tests/integration/helpers.py +++ b/tests/integration/helpers.py @@ -5,7 +5,6 @@ from string import ascii_lowercase from typing import Callable, Container, Iterable, List, TypeVar -from linodecli import ExitCodes from linodecli.exit_codes import ExitCodes BASE_URL = "https://api.linode.com/v4/" @@ -18,6 +17,41 @@ T = TypeVar("T") +MODULES = [ + "account", + "domains", + "linodes", + "nodebalancers", + "betas", + "databases", + "domains", + "events", + "image", + "image-upload", + "firewalls", + "kernels", + "linodes", + "lke", + "longview", + "managed", + "networking", + "obj", + "object-storage", + "placement", + "profile", + "regions", + "ssh", + "stackscripts", + "tickets", + "tags", + "users", + "vlans", + "volumes", + "vpcs", +] +BASE_CMDS = {module: ["linode-cli", module] for module in MODULES} + + def get_random_text(length: int = 10): return "".join(random.choice(ascii_lowercase) for i in range(length)) @@ -37,115 +71,50 @@ def wait_for_condition(interval: int, timeout: int, condition: Callable): def exec_test_command(args: List[str]): process = subprocess.run( - args, stdout=subprocess.PIPE, stderr=subprocess.PIPE + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True ) if process.returncode != 0: raise RuntimeError( f"Command failed with exit code {process.returncode}\n" f"Command: {' '.join(args)}\n" - f"Stdout:\n{process.stdout.decode()}\n" - f"Stderr:\n{process.stderr.decode()}" + f"Stdout:\n{process.stdout}\n" + f"Stderr:\n{process.stderr}" ) - return process + return process.stdout.rstrip() def exec_failing_test_command( args: List[str], expected_code: int = ExitCodes.REQUEST_FAILED ): - process = subprocess.run(args, stderr=subprocess.PIPE) - assert process.returncode == expected_code - return process - - -# Delete/Remove helper functions (mainly used in clean-ups after test -def delete_all_domains(): - domain_ids = exec_test_command( - [ - "linode-cli", - "--text", - "--no-headers", - "domains", - "list", - "--format=id", - ] - ).stdout.decode() - domain_id_arr = domain_ids.splitlines() - - for id in domain_id_arr: - exec_test_command(["linode-cli", "domains", "delete", id]) + process = subprocess.run(args, stderr=subprocess.PIPE, text=True) + if process.returncode != expected_code: + raise AssertionError( + f"Expected exit code {expected_code}, got {process.returncode}\n" + f"Command: {' '.join(args)}\n" + f"Stdout:\n{process.stdout}\n" + f"Stderr: {process.stderr}" + ) -def delete_tag(arg: str): - result = exec_test_command(["linode-cli", "tags", "delete", arg]) - assert result.returncode == SUCCESS_STATUS_CODE + return process.stderr.rstrip() +# Delete/Remove helper functions (mainly used in clean-ups after tests) def delete_target_id(target: str, id: str, delete_command: str = "delete"): command = ["linode-cli", target, delete_command, id] - result = exec_test_command(command) - assert result.returncode == SUCCESS_STATUS_CODE - - -def remove_lke_clusters(): - cluster_ids = ( - exec_test_command( - [ - "linode-cli", - "--text", - "--no-headers", - "lke", - "clusters-list", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) - for id in cluster_ids: - exec_test_command(["linode-cli", "lke", "cluster-delete", id]) - - -def remove_all(target: str): - entity_ids = "" - if target == "stackscripts": - entity_ids = ( - exec_test_command( - [ - "linode-cli", - "--is_public=false", - "--text", - "--no-headers", - target, - "list", - "--format", - "id", - ] - ) - .stdout.decode() - .splitlines() + try: + subprocess.run( + command, + check=True, # Raises CalledProcessError on non-zero exit + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, ) - else: - entity_ids = ( - exec_test_command( - [ - "linode-cli", - "--text", - "--no-headers", - target, - "list", - "--format", - "id", - ] - ) - .stdout.decode() - .splitlines() + except Exception as e: + raise RuntimeError( + f"Error executing command '{' '.join(command)}': {e}" ) - for id in entity_ids: - exec_test_command(["linode-cli", target, "delete", id]) - def count_lines(text: str): return len(list(filter(len, text.split("\n")))) @@ -164,11 +133,11 @@ def retry_exec_test_command_with_delay( args: List[str], retries: int = 3, delay: int = 2 ): for attempt in range(retries): - process = subprocess.run(args, stdout=subprocess.PIPE) + process = subprocess.run(args, stdout=subprocess.PIPE, text=True) # Check if the command succeeded if process.returncode == 0: - return process + return process.stdout.rstrip() else: print( f"Attempt {attempt + 1} failed, retrying in {delay} seconds..." @@ -176,16 +145,14 @@ def retry_exec_test_command_with_delay( time.sleep(delay) assert process.returncode == 0, f"Command failed after {retries} retries" - return process + return process.stdout.rstrip() def get_random_region_with_caps( required_capabilities: List[str], site_type="core" ): - json_regions_data = ( - exec_test_command(["linode-cli", "regions", "ls", "--json"]) - .stdout.decode() - .strip() + json_regions_data = exec_test_command( + ["linode-cli", "regions", "ls", "--json"] ) # Parse regions JSON data @@ -202,24 +169,3 @@ def get_random_region_with_caps( matching_region_ids = [region["id"] for region in matching_regions] return random.choice(matching_region_ids) if matching_region_ids else None - - -def get_cluster_id(label: str): - cluster_id = ( - exec_test_command( - [ - "linode-cli", - "lke", - "clusters-list", - "--text", - "--format=id", - "--no-headers", - "--label", - label, - ] - ) - .stdout.decode() - .rstrip() - ) - - return cluster_id diff --git a/tests/integration/image/fixtures.py b/tests/integration/image/fixtures.py new file mode 100644 index 000000000..13960f74b --- /dev/null +++ b/tests/integration/image/fixtures.py @@ -0,0 +1,21 @@ +import os +import tempfile + +import pytest + +# A minimal gzipped image that will be accepted by the API +TEST_IMAGE_CONTENT = ( + b"\x1f\x8b\x08\x08\xbd\x5c\x91\x60\x00\x03\x74\x65\x73\x74\x2e\x69" + b"\x6d\x67\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00" +) + + +@pytest.fixture(scope="session", autouse=True) +def fake_image_file(): + with tempfile.NamedTemporaryFile(delete=False) as fp: + fp.write(TEST_IMAGE_CONTENT) + file_path = fp.name + + yield file_path + + os.remove(file_path) diff --git a/tests/integration/image/test_plugin_image_upload.py b/tests/integration/image/test_plugin_image_upload.py index 5a05b0536..2c5aaa01f 100644 --- a/tests/integration/image/test_plugin_image_upload.py +++ b/tests/integration/image/test_plugin_image_upload.py @@ -1,53 +1,24 @@ import json -import os import re -import subprocess -import tempfile from sys import platform -from typing import List import pytest from tests.integration.helpers import ( + BASE_CMDS, assert_headers_in_lines, exec_failing_test_command, + exec_test_command, get_random_text, ) +from tests.integration.image.fixtures import fake_image_file # noqa: F401 -REGION = "us-iad" -BASE_CMD = ["linode-cli", "image-upload", "--region", REGION] - -# A minimal gzipped image that will be accepted by the API -TEST_IMAGE_CONTENT = ( - b"\x1f\x8b\x08\x08\xbd\x5c\x91\x60\x00\x03\x74\x65\x73\x74\x2e\x69" - b"\x6d\x67\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00" -) - - -@pytest.fixture(scope="session", autouse=True) -def fake_image_file(): - with tempfile.NamedTemporaryFile(delete=False) as fp: - fp.write(TEST_IMAGE_CONTENT) - file_path = fp.name - - yield file_path - - os.remove(file_path) - - -def exec_test_command(args: List[str]): - process = subprocess.run( - args, - stdout=subprocess.PIPE, - ) - return process +BASE_CMD = BASE_CMDS["image-upload"] + ["--region", "us-iad"] def test_help(): - process = exec_test_command(BASE_CMD + ["--help"]) - output = process.stdout.decode() + output = exec_test_command(BASE_CMD + ["--help"]) - assert process.returncode == 0 assert "The image file to upload" in output assert "The region to upload the image to" in output @@ -56,13 +27,11 @@ def test_invalid_file( fake_image_file, ): file_path = fake_image_file + "_fake" - process = exec_failing_test_command( + output = exec_failing_test_command( BASE_CMD + ["--label", "notimportant", file_path], expected_code=8 ) - error_output = process.stderr.decode() - assert process.returncode == 8 - assert f"No file at {file_path}" in error_output + assert f"No file at {file_path}" in output @pytest.mark.smoke @@ -75,31 +44,24 @@ def test_file_upload( description = "test description" # Upload the test image - process = exec_test_command( + output = exec_test_command( BASE_CMD + ["--label", label, "--description", description, file_path] ) - output = process.stdout.decode() - - assert process.returncode == 0 - # Assertions now using keywords due to some chars getting cut off from lack of terminal space assert re.search("[0-9][0-9]+.[0-9]%", output) assert re.search("test", output) # Get the new image from the API - process = exec_test_command( + output = exec_test_command( ["linode-cli", "images", "ls", "--json", "--label", label] ) - assert process.returncode == 0 - - image = json.loads(process.stdout.decode()) + image = json.loads(output) assert image[0]["label"] == label # Delete the image - process = exec_test_command(["linode-cli", "images", "rm", image[0]["id"]]) - assert process.returncode == 0 + exec_test_command(["linode-cli", "images", "rm", image[0]["id"]]) @pytest.mark.skipif(platform == "win32", reason="Test N/A on Windows") @@ -111,7 +73,7 @@ def test_file_upload_cloud_init( description = "test description" # Upload the test image - process = exec_test_command( + exec_test_command( BASE_CMD + [ "--label", @@ -123,31 +85,23 @@ def test_file_upload_cloud_init( ] ) - assert process.returncode == 0 - # Get the new image from the API - process = exec_test_command( + output = exec_test_command( ["linode-cli", "images", "ls", "--json", "--label", label] ) - assert process.returncode == 0 - image = json.loads(process.stdout.decode()) + image = json.loads(output) assert image[0]["label"] == label assert "cloud-init" in image[0]["capabilities"] # Delete the image - process = exec_test_command(["linode-cli", "images", "rm", image[0]["id"]]) - assert process.returncode == 0 + exec_test_command(["linode-cli", "images", "rm", image[0]["id"]]) def test_image_list(): - res = ( - exec_test_command( - ["linode-cli", "images", "list", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + ["linode-cli", "images", "list", "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -155,50 +109,36 @@ def test_image_list(): assert_headers_in_lines(headers, lines) -@pytest.fixture -def get_image_id(): - image_id = ( - exec_test_command( - [ - "linode-cli", - "images", - "list", - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) - first_id = image_id[0].split(",")[0] - yield first_id - - -def test_image_view(get_image_id): - image_id = get_image_id - res = ( - exec_test_command( - [ - "linode-cli", - "images", - "view", - image_id, - "--text", - "--delimiter=,", - ] - ) - .stdout.decode() - .rstrip() +def test_image_view(): + image_ids = exec_test_command( + [ + "linode-cli", + "images", + "list", + "--text", + "--no-headers", + "--delimiter", + ",", + "--format", + "id", + ] + ).splitlines() + image_id = image_ids[0].split(",")[0] + + res = exec_test_command( + [ + "linode-cli", + "images", + "view", + image_id, + "--text", + "--delimiter=,", + ] ) lines = res.splitlines() headers = ["label", "description"] assert_headers_in_lines(headers, lines) - # assert that regions in the output assert "regions" in lines + assert image_id in str(lines) diff --git a/tests/integration/kernels/test_kernels.py b/tests/integration/kernels/test_kernels.py index 9b6ec0326..214c30bb2 100644 --- a/tests/integration/kernels/test_kernels.py +++ b/tests/integration/kernels/test_kernels.py @@ -2,30 +2,32 @@ import pytest -from tests.integration.helpers import exec_test_command - -BASE_CMD = ["linode-cli", "kernels", "list", "--text", "--no-headers"] +from tests.integration.helpers import BASE_CMDS, exec_test_command def test_list_available_kernels(): - process = exec_test_command(BASE_CMD + ["--format", "id"]) - output = process.stdout.decode() + output = exec_test_command( + BASE_CMDS["kernels"] + + ["list", "--text", "--no-headers", "--format", "id"] + ) for line in output.splitlines(): assert "linode" in line def test_fields_from_kernels_list(): - process = exec_test_command( - BASE_CMD + output = exec_test_command( + BASE_CMDS["kernels"] + [ + "list", + "--text", + "--no-headers", "--delimiter", ",", "--format", "id,version,kvm,architecture,pvops,deprecated,built", ] ) - output = process.stdout.decode() for line in output.splitlines(): assert re.search( @@ -36,15 +38,16 @@ def test_fields_from_kernels_list(): @pytest.mark.smoke def test_view_kernel(): - process = exec_test_command(BASE_CMD + ["--format", "id"]) - output = process.stdout.decode() + output = exec_test_command( + BASE_CMDS["kernels"] + + ["list", "--text", "--no-headers", "--format", "id"] + ) lines = output.splitlines() - process = exec_test_command( - [ - "linode-cli", - "kernels", + output = exec_test_command( + BASE_CMDS["kernels"] + + [ "view", str(lines[0]), "--format", @@ -54,7 +57,6 @@ def test_view_kernel(): ",", ] ) - output = process.stdout.decode() assert "id,version,kvm,architecture,pvops,deprecated,built" in output diff --git a/tests/integration/linodes/fixtures.py b/tests/integration/linodes/fixtures.py new file mode 100644 index 000000000..f7b15ad30 --- /dev/null +++ b/tests/integration/linodes/fixtures.py @@ -0,0 +1,603 @@ +import json +import time + +import pytest + +from tests.integration.helpers import ( + BASE_CMDS, + delete_target_id, + exec_test_command, + get_random_region_with_caps, + get_random_text, + retry_exec_test_command_with_delay, +) +from tests.integration.linodes.helpers import ( + DEFAULT_LABEL, + DEFAULT_LINODE_TYPE, + DEFAULT_RANDOM_PASS, + DEFAULT_REGION, + DEFAULT_TEST_IMAGE, + create_linode, + create_linode_and_wait, + create_linode_backup_disabled, + get_disk_ids, + get_subnet_id, + set_backups_enabled_in_account_settings, + wait_until, +) + + +# Backups +@pytest.fixture(scope="module") +def linode_basic_with_firewall(linode_cloud_firewall): + linode_id = create_linode(firewall_id=linode_cloud_firewall) + + yield linode_id + + delete_target_id("linodes", linode_id) + + +@pytest.fixture(scope="module") +def linode_backup_enabled(linode_cloud_firewall): + # create linode with backups enabled + linode_id = exec_test_command( + [ + "linode-cli", + "linodes", + "create", + "--backups_enabled", + "true", + "--type", + DEFAULT_LINODE_TYPE, + "--region", + DEFAULT_REGION, + "--image", + DEFAULT_TEST_IMAGE, + "--root_pass", + DEFAULT_RANDOM_PASS, + "--firewall_id", + linode_cloud_firewall, + "--text", + "--no-headers", + "--format=id", + ] + ) + + yield linode_id + + delete_target_id("linodes", linode_id) + + +@pytest.fixture(scope="module") +def linode_backup_disabled(linode_cloud_firewall): + res = set_backups_enabled_in_account_settings(toggle=False) + + if res == "True": + raise ValueError( + "Backups are unexpectedly enabled before setting up the test." + ) + + linode_id = create_linode_backup_disabled(firewall_id=linode_cloud_firewall) + + yield linode_id + + delete_target_id("linodes", linode_id) + + +@pytest.fixture(scope="module") +def snapshot_of_linode(): + # get linode id after creation and wait for "running" status + linode_id = create_linode_and_wait() + new_snapshot_label = "test_snapshot_" + get_random_text(5) + + result = exec_test_command( + BASE_CMDS["linodes"] + + [ + "snapshot", + linode_id, + "--label", + new_snapshot_label, + "--text", + "--delimiter", + ",", + "--no-headers", + ] + ) + + yield linode_id, new_snapshot_label + + delete_target_id("linodes", linode_id) + + +# Configs + +TEST_REGION = get_random_region_with_caps( + required_capabilities=["Linodes", "VPCs"] +) + + +@pytest.fixture(scope="module") +def linode_instance_config_tests(linode_cloud_firewall): + linode_id = create_linode( + firewall_id=linode_cloud_firewall, + disk_encryption=False, + test_region=TEST_REGION, + ) + + yield linode_id + + delete_target_id(target="linodes", id=linode_id) + + +@pytest.fixture(scope="module") +def linode_disk_config(linode_instance_config_tests): + linode_id = linode_instance_config_tests + + label = get_random_text(5) + "_config" + disk_id = get_disk_ids(linode_id=linode_id)[1] + + config_id = exec_test_command( + BASE_CMDS["linodes"] + + [ + "config-create", + linode_id, + "--label", + label, + "--devices.sda.disk_id", + disk_id, + "--no-headers", + "--format=id", + "--text", + ] + ) + + yield config_id + + +@pytest.fixture(scope="module") +def test_vpc_w_subnet(request): + vpc_json = create_vpc_w_subnet() + vpc_id = str(vpc_json["id"]) + + yield vpc_id + + +@pytest.fixture(scope="module") +def cleanup_vpc(request, test_vpc_w_subnet): + # Register finalizer to delete VPC after the entire session, with a delay + def delayed_cleanup(): + time.sleep(5) # Delay if necessary + delete_target_id(target="vpcs", id=test_vpc_w_subnet) + + request.addfinalizer(delayed_cleanup) + + +@pytest.fixture(scope="module") +def config_vpc_interface( + linode_instance_config_tests, linode_disk_config, test_vpc_w_subnet +): + linode_id = linode_instance_config_tests + config_id = linode_disk_config + subnet_id = get_subnet_id(vpc_id=test_vpc_w_subnet) + + interface_id = exec_test_command( + BASE_CMDS["linodes"] + + [ + "config-interface-add", + linode_id, + config_id, + "--purpose", + "vpc", + "--primary", + "false", + "--subnet_id", + subnet_id, + "--ipv4.vpc", + "10.0.0.3", + "--text", + "--no-headers", + "--format", + "id", + ] + ) + + yield interface_id + + retry_exec_test_command_with_delay( + BASE_CMDS["linodes"] + + [ + "config-interface-delete", + linode_id, + config_id, + interface_id, + ] + ) + + +def create_vpc_w_subnet(): + vpc_label = get_random_text(5) + "vpc" + subnet_label = get_random_text(5) + "subnet" + + vpc_json = json.loads( + exec_test_command( + BASE_CMDS["vpcs"] + + [ + "create", + "--label", + vpc_label, + "--region", + TEST_REGION, + "--subnets.ipv4", + "10.0.0.0/24", + "--subnets.label", + subnet_label, + "--json", + "--suppress-warnings", + ] + ) + )[0] + + return vpc_json + + +# Disks +@pytest.fixture(scope="module") +def linode_instance_disk_tests(linode_cloud_firewall): + test_region = get_random_region_with_caps(required_capabilities=["Linodes"]) + linode_id = create_linode_and_wait( + firewall_id=linode_cloud_firewall, + disk_encryption=False, + test_region=test_region, + test_plan="g6-standard-4", + ) + + retry_exec_test_command_with_delay( + BASE_CMDS["linodes"] + ["shutdown", linode_id] + ) + + wait_until(linode_id=linode_id, timeout=240, status="offline") + + yield linode_id + + delete_target_id(target="linodes", id=linode_id) + + +# Interfaces +@pytest.fixture(scope="module") +def linode_with_vpc_interface_as_json(linode_cloud_firewall): + vpc_json = create_vpc_w_subnet() + + vpc_region = vpc_json["region"] + vpc_id = str(vpc_json["id"]) + subnet_id = int(vpc_json["subnets"][0]["id"]) + + linode_json = json.loads( + exec_test_command( + BASE_CMDS["linodes"] + + [ + "create", + "--type", + "g6-nanode-1", + "--region", + vpc_region, + "--image", + DEFAULT_TEST_IMAGE, + "--root_pass", + DEFAULT_RANDOM_PASS, + "--firewall_id", + linode_cloud_firewall, + "--interfaces", + json.dumps( + [ + { + "purpose": "vpc", + "primary": True, + "subnet_id": subnet_id, + "ipv4": {"nat_1_1": "any", "vpc": "10.0.0.5"}, + "ip_ranges": ["10.0.0.6/32"], + }, + {"purpose": "public"}, + ] + ), + "--json", + "--suppress-warnings", + ] + ) + )[0] + + yield linode_json, vpc_json + + delete_target_id(target="linodes", id=str(linode_json["id"])) + delete_target_id(target="vpcs", id=vpc_id) + + +# Interfaces new +@pytest.fixture(scope="module") +def linode_interface_public(linode_cloud_firewall): + linode_id = create_linode( + firewall_id=linode_cloud_firewall, + test_region="us-sea", + interface_generation="linode", + interfaces='[{"public": {"ipv4": {"addresses": [{"primary": true}]}}, "default_route": {"ipv4": true, "ipv6": true }, "firewall_id":' + + linode_cloud_firewall + + "}]", + ) + + wait_until(linode_id=linode_id, timeout=300, status="running") + + # shutdown linode + wait_until(linode_id=linode_id, timeout=60, status="offline") + + yield linode_id + + delete_target_id(target="linodes", id=linode_id) + + +@pytest.fixture(scope="module") +def linode_interface_vlan(linode_cloud_firewall): + linode_id = create_linode( + firewall_id=linode_cloud_firewall, + test_region="us-sea", + interface_generation="linode", + interfaces='[{"vlan": {"ipam_address": "10.0.0.1/24","vlan_label": "my-vlan"}}]', + ) + + yield linode_id + + delete_target_id(target="linodes", id=linode_id) + + +@pytest.fixture(scope="module") +def linode_interface_legacy(linode_cloud_firewall): + linode_id = create_linode( + firewall_id=linode_cloud_firewall, + test_region="us-sea", + interface_generation="legacy_config", + ) + + yield linode_id + + delete_target_id(target="linodes", id=linode_id) + + +@pytest.fixture(scope="module") +def linode_interface_vpc(linode_cloud_firewall): + vpc_output = json.loads( + exec_test_command( + [ + "linode-cli", + "vpcs", + "create", + "--label", + get_random_text(5) + "-vpc", + "--region", + "us-sea", + "--subnets.ipv4", + "10.0.0.0/24", + "--subnets.label", + get_random_text(5) + "-vpc", + "--json", + "--suppress-warnings", + ] + ) + ) + + subnet_id = vpc_output[0]["subnets"][0]["id"] + + linode_id = create_linode( + firewall_id=linode_cloud_firewall, + test_region="us-sea", + interface_generation="linode", + interfaces='[{"default_route":{"ipv4":true},"firewall_id":' + + str(linode_cloud_firewall) + + ',"vpc":{"ipv4":{"addresses":[{"address":"auto","nat_1_1_address":"auto","primary":true}]},"subnet_id":' + + str(subnet_id) + + "}}]", + ) + + yield linode_id + + delete_target_id(target="linodes", id=linode_id) + delete_target_id(target="vpcs", id=str(vpc_output[0]["id"])) + + +# Linodes +@pytest.fixture(scope="module") +def test_linode_instance(linode_cloud_firewall): + linode_label = DEFAULT_LABEL + get_random_text(5) + + linode_id = exec_test_command( + BASE_CMDS["linodes"] + + [ + "create", + "--type", + "g6-nanode-1", + "--region", + "us-ord", + "--image", + DEFAULT_TEST_IMAGE, + "--label", + linode_label, + "--root_pass", + DEFAULT_RANDOM_PASS, + "--firewall_id", + linode_cloud_firewall, + "--text", + "--delimiter", + ",", + "--no-headers", + "--format", + "id", + "--no-defaults", + "--format", + "id", + ] + ) + + yield linode_id + + delete_target_id(target="linodes", id=linode_id) + + +# Linode Power Status +@pytest.fixture(scope="module") +def linode_instance_basic(linode_cloud_firewall): + linode_id = create_linode_and_wait(firewall_id=linode_cloud_firewall) + + yield linode_id + + delete_target_id(target="linodes", id=linode_id) + + +@pytest.fixture(scope="module") +def linode_in_running_state(linode_cloud_firewall): + linode_id = create_linode_and_wait(firewall_id=linode_cloud_firewall) + + yield linode_id + + delete_target_id("linodes", linode_id) + + +@pytest.fixture(scope="session") +def linode_in_running_state_for_reboot(linode_cloud_firewall): + linode_id = create_linode_and_wait(firewall_id=linode_cloud_firewall) + + yield linode_id + + delete_target_id("linodes", linode_id) + + +# Rebuild +@pytest.fixture(scope="module") +def linode_for_rebuild_tests(linode_cloud_firewall): + test_region = get_random_region_with_caps( + required_capabilities=["Linodes", "Disk Encryption"] + ) + linode_id = create_linode_and_wait( + firewall_id=linode_cloud_firewall, + disk_encryption=False, + test_region=test_region, + ) + + yield linode_id + + delete_target_id(target="linodes", id=linode_id) + + +# Resize +@pytest.fixture(scope="module") +def linode_instance_for_resize_tests(linode_cloud_firewall): + plan = exec_test_command( + BASE_CMDS["linodes"] + + [ + "types", + "--format", + "id", + "--text", + "--no-headers", + ] + ).splitlines()[1] + linode_id = create_linode_and_wait( + firewall_id=linode_cloud_firewall, test_plan=plan + ) + + yield linode_id + + delete_target_id(target="linodes", id=linode_id) + + +# Linodes +@pytest.fixture(scope="module") +def linode_wo_image(linode_cloud_firewall): + label = get_random_text(5) + "-label" + linode_id = exec_test_command( + BASE_CMDS["linodes"] + + [ + "create", + "--no-defaults", + "--label", + label, + "--type", + DEFAULT_LINODE_TYPE, + "--region", + DEFAULT_REGION, + "--root_pass", + DEFAULT_RANDOM_PASS, + "--firewall_id", + linode_cloud_firewall, + "--format", + "id", + "--no-headers", + "--text", + ] + ) + + yield linode_id + + delete_target_id(target="linodes", id=linode_id) + + +@pytest.fixture(scope="module") +def linode_min_req(linode_cloud_firewall): + result = exec_test_command( + BASE_CMDS["linodes"] + + [ + "create", + "--type", + "g6-nanode-1", + "--region", + "us-ord", + "--root_pass", + DEFAULT_RANDOM_PASS, + "--firewall_id", + linode_cloud_firewall, + "--no-defaults", + "--text", + "--delimiter", + ",", + "--no-headers", + "--format", + "id,region,type", + ] + ) + + yield result + + res_arr = result.split(",") + linode_id = res_arr[0] + delete_target_id(target="linodes", id=linode_id) + + +@pytest.fixture(scope="module") +def linode_with_label(linode_cloud_firewall): + label = "cli" + get_random_text(5) + result = exec_test_command( + BASE_CMDS["linodes"] + + [ + "create", + "--type", + "g6-nanode-1", + "--region", + "us-ord", + "--image", + DEFAULT_TEST_IMAGE, + "--label", + label, + "--root_pass", + DEFAULT_RANDOM_PASS, + "--firewall_id", + linode_cloud_firewall, + "--text", + "--delimiter", + ",", + "--no-headers", + "--format", + "label,region,type,image,id", + "--no-defaults", + ] + ) + + yield result + res_arr = result.split(",") + linode_id = res_arr[4] + delete_target_id(target="linodes", id=linode_id) diff --git a/tests/integration/linodes/helpers_linodes.py b/tests/integration/linodes/helpers.py similarity index 56% rename from tests/integration/linodes/helpers_linodes.py rename to tests/integration/linodes/helpers.py index 968c95765..d95cc34b0 100644 --- a/tests/integration/linodes/helpers_linodes.py +++ b/tests/integration/linodes/helpers.py @@ -1,54 +1,39 @@ +import json import time -from tests.integration.helpers import exec_test_command +from tests.integration.helpers import BASE_CMDS, exec_test_command -DEFAULT_RANDOM_PASS = ( - exec_test_command(["openssl", "rand", "-base64", "32"]) - .stdout.decode() - .rstrip() -) +DEFAULT_RANDOM_PASS = exec_test_command(["openssl", "rand", "-base64", "32"]) DEFAULT_REGION = "us-ord" -DEFAULT_TEST_IMAGE = ( - exec_test_command( - [ - "linode-cli", - "images", - "list", - "--text", - "--format", - "id", - "--no-headers", - "--is_public", - "True", - ] - ) - .stdout.decode() - .rstrip() - .splitlines()[0] -) +DEFAULT_TEST_IMAGE = exec_test_command( + [ + "linode-cli", + "images", + "list", + "--text", + "--format", + "id", + "--no-headers", + "--is_public", + "True", + ] +).splitlines()[0] -DEFAULT_LINODE_TYPE = ( - exec_test_command( - [ - "linode-cli", - "linodes", - "types", - "--text", - "--no-headers", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines()[0] -) +DEFAULT_LINODE_TYPE = exec_test_command( + [ + "linode-cli", + "linodes", + "types", + "--text", + "--no-headers", + "--format", + "id", + ] +).splitlines()[0] DEFAULT_LABEL = "cli-default" -BASE_CMD = ["linode-cli", "linodes"] - def wait_until(linode_id: "str", timeout, status: "str", period=5): must_end = time.time() + timeout @@ -64,7 +49,7 @@ def wait_until(linode_id: "str", timeout, status: "str", period=5): "--text", "--no-headers", ] - ).stdout.decode() + ) if status in result: return True time.sleep(period) @@ -79,9 +64,7 @@ def create_linode( interfaces: str = None, ): # Base command - command = [ - "linode-cli", - "linodes", + command = BASE_CMDS["linodes"] + [ "create", "--type", DEFAULT_LINODE_TYPE, @@ -105,7 +88,7 @@ def create_linode( command.extend(["--format=id", "--text", "--no-headers"]) - linode_id = exec_test_command(command).stdout.decode().rstrip() + linode_id = exec_test_command(command) return linode_id @@ -113,79 +96,34 @@ def create_linode( def create_linode_backup_disabled( firewall_id: "str", test_region=DEFAULT_REGION ): - result = set_backups_enabled_in_account_settings(toggle=False) + set_backups_enabled_in_account_settings(toggle=False) # create linode - linode_id = ( - exec_test_command( - [ - "linode-cli", - "linodes", - "create", - "--type", - DEFAULT_LINODE_TYPE, - "--region", - test_region, - "--image", - DEFAULT_TEST_IMAGE, - "--root_pass", - DEFAULT_RANDOM_PASS, - "--firewall_id", - firewall_id, - "--format=id", - "--text", - "--no-headers", - "--backups_enabled", - "false", - ] - ) - .stdout.decode() - .rstrip() + linode_id = exec_test_command( + BASE_CMDS["linodes"] + + [ + "create", + "--type", + DEFAULT_LINODE_TYPE, + "--region", + test_region, + "--image", + DEFAULT_TEST_IMAGE, + "--root_pass", + DEFAULT_RANDOM_PASS, + "--firewall_id", + firewall_id, + "--format=id", + "--text", + "--no-headers", + "--backups_enabled", + "false", + ] ) return linode_id -def shutdown_linodes(): - linode_ids = ( - exec_test_command( - [ - BASE_CMD, - "linodes", - "list", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) - - for id in linode_ids: - exec_test_command(["linode-cli", "linodes", "shutdown", id]) - - -def remove_linodes(): - linode_ids = ( - exec_test_command( - [ - BASE_CMD, - "linodes", - "list", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) - - for id in linode_ids: - exec_test_command(["linode-cli", "linodes", "delete", id]) - - def create_linode_and_wait( firewall_id: "str", ssh_key="", @@ -222,7 +160,7 @@ def create_linode_and_wait( if ssh_key: command.extend(["--authorized_keys", ssh_key]) - linode_id = exec_test_command(command).stdout.decode().strip() + linode_id = exec_test_command(command) # wait until linode is running, wait_until returns True when it is in running state result = wait_until(linode_id=linode_id, timeout=240, status="running") @@ -248,27 +186,115 @@ def set_backups_enabled_in_account_settings(toggle: bool): else: command.extend(["--backups_enabled", "false"]) - result = exec_test_command(command).stdout.decode().rstrip() + result = exec_test_command(command) return result def get_disk_ids(linode_id): - disk_ids = ( + disk_ids = exec_test_command( + BASE_CMDS["linodes"] + + [ + "disks-list", + linode_id, + "--text", + "--no-headers", + "--format", + "id", + ] + ).splitlines() + + return disk_ids + + +# Backups +def check_account_settings(): + acc_settings = exec_test_command( + [ + "linode-cli", + "account", + "settings", + "--text", + "--format", + "managed", + "--no-headers", + ] + ) + + return acc_settings + + +# Config + + +def get_subnet_id(vpc_id): + subnet_id = exec_test_command( + [ + "linode-cli", + "vpcs", + "subnets-list", + vpc_id, + "--text", + "--format=id", + "--no-headers", + ] + ) + + return subnet_id + + +# Interfaces +def get_interface_id(linode_id: str): + data = json.loads( exec_test_command( - BASE_CMD - + [ - "disks-list", + [ + "linode-cli", + "linodes", + "interfaces-list", linode_id, - "--text", - "--no-headers", - "--format", - "id", + "--json", ] ) - .stdout.decode() - .rstrip() - .splitlines() ) - return disk_ids + interface_id = data[0]["interfaces"]["id"] + + return str(interface_id) + + +def get_ipv4_addr(linode_id: "str"): + data = json.loads( + exec_test_command( + [ + "linode-cli", + "linodes", + "ips-list", + linode_id, + "--json", + ] + ) + ) + + ipv4_public = data[0]["ipv4"]["public"] + ipv4_addr = ipv4_public[0]["address"] if ipv4_public else None + + return str(ipv4_addr) + + +def get_disk_id(test_linode_instance): + linode_id = test_linode_instance + disk_id = exec_test_command( + BASE_CMDS["linodes"] + + [ + "disks-list", + linode_id, + "--text", + "--no-headers", + "--delimiter", + ",", + "--format", + "id", + ] + ).splitlines() + first_id = disk_id[0].split(",")[0] + return first_id diff --git a/tests/integration/linodes/test_backups.py b/tests/integration/linodes/test_backups.py index dd5c904e7..3b09f1f0a 100755 --- a/tests/integration/linodes/test_backups.py +++ b/tests/integration/linodes/test_backups.py @@ -3,72 +3,28 @@ import pytest -from tests.integration.helpers import delete_target_id, exec_test_command -from tests.integration.linodes.helpers_linodes import ( - BASE_CMD, - create_linode, +from tests.integration.helpers import BASE_CMDS, exec_test_command +from tests.integration.linodes.fixtures import ( # noqa: F401 + linode_backup_disabled, + linode_backup_enabled, + linode_basic_with_firewall, +) +from tests.integration.linodes.helpers import ( + check_account_settings, create_linode_and_wait, - create_linode_backup_disabled, set_backups_enabled_in_account_settings, ) -# ################################################################## -# # WARNING: THIS TEST WILL DELETE ALL OF YOUR LINODES # -# # WARNING: USE A SEPARATE TEST ACCOUNT WHEN RUNNING THESE TESTS # -# ################################################################## -snapshot_label = "test_snapshot1" - - -@pytest.fixture -def create_linode_setup(linode_cloud_firewall): - linode_id = create_linode(firewall_id=linode_cloud_firewall) - - yield linode_id - - delete_target_id("linodes", linode_id) - - -@pytest.fixture -def create_linode_backup_disabled_setup(linode_cloud_firewall): - res = set_backups_enabled_in_account_settings(toggle=False) - - if res == "True": - raise ValueError( - "Backups are unexpectedly enabled before setting up the test." - ) - - linode_id = create_linode_backup_disabled(firewall_id=linode_cloud_firewall) - - yield linode_id - - delete_target_id("linodes", linode_id) - - -def check_account_settings(): - result = exec_test_command( - [ - "linode-cli", - "account", - "settings", - "--text", - "--format", - "managed", - "--no-headers", - ] - ).stdout.decode() - - return result - @pytest.mark.skipif( check_account_settings(), reason="Account is managed, skipping the test.." ) def test_create_linode_with_backup_disabled( - create_linode_backup_disabled_setup, + linode_backup_disabled, ): - linode_id = create_linode_backup_disabled_setup + linode_id = linode_backup_disabled result = exec_test_command( - BASE_CMD + BASE_CMDS["linodes"] + [ "list", "--id", @@ -79,7 +35,7 @@ def test_create_linode_with_backup_disabled( "--text", "--no-headers", ] - ).stdout.decode() + ) assert re.search(linode_id + ",False", result) @@ -89,17 +45,18 @@ def test_create_linode_with_backup_disabled( @pytest.mark.smoke -def test_enable_backups(create_linode_setup): +def test_enable_backups(linode_basic_with_firewall): # get linode id - linode_id = create_linode_setup + linode_id = linode_basic_with_firewall # enable backup exec_test_command( - BASE_CMD + ["backups-enable", linode_id, "--text", "--no-headers"] + BASE_CMDS["linodes"] + + ["backups-enable", linode_id, "--text", "--no-headers"] ) result = exec_test_command( - BASE_CMD + BASE_CMDS["linodes"] + [ "list", "--format=id,backups.enabled", @@ -108,7 +65,7 @@ def test_enable_backups(create_linode_setup): "--text", "--no-headers", ] - ).stdout.decode() + ) assert re.search(linode_id + ",True", result) @@ -116,7 +73,7 @@ def test_enable_backups(create_linode_setup): def test_create_backup_with_backup_enabled(linode_backup_enabled): linode_id = linode_backup_enabled result = exec_test_command( - BASE_CMD + BASE_CMDS["linodes"] + [ "list", "--format=id,backups.enabled", @@ -125,7 +82,7 @@ def test_create_backup_with_backup_enabled(linode_backup_enabled): "--text", "--no-headers", ] - ).stdout.decode() + ) assert re.search(linode_id + ",True", result) @@ -138,8 +95,10 @@ def test_take_snapshot_of_linode(): # get linode id after creation and wait for "running" status linode_id = create_linode_and_wait() + snapshot_label = "test_snapshot1" + result = exec_test_command( - BASE_CMD + BASE_CMDS["linodes"] + [ "snapshot", linode_id, @@ -150,7 +109,7 @@ def test_take_snapshot_of_linode(): ",", "--no-headers", ] - ).stdout.decode() + ) assert re.search( "[0-9]+,pending,snapshot,[0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+," + snapshot_label, @@ -168,7 +127,7 @@ def test_view_the_snapshot(snapshot_of_linode): new_snapshot_label = snapshot_of_linode[1] result = exec_test_command( - BASE_CMD + BASE_CMDS["linodes"] + [ "backups-list", linode_id, @@ -177,7 +136,7 @@ def test_view_the_snapshot(snapshot_of_linode): "--text", "--no-headers", ] - ).stdout.decode() + ) assert re.search( "[0-9]+,pending,snapshot,[0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+," @@ -196,7 +155,7 @@ def test_cancel_backups(snapshot_of_linode): new_snapshot_label = snapshot_of_linode[1] result = exec_test_command( - BASE_CMD + BASE_CMDS["linodes"] + [ "snapshot", linode_id, @@ -207,7 +166,7 @@ def test_cancel_backups(snapshot_of_linode): ",", "--no-headers", ] - ).stdout.decode() + ) assert re.search( "[0-9]+,pending,snapshot,[0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+," + new_snapshot_label, @@ -216,5 +175,6 @@ def test_cancel_backups(snapshot_of_linode): # cancel snapshot exec_test_command( - BASE_CMD + ["backups-cancel", linode_id, "--text", "--no-headers"] + BASE_CMDS["linodes"] + + ["backups-cancel", linode_id, "--text", "--no-headers"] ) diff --git a/tests/integration/linodes/test_configs.py b/tests/integration/linodes/test_configs.py index 14fb090fa..fcde7f385 100644 --- a/tests/integration/linodes/test_configs.py +++ b/tests/integration/linodes/test_configs.py @@ -1,206 +1,42 @@ -import json -import time - -import pytest - -from tests.integration.conftest import create_vpc_w_subnet from tests.integration.helpers import ( + BASE_CMDS, assert_headers_in_lines, - delete_target_id, exec_test_command, - get_random_region_with_caps, get_random_text, retry_exec_test_command_with_delay, ) -from tests.integration.linodes.helpers_linodes import ( - BASE_CMD, - create_linode, - get_disk_ids, +from tests.integration.linodes.fixtures import ( # noqa: F401 + TEST_REGION, + cleanup_vpc, + config_vpc_interface, + linode_disk_config, + linode_instance_config_tests, + test_vpc_w_subnet, ) - -TEST_REGION = get_random_region_with_caps( - required_capabilities=["Linodes", "VPCs"] +from tests.integration.linodes.helpers import ( + get_disk_ids, ) -@pytest.fixture(scope="session", autouse=True) -def linode_instance_config_tests(linode_cloud_firewall): - linode_id = create_linode( - firewall_id=linode_cloud_firewall, - disk_encryption=False, - test_region=TEST_REGION, - ) - - yield linode_id - - delete_target_id(target="linodes", id=linode_id) - - -@pytest.fixture(scope="session", autouse=True) -def linode_disk_config(linode_instance_config_tests): +def test_config_create(linode_instance_config_tests): linode_id = linode_instance_config_tests label = get_random_text(5) + "_config" disk_id = get_disk_ids(linode_id=linode_id)[1] - config_id = ( - exec_test_command( - BASE_CMD - + [ - "config-create", - linode_id, - "--label", - label, - "--devices.sda.disk_id", - disk_id, - "--no-headers", - "--format=id", - "--text", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield config_id - - -@pytest.fixture(scope="session", autouse=True) -def test_vpc_w_subnet(request): - vpc_json = create_vpc_w_subnet() - vpc_id = str(vpc_json["id"]) - - yield vpc_id - - -@pytest.fixture(scope="session", autouse=True) -def cleanup_vpc(request, test_vpc_w_subnet): - # Register finalizer to delete VPC after the entire session, with a delay - def delayed_cleanup(): - time.sleep(5) # Delay if necessary - delete_target_id(target="vpcs", id=test_vpc_w_subnet) - - request.addfinalizer(delayed_cleanup) - - -@pytest.fixture(scope="session", autouse=True) -def config_vpc_interface( - linode_instance_config_tests, linode_disk_config, test_vpc_w_subnet -): - linode_id = linode_instance_config_tests - config_id = linode_disk_config - subnet_id = get_subnet_id(vpc_id=test_vpc_w_subnet) - - interface_id = ( - exec_test_command( - BASE_CMD - + [ - "config-interface-add", - linode_id, - config_id, - "--purpose", - "vpc", - "--primary", - "false", - "--subnet_id", - subnet_id, - "--ipv4.vpc", - "10.0.0.3", - "--text", - "--no-headers", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield interface_id - - retry_exec_test_command_with_delay( - BASE_CMD + result = exec_test_command( + BASE_CMDS["linodes"] + [ - "config-interface-delete", + "config-create", linode_id, - config_id, - interface_id, + "--label", + label, + "--devices.sda.disk_id", + disk_id, + "--text", ] ) - -def create_vpc_w_subnet(): - vpc_label = get_random_text(5) + "vpc" - subnet_label = get_random_text(5) + "subnet" - - vpc_json = json.loads( - exec_test_command( - [ - "linode-cli", - "vpcs", - "create", - "--label", - vpc_label, - "--region", - TEST_REGION, - "--subnets.ipv4", - "10.0.0.0/24", - "--subnets.label", - subnet_label, - "--json", - "--suppress-warnings", - ] - ) - .stdout.decode() - .rstrip() - )[0] - - return vpc_json - - -def get_subnet_id(vpc_id): - subnet_id = ( - exec_test_command( - [ - "linode-cli", - "vpcs", - "subnets-list", - vpc_id, - "--text", - "--format=id", - "--no-headers", - ] - ) - .stdout.decode() - .rstrip() - ) - - return subnet_id - - -def test_config_create(linode_instance_config_tests): - linode_id = linode_instance_config_tests - - label = get_random_text(5) + "_config" - disk_id = get_disk_ids(linode_id=linode_id)[1] - - result = ( - exec_test_command( - BASE_CMD - + [ - "config-create", - linode_id, - "--label", - label, - "--devices.sda.disk_id", - disk_id, - "--text", - ] - ) - .stdout.decode() - .rstrip() - ) - headers = ["id", "label", "kernel"] assert_headers_in_lines(headers, result.splitlines()) @@ -213,27 +49,23 @@ def test_config_delete(linode_instance_config_tests): label = get_random_text(5) + "_config" disk_id = get_disk_ids(linode_id=linode_id)[1] - config_id = ( - exec_test_command( - BASE_CMD - + [ - "config-create", - linode_id, - "--label", - label, - "--devices.sda.disk_id", - disk_id, - "--no-headers", - "--format=id", - "--text", - ] - ) - .stdout.decode() - .rstrip() + config_id = exec_test_command( + BASE_CMDS["linodes"] + + [ + "config-create", + linode_id, + "--label", + label, + "--devices.sda.disk_id", + disk_id, + "--no-headers", + "--format=id", + "--text", + ] ) - res = retry_exec_test_command_with_delay( - BASE_CMD + retry_exec_test_command_with_delay( + BASE_CMDS["linodes"] + [ "config-delete", linode_id, @@ -241,8 +73,6 @@ def test_config_delete(linode_instance_config_tests): ] ) - assert res.returncode == 0 - def test_config_update_label(linode_instance_config_tests, linode_disk_config): linode_id = linode_instance_config_tests @@ -250,20 +80,16 @@ def test_config_update_label(linode_instance_config_tests, linode_disk_config): updated_label = get_random_text(4) + "_updatedconfig" - res = ( - exec_test_command( - BASE_CMD - + [ - "config-update", - linode_id, - config_id, - "--label", - updated_label, - "--text", - ] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["linodes"] + + [ + "config-update", + linode_id, + config_id, + "--label", + updated_label, + "--text", + ] ) headers = ["id", "label", "kernel"] @@ -276,12 +102,8 @@ def test_config_view(linode_instance_config_tests, linode_disk_config): linode_id = linode_instance_config_tests config_id = linode_disk_config - res = ( - exec_test_command( - BASE_CMD + ["config-view", linode_id, config_id, "--text"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["linodes"] + ["config-view", linode_id, config_id, "--text"] ) headers = ["id", "label", "kernel"] @@ -293,10 +115,8 @@ def test_config_view(linode_instance_config_tests, linode_disk_config): def test_configs_list(linode_instance_config_tests): linode_id = linode_instance_config_tests - res = ( - exec_test_command(BASE_CMD + ["configs-list", linode_id, "--text"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["linodes"] + ["configs-list", linode_id, "--text"] ) headers = ["id", "label", "kernel"] @@ -312,24 +132,20 @@ def test_config_interface_add_vlan( label = get_random_text(5) + "vlan" - res = ( - exec_test_command( - BASE_CMD - + [ - "config-interface-add", - linode_id, - config_id, - "--purpose", - "vlan", - "--primary", - "false", - "--label", - label, - "--text", - ] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["linodes"] + + [ + "config-interface-add", + linode_id, + config_id, + "--purpose", + "vlan", + "--primary", + "false", + "--label", + label, + "--text", + ] ) headers = ["id", "label", "purpose", "ipam_address"] @@ -346,21 +162,17 @@ def test_config_interface_update( config_id = linode_disk_config interface_id = config_vpc_interface - res = ( - exec_test_command( - BASE_CMD - + [ - "config-interface-update", - linode_id, - config_id, - interface_id, - "--ipv4.vpc", - "10.0.0.5", - "--text", - ] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["linodes"] + + [ + "config-interface-update", + linode_id, + config_id, + interface_id, + "--ipv4.vpc", + "10.0.0.5", + "--text", + ] ) headers = ["id", "label", "purpose", "ipam_address"] @@ -375,19 +187,15 @@ def test_config_interface_view( config_id = linode_disk_config interface_id = config_vpc_interface - res = ( - exec_test_command( - BASE_CMD - + [ - "config-interface-view", - linode_id, - config_id, - interface_id, - "--text", - ] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["linodes"] + + [ + "config-interface-view", + linode_id, + config_id, + interface_id, + "--text", + ] ) headers = ["id", "label", "purpose", "ipam_address"] @@ -402,18 +210,14 @@ def test_config_interfaces_list( linode_id = linode_instance_config_tests config_id = linode_disk_config - res = ( - exec_test_command( - BASE_CMD - + [ - "config-interfaces-list", - linode_id, - config_id, - "--text", - ] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["linodes"] + + [ + "config-interfaces-list", + linode_id, + config_id, + "--text", + ] ) headers = ["id", "label", "purpose", "ipam_address"] @@ -428,19 +232,15 @@ def config_interfaces_order( config_id = linode_disk_config interface_id = config_vpc_interface - process = ( - exec_test_command( - BASE_CMD - + [ - "config-interfaces-order", - linode_id, - config_id, - "--ids", - interface_id, - ] - ) - .stdout.decode() - .rstrip() + process = exec_test_command( + BASE_CMDS["linodes"] + + [ + "config-interfaces-order", + linode_id, + config_id, + "--ids", + interface_id, + ] ) assert process.returncode == 0 diff --git a/tests/integration/linodes/test_disk.py b/tests/integration/linodes/test_disk.py index 9472418c8..a0cef3a71 100644 --- a/tests/integration/linodes/test_disk.py +++ b/tests/integration/linodes/test_disk.py @@ -1,41 +1,18 @@ -import pytest - from tests.integration.helpers import ( + BASE_CMDS, assert_headers_in_lines, - delete_target_id, exec_test_command, - get_random_region_with_caps, get_random_text, retry_exec_test_command_with_delay, wait_for_condition, ) -from tests.integration.linodes.helpers_linodes import ( - BASE_CMD, - create_linode_and_wait, +from tests.integration.linodes.fixtures import ( # noqa: F401 + linode_instance_disk_tests, +) +from tests.integration.linodes.helpers import ( get_disk_ids, - wait_until, ) -TEST_REGION = get_random_region_with_caps(required_capabilities=["Linodes"]) - - -@pytest.fixture(scope="session", autouse=True) -def linode_instance_disk_tests(linode_cloud_firewall): - linode_id = create_linode_and_wait( - firewall_id=linode_cloud_firewall, - disk_encryption=False, - test_region=TEST_REGION, - test_plan="g6-standard-4", - ) - - retry_exec_test_command_with_delay(BASE_CMD + ["shutdown", linode_id]) - - wait_until(linode_id=linode_id, timeout=240, status="offline") - - yield linode_id - - delete_target_id(target="linodes", id=linode_id) - def test_disk_resize_clone_and_create(linode_instance_disk_tests): linode_id = linode_instance_disk_tests @@ -44,7 +21,7 @@ def test_disk_resize_clone_and_create(linode_instance_disk_tests): # resize disk retry_exec_test_command_with_delay( - BASE_CMD + BASE_CMDS["linodes"] + [ "disk-resize", linode_id, @@ -57,20 +34,16 @@ def test_disk_resize_clone_and_create(linode_instance_disk_tests): ) def disk_poll_func(): - status = ( - exec_test_command( - BASE_CMD - + [ - "disk-view", - linode_id, - disk_id, - "--text", - "--no-headers", - "--format=status", - ] - ) - .stdout.decode() - .rstrip() + status = exec_test_command( + BASE_CMDS["linodes"] + + [ + "disk-view", + linode_id, + disk_id, + "--text", + "--no-headers", + "--format=status", + ] ) return status == "ready" @@ -79,20 +52,16 @@ def disk_poll_func(): wait_for_condition(15, 150, disk_poll_func) # clone disk - res = ( - retry_exec_test_command_with_delay( - BASE_CMD - + [ - "disk-clone", - linode_id, - disk_id, - "--text", - ], - retries=3, - delay=10, - ) - .stdout.decode() - .rstrip() + res = retry_exec_test_command_with_delay( + BASE_CMDS["linodes"] + + [ + "disk-clone", + linode_id, + disk_id, + "--text", + ], + retries=3, + delay=10, ) headers = ["id", "label", "status", "size", "filesystem", "disk_encryption"] @@ -105,23 +74,19 @@ def disk_poll_func(): label = get_random_text(5) + "disk" # create new disk - res = ( - retry_exec_test_command_with_delay( - BASE_CMD - + [ - "disk-create", - linode_id, - "--size", - "15", - "--label", - label, - "--text", - ], - retries=3, - delay=10, - ) - .stdout.decode() - .rstrip() + res = retry_exec_test_command_with_delay( + BASE_CMDS["linodes"] + + [ + "disk-create", + linode_id, + "--size", + "15", + "--label", + label, + "--text", + ], + retries=3, + delay=10, ) headers = ["id", "label", "status", "size", "filesystem", "disk_encryption"] @@ -130,15 +95,14 @@ def disk_poll_func(): assert label in res assert "15" in res - # assert "disabled" in res def test_disk_reset_password(linode_instance_disk_tests): linode_id = linode_instance_disk_tests disk_id = get_disk_ids(linode_id)[1] - res = retry_exec_test_command_with_delay( - BASE_CMD + retry_exec_test_command_with_delay( + BASE_CMDS["linodes"] + [ "disk-reset-password", linode_id, @@ -151,8 +115,6 @@ def test_disk_reset_password(linode_instance_disk_tests): delay=10, ) - assert res.returncode == 0 - def test_disk_update(linode_instance_disk_tests): linode_id = linode_instance_disk_tests @@ -160,24 +122,18 @@ def test_disk_update(linode_instance_disk_tests): update_label = get_random_text(5) + "newdisk" - res = ( - ( - retry_exec_test_command_with_delay( - BASE_CMD - + [ - "disk-update", - linode_id, - disk_id, - "--label", - update_label, - "--text", - ], - retries=3, - delay=10, - ) - ) - .stdout.decode() - .rstrip() + res = retry_exec_test_command_with_delay( + BASE_CMDS["linodes"] + + [ + "disk-update", + linode_id, + disk_id, + "--label", + update_label, + "--text", + ], + retries=3, + delay=10, ) headers = ["id", "label", "status", "size", "filesystem", "disk_encryption"] @@ -189,19 +145,13 @@ def test_disk_update(linode_instance_disk_tests): def test_disks_list(linode_instance_disk_tests): linode_id = linode_instance_disk_tests - res = ( - ( - retry_exec_test_command_with_delay( - BASE_CMD - + [ - "disks-list", - linode_id, - "--text", - ] - ) - ) - .stdout.decode() - .rstrip() + res = retry_exec_test_command_with_delay( + BASE_CMDS["linodes"] + + [ + "disks-list", + linode_id, + "--text", + ] ) headers = ["id", "label", "status", "size", "filesystem", "disk_encryption"] diff --git a/tests/integration/linodes/test_interfaces.py b/tests/integration/linodes/test_interfaces.py index 7adf67472..ef47ba8e5 100644 --- a/tests/integration/linodes/test_interfaces.py +++ b/tests/integration/linodes/test_interfaces.py @@ -1,65 +1,13 @@ import json from typing import Any, Dict -import pytest - -from tests.integration.conftest import create_vpc_w_subnet -from tests.integration.helpers import delete_target_id, exec_test_command -from tests.integration.linodes.helpers_linodes import ( - BASE_CMD, - DEFAULT_RANDOM_PASS, - DEFAULT_TEST_IMAGE, +from tests.integration.helpers import ( + BASE_CMDS, + exec_test_command, +) +from tests.integration.linodes.fixtures import ( # noqa: F401 + linode_with_vpc_interface_as_json, ) - - -@pytest.fixture -def linode_with_vpc_interface_as_json(linode_cloud_firewall): - vpc_json = create_vpc_w_subnet() - - vpc_region = vpc_json["region"] - vpc_id = str(vpc_json["id"]) - subnet_id = int(vpc_json["subnets"][0]["id"]) - - linode_json = json.loads( - exec_test_command( - BASE_CMD - + [ - "create", - "--type", - "g6-nanode-1", - "--region", - vpc_region, - "--image", - DEFAULT_TEST_IMAGE, - "--root_pass", - DEFAULT_RANDOM_PASS, - "--firewall_id", - linode_cloud_firewall, - "--interfaces", - json.dumps( - [ - { - "purpose": "vpc", - "primary": True, - "subnet_id": subnet_id, - "ipv4": {"nat_1_1": "any", "vpc": "10.0.0.5"}, - "ip_ranges": ["10.0.0.6/32"], - }, - {"purpose": "public"}, - ] - ), - "--json", - "--suppress-warnings", - ] - ) - .stdout.decode() - .rstrip() - )[0] - - yield linode_json, vpc_json - - delete_target_id(target="linodes", id=str(linode_json["id"])) - delete_target_id(target="vpcs", id=vpc_id) def assert_interface_configuration( @@ -67,7 +15,7 @@ def assert_interface_configuration( ): config_json = json.loads( exec_test_command( - BASE_CMD + BASE_CMDS["linodes"] + [ "configs-list", str(linode_json["id"]), @@ -75,8 +23,6 @@ def assert_interface_configuration( "--suppress-warnings", ] ) - .stdout.decode() - .rstrip() )[0] vpc_interface = config_json["interfaces"][0] diff --git a/tests/integration/linodes/test_linode_interfaces.py b/tests/integration/linodes/test_linode_interfaces.py index b9e0b70ae..f36ed1a50 100644 --- a/tests/integration/linodes/test_linode_interfaces.py +++ b/tests/integration/linodes/test_linode_interfaces.py @@ -6,143 +6,20 @@ from tests.integration.helpers import ( delete_target_id, exec_test_command, - get_random_text, ) -from tests.integration.linodes.helpers_linodes import ( - DEFAULT_LABEL, +from tests.integration.linodes.fixtures import ( # noqa: F401 + linode_interface_legacy, + linode_interface_public, + linode_interface_vlan, + linode_interface_vpc, +) +from tests.integration.linodes.helpers import ( create_linode, + get_interface_id, + get_ipv4_addr, wait_until, ) -linode_label = DEFAULT_LABEL + get_random_text(5) - - -@pytest.fixture(scope="session") -def linode_interface_public(linode_cloud_firewall): - linode_id = create_linode( - firewall_id=linode_cloud_firewall, - test_region="us-sea", - interface_generation="linode", - interfaces='[{"public": {"ipv4": {"addresses": [{"primary": true}]}}, "default_route": {"ipv4": true, "ipv6": true }, "firewall_id":' - + linode_cloud_firewall - + "}]", - ) - - yield linode_id - - delete_target_id(target="linodes", id=linode_id) - - -@pytest.fixture(scope="session") -def linode_interface_vlan(linode_cloud_firewall): - linode_id = create_linode( - firewall_id=linode_cloud_firewall, - test_region="us-sea", - interface_generation="linode", - interfaces='[{"vlan": {"ipam_address": "10.0.0.1/24","vlan_label": "my-vlan"}}]', - ) - - yield linode_id - - delete_target_id(target="linodes", id=linode_id) - - -@pytest.fixture(scope="session") -def linode_interface_legacy(linode_cloud_firewall): - linode_id = create_linode( - firewall_id=linode_cloud_firewall, - test_region="us-sea", - interface_generation="legacy_config", - ) - - yield linode_id - - delete_target_id(target="linodes", id=linode_id) - - -@pytest.fixture(scope="session") -def linode_interface_vpc(linode_cloud_firewall): - vpc_output = json.loads( - exec_test_command( - [ - "linode-cli", - "vpcs", - "create", - "--label", - get_random_text(5) + "-vpc", - "--region", - "us-sea", - "--subnets.ipv4", - "10.0.0.0/24", - "--subnets.label", - get_random_text(5) + "-vpc", - "--json", - "--suppress-warnings", - ] - ) - .stdout.decode() - .rstrip() - ) - - subnet_id = vpc_output[0]["subnets"][0]["id"] - - linode_id = create_linode( - firewall_id=linode_cloud_firewall, - test_region="us-sea", - interface_generation="linode", - interfaces='[{"default_route":{"ipv4":true},"firewall_id":' - + str(linode_cloud_firewall) - + ',"vpc":{"ipv4":{"addresses":[{"address":"auto","nat_1_1_address":"auto","primary":true}]},"subnet_id":' - + str(subnet_id) - + "}}]", - ) - - yield linode_id - - delete_target_id(target="linodes", id=linode_id) - delete_target_id(target="vpcs", id=str(vpc_output[0]["id"])) - - -def get_interface_id(linode_id: str): - data = json.loads( - exec_test_command( - [ - "linode-cli", - "linodes", - "interfaces-list", - linode_id, - "--json", - ] - ) - .stdout.decode() - .rstrip() - ) - - interface_id = data[0]["interfaces"]["id"] - - return str(interface_id) - - -def get_ipv4_addr(linode_id: "str"): - data = json.loads( - exec_test_command( - [ - "linode-cli", - "linodes", - "ips-list", - linode_id, - "--json", - ] - ) - .stdout.decode() - .rstrip() - ) - - ipv4_public = data[0]["ipv4"]["public"] - ipv4_addr = ipv4_public[0]["address"] if ipv4_public else None - - return str(ipv4_addr) - def test_interface_add(linode_cloud_firewall, monkeypatch: MonkeyPatch): monkeypatch.setenv("LINODE_CLI_API_VERSION", "v4beta") @@ -188,8 +65,6 @@ def test_interface_add(linode_cloud_firewall, monkeypatch: MonkeyPatch): "--json", ] ) - .stdout.decode() - .rstrip() ) interface = data[0] @@ -235,8 +110,6 @@ def test_interface_firewalls_list( "--json", ] ) - .stdout.decode() - .rstrip() ) firewall = data[0] @@ -280,8 +153,6 @@ def test_interface_settings_update( "--json", ] ) - .stdout.decode() - .rstrip() ) settings = data[0] @@ -328,8 +199,6 @@ def test_interface_update(linode_interface_public, monkeypatch: MonkeyPatch): "--json", ] ) - .stdout.decode() - .rstrip() ) assert data[0]["id"] == int(interface_id) @@ -367,8 +236,6 @@ def test_interface_view(linode_interface_vpc, monkeypatch: MonkeyPatch): "--json", ] ) - .stdout.decode() - .rstrip() ) interface = data[0] @@ -404,8 +271,6 @@ def test_interfaces_list(linode_interface_vlan, monkeypatch: MonkeyPatch): "--json", ] ) - .stdout.decode() - .rstrip() ) assert len(data) >= 1 # At least one interface listed @@ -455,8 +320,6 @@ def test_interfaces_upgrade(linode_interface_legacy, monkeypatch: MonkeyPatch): "--json", ] ) - .stdout.decode() - .rstrip() ) upgrade = data[0] diff --git a/tests/integration/linodes/test_linodes.py b/tests/integration/linodes/test_linodes.py index 2bc07a8f1..c21b09fdf 100644 --- a/tests/integration/linodes/test_linodes.py +++ b/tests/integration/linodes/test_linodes.py @@ -5,91 +5,30 @@ from linodecli.exit_codes import ExitCodes from tests.integration.helpers import ( + BASE_CMDS, assert_headers_in_lines, delete_target_id, exec_failing_test_command, exec_test_command, get_random_region_with_caps, ) -from tests.integration.linodes.helpers_linodes import ( - BASE_CMD, +from tests.integration.linodes.fixtures import ( # noqa: F401 + linode_min_req, + linode_with_label, + linode_wo_image, + test_linode_instance, +) +from tests.integration.linodes.helpers import ( DEFAULT_LABEL, - DEFAULT_RANDOM_PASS, DEFAULT_TEST_IMAGE, create_linode, + get_disk_id, wait_until, ) -timestamp = str(time.time_ns()) -linode_label = DEFAULT_LABEL + timestamp - - -@pytest.fixture(scope="package", autouse=True) -def test_linode_instance(linode_cloud_firewall): - linode_id = ( - exec_test_command( - BASE_CMD - + [ - "create", - "--type", - "g6-nanode-1", - "--region", - "us-ord", - "--image", - DEFAULT_TEST_IMAGE, - "--label", - linode_label, - "--root_pass", - DEFAULT_RANDOM_PASS, - "--firewall_id", - linode_cloud_firewall, - "--text", - "--delimiter", - ",", - "--no-headers", - "--format", - "id", - "--no-defaults", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield linode_id - - delete_target_id(target="linodes", id=linode_id) - - -@pytest.fixture -def test_disk_id(test_linode_instance): - linode_id = test_linode_instance - disk_id = ( - exec_test_command( - BASE_CMD - + [ - "disks-list", - linode_id, - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) - first_id = disk_id[0].split(",")[0] - yield first_id - def test_update_linode_with_a_image(): - result = exec_test_command(BASE_CMD + ["update", "--help"]).stdout.decode() + result = exec_test_command(BASE_CMDS["linodes"] + ["update", "--help"]) assert "--image" not in result @@ -107,7 +46,7 @@ def test_create_linodes_with_a_label(linode_with_label): def test_view_linode_configuration(test_linode_instance): linode_id = test_linode_instance result = exec_test_command( - BASE_CMD + BASE_CMDS["linodes"] + [ "view", linode_id, @@ -119,17 +58,17 @@ def test_view_linode_configuration(test_linode_instance): "id,label,region,type,image", "--no-defaults", ] - ).stdout.decode() + ) - assert re.search( - linode_id - + "," - + linode_label - + ",us-ord,g6-nanode-1," - + DEFAULT_TEST_IMAGE, - result, + pattern = ( + re.escape(linode_id) + + r",[^,]+" # matches label (with hyphens) + + r",us-ord,g6-nanode-1," + + re.escape(DEFAULT_TEST_IMAGE) ) + assert re.search(pattern, result) + def test_create_linode_with_min_required_props(linode_min_req): result = linode_min_req @@ -138,7 +77,7 @@ def test_create_linode_with_min_required_props(linode_min_req): def test_create_linodes_fails_without_a_root_pass(): result = exec_failing_test_command( - BASE_CMD + BASE_CMDS["linodes"] + [ "create", "--type", @@ -151,7 +90,7 @@ def test_create_linodes_fails_without_a_root_pass(): "--no-headers", ], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) assert "Request failed: 400" in result assert "root_pass root_pass is required" in result @@ -162,18 +101,19 @@ def test_create_linode_without_image_and_not_boot(linode_wo_image): wait_until(linode_id=linode_id, timeout=180, status="offline") result = exec_test_command( - BASE_CMD + BASE_CMDS["linodes"] + ["view", linode_id, "--format", "status", "--text", "--no-headers"] - ).stdout.decode() + ) assert "offline" in result def test_list_linodes(test_linode_instance): result = exec_test_command( - BASE_CMD + ["list", "--format", "label", "--text", "--no-headers"] - ).stdout.decode() - assert linode_label in result + BASE_CMDS["linodes"] + + ["list", "--format", "label", "--text", "--no-headers"] + ) + assert DEFAULT_LABEL in result def test_add_tag_to_linode(test_linode_instance): @@ -181,7 +121,7 @@ def test_add_tag_to_linode(test_linode_instance): unique_tag = "tag" + str(int(time.time())) result = exec_test_command( - BASE_CMD + BASE_CMDS["linodes"] + [ "update", linode_id, @@ -192,25 +132,21 @@ def test_add_tag_to_linode(test_linode_instance): "--text", "--no-headers", ] - ).stdout.decode() + ) assert unique_tag in result def list_disk_list(test_linode_instance): linode_id = test_linode_instance - res = ( - exec_test_command( - BASE_CMD - + [ - "disks-list", - linode_id, - "--text", - "--delimiter=,", - ] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["linodes"] + + [ + "disks-list", + linode_id, + "--text", + "--delimiter=,", + ] ) lines = res.splitlines() @@ -218,22 +154,18 @@ def list_disk_list(test_linode_instance): assert_headers_in_lines(headers, lines) -def test_disk_view(test_linode_instance, test_disk_id): +def test_disk_view(test_linode_instance): linode_id = test_linode_instance - disk_id = test_disk_id - res = ( - exec_test_command( - BASE_CMD - + [ - "disk-view", - linode_id, - disk_id, - "--text", - "--delimiter=,", - ] - ) - .stdout.decode() - .rstrip() + disk_id = get_disk_id(linode_id) + res = exec_test_command( + BASE_CMDS["linodes"] + + [ + "disk-view", + linode_id, + disk_id, + "--text", + "--delimiter=,", + ] ) lines = res.splitlines() @@ -252,19 +184,15 @@ def test_create_linode_disk_encryption_enabled(linode_cloud_firewall): test_region=test_region, ) - res = ( - exec_test_command( - BASE_CMD - + [ - "view", - linode_id, - "--text", - "--delimiter=,", - "--format=id,disk_encryption", - ] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["linodes"] + + [ + "view", + linode_id, + "--text", + "--delimiter=,", + "--format=id,disk_encryption", + ] ) headers = ["id", "disk_encryption"] @@ -286,19 +214,15 @@ def test_create_linode_disk_encryption_disabled(linode_cloud_firewall): test_region=test_region, ) - res = ( - exec_test_command( - BASE_CMD - + [ - "view", - linode_id, - "--text", - "--delimiter=,", - "--format=id,disk_encryption", - ] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["linodes"] + + [ + "view", + linode_id, + "--text", + "--delimiter=,", + "--format=id,disk_encryption", + ] ) headers = ["id", "disk_encryption"] diff --git a/tests/integration/linodes/test_power_status.py b/tests/integration/linodes/test_power_status.py index 984035ad5..2bd3aac2a 100644 --- a/tests/integration/linodes/test_power_status.py +++ b/tests/integration/linodes/test_power_status.py @@ -1,47 +1,22 @@ import pytest from tests.integration.helpers import ( - delete_target_id, + BASE_CMDS, exec_test_command, retry_exec_test_command_with_delay, ) -from tests.integration.linodes.helpers_linodes import ( - BASE_CMD, - create_linode_and_wait, +from tests.integration.linodes.fixtures import ( # noqa: F401 + linode_in_running_state_for_reboot, + linode_instance_basic, +) +from tests.integration.linodes.helpers import ( wait_until, ) -@pytest.fixture -def test_linode_id(linode_cloud_firewall): - linode_id = create_linode_and_wait(firewall_id=linode_cloud_firewall) - - yield linode_id - - delete_target_id(target="linodes", id=linode_id) - - -@pytest.fixture -def linode_in_running_state(linode_cloud_firewall): - linode_id = create_linode_and_wait(firewall_id=linode_cloud_firewall) - - yield linode_id - - delete_target_id("linodes", linode_id) - - -@pytest.fixture -def linode_in_running_state_for_reboot(linode_cloud_firewall): - linode_id = create_linode_and_wait(firewall_id=linode_cloud_firewall) - - yield linode_id - - delete_target_id("linodes", linode_id) - - @pytest.mark.smoke -def test_create_linode_and_boot(test_linode_id): - linode_id = test_linode_id +def test_create_linode_and_boot(linode_instance_basic): + linode_id = linode_instance_basic # returns false if status is not running after 240s result = wait_until(linode_id=linode_id, timeout=240, status="running") @@ -56,7 +31,9 @@ def test_reboot_linode(linode_in_running_state_for_reboot): # reboot linode from "running" status retry_exec_test_command_with_delay( - BASE_CMD + ["reboot", linode_id, "--text", "--no-headers"], 3, 20 + BASE_CMDS["linodes"] + ["reboot", linode_id, "--text", "--no-headers"], + 3, + 20, ) assert wait_until( @@ -65,8 +42,8 @@ def test_reboot_linode(linode_in_running_state_for_reboot): @pytest.mark.flaky(reruns=3, reruns_delay=2) -def test_shutdown_linode(test_linode_id): - linode_id = test_linode_id +def test_shutdown_linode(linode_instance_basic): + linode_id = linode_instance_basic # returns false if status is not running after 240s after reboot assert wait_until( @@ -74,7 +51,7 @@ def test_shutdown_linode(test_linode_id): ), "Linode status has not changed to running from provisioning" # shutdown linode that is in running state - exec_test_command(BASE_CMD + ["shutdown", linode_id]) + exec_test_command(BASE_CMDS["linodes"] + ["shutdown", linode_id]) result = wait_until(linode_id=linode_id, timeout=180, status="offline") diff --git a/tests/integration/linodes/test_rebuild.py b/tests/integration/linodes/test_rebuild.py index 707bebff0..0d0530e4d 100644 --- a/tests/integration/linodes/test_rebuild.py +++ b/tests/integration/linodes/test_rebuild.py @@ -4,40 +4,24 @@ from linodecli.exit_codes import ExitCodes from tests.integration.helpers import ( - delete_target_id, + BASE_CMDS, exec_failing_test_command, exec_test_command, - get_random_region_with_caps, retry_exec_test_command_with_delay, ) -from tests.integration.linodes.helpers_linodes import ( - BASE_CMD, +from tests.integration.linodes.fixtures import ( # noqa: F401 + linode_for_rebuild_tests, +) +from tests.integration.linodes.helpers import ( DEFAULT_RANDOM_PASS, - create_linode_and_wait, wait_until, ) -@pytest.fixture -def linode_for_rebuild_tests(linode_cloud_firewall): - test_region = get_random_region_with_caps( - required_capabilities=["Linodes", "Disk Encryption"] - ) - linode_id = create_linode_and_wait( - firewall_id=linode_cloud_firewall, - disk_encryption=False, - test_region=test_region, - ) - - yield linode_id - - delete_target_id(target="linodes", id=linode_id) - - @pytest.mark.flaky(reruns=3, reruns_delay=2) def test_rebuild_fails_without_image(linode_for_rebuild_tests): result = exec_failing_test_command( - BASE_CMD + BASE_CMDS["linodes"] + [ "rebuild", "--root_pass", @@ -47,7 +31,7 @@ def test_rebuild_fails_without_image(linode_for_rebuild_tests): "--no-headers", ], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) assert "Request failed: 400" in result assert "You must specify an image" in result @@ -58,7 +42,7 @@ def test_rebuild_fails_with_invalid_image(linode_for_rebuild_tests): rebuild_image = "bad/image" result = exec_failing_test_command( - BASE_CMD + BASE_CMDS["linodes"] + [ "rebuild", "--image", @@ -69,7 +53,7 @@ def test_rebuild_fails_with_invalid_image(linode_for_rebuild_tests): "--text", "--no-headers", ] - ).stderr.decode() + ) assert "Request failed: 400" in result @@ -85,7 +69,7 @@ def test_rebuild_a_linode(linode_for_rebuild_tests): # trigger rebuild exec_test_command( - BASE_CMD + BASE_CMDS["linodes"] + [ "rebuild", "--image", @@ -96,7 +80,7 @@ def test_rebuild_a_linode(linode_for_rebuild_tests): "--no-headers", linode_id, ] - ).stdout.decode() + ) # check status for rebuilding assert wait_until( @@ -109,7 +93,7 @@ def test_rebuild_a_linode(linode_for_rebuild_tests): ), "linode failed to change status to running from rebuilding.." result = exec_test_command( - BASE_CMD + BASE_CMDS["linodes"] + ["view", linode_id, "--format", "image", "--text", "--no-headers"] ).stdout.decode() assert rebuild_image in result @@ -126,7 +110,7 @@ def test_rebuild_linode_disk_encryption_enabled(linode_for_rebuild_tests): # trigger rebuild retry_exec_test_command_with_delay( - BASE_CMD + BASE_CMDS["linodes"] + [ "rebuild", linode_id, @@ -141,7 +125,7 @@ def test_rebuild_linode_disk_encryption_enabled(linode_for_rebuild_tests): ], retries=3, delay=10, - ).stdout.decode() + ) # check status for rebuilding assert wait_until( @@ -154,7 +138,7 @@ def test_rebuild_linode_disk_encryption_enabled(linode_for_rebuild_tests): ), "linode failed to change status to running from rebuilding.." result = exec_test_command( - BASE_CMD + BASE_CMDS["linodes"] + [ "view", linode_id, @@ -164,7 +148,7 @@ def test_rebuild_linode_disk_encryption_enabled(linode_for_rebuild_tests): "--no-headers", "--format=id,image,disk_encryption", ] - ).stdout.decode() + ) assert "enabled" in result assert rebuild_image in result @@ -181,7 +165,7 @@ def test_rebuild_linode_disk_encryption_disabled(linode_for_rebuild_tests): # trigger rebuild retry_exec_test_command_with_delay( - BASE_CMD + BASE_CMDS["linodes"] + [ "rebuild", linode_id, @@ -196,7 +180,7 @@ def test_rebuild_linode_disk_encryption_disabled(linode_for_rebuild_tests): ], retries=3, delay=10, - ).stdout.decode() + ) # check status for rebuilding assert wait_until( @@ -209,7 +193,7 @@ def test_rebuild_linode_disk_encryption_disabled(linode_for_rebuild_tests): ), "linode failed to change status to running from rebuilding.." result = retry_exec_test_command_with_delay( - BASE_CMD + BASE_CMDS["linodes"] + [ "view", linode_id, @@ -221,7 +205,7 @@ def test_rebuild_linode_disk_encryption_disabled(linode_for_rebuild_tests): ], retries=3, delay=10, - ).stdout.decode() + ) assert "disabled" in result assert rebuild_image in result diff --git a/tests/integration/linodes/test_resize.py b/tests/integration/linodes/test_resize.py index 5213cee7b..32e2d6ece 100644 --- a/tests/integration/linodes/test_resize.py +++ b/tests/integration/linodes/test_resize.py @@ -4,66 +4,35 @@ from linodecli.exit_codes import ExitCodes from tests.integration.helpers import ( - delete_target_id, + BASE_CMDS, exec_failing_test_command, exec_test_command, ) -from tests.integration.linodes.helpers_linodes import ( - BASE_CMD, - create_linode_and_wait, +from tests.integration.linodes.fixtures import ( # noqa: F401 + linode_instance_for_resize_tests, +) +from tests.integration.linodes.helpers import ( wait_until, ) -@pytest.fixture(scope="session") -def test_linode_id(linode_cloud_firewall): - plan = ( - exec_test_command( - [ - "linode-cli", - "linodes", - "types", - "--format", - "id", - "--text", - "--no-headers", - ] - ) - .stdout.decode() - .rstrip() - .splitlines()[1] - ) - linode_id = create_linode_and_wait( - firewall_id=linode_cloud_firewall, test_plan=plan - ) - - yield linode_id - - delete_target_id(target="linodes", id=linode_id) - - @pytest.mark.flaky(reruns=3, reruns_delay=2) -def test_resize_fails_to_the_same_plan(test_linode_id): - linode_id = test_linode_id - linode_plan = ( - exec_test_command( - [ - "linode-cli", - "linodes", - "view", - linode_id, - "--format", - "type", - "--text", - "--no-headers", - ] - ) - .stdout.decode() - .rstrip() +def test_resize_fails_to_the_same_plan(linode_instance_for_resize_tests): + linode_id = linode_instance_for_resize_tests + linode_plan = exec_test_command( + BASE_CMDS["linodes"] + + [ + "view", + linode_id, + "--format", + "type", + "--text", + "--no-headers", + ] ) result = exec_failing_test_command( - BASE_CMD + BASE_CMDS["linodes"] + [ "resize", "--type", @@ -73,33 +42,27 @@ def test_resize_fails_to_the_same_plan(test_linode_id): linode_id, ], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) assert "Request failed: 400" in result assert "Linode is already running this service plan." in result -def test_resize_fails_to_smaller_plan(test_linode_id): - linode_id = test_linode_id - smaller_plan = ( - exec_test_command( - [ - "linode-cli", - "linodes", - "types", - "--format", - "id", - "--text", - "--no-headers", - ] - ) - .stdout.decode() - .rstrip() - .splitlines()[0] - ) +def test_resize_fails_to_smaller_plan(linode_instance_for_resize_tests): + linode_id = linode_instance_for_resize_tests + smaller_plan = exec_test_command( + BASE_CMDS["linodes"] + + [ + "types", + "--format", + "id", + "--text", + "--no-headers", + ] + ).splitlines()[0] result = exec_failing_test_command( - BASE_CMD + BASE_CMDS["linodes"] + [ "resize", "--type", @@ -109,7 +72,7 @@ def test_resize_fails_to_smaller_plan(test_linode_id): linode_id, ], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) assert "Request failed: 400" in result assert ( @@ -118,12 +81,12 @@ def test_resize_fails_to_smaller_plan(test_linode_id): ) -def test_resize_fail_to_invalid_plan(test_linode_id): +def test_resize_fail_to_invalid_plan(linode_instance_for_resize_tests): invalid_plan = "g15-bad-plan" - linode_id = test_linode_id + linode_id = linode_instance_for_resize_tests result = exec_failing_test_command( - BASE_CMD + BASE_CMDS["linodes"] + [ "resize", "--type", @@ -133,7 +96,7 @@ def test_resize_fail_to_invalid_plan(test_linode_id): linode_id, ], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) assert "Request failed: 400" in result assert "type A valid plan type by that ID was not found" in result @@ -143,27 +106,20 @@ def test_resize_fail_to_invalid_plan(test_linode_id): os.environ.get("RUN_LONG_TESTS", None) != "True", reason="Skipping long-running Test, to run set RUN_LONG_TESTS=True", ) -def test_resize_to_next_size_plan(test_linode_id): - linode_id = test_linode_id - larger_plan = ( - exec_test_command( - [ - "linode-cli", - "linodes", - "types", - "--format", - "id", - "--text", - "--no-headers", - ] - ) - .stdout.decode() - .rstrip() - .splitlines()[2] - ) +def test_resize_to_next_size_plan(linode_instance_for_resize_tests): + linode_id = linode_instance_for_resize_tests + larger_plan = exec_test_command( + BASE_CMDS["linodes"][ + "types", + "--format", + "id", + "--text", + "--no-headers", + ] + ).splitlines()[2] exec_test_command( - BASE_CMD + BASE_CMDS["linodes"] + [ "resize", "--allow_auto_disk_resize=true", @@ -187,8 +143,8 @@ def test_resize_to_next_size_plan(test_linode_id): ), "linode failed to change status to resizing.." result = exec_test_command( - BASE_CMD + BASE_CMDS["linodes"] + ["view", linode_id, "--format", "type", "--text", "--no-headers"] - ).stdout.decode() + ) assert larger_plan in result diff --git a/tests/integration/linodes/test_types.py b/tests/integration/linodes/test_types.py index a92b53fac..73939956d 100644 --- a/tests/integration/linodes/test_types.py +++ b/tests/integration/linodes/test_types.py @@ -6,9 +6,7 @@ # verifying the DC pricing changes along with types @pytest.mark.smoke def test_linode_type(): - output = exec_test_command( - ["linode-cli", "linodes", "types", "--text"] - ).stdout.decode() + output = exec_test_command(["linode-cli", "linodes", "types", "--text"]) headers = [ "id", diff --git a/tests/integration/lke/fixtures.py b/tests/integration/lke/fixtures.py new file mode 100644 index 000000000..6b2c285ad --- /dev/null +++ b/tests/integration/lke/fixtures.py @@ -0,0 +1,140 @@ +import pytest + +from tests.integration.helpers import ( + BASE_CMDS, + delete_target_id, + exec_test_command, + get_random_region_with_caps, + get_random_text, +) +from tests.integration.lke.helpers import get_cluster_id + + +@pytest.fixture(scope="session") +def lke_cluster(): + label = get_random_text(8) + "_cluster" + + test_region = get_random_region_with_caps( + required_capabilities=["Linodes", "Kubernetes"] + ) + lke_version = exec_test_command( + BASE_CMDS["lke"] + + [ + "versions-list", + "--text", + "--no-headers", + ] + ).splitlines()[0] + + cluster_label = exec_test_command( + BASE_CMDS["lke"] + + [ + "cluster-create", + "--region", + test_region, + "--label", + label, + "--node_pools.type", + "g6-standard-1", + "--node_pools.count", + "1", + "--node_pools.disks", + '[{"type":"ext4","size":1024}]', + "--k8s_version", + lke_version, + "--text", + "--delimiter", + ",", + "--no-headers", + "--format", + "label", + "--no-defaults", + ] + ) + + cluster_id = get_cluster_id(label=cluster_label) + + yield cluster_id + + delete_target_id( + target="lke", id=cluster_id, delete_command="cluster-delete" + ) + + +@pytest.fixture(scope="session") +def node_pool(lke_cluster): + cluster_id = lke_cluster + + node_pool_id = exec_test_command( + BASE_CMDS["lke"] + + [ + "pool-create", + cluster_id, + "--count", + "1", + "--type", + "g6-standard-4", + "--labels", + '{ "example.com/my-app":"team1" }', + "--text", + "--format=id", + "--no-headers", + ] + ) + + yield node_pool_id + + +@pytest.fixture(scope="session") +def lke_cluster_acl(): + label = get_random_text(8) + "_cluster" + + test_region = get_random_region_with_caps( + required_capabilities=["Linodes", "Kubernetes"] + ) + lke_version = exec_test_command( + BASE_CMDS["lke"] + + [ + "versions-list", + "--text", + "--no-headers", + ] + ).splitlines()[0] + + cluster_label = exec_test_command( + BASE_CMDS["lke"] + + [ + "cluster-create", + "--region", + test_region, + "--label", + label, + "--node_pools.type", + "g6-standard-1", + "--node_pools.count", + "1", + "--node_pools.disks", + '[{"type":"ext4","size":1024}]', + "--k8s_version", + lke_version, + "--control_plane.high_availability", + "true", + "--control_plane.acl.enabled", + "true", + "--text", + "--delimiter", + ",", + "--no-headers", + "--format", + "label", + "--no-defaults", + ] + ) + + cluster_id = get_cluster_id(label=cluster_label) + + yield cluster_id + + delete_target_id( + target="lke", id=cluster_id, delete_command="cluster-delete" + ) diff --git a/tests/integration/lke/helpers.py b/tests/integration/lke/helpers.py new file mode 100644 index 000000000..b8e7f72cc --- /dev/null +++ b/tests/integration/lke/helpers.py @@ -0,0 +1,97 @@ +import json + +from tests.integration.helpers import ( + BASE_CMDS, + exec_test_command, +) + + +def get_lke_version_id(): + version_id = exec_test_command( + BASE_CMDS["lke"] + + [ + "versions-list", + "--text", + "--no-headers", + "--delimiter", + ",", + "--format", + "id", + ] + ).splitlines() + + first_id = version_id[0] + + return first_id + + +def get_node_pool_id(cluster_id): + cluster_id + nodepool_id = exec_test_command( + BASE_CMDS["lke"] + + [ + "pools-list", + cluster_id, + "--text", + "--no-headers", + "--format", + "id", + ] + ).splitlines() + + first_id = nodepool_id[0] + + return first_id + + +def get_pool_nodesid(cluster_id): + cluster_id + nodepool_id = exec_test_command( + BASE_CMDS["lke"] + + [ + "pools-list", + cluster_id, + "--text", + "--no-headers", + "--format", + "nodes.id", + ] + ).splitlines() + + first_id = nodepool_id[0] + + return first_id + + +def get_lke_enterprise_id(): + enterprise_tier_info_list = exec_test_command( + BASE_CMDS["lke"] + + [ + "tiered-versions-list", + "enterprise", + "--json", + ] + ) + + parsed = json.loads(enterprise_tier_info_list) + + enterprise_ti = parsed[0] + + return enterprise_ti.get("id") + + +def get_cluster_id(label: str): + cluster_id = exec_test_command( + [ + "linode-cli", + "lke", + "clusters-list", + "--text", + "--format=id", + "--no-headers", + "--label", + label, + ] + ) + + return cluster_id diff --git a/tests/integration/lke/test_clusters.py b/tests/integration/lke/test_clusters.py index f79249dcb..b80aa5f1a 100644 --- a/tests/integration/lke/test_clusters.py +++ b/tests/integration/lke/test_clusters.py @@ -3,148 +3,21 @@ import pytest from tests.integration.helpers import ( + BASE_CMDS, assert_headers_in_lines, delete_target_id, exec_test_command, - get_cluster_id, get_random_region_with_caps, get_random_text, retry_exec_test_command_with_delay, ) - -BASE_CMD = ["linode-cli", "lke"] - - -def get_lke_version_id(): - version_id = ( - exec_test_command( - BASE_CMD - + [ - "versions-list", - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) - - first_id = version_id[0] - - return first_id - - -def get_node_pool_id(cluster_id): - cluster_id - nodepool_id = ( - exec_test_command( - BASE_CMD - + [ - "pools-list", - cluster_id, - "--text", - "--no-headers", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) - - first_id = nodepool_id[0] - - return first_id - - -def get_pool_nodesid(cluster_id): - cluster_id - nodepool_id = ( - exec_test_command( - BASE_CMD - + [ - "pools-list", - cluster_id, - "--text", - "--no-headers", - "--format", - "nodes.id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) - - first_id = nodepool_id[0] - - return first_id - - -@pytest.fixture -def test_lke_cluster(): - label = get_random_text(8) + "_cluster" - - test_region = get_random_region_with_caps( - required_capabilities=["Linodes", "Kubernetes"] - ) - lke_version = ( - exec_test_command( - BASE_CMD - + [ - "versions-list", - "--text", - "--no-headers", - ] - ) - .stdout.decode() - .rstrip() - .splitlines()[0] - ) - - cluster_label = ( - exec_test_command( - BASE_CMD - + [ - "cluster-create", - "--region", - test_region, - "--label", - label, - "--node_pools.type", - "g6-standard-1", - "--node_pools.count", - "1", - "--node_pools.disks", - '[{"type":"ext4","size":1024}]', - "--k8s_version", - lke_version, - "--text", - "--delimiter", - ",", - "--no-headers", - "--format", - "label", - "--no-defaults", - ] - ) - .stdout.decode() - .rstrip() - ) - - cluster_id = get_cluster_id(label=cluster_label) - - yield cluster_id - - delete_target_id( - target="lke", id=cluster_id, delete_command="cluster-delete" - ) +from tests.integration.lke.fixtures import lke_cluster, node_pool # noqa: F401 +from tests.integration.lke.helpers import ( + get_cluster_id, + get_lke_version_id, + get_node_pool_id, + get_pool_nodesid, +) @pytest.mark.smoke @@ -154,46 +27,37 @@ def test_deploy_an_lke_cluster(): test_region = get_random_region_with_caps( required_capabilities=["Linodes", "Kubernetes"] ) - lke_version = ( - exec_test_command( - BASE_CMD - + [ - "versions-list", - "--text", - "--no-headers", - ] - ) - .stdout.decode() - .rstrip() - .splitlines()[0] - ) - - cluster_label = ( - exec_test_command( - BASE_CMD - + [ - "cluster-create", - "--region", - test_region, - "--label", - label, - "--node_pools.type", - "g6-standard-1", - "--node_pools.count", - "1", - "--node_pools.disks", - '[{"type":"ext4","size":1024}]', - "--k8s_version", - lke_version, - "--text", - "--delimiter", - ",", - "--no-headers", - "--format=label", - ] - ) - .stdout.decode() - .rstrip() + lke_version = exec_test_command( + BASE_CMDS["lke"] + + [ + "versions-list", + "--text", + "--no-headers", + ] + ).splitlines()[0] + + cluster_label = exec_test_command( + BASE_CMDS["lke"] + + [ + "cluster-create", + "--region", + test_region, + "--label", + label, + "--node_pools.type", + "g6-standard-1", + "--node_pools.count", + "1", + "--node_pools.disks", + '[{"type":"ext4","size":1024}]', + "--k8s_version", + lke_version, + "--text", + "--delimiter", + ",", + "--no-headers", + "--format=label", + ] ) assert label == cluster_label @@ -206,12 +70,8 @@ def test_deploy_an_lke_cluster(): def test_lke_cluster_list(): - res = ( - exec_test_command( - BASE_CMD + ["clusters-list", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["lke"] + ["clusters-list", "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -219,56 +79,45 @@ def test_lke_cluster_list(): assert_headers_in_lines(headers, lines) -def test_view_lke_cluster(test_lke_cluster): - cluster_id = test_lke_cluster +def test_view_lke_cluster(lke_cluster): + cluster_id = lke_cluster - res = ( - exec_test_command( - BASE_CMD + ["cluster-view", cluster_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["lke"] + + ["cluster-view", cluster_id, "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["label", "k8s_version"] assert_headers_in_lines(headers, lines) -def test_update_kubernetes_cluster(test_lke_cluster): - cluster_id = test_lke_cluster +def test_update_kubernetes_cluster(lke_cluster): + cluster_id = lke_cluster new_label = get_random_text(5) + "_updated_cluster" - updated_label = ( - exec_test_command( - BASE_CMD - + [ - "cluster-update", - cluster_id, - "--label", - new_label, - "--text", - "--no-headers", - "--format=label", - ] - ) - .stdout.decode() - .rstrip() + updated_label = exec_test_command( + BASE_CMDS["lke"] + + [ + "cluster-update", + cluster_id, + "--label", + new_label, + "--text", + "--no-headers", + "--format=label", + ] ) assert new_label == updated_label -def test_list_kubernetes_endpoint(test_lke_cluster): - cluster_id = test_lke_cluster - res = ( - retry_exec_test_command_with_delay( - BASE_CMD - + ["api-endpoints-list", cluster_id, "--text", "--delimiter=,"], - retries=3, - delay=30, - ) - .stdout.decode() - .rstrip() +def test_list_kubernetes_endpoint(lke_cluster): + cluster_id = lke_cluster + res = retry_exec_test_command_with_delay( + BASE_CMDS["lke"] + + ["api-endpoints-list", cluster_id, "--text", "--delimiter=,"], + retries=3, + delay=30, ) lines = res.splitlines() @@ -276,15 +125,11 @@ def test_list_kubernetes_endpoint(test_lke_cluster): assert_headers_in_lines(headers, lines) -def test_cluster_dashboard_url(test_lke_cluster): - cluster_id = test_lke_cluster - res = ( - exec_test_command( - BASE_CMD - + ["cluster-dashboard-url", cluster_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() +def test_cluster_dashboard_url(lke_cluster): + cluster_id = lke_cluster + res = exec_test_command( + BASE_CMDS["lke"] + + ["cluster-dashboard-url", cluster_id, "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -292,14 +137,10 @@ def test_cluster_dashboard_url(test_lke_cluster): assert_headers_in_lines(headers, lines) -def test_node_pool_list(test_lke_cluster): - cluster_id = test_lke_cluster - res = ( - exec_test_command( - BASE_CMD + ["pools-list", cluster_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() +def test_node_pool_list(lke_cluster): + cluster_id = lke_cluster + res = exec_test_command( + BASE_CMDS["lke"] + ["pools-list", cluster_id, "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -307,17 +148,13 @@ def test_node_pool_list(test_lke_cluster): assert_headers_in_lines(headers, lines) -def test_view_pool(test_lke_cluster): - cluster_id = test_lke_cluster +def test_view_pool(lke_cluster): + cluster_id = lke_cluster node_pool_id = get_node_pool_id(cluster_id) - res = ( - exec_test_command( - BASE_CMD - + ["pool-view", cluster_id, node_pool_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["lke"] + + ["pool-view", cluster_id, node_pool_id, "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -325,14 +162,14 @@ def test_view_pool(test_lke_cluster): assert_headers_in_lines(headers, lines) -def test_update_node_pool(test_lke_cluster): - cluster_id = test_lke_cluster +def test_update_node_pool(lke_cluster): + cluster_id = lke_cluster node_pool_id = get_node_pool_id(cluster_id) new_value = get_random_text(8) + "updated_pool" result = json.loads( exec_test_command( - BASE_CMD + BASE_CMDS["lke"] + [ "pool-update", cluster_id, @@ -345,7 +182,7 @@ def test_update_node_pool(test_lke_cluster): '[{"key": "test-key", "value": "test-value", "effect": "NoSchedule"}]', "--json", ] - ).stdout.decode() + ) ) assert result[0]["labels"] == {"label-key": new_value} @@ -361,7 +198,7 @@ def test_update_node_pool(test_lke_cluster): # Reset the values for labels and taints (TPT-3665) result = json.loads( exec_test_command( - BASE_CMD + BASE_CMDS["lke"] + [ "pool-update", cluster_id, @@ -372,24 +209,20 @@ def test_update_node_pool(test_lke_cluster): "[]", "--json", ] - ).stdout.decode() + ) ) assert result[0]["labels"] == {} assert result[0]["taints"] == [] -def test_view_node(test_lke_cluster): - cluster_id = test_lke_cluster +def test_view_node(lke_cluster): + cluster_id = lke_cluster node_pool_id = get_pool_nodesid(cluster_id) - res = ( - exec_test_command( - BASE_CMD - + ["node-view", cluster_id, node_pool_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["lke"] + + ["node-view", cluster_id, node_pool_id, "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -399,12 +232,9 @@ def test_view_node(test_lke_cluster): def test_version_view(): version_id = get_lke_version_id() - res = ( - exec_test_command( - BASE_CMD + ["version-view", version_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["lke"] + + ["version-view", version_id, "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -413,16 +243,12 @@ def test_version_view(): def test_list_lke_types(): - types = ( - exec_test_command( - BASE_CMD - + [ - "types", - "--text", - ] - ) - .stdout.decode() - .rstrip() + types = exec_test_command( + BASE_CMDS["lke"] + + [ + "types", + "--text", + ] ) headers = ["id", "label", "price.hourly", "price.monthly", "transfer"] @@ -433,26 +259,22 @@ def test_list_lke_types(): assert "LKE High Availability" in types -def test_create_node_pool_has_disk_encryption_field_set(test_lke_cluster): - cluster_id = test_lke_cluster - - result = ( - exec_test_command( - BASE_CMD - + [ - "pool-create", - cluster_id, - "--count", - "1", - "--type", - "g6-standard-4", - "--text", - "--format=id,disk_encryption,type", - # "--no-headers", - ] - ) - .stdout.decode() - .rstrip() +def test_create_node_pool_has_disk_encryption_field_set(lke_cluster): + cluster_id = lke_cluster + + result = exec_test_command( + BASE_CMDS["lke"] + + [ + "pool-create", + cluster_id, + "--count", + "1", + "--type", + "g6-standard-4", + "--text", + "--format=id,disk_encryption,type", + # "--no-headers", + ] ) lines = result.splitlines() headers = lines[0].split() @@ -467,56 +289,24 @@ def test_create_node_pool_has_disk_encryption_field_set(test_lke_cluster): assert "g6-standard-4" in result -@pytest.fixture -def test_node_pool(test_lke_cluster): - cluster_id = test_lke_cluster - - node_pool_id = ( - exec_test_command( - BASE_CMD - + [ - "pool-create", - cluster_id, - "--count", - "1", - "--type", - "g6-standard-4", - "--labels", - '{ "example.com/my-app":"team1" }', - "--text", - "--format=id", - "--no-headers", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield node_pool_id - - -def test_update_autoscaler(test_lke_cluster, test_node_pool): - cluster_id = test_lke_cluster - node_pool_id = test_node_pool - - result = ( - exec_test_command( - BASE_CMD - + [ - "pool-update", - cluster_id, - node_pool_id, - "--autoscaler.enabled", - "true", - "--autoscaler.min", - "1", - "--autoscaler.max", - "3", - "--text", - ] - ) - .stdout.decode() - .rstrip() +def test_update_autoscaler(lke_cluster, node_pool): + cluster_id = lke_cluster + node_pool_id = node_pool + + result = exec_test_command( + BASE_CMDS["lke"] + + [ + "pool-update", + cluster_id, + node_pool_id, + "--autoscaler.enabled", + "true", + "--autoscaler.min", + "1", + "--autoscaler.max", + "3", + "--text", + ] ) headers = [ @@ -538,22 +328,18 @@ def test_update_autoscaler(test_lke_cluster, test_node_pool): assert "1" in result -def test_kubeconfig_view(test_lke_cluster): - cluster_id = test_lke_cluster +def test_kubeconfig_view(lke_cluster): + cluster_id = lke_cluster - kubeconfig = ( - retry_exec_test_command_with_delay( - BASE_CMD - + [ - "kubeconfig-view", - cluster_id, - "--text", - ], - retries=5, - delay=60, - ) - .stdout.decode() - .strip() + kubeconfig = retry_exec_test_command_with_delay( + BASE_CMDS["lke"] + + [ + "kubeconfig-view", + cluster_id, + "--text", + ], + retries=5, + delay=60, ) header = ["kubeconfig"] @@ -563,7 +349,7 @@ def test_kubeconfig_view(test_lke_cluster): assert kubeconfig -def test_cluster_nodes_recycle(test_lke_cluster): - cluster_id = test_lke_cluster +def test_cluster_nodes_recycle(lke_cluster): + cluster_id = lke_cluster - exec_test_command(BASE_CMD + ["cluster-nodes-recycle", cluster_id]) + exec_test_command(BASE_CMDS["lke"] + ["cluster-nodes-recycle", cluster_id]) diff --git a/tests/integration/lke/test_lke_acl.py b/tests/integration/lke/test_lke_acl.py index 2de03e47b..3b3bf34c2 100644 --- a/tests/integration/lke/test_lke_acl.py +++ b/tests/integration/lke/test_lke_acl.py @@ -1,99 +1,22 @@ -# │ cluster-acl-delete │ Delete the control plane access control list. │ -# │ cluster-acl-update │ Update the control plane access control list. │ -# │ cluster-acl-view │ Get the control plane access control list. │ -import pytest - from tests.integration.helpers import ( + BASE_CMDS, assert_headers_in_lines, - delete_target_id, exec_test_command, - get_cluster_id, - get_random_region_with_caps, - get_random_text, retry_exec_test_command_with_delay, ) +from tests.integration.lke.fixtures import lke_cluster_acl # noqa: F401 -BASE_CMD = ["linode-cli", "lke"] - - -@pytest.fixture -def test_lke_cluster_acl(): - label = get_random_text(8) + "_cluster" - - test_region = get_random_region_with_caps( - required_capabilities=["Linodes", "Kubernetes"] - ) - lke_version = ( - exec_test_command( - BASE_CMD - + [ - "versions-list", - "--text", - "--no-headers", - ] - ) - .stdout.decode() - .rstrip() - .splitlines()[0] - ) - - cluster_label = ( - exec_test_command( - BASE_CMD - + [ - "cluster-create", - "--region", - test_region, - "--label", - label, - "--node_pools.type", - "g6-standard-1", - "--node_pools.count", - "1", - "--node_pools.disks", - '[{"type":"ext4","size":1024}]', - "--k8s_version", - lke_version, - "--control_plane.high_availability", - "true", - "--control_plane.acl.enabled", - "true", - "--text", - "--delimiter", - ",", - "--no-headers", - "--format", - "label", - "--no-defaults", - ] - ) - .stdout.decode() - .rstrip() - ) - - cluster_id = get_cluster_id(label=cluster_label) - - yield cluster_id - - delete_target_id( - target="lke", id=cluster_id, delete_command="cluster-delete" - ) +def test_cluster_acl_view(lke_cluster_acl): + cluster_id = lke_cluster_acl -def test_cluster_acl_view(test_lke_cluster_acl): - cluster_id = test_lke_cluster_acl - - acl = ( - exec_test_command( - BASE_CMD - + [ - "cluster-acl-view", - cluster_id, - "--text", - ] - ) - .stdout.decode() - .strip() + acl = exec_test_command( + BASE_CMDS["lke"] + + [ + "cluster-acl-view", + cluster_id, + "--text", + ] ) headers = [ @@ -108,29 +31,25 @@ def test_cluster_acl_view(test_lke_cluster_acl): assert "True" in acl -def test_cluster_acl_update(test_lke_cluster_acl): - cluster_id = test_lke_cluster_acl +def test_cluster_acl_update(lke_cluster_acl): + cluster_id = lke_cluster_acl print("RUNNING TEST") # Verify the update - acl = ( - exec_test_command( - BASE_CMD - + [ - "cluster-acl-update", - cluster_id, - "--acl.addresses.ipv4", - "203.0.113.1", - "--acl.addresses.ipv6", - "2001:db8:1234:abcd::/64", - "--acl.enabled", - "true", - "--text", - ] - ) - .stdout.decode() - .strip() + acl = exec_test_command( + BASE_CMDS["lke"] + + [ + "cluster-acl-update", + cluster_id, + "--acl.addresses.ipv4", + "203.0.113.1", + "--acl.addresses.ipv6", + "2001:db8:1234:abcd::/64", + "--acl.enabled", + "true", + "--text", + ] ) headers = [ @@ -146,27 +65,23 @@ def test_cluster_acl_update(test_lke_cluster_acl): assert "2001:db8:1234:abcd::/64" in acl -def test_cluster_acl_delete(test_lke_cluster_acl): - cluster_id = test_lke_cluster_acl +def test_cluster_acl_delete(lke_cluster_acl): + cluster_id = lke_cluster_acl retry_exec_test_command_with_delay( - BASE_CMD + ["cluster-acl-delete", cluster_id] + BASE_CMDS["lke"] + ["cluster-acl-delete", cluster_id] ) # Verify the deletion - acl = ( - exec_test_command( - BASE_CMD - + [ - "cluster-acl-view", - cluster_id, - "--text", - "--format=acl.enabled", - "--text", - ] - ) - .stdout.decode() - .strip() + acl = exec_test_command( + BASE_CMDS["lke"] + + [ + "cluster-acl-view", + cluster_id, + "--text", + "--format=acl.enabled", + "--text", + ] ) assert "False" in acl diff --git a/tests/integration/lke/test_lke_enterprise.py b/tests/integration/lke/test_lke_enterprise.py index 4a9cfe379..ce805ceee 100644 --- a/tests/integration/lke/test_lke_enterprise.py +++ b/tests/integration/lke/test_lke_enterprise.py @@ -4,50 +4,24 @@ from pytest import MonkeyPatch from tests.integration.helpers import ( + BASE_CMDS, assert_headers_in_lines, delete_target_id, exec_test_command, - get_cluster_id, get_random_region_with_caps, get_random_text, ) - -BASE_CMD = ["linode-cli", "lke"] - - -def get_lke_enterprise_id(): - enterprise_tier_info_list = ( - exec_test_command( - BASE_CMD - + [ - "tiered-versions-list", - "enterprise", - "--json", - ] - ) - .stdout.decode() - .rstrip() - ) - - parsed = json.loads(enterprise_tier_info_list) - - enterprise_ti = parsed[0] - - return enterprise_ti.get("id") +from tests.integration.lke.helpers import get_cluster_id, get_lke_enterprise_id def test_enterprise_tier_available_in_types(monkeypatch: MonkeyPatch): monkeypatch.setenv("LINODE_CLI_API_VERSION", "v4beta") - lke_types = ( - exec_test_command( - BASE_CMD - + [ - "types", - "--text", - ] - ) - .stdout.decode() - .rstrip() + lke_types = exec_test_command( + BASE_CMDS["lke"] + + [ + "types", + "--text", + ] ) assert "lke-e" in lke_types @@ -64,28 +38,24 @@ def test_create_lke_enterprise(monkeypatch: MonkeyPatch): k8s_version = get_lke_enterprise_id() - output = ( - exec_test_command( - BASE_CMD - + [ - "cluster-create", - "--label", - label, - "--tier", - "enterprise", - "--k8s_version", - k8s_version, - "--node_pools.type", - "g6-standard-6", - "--node_pools.count", - "3", - "--region", - test_region, - "--text", - ] - ) - .stdout.decode() - .rstrip() + output = exec_test_command( + BASE_CMDS["lke"] + + [ + "cluster-create", + "--label", + label, + "--tier", + "enterprise", + "--k8s_version", + k8s_version, + "--node_pools.type", + "g6-standard-6", + "--node_pools.count", + "3", + "--region", + test_region, + "--text", + ] ) headers = [ @@ -111,17 +81,13 @@ def test_create_lke_enterprise(monkeypatch: MonkeyPatch): def test_lke_tiered_versions_list(): - enterprise_tier_info_list = ( - exec_test_command( - BASE_CMD - + [ - "tiered-versions-list", - "enterprise", - "--json", - ] - ) - .stdout.decode() - .rstrip() + enterprise_tier_info_list = exec_test_command( + BASE_CMDS["lke"] + + [ + "tiered-versions-list", + "enterprise", + "--json", + ] ) parsed = json.loads(enterprise_tier_info_list) @@ -131,17 +97,13 @@ def test_lke_tiered_versions_list(): assert re.match(r"^v\d+\.\d+\.\d+\+lke\d+$", enterprise_ti.get("id")) assert enterprise_ti.get("tier") == "enterprise" - standard_tier_info_list = ( - exec_test_command( - BASE_CMD - + [ - "tiered-versions-list", - "standard", - "--json", - ] - ) - .stdout.decode() - .rstrip() + standard_tier_info_list = exec_test_command( + BASE_CMDS["lke"] + + [ + "tiered-versions-list", + "standard", + "--json", + ] ) s_ti_list = json.loads(standard_tier_info_list) @@ -154,18 +116,14 @@ def test_lke_tiered_versions_list(): def test_lke_tiered_versions_view(): enterprise_id = get_lke_enterprise_id() - enterprise_tier_info = ( - exec_test_command( - BASE_CMD - + [ - "tiered-version-view", - "enterprise", - enterprise_id, - "--json", - ] - ) - .stdout.decode() - .rstrip() + enterprise_tier_info = exec_test_command( + BASE_CMDS["lke"] + + [ + "tiered-version-view", + "enterprise", + enterprise_id, + "--json", + ] ) parsed = json.loads(enterprise_tier_info) @@ -175,18 +133,14 @@ def test_lke_tiered_versions_view(): assert enterprise_ti.get("id") == enterprise_id assert enterprise_ti.get("tier") == "enterprise" - standard_tier_info = ( - exec_test_command( - BASE_CMD - + [ - "tiered-version-view", - "standard", - "1.31", - "--json", - ] - ) - .stdout.decode() - .rstrip() + standard_tier_info = exec_test_command( + BASE_CMDS["lke"] + + [ + "tiered-version-view", + "standard", + "1.31", + "--json", + ] ) parsed = json.loads(standard_tier_info) diff --git a/tests/integration/longview/test_longview.py b/tests/integration/longview/test_longview.py index a332874c2..76ab08997 100644 --- a/tests/integration/longview/test_longview.py +++ b/tests/integration/longview/test_longview.py @@ -1,21 +1,19 @@ -import time - import pytest from tests.integration.helpers import ( + BASE_CMDS, assert_headers_in_lines, delete_target_id, exec_test_command, + get_random_text, ) -BASE_CMD = ["linode-cli", "longview"] - @pytest.mark.smoke def test_create_longview_client(): - new_label = str(time.time_ns()) + "label" + new_label = get_random_text(5) + "label" result = exec_test_command( - BASE_CMD + BASE_CMDS["longview"] + [ "create", "--label", @@ -25,87 +23,52 @@ def test_create_longview_client(): "--delimiter", ",", ] - ).stdout.decode() + ) assert new_label in result def test_longview_client_list(): - res = ( - exec_test_command(BASE_CMD + ["list", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["longview"] + ["list", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["id", "label", "created"] assert_headers_in_lines(headers, lines) -@pytest.fixture -def get_client_id(): - client_id = ( - exec_test_command( - BASE_CMD - + [ - "list", - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) - first_id = client_id[0] - yield first_id - - -def test_client_view(get_client_id): - client_id = get_client_id - res = ( - exec_test_command( - BASE_CMD + ["view", client_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() +def test_client_view(): + client_id = get_client_id() + res = exec_test_command( + BASE_CMDS["longview"] + ["view", client_id, "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["id", "label", "created"] assert_headers_in_lines(headers, lines) -def test_update_longview_client_list(get_client_id): - client_id = get_client_id - new_label = str(time.time_ns()) + "label" - updated_label = ( - exec_test_command( - BASE_CMD - + [ - "update", - client_id, - "--label", - new_label, - "--text", - "--no-headers", - "--format=label", - ] - ) - .stdout.decode() - .rstrip() +def test_update_longview_client_list(): + client_id = get_client_id() + new_label = get_random_text(5) + "label" + updated_label = exec_test_command( + BASE_CMDS["longview"] + + [ + "update", + client_id, + "--label", + new_label, + "--text", + "--no-headers", + "--format=label", + ] ) assert new_label == updated_label delete_target_id(target="longview", id=client_id) def test_longview_plan_view(): - res = ( - exec_test_command(BASE_CMD + ["plan-view", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["longview"] + ["plan-view", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["id", "label", "clients_included"] @@ -113,51 +76,57 @@ def test_longview_plan_view(): def test_longview_subscriptions_list(): - res = ( - exec_test_command( - BASE_CMD + ["subscriptions-list", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["longview"] + + ["subscriptions-list", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["id", "label", "clients_included"] assert_headers_in_lines(headers, lines) -@pytest.fixture -def get_subscriptions_id(): - subscriptions_id = ( - exec_test_command( - BASE_CMD - + [ - "subscriptions-list", - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) - first_id = subscriptions_id[0] - yield first_id - - -def test_longview_subscriptions_list_view(get_subscriptions_id): - subscriptions_id = get_subscriptions_id - res = ( - exec_test_command( - BASE_CMD - + ["subscription-view", subscriptions_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() +def test_longview_subscriptions_list_view(): + subscriptions_id = get_subscriptions_id() + res = exec_test_command( + BASE_CMDS["longview"] + + ["subscription-view", subscriptions_id, "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["id", "label", "clients_included"] assert_headers_in_lines(headers, lines) + + +def get_client_id(): + client_id = exec_test_command( + BASE_CMDS["longview"] + + [ + "list", + "--text", + "--no-headers", + "--delimiter", + ",", + "--format", + "id", + ] + ).splitlines() + first_id = client_id[0] + + return first_id + + +def get_subscriptions_id(): + subscriptions_id = exec_test_command( + BASE_CMDS["longview"] + + [ + "subscriptions-list", + "--text", + "--no-headers", + "--delimiter", + ",", + "--format", + "id", + ] + ).splitlines() + first_id = subscriptions_id[0] + + return first_id diff --git a/tests/integration/managed/test_managed.py b/tests/integration/managed/test_managed.py index 28baa23dd..b835d698f 100644 --- a/tests/integration/managed/test_managed.py +++ b/tests/integration/managed/test_managed.py @@ -1,22 +1,35 @@ -import secrets -import time - import pytest from tests.integration.helpers import ( + BASE_CMDS, assert_headers_in_lines, delete_target_id, exec_test_command, get_random_text, ) -BASE_CMD = ["linode-cli", "managed"] -unique_name = "test-user-" + str(int(time.time())) + +def get_contact_id(): + contact_id = exec_test_command( + BASE_CMDS["managed"] + + [ + "contacts-list", + "--text", + "--no-headers", + "--delimiter", + ",", + "--format", + "id", + ] + ).splitlines() + first_id = contact_id[0] + return first_id def test_managed_contact_create(): + unique_name = "test-user-" + get_random_text(5) exec_test_command( - BASE_CMD + BASE_CMDS["managed"] + [ "contact-create", "--name", @@ -30,49 +43,19 @@ def test_managed_contact_create(): def test_managed_contact_list(): - res = ( - exec_test_command( - BASE_CMD + ["contacts-list", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["managed"] + ["contacts-list", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["name", "email", "group"] assert_headers_in_lines(headers, lines) -@pytest.fixture -def get_contact_id(): - contact_id = ( - exec_test_command( - BASE_CMD - + [ - "contacts-list", - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) - first_id = contact_id[0] - yield first_id - - -def test_managed_contact_view(get_contact_id): - contact_id = get_contact_id - res = ( - exec_test_command( - BASE_CMD + ["contact-view", contact_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() +def test_managed_contact_view(): + contact_id = get_contact_id() + res = exec_test_command( + BASE_CMDS["managed"] + + ["contact-view", contact_id, "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -80,36 +63,33 @@ def test_managed_contact_view(get_contact_id): assert_headers_in_lines(headers, lines) -def test_managed_contact_update(get_contact_id): - contact_id = get_contact_id - unique_name1 = str(time.time_ns()) + "test" - update_name = ( - exec_test_command( - BASE_CMD - + [ - "contact-update", - contact_id, - "--name", - unique_name1, - "--text", - "--no-headers", - "--format=name", - ] - ) - .stdout.decode() - .rstrip() +def test_managed_contact_update(): + contact_id = get_contact_id() + unique_name_1 = get_random_text(5) + "test" + update_name = exec_test_command( + BASE_CMDS["managed"] + + [ + "contact-update", + contact_id, + "--name", + unique_name_1, + "--text", + "--no-headers", + "--format=name", + ] ) - assert update_name == unique_name1 + assert update_name == unique_name_1 delete_target_id( target="managed", delete_command="contact-delete", id=contact_id ) def test_managed_credential_create(): - label = "test-label" + secrets.token_hex(4) + unique_name = get_random_text(5) + "test" + label = "test-label-" + get_random_text(5) password = get_random_text() exec_test_command( - BASE_CMD + BASE_CMDS["managed"] + [ "credential-create", "--label", @@ -125,12 +105,8 @@ def test_managed_credential_create(): def test_managed_credentials_list(): - res = ( - exec_test_command( - BASE_CMD + ["credentials-list", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["managed"] + ["credentials-list", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["label", "last_decrypted"] @@ -139,36 +115,27 @@ def test_managed_credentials_list(): @pytest.fixture def get_credential_id(): - credential_id = ( - exec_test_command( - BASE_CMD - + [ - "credentials-list", - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) + credential_id = exec_test_command( + BASE_CMDS["managed"] + + [ + "credentials-list", + "--text", + "--no-headers", + "--delimiter", + ",", + "--format", + "id", + ] + ).splitlines() first_id = credential_id[0] yield first_id def test_managed_credentials_view(get_credential_id): credential_id = get_credential_id - res = ( - exec_test_command( - BASE_CMD - + ["credential-view", credential_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["managed"] + + ["credential-view", credential_id, "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -178,22 +145,18 @@ def test_managed_credentials_view(get_credential_id): def test_managed_credentials_update(get_credential_id): credential_id = get_credential_id - new_label = "test-label" + secrets.token_hex(4) - update_label = ( - exec_test_command( - BASE_CMD - + [ - "credential-update", - credential_id, - "--label", - new_label, - "--text", - "--no-headers", - "--format=label", - ] - ) - .stdout.decode() - .rstrip() + new_label = "test-label" + get_random_text(4) + update_label = exec_test_command( + BASE_CMDS["managed"] + + [ + "credential-update", + credential_id, + "--label", + new_label, + "--text", + "--no-headers", + "--format=label", + ] ) assert update_label == new_label delete_target_id( @@ -202,12 +165,9 @@ def test_managed_credentials_update(get_credential_id): def test_managed_credentials_sshkey_view(): - res = ( - exec_test_command( - BASE_CMD + ["credential-sshkey-view", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["managed"] + + ["credential-sshkey-view", "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -216,10 +176,8 @@ def test_managed_credentials_sshkey_view(): def test_managed_issues_list(): - res = ( - exec_test_command(BASE_CMD + ["issues-list", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["managed"] + ["issues-list", "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -228,12 +186,9 @@ def test_managed_issues_list(): def test_managed_linode_settings_list(): - res = ( - exec_test_command( - BASE_CMD + ["linode-settings-list", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["managed"] + + ["linode-settings-list", "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -242,12 +197,8 @@ def test_managed_linode_settings_list(): def test_managed_linode_service_list(): - res = ( - exec_test_command( - BASE_CMD + ["services-list", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["managed"] + ["services-list", "--text", "--delimiter=,"] ) lines = res.splitlines() diff --git a/tests/integration/networking/test_networking.py b/tests/integration/networking/test_networking.py index 2d29dd166..083dd0402 100644 --- a/tests/integration/networking/test_networking.py +++ b/tests/integration/networking/test_networking.py @@ -4,14 +4,16 @@ import pytest from _pytest.monkeypatch import MonkeyPatch -from tests.integration.helpers import delete_target_id, exec_test_command -from tests.integration.linodes.helpers_linodes import ( +from tests.integration.helpers import ( + BASE_CMDS, + delete_target_id, + exec_test_command, +) +from tests.integration.linodes.helpers import ( create_linode, create_linode_and_wait, ) -BASE_CMD = ["linode-cli", "networking"] - @pytest.fixture(scope="package") def test_linode_id(linode_cloud_firewall): @@ -45,7 +47,7 @@ def has_shared_ip(linode_id: int, ip: str) -> bool: shared_ips = json.loads( exec_test_command( ["linode-cli", "linodes", "ips-list", "--json", linode_id] - ).stdout.decode() + ) )[0]["ipv4"]["shared"] # Ensure there is a matching shared IP @@ -54,8 +56,9 @@ def has_shared_ip(linode_id: int, ip: str) -> bool: def test_display_ips_for_available_linodes(test_linode_id): result = exec_test_command( - BASE_CMD + ["ips-list", "--text", "--no-headers", "--delimiter", ","] - ).stdout.decode() + BASE_CMDS["networking"] + + ["ips-list", "--text", "--no-headers", "--delimiter", ","] + ) assert re.search(r"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}", result) assert re.search( @@ -83,10 +86,10 @@ def test_view_an_ip_address(test_linode_id): "--text", "--no-headers", ] - ).stdout.rstrip() + ) result = exec_test_command( - BASE_CMD + BASE_CMDS["networking"] + [ "ip-view", "--text", @@ -95,7 +98,7 @@ def test_view_an_ip_address(test_linode_id): ",", linode_ipv4, ] - ).stdout.decode() + ) assert re.search(r"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}", result) @@ -104,7 +107,7 @@ def test_allocate_additional_private_ipv4_address(test_linode_id): linode_id = test_linode_id result = exec_test_command( - BASE_CMD + BASE_CMDS["networking"] + [ "ip-add", "--type", @@ -118,7 +121,7 @@ def test_allocate_additional_private_ipv4_address(test_linode_id): "--text", "--no-headers", ] - ).stdout.decode() + ) assert re.search(r"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}", result) assert re.search( @@ -135,7 +138,7 @@ def test_share_ipv4_address( # Allocate an IPv4 address on the parent Linode ip_address = json.loads( exec_test_command( - BASE_CMD + BASE_CMDS["networking"] + [ "ip-add", "--type", @@ -146,12 +149,12 @@ def test_share_ipv4_address( "--public", "true", ] - ).stdout.decode() + ) )[0]["address"] # Share the IP address to the target Linode exec_test_command( - BASE_CMD + BASE_CMDS["networking"] + [ "ip-share", "--ips", @@ -166,7 +169,7 @@ def test_share_ipv4_address( # Remove the IP shares exec_test_command( - BASE_CMD + BASE_CMDS["networking"] + [ "ip-share", "--ips", diff --git a/tests/integration/nodebalancers/fixtures.py b/tests/integration/nodebalancers/fixtures.py new file mode 100644 index 000000000..5ce2d3327 --- /dev/null +++ b/tests/integration/nodebalancers/fixtures.py @@ -0,0 +1,167 @@ +import pytest + +from tests.integration.helpers import ( + BASE_CMDS, + delete_target_id, + exec_test_command, +) +from tests.integration.linodes.helpers import DEFAULT_TEST_IMAGE + + +@pytest.fixture(scope="package") +def nodebalancer_w_config_and_node(linode_cloud_firewall): + # create a default nodebalancer + nodebalancer_id = exec_test_command( + BASE_CMDS["nodebalancers"] + + [ + "create", + "--region", + "us-ord", + "--firewall_id", + linode_cloud_firewall, + "--text", + "--delimiter", + ",", + "--format", + "id", + "--no-headers", + ] + ) + # create one standard config + config_id = exec_test_command( + BASE_CMDS["nodebalancers"] + + [ + "config-create", + nodebalancer_id, + "--delimiter", + ",", + "--text", + "--no-headers", + "--format", + "id", + ] + ) + + linode_create = exec_test_command( + [ + "linode-cli", + "linodes", + "create", + "--root_pass", + "aComplex@Password", + "--booted", + "true", + "--region", + "us-ord", + "--type", + "g6-nanode-1", + "--private_ip", + "true", + "--image", + DEFAULT_TEST_IMAGE, + "--firewall_id", + linode_cloud_firewall, + "--text", + "--delimiter", + ",", + "--format", + "id,ipv4", + "--no-header", + "--suppress-warnings", + ] + ) + linode_arr = linode_create.split(",") + linode_id = linode_arr[0] + ip_arr = linode_arr[1].split(" ") + node_ip = ip_arr[1] + node_label = "defaultnode1" + + node_id = exec_test_command( + BASE_CMDS["nodebalancers"] + + [ + "node-create", + "--address", + node_ip + ":80", + "--label", + node_label, + "--weight", + "100", + "--text", + "--no-headers", + "--delimiter", + ",", + nodebalancer_id, + config_id, + "--format", + "id", + ] + ) + + yield nodebalancer_id, config_id, node_id, node_ip + + delete_target_id(target="nodebalancers", id=nodebalancer_id) + delete_target_id(target="linodes", id=linode_id) + + +@pytest.fixture(scope="module") +def linode_to_add(linode_cloud_firewall): + linode = exec_test_command( + BASE_CMDS["linodes"] + + [ + "create", + "--root_pass", + "aComplex@Password", + "--booted", + "true", + "--region", + "us-ord", + "--type", + "g6-nanode-1", + "--private_ip", + "true", + "--image", + DEFAULT_TEST_IMAGE, + "--firewall_id", + linode_cloud_firewall, + "--text", + "--delimiter", + ",", + "--format", + "id,ipv4", + "--no-header", + "--suppress-warnings", + ] + ) + + yield linode + + linode_arr = linode.split(",") + linode_id = linode_arr[0] + delete_target_id("linodes", linode_id) + + +@pytest.fixture(scope="module") +def nodebalancer_with_default_conf(linode_cloud_firewall): + result = exec_test_command( + BASE_CMDS["nodebalancers"] + + [ + "create", + "--region", + "us-ord", + "--firewall_id", + linode_cloud_firewall, + "--text", + "--delimiter", + ",", + "--format", + "id,label,region,hostname,client_conn_throttle", + "--suppress-warnings", + "--no-headers", + ] + ) + + yield result + + res_arr = result.split(",") + nodebalancer_id = res_arr[0] + delete_target_id(target="nodebalancers", id=nodebalancer_id) diff --git a/tests/integration/nodebalancers/test_node_balancers.py b/tests/integration/nodebalancers/test_node_balancers.py index 692ed5e89..4ebb8b6db 100644 --- a/tests/integration/nodebalancers/test_node_balancers.py +++ b/tests/integration/nodebalancers/test_node_balancers.py @@ -4,174 +4,23 @@ from linodecli.exit_codes import ExitCodes from tests.integration.helpers import ( - delete_target_id, + BASE_CMDS, exec_failing_test_command, exec_test_command, ) -from tests.integration.linodes.helpers_linodes import DEFAULT_TEST_IMAGE - -BASE_CMD = ["linode-cli", "nodebalancers"] -nodebalancer_created = "[0-9]+,balancer[0-9]+,us-ord,[0-9]+-[0-9]+-[0-9]+-[0-9]+.ip.linodeusercontent.com,0" - - -@pytest.fixture(scope="package") -def test_node_balancers(linode_cloud_firewall): - # create a default nodebalancer - nodebalancer_id = ( - exec_test_command( - BASE_CMD - + [ - "create", - "--region", - "us-ord", - "--firewall_id", - linode_cloud_firewall, - "--text", - "--delimiter", - ",", - "--format", - "id", - "--no-headers", - ] - ) - .stdout.decode() - .rstrip() - ) - # create one standard config - config_id = ( - exec_test_command( - BASE_CMD - + [ - "config-create", - nodebalancer_id, - "--delimiter", - ",", - "--text", - "--no-headers", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - ) - - linode_create = ( - exec_test_command( - [ - "linode-cli", - "linodes", - "create", - "--root_pass", - "aComplex@Password", - "--booted", - "true", - "--region", - "us-ord", - "--type", - "g6-nanode-1", - "--private_ip", - "true", - "--image", - DEFAULT_TEST_IMAGE, - "--firewall_id", - linode_cloud_firewall, - "--text", - "--delimiter", - ",", - "--format", - "id,ipv4", - "--no-header", - "--suppress-warnings", - ] - ) - .stdout.decode() - .rstrip() - ) - linode_arr = linode_create.split(",") - linode_id = linode_arr[0] - ip_arr = linode_arr[1].split(" ") - node_ip = ip_arr[1] - node_label = "defaultnode1" - - node_id = ( - exec_test_command( - BASE_CMD - + [ - "node-create", - "--address", - node_ip + ":80", - "--label", - node_label, - "--weight", - "100", - "--text", - "--no-headers", - "--delimiter", - ",", - nodebalancer_id, - config_id, - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield nodebalancer_id, config_id, node_id, node_ip - - delete_target_id(target="nodebalancers", id=nodebalancer_id) - delete_target_id(target="linodes", id=linode_id) - - -@pytest.fixture -def create_linode_to_add(linode_cloud_firewall): - linode = ( - exec_test_command( - [ - "linode-cli", - "linodes", - "create", - "--root_pass", - "aComplex@Password", - "--booted", - "true", - "--region", - "us-ord", - "--type", - "g6-nanode-1", - "--private_ip", - "true", - "--image", - DEFAULT_TEST_IMAGE, - "--firewall_id", - linode_cloud_firewall, - "--text", - "--delimiter", - ",", - "--format", - "id,ipv4", - "--no-header", - "--suppress-warnings", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield linode - - linode_arr = linode.split(",") - linode_id = linode_arr[0] - delete_target_id("linodes", linode_id) +from tests.integration.nodebalancers.fixtures import ( # noqa: F401 + linode_to_add, + nodebalancer_w_config_and_node, + nodebalancer_with_default_conf, +) def test_fail_to_create_nodebalancer_without_region(): result = exec_failing_test_command( - BASE_CMD + ["create", "--text", "--no-headers", "--no-defaults"], + BASE_CMDS["nodebalancers"] + + ["create", "--text", "--no-headers", "--no-defaults"], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) assert "Request failed: 400" in result assert "region region is required" in result @@ -181,12 +30,12 @@ def test_create_nodebalancer_with_default_conf( nodebalancer_with_default_conf, ): result = nodebalancer_with_default_conf - assert re.search(nodebalancer_created, result) + assert re.search(nodebalancer_created(), result) -def test_list_nodebalancers_and_status(test_node_balancers): +def test_list_nodebalancers_and_status(nodebalancer_w_config_and_node): result = exec_test_command( - BASE_CMD + BASE_CMDS["nodebalancers"] + [ "list", "--no-headers", @@ -196,15 +45,15 @@ def test_list_nodebalancers_and_status(test_node_balancers): "--format", "id,label,region,hostname,client_conn_throttle", ] - ).stdout.decode() - assert re.search(nodebalancer_created, result) + ) + assert re.search(nodebalancer_created(), result) -def test_display_public_ipv4_for_nodebalancer(test_node_balancers): - nodebalancer_id = test_node_balancers[0] +def test_display_public_ipv4_for_nodebalancer(nodebalancer_w_config_and_node): + nodebalancer_id = nodebalancer_w_config_and_node[0] result = exec_test_command( - BASE_CMD + BASE_CMDS["nodebalancers"] + [ "view", nodebalancer_id, @@ -213,24 +62,24 @@ def test_display_public_ipv4_for_nodebalancer(test_node_balancers): "--text", "--no-headers", ] - ).stdout.decode() + ) assert re.search(r"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}", result) def test_fail_to_view_nodebalancer_with_invalid_id(): result = exec_failing_test_command( - BASE_CMD + ["view", "535", "--text", "--no-headers"], + BASE_CMDS["nodebalancers"] + ["view", "535", "--text", "--no-headers"], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) assert "Request failed: 404" in result -def test_create_standard_configuration_profile(test_node_balancers): - nodebalancer_id = test_node_balancers[0] +def test_create_standard_configuration_profile(nodebalancer_w_config_and_node): + nodebalancer_id = nodebalancer_w_config_and_node[0] result = exec_test_command( - BASE_CMD + BASE_CMDS["nodebalancers"] + [ "config-create", nodebalancer_id, @@ -241,18 +90,18 @@ def test_create_standard_configuration_profile(test_node_balancers): "--port", "82", ] - ).stdout.decode() + ) assert re.search( "[0-9]+,82,http,roundrobin,none,True,recommended,,", result ) -def test_view_configuration_profile(test_node_balancers): - nodebalancer_id = test_node_balancers[0] - config_id = test_node_balancers[1] +def test_view_configuration_profile(nodebalancer_w_config_and_node): + nodebalancer_id = nodebalancer_w_config_and_node[0] + config_id = nodebalancer_w_config_and_node[1] result = exec_test_command( - BASE_CMD + BASE_CMDS["nodebalancers"] + [ "config-view", nodebalancer_id, @@ -262,24 +111,26 @@ def test_view_configuration_profile(test_node_balancers): "--text", "--no-headers", ] - ).stdout.decode() + ) assert re.search( "[0-9]+,80,http,roundrobin,none,True,recommended,,", result ) -def test_add_node_to_conf_profile(test_node_balancers, create_linode_to_add): - linode_create = create_linode_to_add +def test_add_node_to_conf_profile( + nodebalancer_w_config_and_node, linode_to_add +): + linode_create = linode_to_add linode_arr = linode_create.split(",") ip_arr = linode_arr[1].split(" ") node_ip = ip_arr[1] node_label = "testnode1" - nodebalancer_id = test_node_balancers[0] - config_id = test_node_balancers[1] + nodebalancer_id = nodebalancer_w_config_and_node[0] + config_id = nodebalancer_w_config_and_node[1] result = exec_test_command( - BASE_CMD + BASE_CMDS["nodebalancers"] + [ "node-create", "--address", @@ -295,7 +146,7 @@ def test_add_node_to_conf_profile(test_node_balancers, create_linode_to_add): nodebalancer_id, config_id, ] - ).stdout.decode() + ) assert re.search( "[0-9]+," + node_label + "," + node_ip + ":80,Unknown,100,accept", @@ -303,15 +154,15 @@ def test_add_node_to_conf_profile(test_node_balancers, create_linode_to_add): ) -def test_update_node_label(test_node_balancers): - nodebalancer_id = test_node_balancers[0] - config_id = test_node_balancers[1] - node_id = test_node_balancers[2] - node_ip = test_node_balancers[3] +def test_update_node_label(nodebalancer_w_config_and_node): + nodebalancer_id = nodebalancer_w_config_and_node[0] + config_id = nodebalancer_w_config_and_node[1] + node_id = nodebalancer_w_config_and_node[2] + node_ip = nodebalancer_w_config_and_node[3] new_label = "testnode1-edited" result = exec_test_command( - BASE_CMD + BASE_CMDS["nodebalancers"] + [ "node-update", nodebalancer_id, @@ -324,7 +175,7 @@ def test_update_node_label(test_node_balancers): "--delimiter", ",", ] - ).stdout.decode() + ) assert re.search( "[0-9]+," + new_label + "," + node_ip + ":80" + ",Unknown,100,accept", @@ -332,18 +183,18 @@ def test_update_node_label(test_node_balancers): ) -def test_update_node_port(test_node_balancers): - nodebalancer_id = test_node_balancers[0] - config_id = test_node_balancers[1] - node_id = test_node_balancers[2] - node_ip = test_node_balancers[3] +def test_update_node_port(nodebalancer_w_config_and_node): + nodebalancer_id = nodebalancer_w_config_and_node[0] + config_id = nodebalancer_w_config_and_node[1] + node_id = nodebalancer_w_config_and_node[2] + node_ip = nodebalancer_w_config_and_node[3] updated_port = ":23" new_address = node_ip + updated_port result = exec_test_command( - BASE_CMD + BASE_CMDS["nodebalancers"] + [ "node-update", nodebalancer_id, @@ -356,20 +207,22 @@ def test_update_node_port(test_node_balancers): "--delimiter", ",", ] - ).stdout.decode() + ) assert "[0-9]+,.," + new_address + ",Unknown,100,accept", result -def test_fail_to_update_node_to_public_ipv4_address(test_node_balancers): - nodebalancer_id = test_node_balancers[0] - config_id = test_node_balancers[1] - node_id = test_node_balancers[2] +def test_fail_to_update_node_to_public_ipv4_address( + nodebalancer_w_config_and_node, +): + nodebalancer_id = nodebalancer_w_config_and_node[0] + config_id = nodebalancer_w_config_and_node[1] + node_id = nodebalancer_w_config_and_node[2] public_ip = "8.8.8.8:80" result = exec_failing_test_command( - BASE_CMD + BASE_CMDS["nodebalancers"] + [ "node-update", nodebalancer_id, @@ -383,28 +236,31 @@ def test_fail_to_update_node_to_public_ipv4_address(test_node_balancers): ",", ], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) assert "Request failed: 400" in result assert "Must begin with 192.168" in result -def test_remove_node_from_configuration_profile(test_node_balancers): - nodebalancer_id = test_node_balancers[0] - config_id = test_node_balancers[1] - node_id = test_node_balancers[2] +def test_remove_node_from_configuration_profile(nodebalancer_w_config_and_node): + nodebalancer_id = nodebalancer_w_config_and_node[0] + config_id = nodebalancer_w_config_and_node[1] + node_id = nodebalancer_w_config_and_node[2] exec_test_command( - BASE_CMD + ["node-delete", nodebalancer_id, config_id, node_id] + BASE_CMDS["nodebalancers"] + + ["node-delete", nodebalancer_id, config_id, node_id] ) -def test_update_the_port_of_a_configuration_profile(test_node_balancers): - nodebalancer_id = test_node_balancers[0] - config_id = test_node_balancers[1] +def test_update_the_port_of_a_configuration_profile( + nodebalancer_w_config_and_node, +): + nodebalancer_id = nodebalancer_w_config_and_node[0] + config_id = nodebalancer_w_config_and_node[1] result = exec_test_command( - BASE_CMD + BASE_CMDS["nodebalancers"] + [ "config-update", "--port", @@ -418,18 +274,18 @@ def test_update_the_port_of_a_configuration_profile(test_node_balancers): "--delimiter", ",", ] - ).stdout.decode() + ) assert re.search( "[0-9]+,10700,tcp,roundrobin,none,True,recommended,,", result ) -def test_add_additional_configuration_profile(test_node_balancers): - nodebalancer_id = test_node_balancers[0] +def test_add_additional_configuration_profile(nodebalancer_w_config_and_node): + nodebalancer_id = nodebalancer_w_config_and_node[0] result = exec_test_command( - BASE_CMD + BASE_CMDS["nodebalancers"] + [ "config-create", nodebalancer_id, @@ -440,18 +296,18 @@ def test_add_additional_configuration_profile(test_node_balancers): "--port", "81", ] - ).stdout.decode() + ) assert re.search( "^[0-9](.*),81,http,roundrobin,none,True,recommended,,", result ) -def test_list_multiple_configuration_profile(test_node_balancers): - nodebalancer_id = test_node_balancers[0] +def test_list_multiple_configuration_profile(nodebalancer_w_config_and_node): + nodebalancer_id = nodebalancer_w_config_and_node[0] - result = exec_test_command( - BASE_CMD + exec_test_command( + BASE_CMDS["nodebalancers"] + [ "config-create", nodebalancer_id, @@ -465,7 +321,7 @@ def test_list_multiple_configuration_profile(test_node_balancers): ) result = exec_test_command( - BASE_CMD + BASE_CMDS["nodebalancers"] + [ "configs-list", nodebalancer_id, @@ -474,7 +330,7 @@ def test_list_multiple_configuration_profile(test_node_balancers): "--delimiter", ",", ] - ).stdout.decode() + ) assert re.search( "[0-9]+,8[0-1],http,roundrobin,none,True,recommended,,", result @@ -482,3 +338,7 @@ def test_list_multiple_configuration_profile(test_node_balancers): assert re.search( "[0-9]+,83,http,roundrobin,none,True,recommended,,", result ) + + +def nodebalancer_created(): + return "[0-9]+,balancer[0-9]+,us-ord,[0-9]+-[0-9]+-[0-9]+-[0-9]+.ip.linodeusercontent.com,0" diff --git a/tests/integration/obj/conftest.py b/tests/integration/obj/conftest.py index 01ce0b57b..87006fce7 100644 --- a/tests/integration/obj/conftest.py +++ b/tests/integration/obj/conftest.py @@ -1,17 +1,28 @@ import json from dataclasses import dataclass -from typing import Callable, Optional +from pathlib import Path +from typing import Callable, List, Optional import pytest from pytest import MonkeyPatch from linodecli.plugins.obj import ENV_ACCESS_KEY_NAME, ENV_SECRET_KEY_NAME -from tests.integration.helpers import exec_test_command, get_random_text +from tests.integration.helpers import ( + BASE_CMDS, + exec_test_command, + get_random_text, +) REGION = "us-southeast-1" -CLI_CMD = ["linode-cli", "object-storage"] BASE_CMD = ["linode-cli", "obj", "--cluster", REGION] +""" +Complicated type alias for fixtures and other stuff. +""" + +GetTestFilesType = Callable[[Optional[int], Optional[str]], List[Path]] +GetTestFileType = Callable[[Optional[str], Optional[str], Optional[int]], Path] + @dataclass class Keys: @@ -85,42 +96,40 @@ def static_site_error(): def keys(): response = json.loads( exec_test_command( - CLI_CMD + BASE_CMDS["object-storage"] + [ "keys-create", "--label", "cli-integration-test-obj-key", "--json", ], - ).stdout.decode() + ) )[0] _keys = Keys( access_key=response.get("access_key"), secret_key=response.get("secret_key"), ) yield _keys - exec_test_command(CLI_CMD + ["keys-delete", str(response.get("id"))]) + exec_test_command( + BASE_CMDS["object-storage"] + ["keys-delete", str(response.get("id"))] + ) @pytest.fixture(scope="session") def test_key(): label = get_random_text(10) - key = ( - exec_test_command( - CLI_CMD - + [ - "keys-create", - "--label", - label, - "--text", - "--no-headers", - "--format=id", - ] - ) - .stdout.decode() - .strip() + key = exec_test_command( + BASE_CMDS["object-storage"] + + [ + "keys-create", + "--label", + label, + "--text", + "--no-headers", + "--format=id", + ] ) yield key - exec_test_command(CLI_CMD + ["keys-delete", key]) + exec_test_command(BASE_CMDS["object-storage"] + ["keys-delete", key]) diff --git a/tests/integration/obj/test_obj_plugin.py b/tests/integration/obj/test_obj_plugin.py index abaf3fb26..3960ba81d 100644 --- a/tests/integration/obj/test_obj_plugin.py +++ b/tests/integration/obj/test_obj_plugin.py @@ -6,9 +6,15 @@ from pytest import MonkeyPatch from linodecli.plugins.obj.list import TRUNCATED_MSG -from tests.integration.fixture_types import GetTestFilesType, GetTestFileType from tests.integration.helpers import count_lines, exec_test_command -from tests.integration.obj.conftest import BASE_CMD, REGION, Keys, patch_keys +from tests.integration.obj.conftest import ( + BASE_CMD, + REGION, + GetTestFilesType, + GetTestFileType, + Keys, + patch_keys, +) def test_obj_single_file_single_bucket( @@ -22,7 +28,7 @@ def test_obj_single_file_single_bucket( bucket_name = create_bucket() exec_test_command(BASE_CMD + ["put", str(file_path), bucket_name]) process = exec_test_command(BASE_CMD + ["la"]) - output = process.stdout.decode() + output = process assert f"{bucket_name}/{file_path.name}" in output @@ -30,22 +36,22 @@ def test_obj_single_file_single_bucket( assert str(file_size) in output process = exec_test_command(BASE_CMD + ["ls"]) - output = process.stdout.decode() + output = process assert bucket_name in output assert file_path.name not in output process = exec_test_command(BASE_CMD + ["ls", bucket_name]) - output = process.stdout.decode() + output = process assert bucket_name not in output assert str(file_size) in output assert file_path.name in output downloaded_file_path = file_path.parent / f"downloaded_{file_path.name}" - process = exec_test_command( + exec_test_command( BASE_CMD + ["get", bucket_name, file_path.name, str(downloaded_file_path)] ) - output = process.stdout.decode() + with open(downloaded_file_path) as f2, open(file_path) as f1: assert f1.read() == f2.read() @@ -62,26 +68,23 @@ def test_obj_single_file_single_bucket_with_prefix( exec_test_command( BASE_CMD + ["put", str(file_path), f"{bucket_name}/prefix"] ) - process = exec_test_command(BASE_CMD + ["la"]) - output = process.stdout.decode() + output = exec_test_command(BASE_CMD + ["la"]) assert f"{bucket_name}/prefix/{file_path.name}" in output file_size = file_path.stat().st_size assert str(file_size) in output - process = exec_test_command(BASE_CMD + ["ls"]) - output = process.stdout.decode() + output = exec_test_command(BASE_CMD + ["ls"]) assert bucket_name in output assert file_path.name not in output - process = exec_test_command(BASE_CMD + ["ls", bucket_name]) - output = process.stdout.decode() + output = exec_test_command(BASE_CMD + ["ls", bucket_name]) assert bucket_name not in output assert "prefix" in output downloaded_file_path = file_path.parent / f"downloaded_{file_path.name}" - process = exec_test_command( + exec_test_command( BASE_CMD + [ "get", @@ -90,7 +93,6 @@ def test_obj_single_file_single_bucket_with_prefix( str(downloaded_file_path), ] ) - output = process.stdout.decode() with open(downloaded_file_path) as f2, open(file_path) as f1: assert f1.read() == f2.read() @@ -108,26 +110,23 @@ def test_obj_single_file_single_bucket_with_prefix_ltrim( exec_test_command( BASE_CMD + ["put", str(file_path), f"{bucket_name}/bkprefix"] ) - process = exec_test_command(BASE_CMD + ["la"]) - output = process.stdout.decode() + output = exec_test_command(BASE_CMD + ["la"]) assert f"{bucket_name}/bkprefix/{file_path.name}" in output file_size = file_path.stat().st_size assert str(file_size) in output - process = exec_test_command(BASE_CMD + ["ls"]) - output = process.stdout.decode() + output = exec_test_command(BASE_CMD + ["ls"]) assert bucket_name in output assert file_path.name not in output - process = exec_test_command(BASE_CMD + ["ls", bucket_name]) - output = process.stdout.decode() + output = exec_test_command(BASE_CMD + ["ls", bucket_name]) assert bucket_name not in output assert "bkprefix" in output downloaded_file_path = file_path.parent / f"downloaded_{file_path.name}" - process = exec_test_command( + output = exec_test_command( BASE_CMD + [ "get", @@ -136,7 +135,6 @@ def test_obj_single_file_single_bucket_with_prefix_ltrim( str(downloaded_file_path), ] ) - output = process.stdout.decode() with open(downloaded_file_path) as f2, open(file_path) as f1: assert f1.read() == f2.read() @@ -153,10 +151,9 @@ def test_multi_files_multi_bucket( file_paths = generate_test_files(number) for bucket in bucket_names: for file in file_paths: - process = exec_test_command( + output = exec_test_command( BASE_CMD + ["put", str(file.resolve()), bucket] ) - output = process.stdout.decode() assert "100.0%" in output assert "Done" in output @@ -197,34 +194,30 @@ def test_all_rows( bucket_name = create_bucket() file_paths = generate_test_files(number) - process = exec_test_command( + output = exec_test_command( BASE_CMD + ["put"] + [str(file.resolve()) for file in file_paths] + [bucket_name] ) - output = process.stdout.decode() assert "100.0%" in output assert "Done" in output - process = exec_test_command( + output = exec_test_command( BASE_CMD + ["ls", bucket_name, "--page-size", "2", "--page", "1"] ) - output = process.stdout.decode() assert TRUNCATED_MSG in output assert count_lines(output) == 3 - process = exec_test_command( + output = exec_test_command( BASE_CMD + ["ls", bucket_name, "--page-size", "999"] ) - output = process.stdout.decode() assert TRUNCATED_MSG not in output assert count_lines(output) == 5 - process = exec_test_command( + output = exec_test_command( BASE_CMD + ["ls", bucket_name, "--page-size", "2", "--all-rows"] ) - output = process.stdout.decode() assert TRUNCATED_MSG not in output assert count_lines(output) == 5 @@ -289,14 +282,13 @@ def test_static_site( assert response.status_code == 404 assert "Error!" in response.text - process = exec_test_command(BASE_CMD + ["ws-info", bucket]) - output = process.stdout.decode() + output = exec_test_command(BASE_CMD + ["ws-info", bucket]) assert f"Bucket {bucket}: Website configuration" in output assert f"Website endpoint: {ws_endpoint}" in output assert f"Index document: {index_file.name}" in output assert f"Error document: {error_file.name}" in output - process = exec_test_command(BASE_CMD + ["ws-delete", bucket]) + exec_test_command(BASE_CMD + ["ws-delete", bucket]) response = requests.get(ws_url) assert response.status_code == 404 @@ -324,17 +316,14 @@ def test_show_usage( BASE_CMD + ["put", str(large_file1), str(large_file2), bucket2] ) - process = exec_test_command(BASE_CMD + ["du"]) - output = process.stdout.decode() + output = exec_test_command(BASE_CMD + ["du"]) assert "MB Total" in output - process = exec_test_command(BASE_CMD + ["du", bucket1]) - output = process.stdout.decode() + output = exec_test_command(BASE_CMD + ["du", bucket1]) assert "10.0 MB" in output assert "1 objects" in output - process = exec_test_command(BASE_CMD + ["du", bucket2]) - output = process.stdout.decode() + output = exec_test_command(BASE_CMD + ["du", bucket2]) assert "30.0 MB" in output assert "2 objects" in output @@ -352,10 +341,9 @@ def test_generate_url( exec_test_command(BASE_CMD + ["put", str(test_file), bucket]) - process = exec_test_command( + url = exec_test_command( BASE_CMD + ["signurl", bucket, test_file.name, "+300"] ) - url = process.stdout.decode() response = requests.get(url.strip("\n")) assert response.text == content assert response.status_code == 200 diff --git a/tests/integration/obj/test_obj_quota.py b/tests/integration/obj/test_obj_quota.py index 308216c64..4fbeb6e18 100644 --- a/tests/integration/obj/test_obj_quota.py +++ b/tests/integration/obj/test_obj_quota.py @@ -1,30 +1,12 @@ import json import random -from tests.integration.helpers import exec_test_command -from tests.integration.obj.conftest import CLI_CMD - - -def get_quota_id(): - response = ( - exec_test_command(CLI_CMD + ["get-object-storage-quotas", "--json"]) - .stdout.decode() - .rstrip() - ) - - quotas = json.loads(response) - if not quotas: - return None - - random_quota = random.choice(quotas) - return random_quota["quota_id"] +from tests.integration.helpers import BASE_CMDS, exec_test_command def test_obj_quotas_list(): - response = ( - exec_test_command(CLI_CMD + ["get-object-storage-quotas", "--json"]) - .stdout.decode() - .rstrip() + response = exec_test_command( + BASE_CMDS["object-storage"] + ["get-object-storage-quotas", "--json"] ) quotas = json.loads(response) @@ -47,12 +29,9 @@ def test_obj_quotas_list(): def test_obj_quota_view(): quota_id = get_quota_id() - response = ( - exec_test_command( - CLI_CMD + ["get-object-storage-quota", quota_id, "--json"] - ) - .stdout.decode() - .rstrip() + response = exec_test_command( + BASE_CMDS["object-storage"] + + ["get-object-storage-quota", quota_id, "--json"] ) data = json.loads(response) @@ -79,12 +58,9 @@ def test_obj_quota_view(): def test_obj_quota_usage_view(): quota_id = get_quota_id() - response = ( - exec_test_command( - CLI_CMD + ["get-object-storage-quota-usage", quota_id, "--json"] - ) - .stdout.decode() - .rstrip() + response = exec_test_command( + BASE_CMDS["object-storage"] + + ["get-object-storage-quota-usage", quota_id, "--json"] ) data = json.loads(response) @@ -95,3 +71,16 @@ def test_obj_quota_usage_view(): assert "usage" in item assert isinstance(item["quota_limit"], int) assert isinstance(item["usage"], int) + + +def get_quota_id(): + response = exec_test_command( + BASE_CMDS["object-storage"] + ["get-object-storage-quotas", "--json"] + ) + + quotas = json.loads(response) + if not quotas: + return None + + random_quota = random.choice(quotas) + return random_quota["quota_id"] diff --git a/tests/integration/obj/test_object_storage.py b/tests/integration/obj/test_object_storage.py index 8f45041fe..8a69c34df 100644 --- a/tests/integration/obj/test_object_storage.py +++ b/tests/integration/obj/test_object_storage.py @@ -4,15 +4,17 @@ import pytest from pytest import MonkeyPatch -from tests.integration.helpers import exec_test_command, get_random_text -from tests.integration.obj.conftest import CLI_CMD, REGION, Keys +from tests.integration.helpers import ( + BASE_CMDS, + exec_test_command, + get_random_text, +) +from tests.integration.obj.conftest import REGION, Keys def test_clusters_list(): - response = ( - exec_test_command(CLI_CMD + ["clusters-list", "--json"]) - .stdout.decode() - .rstrip() + response = exec_test_command( + BASE_CMDS["object-storage"] + ["clusters-list", "--json"] ) clusters = json.loads(response) @@ -38,10 +40,8 @@ def test_clusters_list(): def test_clusters_view(): - response = ( - exec_test_command(CLI_CMD + ["clusters-view", REGION, "--json"]) - .stdout.decode() - .rstrip() + response = exec_test_command( + BASE_CMDS["object-storage"] + ["clusters-view", REGION, "--json"] ) clusters = json.loads(response) @@ -74,20 +74,16 @@ def test_keys_create( bucket_name = create_bucket() region = "us-southeast" # Fixed typo label = get_random_text(10) - response = ( - exec_test_command( - CLI_CMD - + [ - "keys-create", - "--label", - label, - "--bucket_access", - f'[{{"region": "{region}", "bucket_name": "{bucket_name}", "permissions": "read_write"}}]', - "--json", - ] - ) - .stdout.decode() - .rstrip() + response = exec_test_command( + BASE_CMDS["object-storage"] + + [ + "keys-create", + "--label", + label, + "--bucket_access", + f'[{{"region": "{region}", "bucket_name": "{bucket_name}", "permissions": "read_write"}}]', + "--json", + ] ) data = json.loads(response) @@ -110,48 +106,42 @@ def test_keys_create( assert region_info["s3_endpoint"].endswith(".linodeobjects.com") assert region_info["endpoint_type"] == "E0" - exec_test_command(CLI_CMD + ["keys-delete", str(key["id"])]) + exec_test_command( + BASE_CMDS["object-storage"] + ["keys-delete", str(key["id"])] + ) def test_keys_delete(): label = get_random_text(10) - key = ( - exec_test_command( - CLI_CMD - + [ - "keys-create", - "--label", - label, - "--text", - "--no-headers", - "--format=id", - ] - ) - .stdout.decode() - .strip() + key = exec_test_command( + BASE_CMDS["object-storage"] + + [ + "keys-create", + "--label", + label, + "--text", + "--no-headers", + "--format=id", + ] ) assert key, "Key creation failed, received empty key ID" # Delete the key - exec_test_command(CLI_CMD + ["keys-delete", key]) + exec_test_command(BASE_CMDS["object-storage"] + ["keys-delete", key]) # Verify deletion by listing keys - keys_list = ( - exec_test_command(CLI_CMD + ["keys-list", "--text"]) - .stdout.decode() - .strip() + keys_list = exec_test_command( + BASE_CMDS["object-storage"] + ["keys-list", "--text"] ) assert key not in keys_list, f"Key {key} still exists after deletion!" def test_keys_list(test_key): - keys_list = ( - exec_test_command(CLI_CMD + ["keys-list", "--text"]) - .stdout.decode() - .strip() + keys_list = exec_test_command( + BASE_CMDS["object-storage"] + ["keys-list", "--text"] ) assert test_key in keys_list @@ -160,38 +150,30 @@ def test_keys_list(test_key): def test_keys_update(test_key): update_label = get_random_text(10) - updated_key_resp = ( - exec_test_command( - CLI_CMD - + [ - "keys-update", - test_key, - "--label", - update_label, - "--region", - "us-east", - "--json", - ] - ) - .stdout.decode() - .strip() + updated_key_resp = exec_test_command( + BASE_CMDS["object-storage"] + + [ + "keys-update", + test_key, + "--label", + update_label, + "--region", + "us-east", + "--json", + ] ) assert update_label in updated_key_resp def test_keys_view(test_key): - view_resp = ( - exec_test_command( - CLI_CMD - + [ - "keys-view", - test_key, - "--json", - ] - ) - .stdout.decode() - .strip() + view_resp = exec_test_command( + BASE_CMDS["object-storage"] + + [ + "keys-view", + test_key, + "--json", + ] ) data = json.loads(view_resp) @@ -211,16 +193,12 @@ def test_keys_view(test_key): def test_types(): - data = ( - exec_test_command( - CLI_CMD - + [ - "types", - "--json", - ] - ) - .stdout.decode() - .strip() + data = exec_test_command( + BASE_CMDS["object-storage"] + + [ + "types", + "--json", + ] ) types = json.loads(data) @@ -260,16 +238,12 @@ def test_types(): def test_endpoints(): - data = ( - exec_test_command( - CLI_CMD - + [ - "endpoints", - "--json", - ] - ) - .stdout.decode() - .strip() + data = exec_test_command( + BASE_CMDS["object-storage"] + + [ + "endpoints", + "--json", + ] ) endpoints = json.loads(data) @@ -288,16 +262,12 @@ def test_endpoints(): reason="Skipping until the command is fixed and aligned with techdocs example. Applicable for spec version after 4.197.1" ) def test_transfers(): - data = ( - exec_test_command( - CLI_CMD - + [ - "transfers", - "--json", - ] - ) - .stdout.decode() - .strip() + data = exec_test_command( + BASE_CMDS["object-storage"] + + [ + "transfers", + "--json", + ] ) transfers = json.loads(data) diff --git a/tests/integration/placements/fixtures.py b/tests/integration/placements/fixtures.py new file mode 100644 index 000000000..6ee1ead1b --- /dev/null +++ b/tests/integration/placements/fixtures.py @@ -0,0 +1,47 @@ +import pytest + +from tests.integration.helpers import ( + BASE_CMDS, + delete_target_id, + exec_test_command, + get_random_text, +) +from tests.integration.linodes.helpers import create_linode + + +@pytest.fixture(scope="session") +def linode_for_placement_tests(linode_cloud_firewall): + linode_id = create_linode( + firewall_id=linode_cloud_firewall, test_region="us-mia" + ) + + yield linode_id + + delete_target_id(target="linodes", id=linode_id) + + +@pytest.fixture(scope="session") +def placement_group(): + new_label = get_random_text(5) + "-label" + placement_group_id = exec_test_command( + BASE_CMDS["placement"] + + [ + "group-create", + "--label", + new_label, + "--region", + "us-mia", + "--placement_group_type", + "anti_affinity:local", + "--placement_group_policy", + "strict", + "--text", + "--no-headers", + "--format=id", + ] + ) + yield placement_group_id + + delete_target_id( + target="placement", delete_command="group-delete", id=placement_group_id + ) diff --git a/tests/integration/placements/test_placements.py b/tests/integration/placements/test_placements.py index a05b97d97..3e199e50c 100644 --- a/tests/integration/placements/test_placements.py +++ b/tests/integration/placements/test_placements.py @@ -1,66 +1,29 @@ -import time - -import pytest - from tests.integration.helpers import ( + BASE_CMDS, assert_headers_in_lines, - delete_target_id, exec_test_command, + get_random_text, +) +from tests.integration.placements.fixtures import ( # noqa: F401 + linode_for_placement_tests, + placement_group, ) - -BASE_CMD = ["linode-cli", "placement"] - - -@pytest.fixture -def create_placement_group(): - new_label = str(time.time_ns()) + "label" - placement_group_id = ( - exec_test_command( - BASE_CMD - + [ - "group-create", - "--label", - new_label, - "--region", - "us-mia", - "--placement_group_type", - "anti_affinity:local", - "--placement_group_policy", - "strict", - "--text", - "--no-headers", - "--format=id", - ] - ) - .stdout.decode() - .rstrip() - ) - yield placement_group_id - delete_target_id( - target="placement", delete_command="group-delete", id=placement_group_id - ) def test_placement_group_list(): - res = ( - exec_test_command(BASE_CMD + ["groups-list", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["placement"] + ["groups-list", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["placement_group_type", "region", "label"] assert_headers_in_lines(headers, lines) -def test_placement_group_view(create_placement_group): - placement_group_id = create_placement_group - res = ( - exec_test_command( - BASE_CMD - + ["group-view", placement_group_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() +def test_placement_group_view(placement_group): + placement_group_id = placement_group + res = exec_test_command( + BASE_CMDS["placement"] + + ["group-view", placement_group_id, "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -68,69 +31,67 @@ def test_placement_group_view(create_placement_group): assert_headers_in_lines(headers, lines) -@pytest.mark.skip(reason="BUG TPT-3109") -def test_assign_placement_group(support_test_linode_id, create_placement_group): - linode_id = support_test_linode_id - placement_group_id = create_placement_group - res = ( - exec_test_command( - BASE_CMD - + [ - "assign-linode", - placement_group_id, - "--linodes", - linode_id, - "--text", - "--delimiter=,", - ] - ) - .stdout.decode() - .rstrip() +def test_assign_placement_group(linode_for_placement_tests, placement_group): + linode_id = linode_for_placement_tests + placement_group_id = placement_group + res = exec_test_command( + BASE_CMDS["placement"] + + [ + "assign-linode", + placement_group_id, + "--linodes", + linode_id, + "--text", + "--delimiter=,", + "--no-headers", + ] ) + assert placement_group_id in res -@pytest.mark.skip(reason="BUG TPT-3109") -def test_unassign_placement_group( - support_test_linode_id, create_placement_group -): - linode_id = support_test_linode_id - placement_group_id = create_placement_group - res = ( - exec_test_command( - BASE_CMD - + [ - "unassign-linode", - placement_group_id, - "--linode", - linode_id, - "--text", - "--delimiter=,", - ] - ) - .stdout.decode() - .rstrip() +def test_unassign_placement_group(linode_for_placement_tests, placement_group): + linode_id = linode_for_placement_tests + placement_group_id = placement_group + exec_test_command( + BASE_CMDS["placement"] + + [ + "unassign-linode", + placement_group_id, + "--linode", + linode_id, + "--text", + "--delimiter=,", + ] ) - assert placement_group_id not in res + res = exec_test_command( + BASE_CMDS["placement"] + + [ + "group-view", + placement_group_id, + "--text", + "--delimiter=,", + ] + ) -def test_update_placement_group(create_placement_group): - placement_group_id = create_placement_group - new_label = str(time.time_ns()) + "label" - updated_label = ( - exec_test_command( - BASE_CMD - + [ - "group-update", - placement_group_id, - "--label", - new_label, - "--text", - "--no-headers", - "--format=label", - ] - ) - .stdout.decode() - .rstrip() + assert placement_group_id in res + assert linode_id not in res + + +def test_update_placement_group(placement_group): + placement_group_id = placement_group + new_label = get_random_text(5) + "label" + updated_label = exec_test_command( + BASE_CMDS["placement"] + + [ + "group-update", + placement_group_id, + "--label", + new_label, + "--text", + "--no-headers", + "--format=label", + ] ) assert new_label == updated_label diff --git a/tests/integration/regions/test_plugin_region_table.py b/tests/integration/regions/test_plugin_region_table.py index 0d4eade82..7540f98cc 100644 --- a/tests/integration/regions/test_plugin_region_table.py +++ b/tests/integration/regions/test_plugin_region_table.py @@ -1,43 +1,22 @@ -import os -import subprocess -from typing import List - import pytest -from tests.integration.helpers import assert_headers_in_lines, exec_test_command - -BASE_CMD = ["linode-cli", "regions"] - -# Set the console width to 150 -env = os.environ.copy() -env["COLUMNS"] = "150" - - -def exe_test_command(args: List[str]): - process = subprocess.run( - args, - stdout=subprocess.PIPE, - env=env, - ) - return process +from tests.integration.helpers import ( + BASE_CMDS, + assert_headers_in_lines, + exec_test_command, +) -def test_output(): - process = exe_test_command(["linode-cli", "region-table"]) - output = process.stdout.decode() - lines = output.split("\n") - lines = lines[3 : len(lines) - 2] - for line in lines: - assert "-" in line - assert "✔" in line - assert "│" in line +def test_regions_table_output(): + output = exec_test_command(["linode-cli", "region-table"]) + assert "-" in output + assert "✔" in output + assert "│" in output def test_regions_list(): - res = ( - exec_test_command(BASE_CMD + ["list", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["regions"] + ["list", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["label", "country", "capabilities"] @@ -46,63 +25,48 @@ def test_regions_list(): @pytest.mark.smoke def test_regions_list_avail(): - res = ( - exec_test_command(BASE_CMD + ["list-avail", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["regions"] + ["list-avail", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["region", "plan", "available"] assert_headers_in_lines(headers, lines) -@pytest.fixture -def get_region_id(): - region_id = ( - exec_test_command( - BASE_CMD - + [ - "list-avail", - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "region", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) - first_id = region_id[0] - yield first_id - - @pytest.mark.smoke -def test_regions_view(get_region_id): - region_id = get_region_id - res = ( - exec_test_command( - BASE_CMD + ["view", region_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() +def test_regions_view(): + region_id = get_region_id() + res = exec_test_command( + BASE_CMDS["regions"] + ["view", region_id, "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["label", "country", "capabilities"] assert_headers_in_lines(headers, lines) -def test_regions_view_avail(get_region_id): - region_id = get_region_id - res = ( - exec_test_command( - BASE_CMD + ["view-avail", region_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() +def test_regions_view_avail(): + region_id = get_region_id() + res = exec_test_command( + BASE_CMDS["regions"] + + ["view-avail", region_id, "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["region", "plan", "available"] assert_headers_in_lines(headers, lines) + + +def get_region_id(): + region_id = exec_test_command( + BASE_CMDS["regions"] + + [ + "list-avail", + "--text", + "--no-headers", + "--delimiter", + ",", + "--format", + "region", + ] + ).splitlines() + first_id = region_id[0] + return first_id diff --git a/tests/integration/ssh/test_plugin_ssh.py b/tests/integration/ssh/test_plugin_ssh.py index 136c8135c..513806221 100644 --- a/tests/integration/ssh/test_plugin_ssh.py +++ b/tests/integration/ssh/test_plugin_ssh.py @@ -1,13 +1,15 @@ import json -import subprocess from sys import platform -from typing import Any, Dict, List, Optional +from typing import Any, Dict import pytest from tests.integration.helpers import ( + BASE_CMDS, COMMAND_JSON_OUTPUT, + delete_target_id, exec_failing_test_command, + exec_test_command, get_random_region_with_caps, get_random_text, wait_for_condition, @@ -35,10 +37,9 @@ def target_instance(ssh_key_pair_generator, linode_cloud_firewall): with open(pubkey_file, "r") as f: pubkey = f.read().rstrip() - process = exec_test_command( - [ - "linode-cli", - "linodes", + output = exec_test_command( + BASE_CMDS["linodes"] + + [ "create", "--image", TEST_IMAGE, @@ -57,39 +58,27 @@ def target_instance(ssh_key_pair_generator, linode_cloud_firewall): ] + COMMAND_JSON_OUTPUT ) - assert process.returncode == 0 - instance_json = json.loads(process.stdout.decode())[0] - yield instance_json - - process = exec_test_command( - ["linode-cli", "linodes", "rm", str(instance_json["id"])] - ) - assert process.returncode == 0 + instance_json = json.loads(output)[0] + yield instance_json -def exec_test_command(args: List[str], timeout=None): - process = subprocess.run(args, stdout=subprocess.PIPE, timeout=timeout) - return process + delete_target_id(target="linodes", id=str(instance_json["id"])) @pytest.mark.skipif(platform == "win32", reason="Test N/A on Windows") def test_help(): - process = exec_test_command(BASE_CMD + ["--help"]) - output = process.stdout.decode() + output = exec_test_command(BASE_CMDS["ssh"] + ["--help"]) - assert process.returncode == 0 assert "[USERNAME@]LABEL" in output assert "uses the Linode's SLAAC address for SSH" in output @pytest.mark.skipif(platform == "win32", reason="Test N/A on Windows") def test_ssh_instance_provisioning(target_instance: Dict[str, Any]): - process = exec_failing_test_command( - BASE_CMD + ["root@" + target_instance["label"]], expected_code=2 + output = exec_failing_test_command( + BASE_CMDS["ssh"] + ["root@" + target_instance["label"]], expected_code=2 ) - assert process.returncode == 2 - output = process.stderr.decode() assert "is not running" in output @@ -101,26 +90,23 @@ def test_ssh_instance_ready( ): pubkey, privkey = ssh_key_pair_generator - process: Optional[subprocess.CompletedProcess] = None instance_data = {} def instance_poll_func(): nonlocal instance_data - nonlocal process - process = exec_test_command( - ["linode-cli", "linodes", "view", str(target_instance["id"])] + output = exec_test_command( + BASE_CMDS["linodes"] + + ["view", str(target_instance["id"])] + COMMAND_JSON_OUTPUT ) - assert process.returncode == 0 - instance_data = json.loads(process.stdout.decode())[0] + instance_data = json.loads(output)[0] return instance_data["status"] == "running" def ssh_poll_func(): - nonlocal process - process = exec_test_command( - BASE_CMD + output = exec_test_command( + BASE_CMDS["ssh"] + [ "root@" + instance_data["label"], "-o", @@ -132,20 +118,15 @@ def ssh_poll_func(): "echo 'hello world!'", ] ) - return process.returncode == 0 + + return "hello world!" in output # Wait for the instance to be ready wait_for_condition( POLL_INTERVAL, INSTANCE_WAIT_TIMEOUT_SECONDS, instance_poll_func ) - assert process.returncode == 0 assert instance_data["status"] == "running" # Wait for SSH to be available wait_for_condition(POLL_INTERVAL, SSH_WAIT_TIMEOUT_SECONDS, ssh_poll_func) - - assert process.returncode == 0 - - # Did we get a response from the instance? - assert "hello world!" in process.stdout.decode() diff --git a/tests/integration/ssh/test_ssh.py b/tests/integration/ssh/test_ssh.py index 219f4e840..a1f1f8e91 100644 --- a/tests/integration/ssh/test_ssh.py +++ b/tests/integration/ssh/test_ssh.py @@ -5,8 +5,12 @@ import pytest -from tests.integration.helpers import delete_target_id, exec_test_command -from tests.integration.linodes.helpers_linodes import create_linode_and_wait +from tests.integration.helpers import ( + BASE_CMDS, + delete_target_id, + exec_test_command, +) +from tests.integration.linodes.helpers import create_linode_and_wait BASE_CMD = ["linode-cli", "ssh"] NUM_OF_RETRIES = 10 @@ -20,40 +24,31 @@ def linode_in_running_state(ssh_key_pair_generator, linode_cloud_firewall): with open(pubkey_file, "r") as f: pubkey = f.read().rstrip() - res = ( - exec_test_command( - [ - "linode-cli", - "images", - "list", - "--format", - "id", - "--text", - "--no-headers", - ] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + [ + "linode-cli", + "images", + "list", + "--format", + "id", + "--text", + "--no-headers", + ] ) alpine_image = re.findall(r"linode/alpine[^\s]+", res)[0] - plan = ( - exec_test_command( - [ - "linode-cli", - "linodes", - "types", - "--format", - "id", - "--text", - "--no-headers", - ] - ) - .stdout.decode() - .rstrip() - .splitlines()[0] - ) + plan = exec_test_command( + [ + "linode-cli", + "linodes", + "types", + "--format", + "id", + "--text", + "--no-headers", + ] + ).splitlines()[0] linode_id = create_linode_and_wait( test_plan=plan, @@ -68,7 +63,7 @@ def linode_in_running_state(ssh_key_pair_generator, linode_cloud_firewall): @pytest.mark.skipif(platform == "win32", reason="Test N/A on Windows") def test_display_ssh_plugin_usage_info(): - result = exec_test_command(BASE_CMD + ["-h"]).stdout.decode() + result = exec_test_command(BASE_CMDS["ssh"] + ["-h"]) assert "[USERNAME@]LABEL" in result assert "uses the Linode's SLAAC address for SSH" in result @@ -89,22 +84,18 @@ def test_ssh_to_linode_and_get_kernel_version( linode_id = linode_in_running_state pubkey_file, privkey_file = ssh_key_pair_generator - linode_label = ( - exec_test_command( - [ - "linode-cli", - "linodes", - "list", - "--id", - linode_id, - "--format", - "label", - "--text", - "--no-headers", - ] - ) - .stdout.decode() - .rstrip() + linode_label = exec_test_command( + [ + "linode-cli", + "linodes", + "list", + "--id", + linode_id, + "--format", + "label", + "--text", + "--no-headers", + ] ) time.sleep(SSH_SLEEP_PERIOD) @@ -126,22 +117,18 @@ def test_check_vm_for_ipv4_connectivity( ): pubkey_file, privkey_file = ssh_key_pair_generator linode_id = linode_in_running_state - linode_label = ( - exec_test_command( - [ - "linode-cli", - "linodes", - "list", - "--id", - linode_id, - "--format", - "label", - "--text", - "--no-headers", - ] - ) - .stdout.decode() - .rstrip() + linode_label = exec_test_command( + [ + "linode-cli", + "linodes", + "list", + "--id", + linode_id, + "--format", + "label", + "--text", + "--no-headers", + ] ) time.sleep(SSH_SLEEP_PERIOD) diff --git a/tests/integration/stackscripts/test_stackscripts.py b/tests/integration/stackscripts/test_stackscripts.py index 48e3a3660..ad371a693 100644 --- a/tests/integration/stackscripts/test_stackscripts.py +++ b/tests/integration/stackscripts/test_stackscripts.py @@ -1,48 +1,24 @@ import re -import time import pytest from linodecli.exit_codes import ExitCodes from tests.integration.helpers import ( + BASE_CMDS, delete_target_id, exec_failing_test_command, exec_test_command, + get_random_text, ) -from tests.integration.linodes.helpers_linodes import DEFAULT_RANDOM_PASS +from tests.integration.linodes.helpers import DEFAULT_RANDOM_PASS -BASE_CMD = ["linode-cli", "stackscripts"] -DEF_LABEL = "stack_script_" + str(int(time.time())) - - -def get_linode_image_lists(): - all_images = ( - ( - exec_test_command( - [ - "linode-cli", - "images", - "list", - "--format", - "id", - "--text", - "--no-headers", - ] - ) - ) - .stdout.decode() - .rstrip() - ) - - images = re.findall(r"linode/[^\s]+", all_images) - - return images +DEF_LABEL = "stack_script_" + get_random_text(5) @pytest.fixture(scope="package", autouse=True) def test_stackscript_id(): result = exec_test_command( - BASE_CMD + BASE_CMDS["stackscripts"] + [ "create", "--script", @@ -57,7 +33,7 @@ def test_stackscript_id(): "--delimiter", ",", ] - ).stdout.decode() + ) assert re.search( "[0-9]+,.*," @@ -74,11 +50,11 @@ def test_stackscript_id(): def test_list_stackscripts(): - result = exec_test_command(BASE_CMD + ["list", "--text"]).stdout.decode() + result = exec_test_command(BASE_CMDS["stackscripts"] + ["list", "--text"]) assert "id username label images is_public created updated" in result result = exec_test_command( - BASE_CMD + BASE_CMDS["stackscripts"] + [ "list", "--text", @@ -88,7 +64,7 @@ def test_list_stackscripts(): "--delimiter", ",", ] - ).stdout.decode() + ) output = result.splitlines() @@ -97,7 +73,7 @@ def test_list_stackscripts(): def test_test_stackscript_id_fails_without_image(): result = exec_failing_test_command( - BASE_CMD + BASE_CMDS["stackscripts"] + [ "create", "--script", @@ -111,7 +87,7 @@ def test_test_stackscript_id_fails_without_image(): ",", ], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) assert "Request failed: 400" in result assert "images is required" in result @@ -119,7 +95,7 @@ def test_test_stackscript_id_fails_without_image(): def test_view_private_stackscript(): result = exec_test_command( - BASE_CMD + BASE_CMDS["stackscripts"] + [ "list", "--text", @@ -129,7 +105,7 @@ def test_view_private_stackscript(): "--delimiter", ",", ] - ).stdout.decode() + ) assert re.search( "[0-9]+,.*," @@ -143,30 +119,21 @@ def test_view_private_stackscript(): def test_update_stackscript_compatible_image(test_stackscript_id): images = get_linode_image_lists() private_stackscript = test_stackscript_id - result = ( - exec_test_command( - BASE_CMD - + [ - "update", - "--images", - images[0], - private_stackscript, - "--text", - "--no-headers", - "--delimiter", - ",", - ] - ) - .stdout.decode() - .rstrip() + result = exec_test_command( + BASE_CMDS["stackscripts"] + + [ + "update", + "--images", + images[0], + private_stackscript, + "--text", + "--no-headers", + "--delimiter", + ",", + ] ) - assert re.search( - "[0-9]+,.*,stack_script_[0-9]+," - + images[0] - + ",False,[0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+,[0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+", - result, - ) + assert images[0] in result def test_update_stackscript_to_be_compatible_with_multiple_images( @@ -176,7 +143,7 @@ def test_update_stackscript_to_be_compatible_with_multiple_images( private_stackscript = test_stackscript_id result = exec_test_command( - BASE_CMD + BASE_CMDS["stackscripts"] + [ "update", "--images", @@ -189,7 +156,7 @@ def test_update_stackscript_to_be_compatible_with_multiple_images( "--delimiter", ",", ] - ).stdout.decode() + ) assert images[0] in result assert images[1] in result @@ -220,7 +187,7 @@ def test_fail_to_deploy_stackscript_to_linode_from_incompatible_image( "--text", ], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) assert "image is not valid" in result assert "Request failed: 400" in result @@ -256,7 +223,7 @@ def test_deploy_linode_from_stackscript(test_stackscript_id): "id,region,type,image", "--no-headers", ] - ).stdout.decode() + ) assert re.search( "[0-9]+," + linode_region + "," + linode_plan + "," + images[0], result @@ -265,3 +232,21 @@ def test_deploy_linode_from_stackscript(test_stackscript_id): linode_id = result.split(",")[0] delete_target_id("linodes", linode_id) + + +def get_linode_image_lists(): + all_images = exec_test_command( + [ + "linode-cli", + "images", + "list", + "--format", + "id", + "--text", + "--no-headers", + ] + ) + + images = re.findall(r"linode/[^\s]+", all_images) + + return images diff --git a/tests/integration/support/test_support.py b/tests/integration/support/test_support.py index f9aee1ae3..7ceb2bd63 100644 --- a/tests/integration/support/test_support.py +++ b/tests/integration/support/test_support.py @@ -1,8 +1,24 @@ import pytest -from tests.integration.helpers import assert_headers_in_lines, exec_test_command +from tests.integration.helpers import ( + BASE_CMDS, + assert_headers_in_lines, + delete_target_id, + exec_test_command, + get_random_text, +) +from tests.integration.linodes.helpers import create_linode + + +@pytest.fixture +def support_test_linode_id(linode_cloud_firewall): + label = "cli-" + get_random_text(5) + + linode_id = create_linode() -BASE_CMD = ["linode-cli", "tickets"] + yield linode_id + + delete_target_id(target="linodes", id=linode_id) # this will create a support ticket on your account @@ -10,7 +26,7 @@ def test_create_support_ticket(support_test_linode_id): linode_id = support_test_linode_id exec_test_command( - BASE_CMD + BASE_CMDS["tickets"] + [ "create", "--description", @@ -25,10 +41,8 @@ def test_create_support_ticket(support_test_linode_id): def test_tickets_list(): - res = ( - exec_test_command(BASE_CMD + ["list", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["tickets"] + ["list", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["summary", "opened_by", "opened"] @@ -37,21 +51,17 @@ def test_tickets_list(): @pytest.fixture def tickets_id(): - res = ( - exec_test_command( - BASE_CMD - + [ - "list", - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["tickets"] + + [ + "list", + "--text", + "--no-headers", + "--delimiter", + ",", + "--format", + "id", + ] ) ticket_ids = res.splitlines() if not ticket_ids or ticket_ids == [""]: @@ -65,12 +75,8 @@ def test_tickets_view(tickets_id): pytest.skip("No support tickets available to view.") ticket_id = tickets_id - res = ( - exec_test_command( - BASE_CMD + ["view", ticket_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["tickets"] + ["view", ticket_id, "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["summary", "opened_by", "opened"] @@ -83,7 +89,7 @@ def test_tickets_view(tickets_id): def test_reply_support_ticket(tickets_id): ticket_id = tickets_id exec_test_command( - BASE_CMD + BASE_CMDS["tickets"] + [ "reply", ticket_id, @@ -100,12 +106,8 @@ def test_view_replies_support_ticket(tickets_id): pytest.skip("No support tickets available to view replies.") ticket_id = tickets_id - res = ( - exec_test_command( - BASE_CMD + ["replies", ticket_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["tickets"] + ["replies", ticket_id, "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["created_by", "created"] diff --git a/tests/integration/tags/test_tags.py b/tests/integration/tags/test_tags.py index 304580149..a51e13629 100644 --- a/tests/integration/tags/test_tags.py +++ b/tests/integration/tags/test_tags.py @@ -1,27 +1,23 @@ -import time - import pytest from linodecli.exit_codes import ExitCodes from tests.integration.helpers import ( + BASE_CMDS, delete_target_id, exec_failing_test_command, exec_test_command, + get_random_text, ) -BASE_CMD = ["linode-cli", "tags"] -unique_tag = str(int(time.time())) + "-tag" - - -def test_display_tags(): - exec_test_command(BASE_CMD + ["list"]) - @pytest.fixture(scope="session") def test_tag_instance(): + unique_tag = get_random_text(5) + "-tag" + exec_test_command( - BASE_CMD + ["create", "--label", unique_tag, "--text", "--no-headers"] - ).stdout.decode() + BASE_CMDS["tags"] + + ["create", "--label", unique_tag, "--text", "--no-headers"] + ) yield unique_tag @@ -31,8 +27,8 @@ def test_tag_instance(): @pytest.mark.smoke def test_view_unique_tag(test_tag_instance): result = exec_test_command( - BASE_CMD + ["list", "--text", "--no-headers"] - ).stdout.decode() + BASE_CMDS["tags"] + ["list", "--text", "--no-headers"] + ) assert test_tag_instance in result @@ -40,8 +36,9 @@ def test_view_unique_tag(test_tag_instance): def test_fail_to_create_tag_shorter_than_three_char(): bad_tag = "aa" result = exec_failing_test_command( - BASE_CMD + ["create", "--label", bad_tag, "--text", "--no-headers"], + BASE_CMDS["tags"] + + ["create", "--label", bad_tag, "--text", "--no-headers"], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) assert "Request failed: 400" in result assert "Length must be 3-50 characters" in result diff --git a/tests/integration/users/test_profile.py b/tests/integration/users/test_profile.py index 3d725f460..e9b3c6130 100644 --- a/tests/integration/users/test_profile.py +++ b/tests/integration/users/test_profile.py @@ -1,15 +1,13 @@ -import pytest - -from tests.integration.helpers import assert_headers_in_lines, exec_test_command - -BASE_CMD = ["linode-cli", "profile"] +from tests.integration.helpers import ( + BASE_CMDS, + assert_headers_in_lines, + exec_test_command, +) def test_profile_view(): - res = ( - exec_test_command(BASE_CMD + ["view", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["profile"] + ["view", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["username", "email", "restricted"] @@ -17,10 +15,8 @@ def test_profile_view(): def test_profile_apps_list(): - res = ( - exec_test_command(BASE_CMD + ["apps-list", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["profile"] + ["apps-list", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["label", "scopes", "website"] @@ -28,60 +24,45 @@ def test_profile_apps_list(): def test_profile_devices_list(): - res = ( - exec_test_command( - BASE_CMD + ["devices-list", "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["profile"] + ["devices-list", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["created", "expiry", "user_agent"] assert_headers_in_lines(headers, lines) -@pytest.fixture -def login_ids(): - login_id = ( - exec_test_command( - BASE_CMD - + [ - "logins-list", - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - .splitlines() - ) +def get_login_id(): + login_id = exec_test_command( + BASE_CMDS["profile"] + + [ + "logins-list", + "--text", + "--no-headers", + "--delimiter", + ",", + "--format", + "id", + ] + ).splitlines() first_login_id = login_id[0] - yield first_login_id + return first_login_id def test_profile_login_list(): - res = ( - exec_test_command(BASE_CMD + ["logins-list", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["profile"] + ["logins-list", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["datetime", "username", "status"] assert_headers_in_lines(headers, lines) -def test_profile_login_view(login_ids): - login_id = login_ids - res = ( - exec_test_command( - BASE_CMD + ["login-view", login_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() +def test_profile_login_view(): + login_id = get_login_id() + res = exec_test_command( + BASE_CMDS["profile"] + + ["login-view", login_id, "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["datetime", "username", "status"] @@ -89,18 +70,14 @@ def test_profile_login_view(login_ids): def test_security_questions_list(): - res = ( - exec_test_command( - [ - "linode-cli", - "security-questions", - "list", - "--text", - "--delimiter=,", - ] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + [ + "linode-cli", + "security-questions", + "list", + "--text", + "--delimiter=,", + ] ) lines = res.splitlines() headers = ["security_questions.id", "security_questions.question"] @@ -108,10 +85,8 @@ def test_security_questions_list(): def test_profile_token_list(): - res = ( - exec_test_command(BASE_CMD + ["tokens-list", "--text", "--delimiter=,"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["profile"] + ["tokens-list", "--text", "--delimiter=,"] ) lines = res.splitlines() headers = ["label", "scopes", "token"] @@ -119,18 +94,14 @@ def test_profile_token_list(): def test_sshkeys_list(): - res = ( - exec_test_command( - [ - "linode-cli", - "sshkeys", - "list", - "--text", - "--delimiter=,", - ] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + [ + "linode-cli", + "sshkeys", + "list", + "--text", + "--delimiter=,", + ] ) lines = res.splitlines() headers = ["label", "ssh_key"] diff --git a/tests/integration/users/test_users.py b/tests/integration/users/test_users.py index 231310ec0..c17a4ab99 100644 --- a/tests/integration/users/test_users.py +++ b/tests/integration/users/test_users.py @@ -1,21 +1,19 @@ -import time - import pytest -from tests.integration.helpers import exec_test_command +from tests.integration.helpers import ( + BASE_CMDS, + exec_test_command, + get_random_text, +) BASE_CMD = ["linode-cli", "users"] -unique_user = "test-user-" + str(int(time.time())) +unique_user = "test-user-" + get_random_text(5) @pytest.fixture(scope="package", autouse=True) def teardown_fixture(): yield "setup" - remove_users() - - -def remove_users(): - exec_test_command(BASE_CMD + ["delete", unique_user]) + exec_test_command(BASE_CMDS["users"] + ["delete", unique_user]) @pytest.fixture @@ -37,10 +35,10 @@ def test_create_user(): def test_display_users(): - exec_test_command(BASE_CMD + ["list"]) + exec_test_command(BASE_CMDS["users"] + ["list"]) @pytest.mark.smoke @pytest.mark.usefixtures("test_create_user") def test_view_user(): - exec_test_command(BASE_CMD + ["view", unique_user]) + exec_test_command(BASE_CMDS["users"] + ["view", unique_user]) diff --git a/tests/integration/vlans/test_vlans.py b/tests/integration/vlans/test_vlans.py index 894fef9f7..553222883 100644 --- a/tests/integration/vlans/test_vlans.py +++ b/tests/integration/vlans/test_vlans.py @@ -1,19 +1,17 @@ -from tests.integration.helpers import assert_headers_in_lines, exec_test_command - -BASE_CMD = ["linode-cli", "vlans"] +from tests.integration.helpers import ( + BASE_CMDS, + assert_headers_in_lines, + exec_test_command, +) def test_list_vlans(): - types = ( - exec_test_command( - BASE_CMD - + [ - "ls", - "--text", - ] - ) - .stdout.decode() - .rstrip() + types = exec_test_command( + BASE_CMDS["vlans"] + + [ + "ls", + "--text", + ] ) headers = ["region", "label", "linodes"] @@ -23,16 +21,12 @@ def test_list_vlans(): def test_list_vlans_help_menu(): - help_menu = ( - exec_test_command( - BASE_CMD - + [ - "ls", - "--h", - ] - ) - .stdout.decode() - .rstrip() + help_menu = exec_test_command( + BASE_CMDS["vlans"] + + [ + "ls", + "--h", + ] ) assert "linode-cli vlans ls" in help_menu @@ -43,16 +37,12 @@ def test_list_vlans_help_menu(): def test_delete_vlans_help_menu(): - help_menu = ( - exec_test_command( - BASE_CMD - + [ - "delete", - "--h", - ] - ) - .stdout.decode() - .rstrip() + help_menu = exec_test_command( + BASE_CMDS["vlans"] + + [ + "delete", + "--h", + ] ) assert "linode-cli vlans delete [LABEL] [REGIONID]" in help_menu diff --git a/tests/integration/volumes/fixtures.py b/tests/integration/volumes/fixtures.py new file mode 100644 index 000000000..b00416b39 --- /dev/null +++ b/tests/integration/volumes/fixtures.py @@ -0,0 +1,35 @@ +import pytest + +from tests.integration.helpers import ( + BASE_CMDS, + delete_target_id, + exec_test_command, + get_random_text, +) + + +@pytest.fixture(scope="session") +def volume_instance_id(): + label = get_random_text(8) + volume_id = exec_test_command( + BASE_CMDS["volumes"] + + [ + "create", + "--label", + label, + "--region", + "us-ord", + "--size", + "10", + "--text", + "--no-headers", + "--delimiter", + ",", + "--format", + "id", + ] + ) + + yield volume_id + + delete_target_id(target="volumes", id=volume_id) diff --git a/tests/integration/volumes/test_volumes.py b/tests/integration/volumes/test_volumes.py index 17dbade72..2c67d90c5 100644 --- a/tests/integration/volumes/test_volumes.py +++ b/tests/integration/volumes/test_volumes.py @@ -1,56 +1,23 @@ import os import re -import time import pytest from linodecli.exit_codes import ExitCodes from tests.integration.helpers import ( - delete_target_id, + BASE_CMDS, exec_failing_test_command, exec_test_command, get_random_text, ) +from tests.integration.volumes.fixtures import volume_instance_id # noqa: F401 -BASE_CMD = ["linode-cli", "volumes"] -label = get_random_text(8) -unique_tag = str(time.time_ns()) + "-tag" - -@pytest.fixture(scope="package") -def test_volume_id(): +def test_fail_to_create_volume_under_10gb(): label = get_random_text(8) - volume_id = ( - exec_test_command( - BASE_CMD - + [ - "create", - "--label", - label, - "--region", - "us-ord", - "--size", - "10", - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield volume_id - - delete_target_id(target="volumes", id=volume_id) - -def test_fail_to_create_volume_under_10gb(): result = exec_failing_test_command( - BASE_CMD + BASE_CMDS["volumes"] + [ "create", "--label", @@ -63,7 +30,7 @@ def test_fail_to_create_volume_under_10gb(): "--no-headers", ], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) if "test" == os.environ.get( "TEST_ENVIRONMENT", None @@ -74,8 +41,10 @@ def test_fail_to_create_volume_under_10gb(): def test_fail_to_create_volume_without_region(): + label = get_random_text(8) + result = exec_failing_test_command( - BASE_CMD + BASE_CMDS["volumes"] + [ "create", "--label", @@ -87,14 +56,14 @@ def test_fail_to_create_volume_without_region(): "--no-defaults", ], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) assert "Request failed: 400" in result assert "Must provide a region or a Linode ID" in result def test_fail_to_create_volume_without_label(): result = exec_failing_test_command( - BASE_CMD + BASE_CMDS["volumes"] + [ "create", "--region", @@ -105,14 +74,16 @@ def test_fail_to_create_volume_without_label(): "--no-headers", ], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) assert "Request failed: 400" in result assert "label label is required" in result def test_fail_to_create_volume_over_1024gb_in_size(): + label = get_random_text(8) + result = exec_failing_test_command( - BASE_CMD + BASE_CMDS["volumes"] + [ "create", "--label", @@ -125,7 +96,7 @@ def test_fail_to_create_volume_over_1024gb_in_size(): "--no-headers", ], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) if "test" == os.environ.get( "TEST_ENVIRONMENT", None ) or "dev" == os.environ.get("TEST_ENVIRONMENT", None): @@ -136,7 +107,7 @@ def test_fail_to_create_volume_over_1024gb_in_size(): def test_fail_to_create_volume_with_all_numberic_label(): result = exec_failing_test_command( - BASE_CMD + BASE_CMDS["volumes"] + [ "create", "--label", @@ -149,14 +120,14 @@ def test_fail_to_create_volume_with_all_numberic_label(): "--no-headers", ], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) assert "Request failed: 400" in result assert "label Must begin with a letter" in result -def test_list_volume(test_volume_id): +def test_list_volume(volume_instance_id): result = exec_test_command( - BASE_CMD + BASE_CMDS["volumes"] + [ "list", "--text", @@ -166,7 +137,7 @@ def test_list_volume(test_volume_id): "--format", "id,label,status,region,size,linode_id,linode_label", ] - ).stdout.decode() + ) assert re.search( "[0-9]+,[A-Za-z0-9-]+,(creating|active|offline),[A-Za-z0-9-]+,[0-9]+,,", result, @@ -174,10 +145,10 @@ def test_list_volume(test_volume_id): @pytest.mark.smoke -def test_view_single_volume(test_volume_id): - volume_id = test_volume_id +def test_view_single_volume(volume_instance_id): + volume_id = volume_instance_id result = exec_test_command( - BASE_CMD + BASE_CMDS["volumes"] + [ "view", volume_id, @@ -188,16 +159,16 @@ def test_view_single_volume(test_volume_id): "--format", "id,label,size,region", ] - ).stdout.decode() + ) assert re.search(volume_id + ",[A-Za-z0-9-]+,[0-9]+,[a-z-]+", result) -def test_update_volume_label(test_volume_id): - volume_id = test_volume_id - new_unique_label = "label-" + str(int(time.time())) +def test_update_volume_label(volume_instance_id): + volume_id = volume_instance_id + new_unique_label = "label-" + get_random_text(5) result = exec_test_command( - BASE_CMD + BASE_CMDS["volumes"] + [ "update", volume_id, @@ -208,15 +179,16 @@ def test_update_volume_label(test_volume_id): "--text", "--no-headers", ] - ).stdout.decode() + ) assert new_unique_label in result -def test_add_new_tag_to_volume(test_volume_id): - volume_id = test_volume_id +def test_add_new_tag_to_volume(volume_instance_id): + unique_tag = get_random_text(5) + "-tag" + volume_id = volume_instance_id result = exec_test_command( - BASE_CMD + BASE_CMDS["volumes"] + [ "update", volume_id, @@ -227,21 +199,21 @@ def test_add_new_tag_to_volume(test_volume_id): "--text", "--no-headers", ] - ).stdout.decode() + ) assert unique_tag in result -def test_view_tags_attached_to_volume(test_volume_id): - volume_id = test_volume_id +def test_view_tags_attached_to_volume(volume_instance_id): + volume_id = volume_instance_id exec_test_command( - BASE_CMD + BASE_CMDS["volumes"] + ["view", volume_id, "--format", "tags", "--text", "--no-headers"] - ).stdout.decode() + ) -def test_fail_to_update_volume_size(test_volume_id): - volume_id = test_volume_id +def test_fail_to_update_volume_size(volume_instance_id): + volume_id = volume_instance_id os.system( "linode-cli volumes update --size=15 " + volume_id diff --git a/tests/integration/volumes/test_volumes_resize.py b/tests/integration/volumes/test_volumes_resize.py index b3f8f2f38..d6925c485 100644 --- a/tests/integration/volumes/test_volumes_resize.py +++ b/tests/integration/volumes/test_volumes_resize.py @@ -1,67 +1,32 @@ import os import time -import pytest - from linodecli.exit_codes import ExitCodes from tests.integration.helpers import ( - delete_target_id, + BASE_CMDS, exec_failing_test_command, exec_test_command, ) +from tests.integration.volumes.fixtures import volume_instance_id # noqa: F401 -BASE_CMD = ["linode-cli", "volumes"] -timestamp = str(time.time_ns()) -VOLUME_CREATION_WAIT = 5 - - -@pytest.fixture(scope="package") -def test_volume_id(): - volume_id = ( - exec_test_command( - BASE_CMD - + [ - "create", - "--label", - "A" + timestamp, - "--region", - "us-ord", - "--size", - "10", - "--text", - "--no-headers", - "--delimiter", - ",", - "--format", - "id", - ] - ) - .stdout.decode() - .rstrip() - ) - - yield volume_id - delete_target_id(target="volumes", id=volume_id) - - -def test_resize_fails_to_smaller_volume(test_volume_id): - volume_id = test_volume_id - time.sleep(VOLUME_CREATION_WAIT) +def test_resize_fails_to_smaller_volume(volume_instance_id): + volume_id = volume_instance_id + time.sleep(5) result = exec_failing_test_command( - BASE_CMD + BASE_CMDS["volumes"] + ["resize", volume_id, "--size", "5", "--text", "--no-headers"], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) assert "Request failed: 400" in result assert "Storage volumes can only be resized up" in result -def test_resize_fails_to_volume_larger_than_1024gb(test_volume_id): - volume_id = test_volume_id +def test_resize_fails_to_volume_larger_than_1024gb(volume_instance_id): + volume_id = volume_instance_id result = exec_failing_test_command( - BASE_CMD + BASE_CMDS["volumes"] + [ "resize", volume_id, @@ -71,7 +36,7 @@ def test_resize_fails_to_volume_larger_than_1024gb(test_volume_id): "--no-headers", ], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) if "test" == os.environ.get( "TEST_ENVIRONMENT", None @@ -87,17 +52,17 @@ def test_resize_fails_to_volume_larger_than_1024gb(test_volume_id): ) -def test_resize_volume(test_volume_id): - volume_id = test_volume_id +def test_resize_volume(volume_instance_id): + volume_id = volume_instance_id exec_test_command( - BASE_CMD + BASE_CMDS["volumes"] + ["resize", volume_id, "--size", "11", "--text", "--no-headers"] ) result = exec_test_command( - BASE_CMD + BASE_CMDS["volumes"] + ["view", volume_id, "--format", "size", "--text", "--no-headers"] - ).stdout.decode() + ) assert "11" in result diff --git a/tests/integration/vpc/conftest.py b/tests/integration/vpc/conftest.py index 4f7165aca..9f231377c 100644 --- a/tests/integration/vpc/conftest.py +++ b/tests/integration/vpc/conftest.py @@ -1,12 +1,12 @@ -import time - import pytest from tests.integration.conftest import create_vpc_w_subnet from tests.integration.helpers import ( + BASE_CMDS, delete_target_id, exec_test_command, get_random_region_with_caps, + get_random_text, ) @@ -24,25 +24,20 @@ def test_vpc_w_subnet(): def test_vpc_wo_subnet(): region = get_random_region_with_caps(required_capabilities=["VPCs"]) - label = str(time.time_ns()) + "label" + label = get_random_text(5) + "-label" - vpc_id = ( - exec_test_command( - [ - "linode-cli", - "vpcs", - "create", - "--label", - label, - "--region", - region, - "--no-headers", - "--text", - "--format=id", - ] - ) - .stdout.decode() - .rstrip() + vpc_id = exec_test_command( + BASE_CMDS["vpcs"] + + [ + "create", + "--label", + label, + "--region", + region, + "--no-headers", + "--text", + "--format=id", + ] ) yield vpc_id @@ -53,25 +48,20 @@ def test_vpc_wo_subnet(): @pytest.fixture def test_subnet(test_vpc_wo_subnet): vpc_id = test_vpc_wo_subnet - subnet_label = str(time.time_ns()) + "label" - res = ( - exec_test_command( - [ - "linode-cli", - "vpcs", - "subnet-create", - "--label", - subnet_label, - "--ipv4", - "10.0.0.0/24", - vpc_id, - "--text", - "--no-headers", - "--delimiter=,", - ] - ) - .stdout.decode() - .rstrip() + subnet_label = get_random_text(5) + "-label" + res = exec_test_command( + BASE_CMDS["vpcs"] + + [ + "subnet-create", + "--label", + subnet_label, + "--ipv4", + "10.0.0.0/24", + vpc_id, + "--text", + "--no-headers", + "--delimiter=,", + ] ) yield res, subnet_label diff --git a/tests/integration/vpc/test_vpc.py b/tests/integration/vpc/test_vpc.py index cee877212..a5f9d1192 100644 --- a/tests/integration/vpc/test_vpc.py +++ b/tests/integration/vpc/test_vpc.py @@ -1,13 +1,14 @@ import re -import time import pytest from linodecli.exit_codes import ExitCodes from tests.integration.helpers import ( + BASE_CMDS, exec_failing_test_command, exec_test_command, get_random_region_with_caps, + get_random_text, ) BASE_CMD = ["linode-cli", "vpcs"] @@ -15,9 +16,7 @@ def test_list_vpcs(test_vpc_wo_subnet): vpc_id = test_vpc_wo_subnet - res = ( - exec_test_command(BASE_CMD + ["ls", "--text"]).stdout.decode().rstrip() - ) + res = exec_test_command(BASE_CMDS["vpcs"] + ["ls", "--text"]) headers = ["id", "label", "description", "region"] for header in headers: @@ -28,10 +27,8 @@ def test_list_vpcs(test_vpc_wo_subnet): def test_view_vpc(test_vpc_wo_subnet): vpc_id = test_vpc_wo_subnet - res = ( - exec_test_command(BASE_CMD + ["view", vpc_id, "--text", "--no-headers"]) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMDS["vpcs"] + ["view", vpc_id, "--text", "--no-headers"] ) assert vpc_id in res @@ -41,34 +38,26 @@ def test_view_vpc(test_vpc_wo_subnet): def test_update_vpc(test_vpc_wo_subnet): vpc_id = test_vpc_wo_subnet - new_label = str(time.time_ns()) + "label" - - updated_label = ( - exec_test_command( - BASE_CMD - + [ - "update", - vpc_id, - "--label", - new_label, - "--description", - "new description", - "--text", - "--no-headers", - "--format=label", - ] - ) - .stdout.decode() - .rstrip() + new_label = get_random_text(5) + "label" + + updated_label = exec_test_command( + BASE_CMDS["vpcs"] + + [ + "update", + vpc_id, + "--label", + new_label, + "--description", + "new description", + "--text", + "--no-headers", + "--format=label", + ] ) - description = ( - exec_test_command( - BASE_CMD - + ["view", vpc_id, "--text", "--no-headers", "--format=description"] - ) - .stdout.decode() - .rstrip() + description = exec_test_command( + BASE_CMD + + ["view", vpc_id, "--text", "--no-headers", "--format=description"] ) assert new_label == updated_label @@ -78,12 +67,8 @@ def test_update_vpc(test_vpc_wo_subnet): def test_list_subnets(test_vpc_w_subnet): vpc_id = test_vpc_w_subnet - res = ( - exec_test_command( - BASE_CMD + ["subnets-list", vpc_id, "--text", "--delimiter=,"] - ) - .stdout.decode() - .rstrip() + res = exec_test_command( + BASE_CMD + ["subnets-list", vpc_id, "--text", "--delimiter=,"] ) lines = res.splitlines() @@ -109,12 +94,8 @@ def test_view_subnet(test_vpc_wo_subnet, test_subnet): vpc_id = test_vpc_wo_subnet - output = ( - exec_test_command( - BASE_CMD + ["subnet-view", vpc_id, vpc_subnet_id, "--text"] - ) - .stdout.decode() - .rstrip() + output = exec_test_command( + BASE_CMDS["vpcs"] + ["subnet-view", vpc_id, vpc_subnet_id, "--text"] ) headers = ["id", "label", "ipv4"] @@ -127,33 +108,25 @@ def test_view_subnet(test_vpc_wo_subnet, test_subnet): def test_update_subnet(test_vpc_w_subnet): vpc_id = test_vpc_w_subnet - new_label = str(time.time_ns()) + "label" + new_label = get_random_text(5) + "label" - subnet_id = ( - exec_test_command( - BASE_CMD - + ["subnets-list", vpc_id, "--text", "--format=id", "--no-headers"] - ) - .stdout.decode() - .rstrip() + subnet_id = exec_test_command( + BASE_CMDS["vpcs"] + + ["subnets-list", vpc_id, "--text", "--format=id", "--no-headers"] ) - updated_label = ( - exec_test_command( - BASE_CMD - + [ - "subnet-update", - vpc_id, - subnet_id, - "--label", - new_label, - "--text", - "--format=label", - "--no-headers", - ] - ) - .stdout.decode() - .rstrip() + updated_label = exec_test_command( + BASE_CMD + + [ + "subnet-update", + vpc_id, + subnet_id, + "--label", + new_label, + "--text", + "--format=label", + "--no-headers", + ] ) assert new_label == updated_label @@ -163,13 +136,9 @@ def test_fails_to_create_vpc_invalid_label(): invalid_label = "invalid_label" region = get_random_region_with_caps(required_capabilities=["VPCs"]) - res = ( - exec_failing_test_command( - BASE_CMD + ["create", "--label", invalid_label, "--region", region], - ExitCodes.REQUEST_FAILED, - ) - .stderr.decode() - .rstrip() + res = exec_failing_test_command( + BASE_CMD + ["create", "--label", invalid_label, "--region", region], + ExitCodes.REQUEST_FAILED, ) assert "Request failed: 400" in res @@ -178,23 +147,14 @@ def test_fails_to_create_vpc_invalid_label(): def test_fails_to_create_vpc_duplicate_label(test_vpc_wo_subnet): vpc_id = test_vpc_wo_subnet - label = ( - exec_test_command( - BASE_CMD - + ["view", vpc_id, "--text", "--no-headers", "--format=label"] - ) - .stdout.decode() - .rstrip() + label = exec_test_command( + BASE_CMD + ["view", vpc_id, "--text", "--no-headers", "--format=label"] ) region = get_random_region_with_caps(required_capabilities=["VPCs"]) - res = ( - exec_failing_test_command( - BASE_CMD + ["create", "--label", label, "--region", region], - ExitCodes.REQUEST_FAILED, - ) - .stderr.decode() - .rstrip() + res = exec_failing_test_command( + BASE_CMD + ["create", "--label", label, "--region", region], + ExitCodes.REQUEST_FAILED, ) assert "Label must be unique among your VPCs" in res @@ -204,13 +164,9 @@ def test_fails_to_update_vpc_invalid_label(test_vpc_wo_subnet): vpc_id = test_vpc_wo_subnet invalid_label = "invalid_label" - res = ( - exec_failing_test_command( - BASE_CMD + ["update", vpc_id, "--label", invalid_label], - ExitCodes.REQUEST_FAILED, - ) - .stderr.decode() - .rstrip() + res = exec_failing_test_command( + BASE_CMD + ["update", vpc_id, "--label", invalid_label], + ExitCodes.REQUEST_FAILED, ) assert "Request failed: 400" in res @@ -232,7 +188,7 @@ def test_fails_to_create_vpc_subnet_w_invalid_label(test_vpc_wo_subnet): vpc_id, ], ExitCodes.REQUEST_FAILED, - ).stderr.decode() + ) assert "Request failed: 400" in res assert "Label must include only ASCII" in res @@ -243,32 +199,24 @@ def test_fails_to_update_vpc_subenet_w_invalid_label(test_vpc_w_subnet): invalid_label = "invalid_label" - subnet_id = ( - exec_test_command( - BASE_CMD - + ["subnets-list", vpc_id, "--text", "--format=id", "--no-headers"] - ) - .stdout.decode() - .rstrip() + subnet_id = exec_test_command( + BASE_CMD + + ["subnets-list", vpc_id, "--text", "--format=id", "--no-headers"] ) - res = ( - exec_failing_test_command( - BASE_CMD - + [ - "subnet-update", - vpc_id, - subnet_id, - "--label", - invalid_label, - "--text", - "--format=label", - "--no-headers", - ], - ExitCodes.REQUEST_FAILED, - ) - .stderr.decode() - .rstrip() + res = exec_failing_test_command( + BASE_CMD + + [ + "subnet-update", + vpc_id, + subnet_id, + "--label", + invalid_label, + "--text", + "--format=label", + "--no-headers", + ], + ExitCodes.REQUEST_FAILED, ) assert "Request failed: 400" in res