dinemic-cli 0.0.19__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. dinemic_cli/__init__.py +1 -0
  2. dinemic_cli/__main__.py +16 -0
  3. dinemic_cli/client.py +75 -0
  4. dinemic_cli/commands/__init__.py +0 -0
  5. dinemic_cli/commands/auth.py +31 -0
  6. dinemic_cli/commands/caa.py +11 -0
  7. dinemic_cli/commands/cert.py +29 -0
  8. dinemic_cli/commands/cert_peer.py +37 -0
  9. dinemic_cli/commands/dns_entry.py +57 -0
  10. dinemic_cli/commands/dns_value.py +43 -0
  11. dinemic_cli/commands/dns_zone.py +98 -0
  12. dinemic_cli/commands/endpoint.py +39 -0
  13. dinemic_cli/commands/healthcheck.py +40 -0
  14. dinemic_cli/commands/loadbalancer.py +39 -0
  15. dinemic_cli/commands/loadbalancer_backend.py +44 -0
  16. dinemic_cli/commands/loadbalancer_host.py +37 -0
  17. dinemic_cli/commands/loadbalancer_route.py +42 -0
  18. dinemic_cli/commands/network.py +34 -0
  19. dinemic_cli/commands/node.py +29 -0
  20. dinemic_cli/commands/peer.py +33 -0
  21. dinemic_cli/commands/peer_config.py +33 -0
  22. dinemic_cli/commands/project.py +58 -0
  23. dinemic_cli/commands/quota.py +22 -0
  24. dinemic_cli/main.py +68 -0
  25. dinemic_cli/output.py +25 -0
  26. dinemic_cli-0.0.19.dist-info/METADATA +13 -0
  27. dinemic_cli-0.0.19.dist-info/RECORD +63 -0
  28. dinemic_cli-0.0.19.dist-info/WHEEL +5 -0
  29. dinemic_cli-0.0.19.dist-info/entry_points.txt +2 -0
  30. dinemic_cli-0.0.19.dist-info/licenses/LICENSE +696 -0
  31. dinemic_cli-0.0.19.dist-info/top_level.txt +2 -0
  32. tests/01-cli-fixes.patch +418 -0
  33. tests/02-tests-after-fixes.patch +208 -0
  34. tests/api_samples.py +214 -0
  35. tests/cert_peer.py +37 -0
  36. tests/cli_runner.py +113 -0
  37. tests/command_matrix.py +612 -0
  38. tests/conftest.py +235 -0
  39. tests/dns_entry.py +41 -0
  40. tests/dns_value.py +43 -0
  41. tests/dns_zone.py +38 -0
  42. tests/endpoint.py +39 -0
  43. tests/healthcheck.py +40 -0
  44. tests/loadbalancer.py +36 -0
  45. tests/loadbalancer_backend.py +44 -0
  46. tests/loadbalancer_host.py +37 -0
  47. tests/loadbalancer_route.py +42 -0
  48. tests/network.py +34 -0
  49. tests/network_node.py +74 -0
  50. tests/node.py +23 -0
  51. tests/peer.py +33 -0
  52. tests/project.py +58 -0
  53. tests/support.py +246 -0
  54. tests/test_auth.py +114 -0
  55. tests/test_columns.py +58 -0
  56. tests/test_commands.py +98 -0
  57. tests/test_confirmations.py +71 -0
  58. tests/test_envvars.py +119 -0
  59. tests/test_error_handling.py +83 -0
  60. tests/test_lifecycle.py +188 -0
  61. tests/test_network_node.py +116 -0
  62. tests/test_option_contract.py +335 -0
  63. tests/test_smoke.py +83 -0
tests/test_commands.py ADDED
@@ -0,0 +1,98 @@
1
+ """Every CLI command calls exactly the endpoint it is supposed to.
2
+
3
+ This is the core of the suite. If a backend refactor changes a path or a field
4
+ name in a payload, this file is the first thing to light up.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import pytest
10
+ from command_matrix import CASES, CREATE_CASES, LIST_CASES
11
+ from support import assert_ok
12
+
13
+
14
+ def _param(case):
15
+ marks = [pytest.mark.xfail(reason=case.xfail, strict=True)] if case.xfail else []
16
+ return pytest.param(case, id=case.id, marks=marks)
17
+
18
+
19
+ ALL_PARAMS = [_param(c) for c in CASES]
20
+ LIST_PARAMS = [_param(c) for c in LIST_CASES]
21
+ CREATE_PARAMS = [_param(c) for c in CREATE_CASES]
22
+ #: unmarked - ``--help`` works even for commands whose logic is broken
23
+ HELP_PARAMS = [pytest.param(c, id=c.id) for c in CASES]
24
+
25
+
26
+ @pytest.mark.parametrize("case", ALL_PARAMS)
27
+ def test_command_calls_expected_endpoint(case, invoke, client):
28
+ """The command succeeds and makes exactly one API call."""
29
+ result, _ = invoke(case.module, list(case.args))
30
+ assert_ok(result)
31
+
32
+ assert len(client.calls) == 1, f"Expected a single API call, got: {client.calls}"
33
+ call = client.last
34
+ assert call.method == case.method
35
+ assert call.path == case.path
36
+
37
+
38
+ @pytest.mark.parametrize("case", ALL_PARAMS)
39
+ def test_command_sends_expected_payload(case, invoke, client):
40
+ """The POST payload matches the matrix exactly.
41
+
42
+ The comparison is exact rather than "contains", because an extra field is a
43
+ bug too — DRF will either reject an unknown field or silently ignore it.
44
+ """
45
+ result, _ = invoke(case.module, list(case.args))
46
+ assert_ok(result)
47
+ call = client.last
48
+ assert call.payload == case.payload
49
+ assert call.params == case.params
50
+
51
+
52
+ @pytest.mark.parametrize("case", LIST_PARAMS)
53
+ def test_list_renders_declared_columns(case, invoke):
54
+ result, render = invoke(case.module, list(case.args))
55
+ assert_ok(result)
56
+ assert render.columns == case.columns
57
+
58
+
59
+ @pytest.mark.parametrize("case", LIST_PARAMS)
60
+ def test_list_passes_output_format_from_context(case, invoke):
61
+ """``--output json`` from the callback has to reach ``render``.
62
+
63
+ Without it the CLI is unusable for automation (section 7.3 of the doc).
64
+ """
65
+ result, render = invoke(case.module, list(case.args), output="json")
66
+ assert_ok(result)
67
+ assert render.last.output == "json"
68
+
69
+
70
+ @pytest.mark.parametrize("case", LIST_PARAMS)
71
+ def test_list_renders_all_rows_from_api(case, invoke, client):
72
+ """The command neither drops nor filters rows returned by the API."""
73
+ rows = [{"id": f"row-{i}"} for i in range(3)]
74
+ client.set_response("GET", case.path, rows)
75
+
76
+ result, render = invoke(case.module, list(case.args))
77
+ assert_ok(result)
78
+ assert render.last.data == rows
79
+
80
+
81
+ @pytest.mark.parametrize("case", CREATE_PARAMS)
82
+ def test_create_reports_created_object_id(case, invoke, client):
83
+ """After creating an object the CLI must show its ID.
84
+
85
+ The entire workflow runs on IDs (create a project, then pass its ID
86
+ everywhere), so a create that hides the ID is useless.
87
+ """
88
+ client.set_response("POST", case.path, {"id": "created-id-123", **(case.payload or {})})
89
+ result, _ = invoke(case.module, list(case.args))
90
+ assert_ok(result)
91
+ assert "created-id-123" in result.stdout
92
+
93
+
94
+ @pytest.mark.parametrize("case", HELP_PARAMS)
95
+ def test_command_shows_help(case, invoke):
96
+ """``--help`` works for every command (catches broken option declarations)."""
97
+ result, _ = invoke(case.module, [case.args[0], "--help"])
98
+ assert result.exit_code == 0, result.stdout
@@ -0,0 +1,71 @@
1
+ """``delete`` commands must not delete anything without confirmation.
2
+
3
+ Every ``delete`` cascades (a project takes all its resources, a zone all its
4
+ records), so a missing confirmation — or a confirmation that deletes anyway — is
5
+ the most expensive possible bug in this CLI.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import pytest
11
+ from command_matrix import DELETE_CASES
12
+ from support import all_output, assert_ok
13
+
14
+
15
+ def _without_yes(case) -> list[str]:
16
+ return [arg for arg in case.args if arg not in ("-y", "--yes")]
17
+
18
+
19
+ def _param(case):
20
+ marks = [pytest.mark.xfail(reason=case.xfail, strict=True)] if case.xfail else []
21
+ return pytest.param(case, id=case.id, marks=marks)
22
+
23
+
24
+ DELETE_PARAMS = [_param(c) for c in DELETE_CASES]
25
+ #: unmarked - a broken command also "does not delete", so this test always passes
26
+ DELETE_PARAMS_RAW = [pytest.param(c, id=c.id) for c in DELETE_CASES]
27
+
28
+
29
+ @pytest.mark.parametrize("case", DELETE_PARAMS_RAW)
30
+ def test_delete_aborts_when_user_says_no(case, invoke, client):
31
+ result, _ = invoke(case.module, _without_yes(case), input="n\n")
32
+
33
+ assert result.exit_code != 0, "Declining the prompt must give a non-zero exit code"
34
+ assert client.calls_of("DELETE") == [], "Resource deleted despite a declined prompt!"
35
+
36
+
37
+ @pytest.mark.parametrize("case", DELETE_PARAMS)
38
+ def test_delete_proceeds_when_user_confirms(case, invoke, client):
39
+ result, _ = invoke(case.module, _without_yes(case), input="y\n")
40
+
41
+ assert_ok(result)
42
+ assert [c.path for c in client.calls_of("DELETE")] == [case.path]
43
+
44
+
45
+ @pytest.mark.parametrize("case", DELETE_PARAMS)
46
+ def test_delete_with_yes_flag_does_not_prompt(case, invoke, client):
47
+ """``--yes`` has to work without stdin, otherwise CI hangs on the prompt."""
48
+ result, _ = invoke(case.module, list(case.args), input="")
49
+
50
+ assert_ok(result)
51
+ assert [c.path for c in client.calls_of("DELETE")] == [case.path]
52
+ assert "?" not in result.stdout, f"Command still asks something despite --yes: {result.stdout!r}"
53
+
54
+
55
+ @pytest.mark.parametrize("case", DELETE_PARAMS)
56
+ def test_confirmation_names_the_object(case, invoke, client):
57
+ """The prompt must contain the ID of the object being deleted.
58
+
59
+ "Remove? [y/N]" without an ID invites a blind press of y.
60
+ """
61
+ result, _ = invoke(case.module, _without_yes(case), input="n\n")
62
+
63
+ object_id = case.path.rstrip("/").rsplit("/", 1)[-1]
64
+ assert object_id in all_output(result)
65
+
66
+
67
+ @pytest.mark.parametrize("case", DELETE_PARAMS)
68
+ def test_delete_reports_success(case, invoke):
69
+ result, _ = invoke(case.module, list(case.args))
70
+ assert_ok(result)
71
+ assert result.stdout.strip(), "The delete command printed nothing"
tests/test_envvars.py ADDED
@@ -0,0 +1,119 @@
1
+ """Options backed by environment variables (DINEMIC_PROJECT, DINEMIC_ZONE, ...).
2
+
3
+ Without them, working with nested resources (zone -> entry -> value,
4
+ lb -> host -> route -> backend) means pasting four IDs into every command. These
5
+ tests make sure an env var really does supply the option, and that a command-line
6
+ option wins over the environment.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import api_samples as s
12
+ import pytest
13
+ from support import assert_ok
14
+
15
+ # (id, module, args, variable, value, expected path, expected query params)
16
+ ENV_CASES = [
17
+ ("zone-list", "dns_zone", ["list"], "DINEMIC_PROJECT", s.PROJECT_ID, "/zone/", {"project": s.PROJECT_ID}),
18
+ ("network-list", "network", ["list"], "DINEMIC_PROJECT", s.PROJECT_ID, "/network/", {"project": s.PROJECT_ID}),
19
+ ("node-list", "node", ["list"], "DINEMIC_PROJECT", s.PROJECT_ID, "/node/", {"project": s.PROJECT_ID}),
20
+ ("peer-list", "peer", ["list"], "DINEMIC_PROJECT", s.PROJECT_ID, "/peer/", {"project": s.PROJECT_ID}),
21
+ ("endpoint-list", "endpoint", ["list"], "DINEMIC_PROJECT", s.PROJECT_ID, "/endpoint/", {"project": s.PROJECT_ID}),
22
+ ("healthcheck-list", "healthcheck", ["list"], "DINEMIC_PROJECT", s.PROJECT_ID, "/healthcheck/", {"project": s.PROJECT_ID}),
23
+ ("lb-list", "loadbalancer", ["list"], "DINEMIC_PROJECT", s.PROJECT_ID, "/loadbalancer/", {"project": s.PROJECT_ID}),
24
+ ("entry-list", "dns_entry", ["list"], "DINEMIC_ZONE", s.ZONE_ID, f"/zone/{s.ZONE_ID}/entry/", {}),
25
+ (
26
+ "value-list",
27
+ "dns_value",
28
+ ["list", "-r", s.ENTRY_ID],
29
+ "DINEMIC_ZONE",
30
+ s.ZONE_ID,
31
+ f"/zone/{s.ZONE_ID}/entry/{s.ENTRY_ID}/value/",
32
+ {},
33
+ ),
34
+ ("host-list", "loadbalancer_host", ["list"], "DINEMIC_LB", s.LB_ID, f"/loadbalancer/{s.LB_ID}/host/", {}),
35
+ (
36
+ "route-list-lb",
37
+ "loadbalancer_route",
38
+ ["list", "-H", s.HOST_ID],
39
+ "DINEMIC_LB",
40
+ s.LB_ID,
41
+ f"/loadbalancer/{s.LB_ID}/host/{s.HOST_ID}/route/",
42
+ {},
43
+ ),
44
+ (
45
+ "route-list-host",
46
+ "loadbalancer_route",
47
+ ["list", "-l", s.LB_ID],
48
+ "DINEMIC_HOST",
49
+ s.HOST_ID,
50
+ f"/loadbalancer/{s.LB_ID}/host/{s.HOST_ID}/route/",
51
+ {},
52
+ ),
53
+ (
54
+ "backend-list-route",
55
+ "loadbalancer_backend",
56
+ ["list", "-l", s.LB_ID, "-H", s.HOST_ID],
57
+ "DINEMIC_ROUTE",
58
+ s.ROUTE_ID,
59
+ f"/loadbalancer/{s.LB_ID}/host/{s.HOST_ID}/route/{s.ROUTE_ID}/backend/",
60
+ {},
61
+ ),
62
+ ]
63
+
64
+
65
+ @pytest.mark.parametrize(
66
+ "module,args,var,value,path,params",
67
+ [pytest.param(*case[1:], id=case[0]) for case in ENV_CASES],
68
+ )
69
+ def test_envvar_supplies_option(module, args, var, value, path, params, invoke, client, monkeypatch):
70
+ monkeypatch.setenv(var, value)
71
+
72
+ result, _ = invoke(module, args)
73
+
74
+ assert_ok(result)
75
+ assert client.last.path == path
76
+ assert client.last.params == params
77
+
78
+
79
+ @pytest.mark.parametrize(
80
+ "module,args,var,value,path,params",
81
+ [pytest.param(*case[1:], id=case[0]) for case in ENV_CASES],
82
+ )
83
+ def test_missing_envvar_and_missing_option_is_a_usage_error(
84
+ module, args, var, value, path, params, invoke, client
85
+ ):
86
+ """A missing required option means exit code 2, not a traceback and not an API call."""
87
+ result, _ = invoke(module, args)
88
+
89
+ if result.exit_code == 0:
90
+ pytest.skip("the option is optional (a filter), not required")
91
+ assert result.exit_code == 2
92
+ assert client.calls == []
93
+
94
+
95
+ def test_cli_option_wins_over_envvar(invoke, client, monkeypatch):
96
+ monkeypatch.setenv("DINEMIC_ZONE", "zone-from-env")
97
+
98
+ result, _ = invoke("dns_entry", ["list", "-z", s.ZONE_ID])
99
+
100
+ assert_ok(result)
101
+ assert client.last.path == f"/zone/{s.ZONE_ID}/entry/"
102
+
103
+
104
+ def test_peer_create_reads_network_from_env(invoke, client, monkeypatch):
105
+ monkeypatch.setenv("DINEMIC_NETWORK", s.NETWORK_ID)
106
+
107
+ result, _ = invoke("peer", ["create", "-N", s.NODE_ID, "-c", s.LB_ID])
108
+
109
+ assert_ok(result)
110
+ assert client.last.payload["network"] == s.NETWORK_ID
111
+
112
+
113
+ def test_delete_commands_honour_the_same_envvars_as_list(invoke, client, monkeypatch):
114
+ monkeypatch.setenv("DINEMIC_ZONE", s.ZONE_ID)
115
+
116
+ result, _ = invoke("dns_value", ["delete", "-r", s.ENTRY_ID, "-v", s.VALUE_ID, "-y"])
117
+
118
+ assert_ok(result)
119
+ assert client.last.path == f"/zone/{s.ZONE_ID}/entry/{s.ENTRY_ID}/value/{s.VALUE_ID}/"
@@ -0,0 +1,83 @@
1
+ """What the CLI does when the API answers with an error.
2
+
3
+ The commands under ``commands/`` catch nothing — they assume ``client.py`` maps
4
+ DRF errors and ``main.py`` handles them globally (section 7.3 of the doc). These
5
+ tests describe the contract a user is entitled to expect. The ones marked
6
+ ``xfail`` will pass once that handler exists; remove the marker then.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import pytest
12
+ from support import ApiError, all_output
13
+
14
+
15
+ def test_unauthorized_gives_nonzero_exit(invoke, client):
16
+ client.set_error("GET", "/project/", ApiError(401, "Invalid token."))
17
+
18
+ result, _ = invoke("project", ["list"])
19
+
20
+ assert result.exit_code != 0
21
+
22
+
23
+ def test_failed_create_does_not_claim_success(invoke, client):
24
+ client.set_error("POST", "/loadbalancer/", ApiError(400, {"name": ["This field is required."]}))
25
+
26
+ result, _ = invoke("loadbalancer", ["create", "-p", "prj-1", "-n", "lb"])
27
+
28
+ assert result.exit_code != 0
29
+ assert "Created" not in result.stdout
30
+
31
+
32
+ def test_failed_delete_does_not_claim_success(invoke, client):
33
+ client.set_error("DELETE", "/network/net-1/", ApiError(404, "Not found."))
34
+
35
+ result, _ = invoke("network", ["delete", "-n", "net-1", "-y"])
36
+
37
+ assert result.exit_code != 0
38
+ assert "Removed" not in result.stdout
39
+
40
+
41
+ def test_failed_list_renders_nothing(invoke, client):
42
+ client.set_error("GET", "/endpoint/", ApiError(500, "Server error"))
43
+
44
+ result, render = invoke("endpoint", ["list"])
45
+
46
+ assert result.exit_code != 0
47
+ assert render.calls == [], "The command tried to render a table despite the API error"
48
+
49
+
50
+ @pytest.mark.xfail(
51
+ strict=False,
52
+ reason=(
53
+ "needs a global error handler (client.py maps DRF -> message, main.py "
54
+ "catches it and calls typer.Exit). Today the user gets a traceback."
55
+ ),
56
+ )
57
+ def test_api_error_is_shown_as_message_not_traceback(invoke, client):
58
+ client.set_error("GET", "/project/", ApiError(401, "Invalid token."))
59
+
60
+ result, _ = invoke("project", ["list"])
61
+
62
+ assert result.exception is None or isinstance(result.exception, SystemExit)
63
+ assert "Traceback" not in all_output(result)
64
+ assert "401" in all_output(result) or "token" in all_output(result).lower()
65
+
66
+
67
+ @pytest.mark.xfail(
68
+ strict=False,
69
+ reason="same as above — a DRF validation message should name the field",
70
+ )
71
+ def test_validation_error_names_the_field(invoke, client):
72
+ client.set_error(
73
+ "POST",
74
+ "/zone/",
75
+ ApiError(400, {"domain": ["Enter a valid domain name."]}),
76
+ )
77
+
78
+ result, _ = invoke(
79
+ "dns_zone",
80
+ ["create", "-d", "not-a-domain", "-p", "prj-1", "-c", "caa-1", "-n", "z"],
81
+ )
82
+
83
+ assert "domain" in all_output(result)
@@ -0,0 +1,188 @@
1
+ """The full flow: project -> network -> LB with routing -> DNS zone -> peer.
2
+
3
+ Mirrors what a human does: create an object, note its ID, hand that ID to the
4
+ next command. Every step checks that the object really shows up in a listing,
5
+ because "create returned 201" and "the object exists with the right fields" are
6
+ two different claims.
7
+
8
+ This test is destructive: it creates resources in a ``pytest-<hash>`` project and
9
+ cascade-deletes it at the end. Do NOT run it against production.
10
+
11
+ pytest -m destructive
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ import pytest
17
+ from cli_runner import extract_id, find_by, path_of
18
+
19
+ pytestmark = [pytest.mark.integration, pytest.mark.destructive]
20
+
21
+
22
+ def test_project_appears_on_list(cli, project):
23
+ rows = cli(*path_of("project"), "list").data
24
+ assert find_by(rows, id=project), f"The created project {project} is not in the listing"
25
+
26
+
27
+ def test_full_stack_lifecycle(cli, project, unique_suffix):
28
+ # --- network ---------------------------------------------------------
29
+ network = extract_id(
30
+ cli(*path_of("network"), "create", "-p", project, "-n", f"mesh-{unique_suffix}",
31
+ "-i", "10.99.0.0", "-m", "255.255.255.0")
32
+ )
33
+ networks = cli(*path_of("network"), "list", "-p", project).data
34
+ row = find_by(networks, id=network)
35
+ assert row, f"Network {network} is not in the project listing"
36
+ assert row.get("ip_address") == "10.99.0.0"
37
+
38
+ # --- endpoint --------------------------------------------------------
39
+ endpoint = extract_id(
40
+ cli(*path_of("endpoint"), "create", "-p", project, "-n", f"api-{unique_suffix}",
41
+ "-s", "HTTP", "-H", "127.0.0.1", "-P", "8080")
42
+ )
43
+ assert find_by(cli(*path_of("endpoint"), "list", "-p", project).data, id=endpoint)
44
+
45
+ # --- healthcheck -----------------------------------------------------
46
+ healthcheck = extract_id(
47
+ cli(*path_of("healthcheck"), "create", "-p", project, "-n", f"hc-{unique_suffix}",
48
+ "-P", "/healthz", "-d", "10", "-m", "3", "-i", "30")
49
+ )
50
+ assert find_by(cli(*path_of("healthcheck"), "list", "-p", project).data, id=healthcheck)
51
+
52
+ # --- DNS zone --------------------------------------------------------
53
+ caa_rows = cli(*path_of("caa"), "list").data
54
+ assert caa_rows, "No CAA entries — a zone cannot be created"
55
+ caa = caa_rows[0]["id"]
56
+
57
+ domain = f"t{unique_suffix}.example.com"
58
+ zone = extract_id(
59
+ cli(*path_of("dns_zone"), "create", "-d", domain, "-p", project, "-c", caa, "-n", domain)
60
+ )
61
+ assert find_by(cli(*path_of("dns_zone"), "list", "-p", project).data, id=zone)
62
+
63
+ entry = extract_id(cli(*path_of("dns_entry"), "create", "-z", zone, "-r", "www", "-t", "A"))
64
+ entries = cli(*path_of("dns_entry"), "list", "-z", zone).data
65
+ assert find_by(entries, id=entry, record="www")
66
+
67
+ value = extract_id(
68
+ cli(*path_of("dns_value"), "create", "-z", zone, "-r", entry,
69
+ "-v", "203.0.113.10", "-w", "1")
70
+ )
71
+ values = cli(*path_of("dns_value"), "list", "-z", zone, "-r", entry).data
72
+ assert find_by(values, id=value)
73
+
74
+ # --- load balancer ---------------------------------------------------
75
+ loadbalancer = extract_id(
76
+ cli(*path_of("loadbalancer"), "create", "-p", project, "-n", f"lb-{unique_suffix}")
77
+ )
78
+ assert find_by(cli(*path_of("loadbalancer"), "list", "-p", project).data, id=loadbalancer)
79
+
80
+ host = extract_id(cli(*path_of("loadbalancer_host"), "create", "-l", loadbalancer, "-d", zone))
81
+ assert find_by(cli(*path_of("loadbalancer_host"), "list", "-l", loadbalancer).data, id=host)
82
+
83
+ route = extract_id(
84
+ cli(*path_of("loadbalancer_route"), "create", "-l", loadbalancer, "-H", host,
85
+ "-p", "/", "-P", "10", "-k", healthcheck)
86
+ )
87
+ routes = cli(*path_of("loadbalancer_route"), "list", "-l", loadbalancer, "-H", host).data
88
+ assert find_by(routes, id=route)
89
+
90
+ backend = extract_id(
91
+ cli(*path_of("loadbalancer_backend"), "create", "-l", loadbalancer,
92
+ "-H", host, "-r", route, "-e", endpoint, "-w", "1")
93
+ )
94
+ backends = cli(*path_of("loadbalancer_backend"), "list",
95
+ "-l", loadbalancer, "-H", host, "-r", route).data
96
+ assert find_by(backends, id=backend, endpoint=endpoint)
97
+
98
+ # --- tear down from the bottom up ------------------------------------
99
+ cli(*path_of("loadbalancer_backend"), "delete", "-l", loadbalancer,
100
+ "-H", host, "-r", route, "-b", backend, "-y")
101
+ remaining = cli(*path_of("loadbalancer_backend"), "list",
102
+ "-l", loadbalancer, "-H", host, "-r", route).data
103
+ assert not find_by(remaining, id=backend), "The backend still exists after delete"
104
+
105
+ cli(*path_of("loadbalancer_route"), "delete", "-l", loadbalancer, "-H", host,
106
+ "-r", route, "-y")
107
+ cli(*path_of("loadbalancer_host"), "delete", "-l", loadbalancer, "-H", host, "-y")
108
+ cli(*path_of("loadbalancer"), "delete", "-l", loadbalancer, "-y")
109
+ assert not find_by(cli(*path_of("loadbalancer"), "list", "-p", project).data, id=loadbalancer)
110
+
111
+ cli(*path_of("dns_value"), "delete", "-z", zone, "-r", entry, "-v", value, "-y")
112
+ cli(*path_of("dns_entry"), "delete", "-z", zone, "-r", entry, "-y")
113
+ cli(*path_of("dns_zone"), "delete", "-z", zone, "-y")
114
+ assert not find_by(cli(*path_of("dns_zone"), "list", "-p", project).data, id=zone)
115
+
116
+
117
+ def test_peer_binds_config_to_node(cli, project, unique_suffix):
118
+ """A peer needs a registered node — without an agent this test is pointless.
119
+
120
+ With NetworkNode in place, this is where the difference shows: two peers of
121
+ the same network on one node should share a single attachment.
122
+ """
123
+ nodes = cli(*path_of("node"), "list", "-p", project).data
124
+ if not nodes:
125
+ pytest.skip("no registered nodes in the project — start an agent")
126
+ node = nodes[0]["id"]
127
+
128
+ network = extract_id(
129
+ cli(*path_of("network"), "create", "-p", project, "-n", f"peer-net-{unique_suffix}",
130
+ "-i", "10.98.0.0", "-m", "255.255.255.0")
131
+ )
132
+ lb_one = extract_id(cli(*path_of("loadbalancer"), "create", "-p", project, "-n", f"lb1-{unique_suffix}"))
133
+ lb_two = extract_id(cli(*path_of("loadbalancer"), "create", "-p", project, "-n", f"lb2-{unique_suffix}"))
134
+
135
+ peer_one = extract_id(cli(*path_of("peer"), "create", "-N", node, "-n", network, "-c", lb_one))
136
+ peer_two = extract_id(cli(*path_of("peer"), "create", "-N", node, "-n", network, "-c", lb_two))
137
+
138
+ peers = cli(*path_of("peer"), "list", "-p", project).data
139
+ row_one = find_by(peers, id=peer_one)
140
+ row_two = find_by(peers, id=peer_two)
141
+ assert row_one and row_two
142
+
143
+ # the role is derived from the config type, the client does not send it (5.8)
144
+ assert row_one.get("role") == "loadbalancer", row_one
145
+
146
+ # two peers of the same network on the same node = one WireGuard attachment
147
+ if "network_node" in row_one:
148
+ assert row_one["network_node"] == row_two["network_node"], (
149
+ "Peers of the same network on one node got different NetworkNodes — "
150
+ "that is exactly the old model that was supposed to be gone"
151
+ )
152
+
153
+ cli(*path_of("peer"), "delete", "-p", peer_one, "-y")
154
+ cli(*path_of("peer"), "delete", "-p", peer_two, "-y")
155
+ assert not find_by(cli(*path_of("peer"), "list", "-p", project).data, id=peer_one)
156
+
157
+
158
+ def test_cert_peer_lifecycle(cli, project, unique_suffix):
159
+ cert_peer = extract_id(
160
+ cli(*path_of("cert_peer"), "create", "-p", project, "-n", f"acme-{unique_suffix}",
161
+ "-d", "https://acme-staging-v02.api.letsencrypt.org/directory", "-m", "2")
162
+ )
163
+ rows = cli(*path_of("cert_peer"), "list").data
164
+ row = find_by(rows, id=cert_peer)
165
+ assert row, "The created cert-peer is not in the listing"
166
+ assert "staging" in (row.get("directory_url") or ""), (
167
+ "The CLI did not pass --directory-url — the test would hit production Let's Encrypt"
168
+ )
169
+
170
+ cli(*path_of("cert_peer"), "delete", "-c", cert_peer, "-y")
171
+ assert not find_by(cli(*path_of("cert_peer"), "list").data, id=cert_peer)
172
+
173
+
174
+ def test_deleting_project_removes_its_resources(cli, unique_suffix):
175
+ """Deleting a project removes its resources — test cleanup depends on it."""
176
+ name = f"pytest-cascade-{unique_suffix}"
177
+ project = extract_id(cli(*path_of("project"), "create", "-n", name))
178
+ network = extract_id(
179
+ cli(*path_of("network"), "create", "-p", project, "-n", "cascade",
180
+ "-i", "10.97.0.0", "-m", "255.255.255.0")
181
+ )
182
+
183
+ cli(*path_of("project"), "delete", "-p", project, "-y")
184
+
185
+ assert not find_by(cli(*path_of("project"), "list").data, id=project)
186
+ assert not find_by(cli(*path_of("network"), "list").data, id=network), (
187
+ "The network survived the project deletion — the cascade is not working"
188
+ )
@@ -0,0 +1,116 @@
1
+ """The NetworkNode model — what the CLI still cannot do.
2
+
3
+ After the model change a node attaches to a network ONCE (one WireGuard
4
+ interface per network+node pair) and the peers of that network share the tunnel.
5
+ Previously every peer stood up its own interface.
6
+
7
+ What that means for the CLI:
8
+
9
+ * there is an entity the CLI cannot see at all (``network-node``) — and it is the
10
+ one holding the WireGuard keys, the port and the attachment state,
11
+ * ``peer list`` has no way to show which attachment a peer goes out through,
12
+ * when diagnosing ("why can't this peer talk to the rest of the network?") there
13
+ is nothing to look at from the command line.
14
+
15
+ The active tests keep the CLI from referring to fields that moved onto
16
+ NetworkNode. The ``skip``-marked ones are a specification of the missing
17
+ commands — unskip them once ``commands/network_node.py`` exists.
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ import api_samples as s
23
+ import pytest
24
+ from support import COMMAND_MODULES, assert_ok, module_source
25
+
26
+ #: Fields that live on NetworkNode after the refactor, not on Peer.
27
+ NETWORK_NODE_FIELDS = ("wg_private_key", "wg_public_key", "wg_port")
28
+
29
+
30
+ @pytest.mark.parametrize("module", COMMAND_MODULES)
31
+ def test_no_command_reads_wireguard_fields_from_peer(module):
32
+ """WireGuard keys and ports belong to NetworkNode — a peer no longer has them.
33
+
34
+ If this test fires, a column or a payload survived somewhere from the
35
+ "one interface per peer" era.
36
+ """
37
+ source = module_source(module)
38
+ for field in NETWORK_NODE_FIELDS:
39
+ assert field not in source, (
40
+ f"{module}: reference to {field!r} — that field moved onto NetworkNode"
41
+ )
42
+
43
+
44
+ def test_peer_list_does_not_promise_per_peer_tunnel(invoke, client):
45
+ """``peer list`` must not suggest that a peer owns a tunnel."""
46
+ result, render = invoke("peer", ["list"])
47
+
48
+ assert_ok(result)
49
+ for column in render.columns:
50
+ assert not column.startswith("wg_"), f"column {column!r} describes a per-peer tunnel"
51
+
52
+
53
+ def test_peer_create_does_not_send_wireguard_data(invoke, client):
54
+ """Creating a peer no longer allocates anything WireGuard-related client-side."""
55
+ result, _ = invoke("peer", ["create", "-N", s.NODE_ID, "-n", s.NETWORK_ID, "-c", s.LB_ID])
56
+
57
+ assert_ok(result)
58
+ payload = client.last.payload
59
+ assert not any(key.startswith("wg_") for key in payload), payload
60
+
61
+
62
+ # ---------------------------------------------------------------------------
63
+ # Specification of the missing commands — unskip when implementing
64
+ # ---------------------------------------------------------------------------
65
+
66
+ MISSING = "no commands/network_node.py — the CLI cannot see the NetworkNode model"
67
+
68
+
69
+ @pytest.mark.skip(reason=MISSING)
70
+ def test_network_node_list(invoke, client):
71
+ """``dinemic network-node list -n <network>`` -> GET /network-node/."""
72
+ result, render = invoke("network_node", ["list", "-n", s.NETWORK_ID])
73
+
74
+ assert_ok(result)
75
+ assert client.last.path == "/network-node/"
76
+ assert client.last.params == {"network": s.NETWORK_ID}
77
+ assert render.columns == [
78
+ "id",
79
+ "network",
80
+ "node",
81
+ "ipv4_address",
82
+ "wg_public_key",
83
+ "wg_port",
84
+ "state",
85
+ ]
86
+
87
+
88
+ @pytest.mark.skip(reason=MISSING)
89
+ def test_network_node_shows_which_peers_use_it(invoke, client):
90
+ """"Who rides this tunnel" — without it there is no way to debug the network."""
91
+ result, render = invoke("network_node", ["list", "-n", s.NETWORK_ID])
92
+
93
+ assert_ok(result)
94
+ assert "peers" in render.columns
95
+
96
+
97
+ @pytest.mark.skip(reason=MISSING)
98
+ def test_peer_list_shows_network_node(invoke, client):
99
+ """``peer list`` should show which attachment the peer goes out through."""
100
+ result, render = invoke("peer", ["list"])
101
+
102
+ assert_ok(result)
103
+ assert "network_node" in render.columns
104
+
105
+
106
+ @pytest.mark.skip(reason=MISSING)
107
+ def test_deleting_last_peer_reports_network_node_cleanup(invoke, client):
108
+ """Deleting the last peer of a network on a node releases the attachment.
109
+
110
+ The user needs to know whether the WireGuard interface went away or stayed —
111
+ it is the only observable difference between the old and the new model.
112
+ """
113
+ result, _ = invoke("peer", ["delete", "-p", s.PEER_ID, "-y"])
114
+
115
+ assert_ok(result)
116
+ assert "network" in result.stdout.lower()