Skip to content

test(k8s): devops k8s commands are thin adapters tested over edge fakes, and the _K8sModule patch forwarding is gone #1323

Closed

Description

@dan-petty

Problem

Counts come from one AST pass over tests/test_*.py at 5c45b1e, the head of release/v0.2.28, and [code: …] lines are at the same commit. A file belongs to the package it references most, as in the census.

  • Files. 14 test files have commands/k8s/ as their primary package: 5,585 lines, 238 tests [measured: AST pass at 5c45b1e].
    • They are test_k8s, test_k8s_bootstrap, test_k8s_context, test_k8s_devops_stack, test_k8s_jaeger, test_k8s_logging_stack, test_k8s_networking, test_k8s_proxy_addressing, test_k8s_qdrant_security, test_k8s_rbac_audit, test_k8s_run_job, test_k8s_runtime_cmd, test_k8s_tls and test_subsystem_containment_and_redaction.
    • Three more, test_k8s_llm_gateway, test_k8s_monitoring_integration and test_k8s_network_policies, check committed manifests and go to tests/invariants/.
  • Coupling. These files hold 265 patch sites aimed at devops_cli internals [measured]. 8 of them name their target through the _RUN_CMD string constant [code: tests/test_k8s_rbac_audit.py:16].
    • 136 of the string-target patches replace the process layer: _run_cmd, _cluster_reachable, _minikube_running and run_subprocess. Tests reach them as devops_cli.commands.k8s.X or ...stack_lifecycle.runtime._run_cmd.
    • The files also make 76 private imports, in 9 files, and 8 of the 14 build a module-level runner = CliRunner().
    • Those three and test_k8s_argocd_apps add 7 private imports of commands/k8s tables and helpers [code: tests/test_k8s_llm_gateway.py:12; tests/test_k8s_monitoring_integration.py:12-13; tests/test_k8s_network_policies.py:11, :229; tests/test_k8s_argocd_apps.py:12].
  • Product code shaped for patching (Q27).
    • commands/k8s/__init__.py swaps its own module class for _K8sModule [code: commands/k8s/init.py:245, :253].
    • That class's __setattr__ forwards a patched name into four submodules. It does so through _forward_submodule_attr [code: commands/k8s/init.py:229], _PATCH_TARGET_MAP [code: commands/k8s/init.py:223] and three name sets [code: commands/k8s/init.py:194-222].
    • 22 of the 57 names in its __all__ are private [code: commands/k8s/init.py:134-192].
  • Edges outside a seam.
    • kubectl wrappers live in each module [code: commands/k8s/cluster_runtime.py:38 _run_cmd; commands/k8s/cluster_jobs.py:82 _kubectl].
    • shutil.which is called directly [code: commands/k8s/cluster_runtime.py:91].
    • The port-forward daemon starts with subprocess.Popen [code: commands/k8s/networking.py:1005].
    • Kubernetes clients are loaded beside KubernetesService [code: commands/k8s/cluster_runtime.py:29; commands/k8s/cluster_context.py:128].
    • time.sleep is called directly [code: commands/k8s/cluster_jobs.py:117, :137; commands/k8s/networking.py:1112].
    • stack_lifecycle.py writes the keyring through the private config.settings._keyring_set [code: commands/k8s/stack_lifecycle.py:456].
    • URL reachability is a raw TCP connect [code: commands/k8s/networking.py:226].
  • Logic living in commands/ (Q19).
    • The stack catalogue: _HELM_REPOS_BY_STACK, _HELM_RELEASES_BY_STACK and _MANIFESTS_BY_STACK [code: commands/k8s/stack_lifecycle.py:54, :78, :168].
    • URL resolution: _parse_minikube_service_url, _extract_first_node_ip, _resolve_k8s_node_port_url and _resolve_accessible_url [code: commands/k8s/networking.py:76, :85, :103, :339].
    • _PROXY_TARGETS_INFRA [code: commands/k8s/networking.py:491] and _get_openwebui_bootstrap_credentials [code: commands/k8s/stack_lifecycle.py:297].
    • The Argo CD host overrides that fix(k8s): argo cd takes homelab values from deploy-time overrides, and the repository holds no private or placeholder config #1290 added: _homelab_applications, _homelab_host_overrides and _set_homelab_hosts [code: commands/k8s/stack_lifecycle.py:766, :806, :855], beside the domain module k8s/argocd_overrides.py.
    • networking.py is 1,229 lines and stack_lifecycle.py is 1,231.
  • Files and names.
    • test_k8s.py (2,051 lines, 67 tests) covers deploy, bootstrap, teardown, URL configuration, contexts, the logs-and-metrics stack (:1298) and, since fix(k8s): argo cd takes homelab values from deploy-time overrides, and the repository holds no private or placeholder config #1290, deployment onto an Argo CD cluster (:1631-:2051). Several of its tests take committed manifests or values files as their subject rather than run code (for example :1126, :1320, :1380, :1499).
    • The fix(k8s): argo cd takes homelab values from deploy-time overrides, and the repository holds no private or placeholder config #1290 tests answer kubectl with _Cluster, a hand-rolled recorder that runs git and kubectl kustomize for real [code: tests/test_k8s.py:1657]. They install it by patching cluster_runtime._run_cmd [code: tests/test_k8s.py:1726, :1943] and write the service config through their own _configure_service [code: tests/test_k8s.py:1715-1721].
    • Two more process doubles are patched onto core.process.subprocess.run: FakeCluster from tests/cluster_secret_fakes.py [code: tests/test_k8s_qdrant_security.py:157-159] and FakeJobs [code: tests/test_k8s_run_job.py:34, :67-69]. Two of these files also use forbid_requests from the same helper module [code: tests/test_k8s.py:184, :215; tests/test_k8s_run_job.py:22].
    • Catch-alls: test_k8s.py:755, :848, :916 and test_k8s_networking.py:35.
    • test_subsystem_containment_and_redaction.py tests four packages through private names (:31, :35, :214, :248).

Acceptance criteria

Key questions

Strategic alignment

Update 2026-10-08: refinement

_KUSTOMIZATIONS_BY_STACK (commands/k8s/stack_lifecycle.py:201) moves too, and stack names live in three places (commands/k8s/networking.py:60, :63-67; stack_lifecycle.py:205).

  • The names are defined once in k8s/.
  • The argv builder passes the context from resolve_context (k8s/context.py:78) when one resolves; resolve_effective_context goes; a test covers cluster_context.py:203 and security_audit.py:44.

Proposed design

Problem statement

The k8s devops command modules in commands/k8s/ hold business logic (stack catalogue, URL resolution, proxy targets, host overrides) and are shaped so that tests can patch their internals. Their 14 test files (5,585 lines, 238 tests) use 242 patch sites and 94 private imports to reach devops_cli internals. The _K8sModule class in commands/k8s/__init__.py forwards patched names into submodules, making the module a test-patching target. Process calls (kubectl, helm, minikube, port-forward) bypass the #775 process runner seam. This item makes commands/k8s/ thin adapters, moves logic into k8s/ domain modules, routes all edges through their seams, and rewrites the tests to use only public interfaces and process fakes.

Acceptance criteria

  • Criterion 1: The test files under tests/commands/k8s/ have zero patch sites aimed at devops_cli internals and zero private imports.
    Verification: Run rg -n 'patch\(|mock\.|_run_cmd|_cluster_reachable|_minikube_running|run_subprocess' tests/commands/k8s/ and confirm no output. Run rg -n 'from devops_cli\..*_\w+ import|from devops_cli\.\w+\.\w+ import _\w+' tests/commands/k8s/ and confirm no output.
  • Criterion 2: _K8sModule, _forward_submodule_attr, _PATCH_TARGET_MAP and the three name sets are deleted from commands/k8s/__init__.py.
    Verification: Run rg -n '_K8sModule|_PATCH_TARGET_MAP|__class__ =|_forward_submodule_attr' src/devops_cli/commands/k8s and confirm no output.
  • Criterion 3: commands/k8s/__init__.py exports no private name in its __all__.
    Verification: Run python -c "import devops_cli.commands.k8s as m; privs=[n for n in m.__all__ if n.startswith('_')]; assert not privs, privs" and confirm it exits 0.
  • Criterion 4: kubectl, helm, minikube and the port-forward daemon all run through the refactor(cli): one process runner owns execution, process groups and dry-run #775 process runner; the per-module wrappers _run_cmd and _kubectl are deleted.
    Verification: Run rg -n 'def _run_cmd|def _kubectl' src/devops_cli/commands/k8s/ and confirm no output. Run rg -n 'subprocess\.run|subprocess\.Popen' src/devops_cli/commands/k8s/ and confirm no output outside the process runner seam.
  • Criterion 5: Kubernetes clients come only from refactor(k8s): KubernetesService is the Kubernetes API seam, with a shared fake cluster and its contract suite #1315's seam; cluster_runtime.py and cluster_context.py load none of their own.
    Verification: Run rg -n 'kubernetes\.|KubernetesService|ApiClient' src/devops_cli/commands/k8s/cluster_runtime.py src/devops_cli/commands/k8s/cluster_context.py and confirm no direct client construction.
  • Criterion 6: Keyring reads and writes go through refactor(security): one keyring seam owns the keyring and the Secret Service lock check, with a shared keyring fake and its contract suite #1318; stack_lifecycle.py no longer imports _keyring_set.
    Verification: Run rg -n '_keyring_set' src/devops_cli/commands/k8s/stack_lifecycle.py and confirm no output.
  • Criterion 7: The stack catalogue (_HELM_REPOS_BY_STACK, _HELM_RELEASES_BY_STACK, _MANIFESTS_BY_STACK, _KUSTOMIZATIONS_BY_STACK) and URL resolution functions move into a public module in k8s/.
    Verification: Run rg -n '_HELM_REPOS_BY_STACK|_HELM_RELEASES_BY_STACK|_MANIFESTS_BY_STACK|_KUSTOMIZATIONS_BY_STACK|_parse_minikube_service_url|_extract_first_node_ip|_resolve_k8s_node_port_url|_resolve_accessible_url' src/devops_cli/commands/k8s/ and confirm no output. Confirm the names exist in src/devops_cli/k8s/ under public names.
  • Criterion 8: Stack names are defined once in k8s/ and not duplicated across networking.py and stack_lifecycle.py.
    Verification: Run rg -n 'STACK_NAMES|_STACKS' src/devops_cli/commands/k8s/networking.py src/devops_cli/commands/k8s/stack_lifecycle.py and confirm the names are imported from k8s/ rather than redefined.
  • Criterion 9: The Argo CD host-override derivation (_homelab_applications, _homelab_host_overrides, _set_homelab_hosts) joins k8s/argocd_overrides.py.
    Verification: Run rg -n '_homelab_applications|_homelab_host_overrides|_set_homelab_hosts' src/devops_cli/commands/k8s/stack_lifecycle.py and confirm no output. Confirm the functions exist in src/devops_cli/k8s/argocd_overrides.py.
  • Criterion 10: The single kubectl/helm argv builder resolves the context through resolve_context and adds --context (or --kube-context for helm). resolve_effective_context is deleted.
    Verification: Run rg -n 'resolve_effective_context' src/devops_cli/ and confirm no output. Run rg -n 'resolve_context' src/devops_cli/k8s/ and confirm the argv builder calls it. Confirm tests cover cluster_context.py:203 and security_audit.py:44 context passing.
  • Criterion 11: No test patches _run_cmd, _cluster_reachable, _minikube_running, run_subprocess or subprocess.run.
    Verification: Run rg -n 'patch.*(_run_cmd|_cluster_reachable|_minikube_running|run_subprocess|subprocess\.run)' tests/ and confirm no output.
  • Criterion 12: test_k8s.py is deleted and its tests move to behaviour files such as test_stack_deploy.py, test_argocd_deploy.py, test_cluster_bootstrap.py and test_stack_teardown.py.
    Verification: Confirm tests/test_k8s.py does not exist. Confirm the behaviour files exist under tests/commands/k8s/ and their test names state behaviour.
  • Criterion 13: test_subsystem_containment_and_redaction.py is deleted and each of its behaviours is tested through the public interface of the owning package.
    Verification: Confirm tests/test_subsystem_containment_and_redaction.py does not exist. Confirm the four behaviours are covered in the respective package test files.
  • Criterion 14: No test under tests/commands/k8s/ reads a file under the repository's k8s/ directory. Those tests are in tests/invariants/test_k8s_manifests.py.
    Verification: Run rg -n 'Path\("k8s/|k8s/.*\.yaml' tests/commands/k8s/ and confirm no output. Confirm tests/invariants/test_k8s_manifests.py exists and holds the manifest tests.
  • Criterion 15: Dry-run previews and command output are compared as syrupy snapshots.
    Verification: Run rg -n 'snapshot' tests/commands/k8s/ and confirm snapshot assertions exist. Confirm syrupy is in the test dependencies.
  • Criterion 16: Every line that no public behaviour reaches is deleted. The 90% coverage gate passes.
    Verification: Run uv run devops ci and confirm the coverage gate passes at 90% or above.
  • Criterion 17: changelog.d/.md records the change.
    Verification: Confirm a file matching changelog.d/*.md describes this change. Confirm CHANGELOG.md and docs/ROADMAP.md are not edited.
  • Criterion 18: uv run devops ci passes.
    Verification: Run uv run devops ci and confirm exit code 0.
  • Criterion 19: The four manifest invariant tests (test_k8s_llm_gateway, test_k8s_monitoring_integration, test_k8s_network_policies, test_k8s_argocd_apps) import public names only.
    Verification: Run rg -n 'from devops_cli\.commands\.k8s\.\w+ import _\w+' tests/test_k8s_llm_gateway.py tests/test_k8s_monitoring_integration.py tests/test_k8s_network_policies.py tests/test_k8s_argocd_apps.py and confirm no output.
  • Criterion 20: The catch-all tests at test_k8s.py:755, :848, :916 and test_k8s_networking.py:35 are dissolved into specific behaviour tests.
    Verification: Confirm the catch-all test functions no longer exist. Confirm their assertions are covered by named behaviour tests.

Key questions

Fit

Needs splitting into 6 items:

  1. Remove _K8sModule patch forwarding and make commands/k8s/__init__.py export only public names
  2. Move stack catalogue, URL resolution, proxy targets and host overrides into k8s/ domain modules under public names
  3. Route kubectl, helm, minikube and port-forward through the refactor(cli): one process runner owns execution, process groups and dry-run #775 process runner; delete per-module wrappers
  4. Route Kubernetes clients through refactor(k8s): KubernetesService is the Kubernetes API seam, with a shared fake cluster and its contract suite #1315, keyring through refactor(security): one keyring seam owns the keyring and the Secret Service lock check, with a shared keyring fake and its contract suite #1318, waits through refactor(cli): tests control time: time-machine fakes wall-clock reads, and retry and rate-limit waits return at once and are recorded #1320, and settings through test(cli): the hermetic floor fails a test that reaches an edge without a fake and resets all process-wide state after every test #1310's config fixture
  5. Rewrite the 14 test files to use public interfaces and process fakes; dissolve test_k8s.py into behaviour files; delete test_subsystem_containment_and_redaction.py
  6. Add syrupy snapshots for dry-run previews and command output; enforce 90% coverage

Suspected block

None.

Dependencies

#1313, #1315, #1318

Alternatives check of 2026-10-29: not-applicable.
Tool: none found.

Activity

  1. added
    type/test
    scope/k8s
    scope/cli
    priority/p2-medium
    on Oct 7, 2026
  2. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Found by the 2026-10-08 client-library audit (05e9388).

    Kubernetes CLI calls that pass no --context/--kube-context run against the ambient kubectl current-context. Native client calls use settings.k8s.context through k8s/context.resolve_context, so the two can target different clusters in one command.

    Sites:

    Proposed criterion for this item: the single kubectl/helm argv builder that replaces the per-module wrappers always resolves the context through resolve_context and adds --context (--kube-context for helm). The sites above in other packages adopt it with their own items.

  3. dan-petty-bot commented on Oct 8, 2026

    @dan-petty-bot
    Collaborator

    Closed as a duplicate of #1322: Both ask to refactor k8s test files to use edge fakes over the public interface and eliminate internal patches and private imports; the candidate's 14 commands/k8s test files and _K8sModule removal are a subset of #1322's 18-file k8s scope.

    If it is not a duplicate, reopen it: intake then places it without the duplicate check.

  4. dan-petty commented on Oct 24, 2026

    @dan-petty
    OwnerAuthor

    Reopened. This issue was closed as a duplicate on 2026-10-08 with no box ticked, and no other issue owns its scope.

    Evidence from the test-suite audit of 2026-10-24 (counts at df8e2ee, lines at b170358):

    Under the owner's rule of 2026-10-24 (#2143), the tests of this issue also start no program. The git and kubectl starts in tests/test_k8s.py go with the k8s issue of the same audit and with the issue that turns the kustomize renders into a devops ci row.

  5. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    The proposal at 69636b6 needs splitting into 6 items. A person splits the item into the items below, or replies in a comment or edits the body to keep one item, and the next refine round looks at the item again.

    1. Remove _K8sModule patch forwarding and make commands/k8s/__init__.py export only public names
    2. Move stack catalogue, URL resolution, proxy targets and host overrides into k8s/ domain modules under public names
    3. Route kubectl, helm, minikube and port-forward through the refactor(cli): one process runner owns execution, process groups and dry-run #775 process runner; delete per-module wrappers
    4. Route Kubernetes clients through refactor(k8s): KubernetesService is the Kubernetes API seam, with a shared fake cluster and its contract suite #1315, keyring through refactor(security): one keyring seam owns the keyring and the Secret Service lock check, with a shared keyring fake and its contract suite #1318, waits through refactor(cli): tests control time: time-machine fakes wall-clock reads, and retry and rate-limit waits return at once and are recorded #1320, and settings through test(cli): the hermetic floor fails a test that reaches an edge without a fake and resets all process-wide state after every test #1310's config fixture
    5. Rewrite the 14 test files to use public interfaces and process fakes; dissolve test_k8s.py into behaviour files; delete test_subsystem_containment_and_redaction.py
    6. Add syrupy snapshots for dry-run previews and command output; enforce 90% coverage
  6. added
    status/new
    on Oct 28, 2026
  7. devops-cli-service commented on Oct 30, 2026

    @devops-cli-service
    Contributor

    Alternatives check: not-applicable

    Tool: none found

    Checked on 2026-10-29. A person decides what this means for the item. The check changes no label and closes no issue.

  8. added
    status/in-progress
    and removed
    status/new
    on Oct 31, 2026
  9. removed
    status/in-progress
    on Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p2-mediumscope/cliscope/k8stype/test

    Type

    No type

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions