Skip to content

test(k8s): k8s/ is tested through its public interface over the Kubernetes API, process, HTTP and keyring fakes, with no internal patches or private imports #1322

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. A patch site is a patch, patch.object, patch.dict, mocker.patch or monkeypatch.setattr/setitem/delattr call aimed at devops_cli. A private import is a _-prefixed name imported from devops_cli.

  • Files. 18 test files have k8s/ as their primary package: 6,752 lines and 352 tests [measured: AST pass at 5c45b1e].
  • Coupling. The 18 files hold 134 internal patch sites and 24 private imports, in 10 files [measured].
    • The most-patched internals: service_proxy._kube_configuration 11, credentials._keyring_set 7, commands.devcontainer.run_subprocess 6, context.configured_context 6, and load_settings 9.
    • 76 more sites patch libraries directly, among them 21 httpx2, 18 kubernetes.* and 13 sys.modules swaps that inject a fake kubernetes, because every kubernetes import is function-local.
    • 9 of the files build a module-level runner = CliRunner().
  • Kubernetes API.
    • KubernetesService is a process singleton [code: k8s/service.py:55, get_instance :76].
    • Two modules load their own client instead [code: k8s/service_proxy.py:178-181; k8s/doctor.py:634].
    • Two read its private _core_v1 [code: k8s/informer.py:100; k8s/service_proxy.py:328].
    • FakeCoreV1 (tests/k8s_fakes.py) is installed by patching the private ui.data_providers._get_k8s_client [code: tests/test_k8s_models.py:383].
  • Other edges outside a seam.
    • HTTP:
      • httpx2 clients are built inline [code: k8s/credentials.py:222, :315, :403, :434], and logql.py calls httpx2.get [code: k8s/logql.py:365].
    • Processes:
      • kubectl runs through two module-local helpers, kubectl [code: k8s/node_port.py:39] and _kubectl [code: k8s/secret_push.py:262];
      • gh is called without run_gh [code: k8s/secret_push.py:323];
      • shutil.which is called directly [code: k8s/logs.py:50];
      • the port-forward daemon has its own process-group kill [code: k8s/port_forward_daemon.py:79-102], one of the four copies refactor(cli): one process runner owns execution, process groups and dry-run #775 consolidates;
      • argocd_overrides.py takes its runner as an untyped run callable [code: k8s/argocd_overrides.py:29, :80], and its tests run real kubectl kustomize [code: tests/test_k8s_argocd_overrides.py:224].
    • Keyring:
      • credentials.py imports the private config.settings._keyring_set [code: k8s/credentials.py:17].
    • Clock:
      • the client cache expires on time.time() [code: k8s/service.py:346], and the chaos run waits with time.sleep for up to 10 s [code: k8s/chaos.py:104];
      • tests/test_k8s_service.py:460-466 sets the private _cache_ttl and sleeps for real to expire an entry.
  • Logic living in commands/.
    • _apply_configured_k8s_context decides which context kubectl follows [code: commands/devcontainer.py:595]. test_k8s_context_resolution.py imports it privately 6 times.
    • Two files import the devops k8s pods renderer commands.k8s.diagnostics._build_pods_table three times [code: tests/test_k8s_logs_diff_chaos.py:98, :125; tests/test_k8s_models.py:20].
  • Files and names.
    • test_k8s_push_secrets.py (1,203 lines, 55 tests) mixes four things: invariants over committed files (every secret under k8s/ has a table row; the README lists the table), the push, the plan and dry run, and the GitHub machine account.
    • test_k8s_argocd_overrides.py also checks the committed Argo CD Applications: it renders each one with kubectl kustomize [code: tests/test_k8s_argocd_overrides.py:250].
    • test_k8s_credentials.py names its tests ..._success/..._failure (:28-:155). There are 18 such names across these files.

Acceptance criteria

Key questions

Strategic alignment

Proposed design

Problem statement

The 18 test files for the k8s/ package test through internal implementation details. They hold 134 internal patch sites and 24 private imports across 10 files. They must test through the public interface using the shared fakes for Kubernetes API, HTTP, process, keyring and clock.

Acceptance criteria

  • Criterion 1: The test files under tests/k8s/ have zero patch sites aimed at devops_cli internals and zero private imports.
    Verification: Run an AST pass over tests/k8s/test_*.py and confirm zero patch sites targeting devops_cli and zero _-prefixed imports from devops_cli.
  • Criterion 2: No module in k8s/ loads a Kubernetes client of its own; service_proxy.py and doctor.py take theirs from refactor(k8s): KubernetesService is the Kubernetes API seam, with a shared fake cluster and its contract suite #1315's seam.
    Verification: Run rg -n 'CoreV1Api|load_kube_config|load_incluster' src/devops_cli/k8s/service_proxy.py src/devops_cli/k8s/doctor.py and confirm no direct client construction.
  • Criterion 3: informer.py and service_proxy.py stop reading the private _core_v1 attribute.
    Verification: Run rg -n '_core_v1' src/devops_cli/k8s/ and confirm no match.
  • Criterion 4: Tests use the promoted FakeCoreV1 and no test patches sys.modules, kubernetes.*, _kube_configuration or _get_k8s_client.
    Verification: Run rg -n 'sys.modules|kubernetes.*|_kube_configuration|_get_k8s_client' tests/k8s/ and confirm no match.
  • Criterion 5: credentials.py and logql.py build no httpx2 client of their own and their tests use the HTTP fake from refactor(cli): Unify HTTP and DNS seams with shared fakes #1314.
    Verification: Run rg -n 'httpx2.(get|Client|AsyncClient)' src/devops_cli/k8s/credentials.py src/devops_cli/k8s/logql.py and confirm no inline client construction.
  • Criterion 6: kubectl, gh and stern run through the process runner from refactor(cli): one process runner owns execution, process groups and dry-run #775; argocd_overrides.py's run parameter is typed by the runner's Protocol.
    Verification: Run rg -n 'subprocess.run|run_subprocess|shutil.which' src/devops_cli/k8s/ and confirm all calls go through the process runner.
  • Criterion 7: The port-forward daemon's own process-group kill is gone.
    Verification: Run rg -n 'os.killpg' src/devops_cli/k8s and confirm no output.
  • Criterion 8: credentials.py no longer imports the private _keyring_set and its tests read and write the keyring fake from refactor(security): one keyring seam owns the keyring and the Secret Service lock check, with a shared keyring fake and its contract suite #1318.
    Verification: Run rg -n '_keyring_set' src/devops_cli/k8s/credentials.py and confirm no match.
  • Criterion 9: Cache expiry and the chaos wait are tested on refactor(cli): tests control time: time-machine fakes wall-clock reads, and retry and rate-limit waits return at once and are recorded #1320's clock; no test sleeps for real or sets _cache_ttl.
    Verification: Run rg -n 'time.sleep|_cache_ttl' tests/k8s/ and confirm no match.
  • Criterion 10: _apply_configured_k8s_context is a public function in k8s/context.py and commands/devcontainer.py calls it.
    Verification: Run rg -n 'apply_configured_k8s_context' src/devops_cli/k8s/context.py src/devops_cli/commands/devcontainer.py and confirm both references.
  • Criterion 11: The three _build_pods_table tests become CLI tests of devops k8s pods under tests/commands/k8s/.
    Verification: Run rg -n '_build_pods_table' tests/k8s/ and confirm no match; confirm the tests exist under tests/commands/k8s/.
  • Criterion 12: test_k8s_push_secrets.py is split by behaviour and its manifest invariants move to tests/invariants/test_k8s_manifests.py.
    Verification: Confirm test_k8s_push_secrets.py no longer exists; confirm the new behaviour files and the invariants file exist.
  • Criterion 13: No test name ends in _success, _failure, basic or command.
    Verification: Run rg -n 'def test
    .*
    (success|failure|basic|command)\b' tests/k8s/ and confirm no match.
  • Criterion 14: Rendered manifests and Kustomize patches are compared as syrupy snapshots.
    Verification: Confirm snapshot files exist under tests/snapshots/ for the template and argocd_overrides tests.
  • Criterion 15: The 9 module-level runner = CliRunner() instances give way to 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 cli fixture.
    Verification: Run rg -n 'runner = CliRunner()' tests/k8s/ and confirm no match.
  • Criterion 16: Every line of k8s/ that no public behaviour reaches is deleted and the 90% coverage gate passes.
    Verification: Run uv run devops ci and confirm the coverage gate passes at 90%.

Key questions

Fit

Needs splitting into 8 items:

  1. Kubernetes API: route service_proxy.py and doctor.py through refactor(k8s): KubernetesService is the Kubernetes API seam, with a shared fake cluster and its contract suite #1315's seam, stop reading _core_v1, use promoted FakeCoreV1, remove sys.modules and kubernetes.* patches
  2. HTTP: route credentials.py and logql.py through refactor(cli): Unify HTTP and DNS seams with shared fakes #1314's seam, use the HTTP fake in their tests
  3. Processes: route kubectl, gh and stern through refactor(cli): one process runner owns execution, process groups and dry-run #775's process runner, type argocd_overrides.py's run parameter, confirm the daemon's process-group kill is gone
  4. Keyring: remove the _keyring_set import from credentials.py, use refactor(security): one keyring seam owns the keyring and the Secret Service lock check, with a shared keyring fake and its contract suite #1318's keyring fake in tests
  5. Clock: test cache expiry and the chaos wait on refactor(cli): tests control time: time-machine fakes wall-clock reads, and retry and rate-limit waits return at once and are recorded #1320's clock, remove real sleeps and _cache_ttl patches
  6. Logic move: make _apply_configured_k8s_context public in k8s/context.py, move _build_pods_table tests to CLI tests under tests/commands/k8s/
  7. Behaviour splits: split test_k8s_push_secrets.py by behaviour, move manifest invariants to tests/invariants/test_k8s_manifests.py, rename _success/_failure tests
  8. Cleanup: add syrupy snapshots, compare whole values, replace CliRunner with the cli fixture, delete dead code, pass the 90% coverage gate

Suspected block

None.

Dependencies

#1313, #1314, #1315, #1318

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

Activity

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

    @dan-petty-bot
    Collaborator

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Priority: P2-Medium. It improves long-term test maintainability and reduces brittleness but does not fix a user-facing defect.
    • Value: Medium. Removing 134 internal patch sites and 24 private imports makes the k8s/ suite far easier to maintain and less fragile to refactors.
    • Effort: High. 18 test files, 6 752 lines, 352 tests, 134 patch sites and 24 private imports must be reworked across HTTP, process, keyring and clock seams.
  3. 3 remaining items

  4. added
    status/new
    effort/medium
    value/high
    on Oct 28, 2026
  5. 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.

  6. added
    status/in-progress
    and removed
    status/new
    on Oct 31, 2026
  7. added
    status/in-review
    and removed
    status/in-progress
    on Oct 31, 2026
  8. removed
    status/in-review
    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

    effort/mediumpriority/p2-mediumscope/k8stype/testvalue/high

    Type

    No type

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions