Skip to content

refactor(infra): DockerEngineService is the Docker engine seam, with a shared in-memory engine fake and its contract suite #1316

Description

@dan-petty

Problem

  • The engine service is the seam, but tests reach past it. DockerEngineService [code: src/devops_cli/docker/engine.py:197] is a process singleton (get_instance :214, reset_instance :221, get_engine :539). Its client() (:248) validates DOCKER_HOST (:228-246) and builds the SDK client with docker.from_env (:257). The docker_engine fixture binds a client by setting the private _client and patching get_instance [code: tests/conftest.py:738-739]; its 7 users pass the clients they build (with docker_engine(mock_client):, tests/test_docker.py:44).
  • One bypass skips the egress check. The dashboard builds its own client with docker.from_env() and no timeout [code: src/devops_cli/ui/data_providers.py:217-221], so a network DOCKER_HOST is used without the validation the engine applies.
  • No shared fake exists. tests/test_docker_engine.py alone builds 31 MagicMock objects [measured: grep at 5c45b1e].
  • docker and podman CLI calls are processes, faked by the process fake of refactor(cli): one process runner owns execution, process groups and dry-run #775, not by this item.

Acceptance criteria

Seam

  • DockerEngineService is the seam that supplies the docker SDK client, after validating DOCKER_HOST as today. A Protocol in docker/ names the SDK calls devops-cli makes: ping and daemon info; container list, get, create, start, stop, remove, exec, logs, log stream, stats and wait; image list; network get, create and list; volume list; disk usage; build-cache prune.
  • A public hook installs a client for a test. The docker_engine fixture uses it instead of setting _client and patching get_instance; its users keep passing their own clients until their package items switch them to the fake.
  • The bypass at ui/data_providers.py:217 is left to test(telemetry): telemetry, server, ui, output and docs tests run on edge fakes, and the dashboard suite splits by behaviour #1334.

Fake and fixture

  • tests/support/fakes/docker_engine.py is an in-memory engine: daemon info; containers with states, exit codes, logs and stats; images; networks; volumes; build cache. It raises docker.errors.NotFound and docker.errors.APIError where the daemon would, can answer as an unreachable daemon, and records each call.
  • A fixture installs it through the hook, resets the service at teardown and returns it, registered once from the root conftest.

Contract suite

  • One suite in tests/docker/ runs each Protocol call against the fake and against the real docker SDK client, the real side reading recorded Engine API responses from a server the test owns on a unix socket under tmp_path. Cases: the result of each call, a missing container (NotFound on both sides) and an unreachable daemon (the same error from the service on both sides).
  • (person-run) The recorded responses are captured once from a local Docker-compatible daemon, scrubbed to example.com hosts, and committed under tests/fixtures/docker_engine/ with the commands that captured them.

Migration

Key questions

  • Fake the SDK client or the service? (proposed) The SDK client. The service's projections into typed models (project_container_stats :98, project_container_state :135, build_cache_report :176) then run for real in every test, and the dashboard needs daemon info, volumes and networks, which the service does not wrap.
  • What about the docker and podman binaries? Decided 2026-10-07: they are process edges, faked by the process fake (refactor(cli): one process runner owns execution, process groups and dry-run #775) (Q23).
  • A live daemon for the real side? Decided 2026-10-07: never; recorded responses (Q26).
  • Fixture name? (proposed) docker_daemon. The docker_engine factory goes once its last user has moved to the fake.

Strategic alignment

Proposed design

Problem statement

The Docker engine service is the seam between devops-cli and the Docker SDK. Tests reach past it by setting a private attribute and patching a singleton method. No shared in-memory fake exists for the SDK client. One dashboard code path bypasses the service and skips the DOCKER_HOST validation. This makes tests brittle and duplicates mock setup across 31 MagicMock objects.

Acceptance criteria

  • Criterion 1: A Protocol in src/devops_cli/docker/ names every SDK call devops-cli makes: ping, daemon info, container list, get, create, start, stop, remove, exec, logs, log stream, stats, wait, image list, network get, create, list, volume list, disk usage, and build-cache prune.
    Verification: The Protocol file exists under src/devops_cli/docker/ and lists each named method. A static check or type checker confirms the service satisfies the Protocol.
  • Criterion 2: A public hook on DockerEngineService installs a client for a test. The docker_engine fixture in tests/conftest.py uses the hook instead of setting _client and patching get_instance.
    Verification: grep for '_client' and 'get_instance' in tests/conftest.py lines 738-739 returns no hits. The fixture calls the public hook method.
  • Criterion 3: The bypass at src/devops_cli/ui/data_providers.py:217 is unchanged and left to test(telemetry): telemetry, server, ui, output and docs tests run on edge fakes, and the dashboard suite splits by behaviour #1334.
    Verification: src/devops_cli/ui/data_providers.py line 217 still calls docker.from_env(). The diff for this PR does not touch that file.
  • Criterion 4: tests/support/fakes/docker_engine.py is an in-memory engine with daemon info, containers with states, exit codes, logs and stats, images, networks, volumes, and build cache. It raises docker.errors.NotFound and docker.errors.APIError where the daemon would, can answer as an unreachable daemon, and records each call.
    Verification: The file exists. A test imports the fake, calls each Protocol method, and asserts the recorded call log and the raised exception types.
  • Criterion 5: A fixture named docker_daemon installs the fake through the hook, resets the service at teardown, and returns the fake. It is registered once from the root conftest.
    Verification: uv run pytest --fixtures shows docker_daemon registered from tests/conftest.py. A test that uses it gets a fresh fake on each call.
  • Criterion 6: One contract suite in tests/docker/ runs each Protocol call against the fake and against the real Docker SDK client. The real side reads recorded Engine API responses from a server on a unix socket under tmp_path. Cases cover the result of each call, a missing container (NotFound on both sides), and an unreachable daemon.
    Verification: uv run pytest tests/docker/ passes. The suite parameterizes over fake and real client. The real side binds a unix socket under tmp_path and serves recorded responses.
  • Criterion 7: Recorded responses are captured once from a local Docker-compatible daemon, scrubbed to example.com hosts, and committed under tests/fixtures/docker_engine/ with the commands that captured them.
    Verification: Files exist under tests/fixtures/docker_engine/. A README or header in each file states the capture command. No host other than example.com appears in the fixtures.
  • Criterion 8: tests/test_docker_engine.py uses the fake or the contract suite, builds no MagicMock client, and reaches no private name of the service.
    Verification: grep -c 'MagicMock' tests/test_docker_engine.py returns 0. grep -c '_client|get_instance' tests/test_docker_engine.py returns 0.
  • Criterion 9: Every unreachable marker that names the Docker daemon, in a test whose code reaches it through the service, is replaced by the fixture in the fake's unreachable state. A marker on the dashboard bypass goes with test(telemetry): telemetry, server, ui, output and docs tests run on edge fakes, and the dashboard suite splits by behaviour #1334.
    Verification: grep -rn 'unreachable' tests/ shows no marker naming the Docker daemon in tests that reach the daemon through DockerEngineService.
  • Criterion 10: All new tests run offline and each runs under 1 second.
    Verification: uv run pytest tests/docker/ --timeout=1 -p no:cacheprovider passes with no network access (run in a network-isolated sandbox).
  • Criterion 11: If test(cli): ratchets hold internal patches, private imports and test accommodations in src at today's counts and name each site that would raise one #1309 has landed, its ceilings drop by the sites this PR removes, including the docker_engine fixture's patch of get_instance.
    Verification: The ceiling configuration file reflects the reduced count. The removed patch site is no longer listed.
  • Criterion 12: uv run devops ci passes.
    Verification: uv run devops ci exits with code 0.

Key questions

  • Fake the SDK client or the service?: The SDK client. The service's projections into typed models then run for real in every test, and the dashboard needs daemon info, volumes, and networks, which the service does not wrap. (proposed)
  • What about the docker and podman binaries?: Decided 2026-10-07: they are process edges, faked by the process fake (refactor(cli): one process runner owns execution, process groups and dry-run #775). (proposed)
  • A live daemon for the real side?: Decided 2026-10-07: never. Use recorded responses. (proposed)
  • Fixture name?: docker_daemon. The docker_engine factory goes once its last user has moved to the fake. (proposed)

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

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

    @dan-petty
    OwnerAuthor

    Linked: #1116 and #1108. #1116 adds prune calls this Protocol lacks; #1108 (v0.2.31) deletes the dashboard bypass (ui/data_providers.py:217-221) this item leaves to #1334. Found by the 2026-10-08 backlog review.

  3. 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 testability and removes a validation bypass but does not fix a user-facing defect or add a feature, so it is important but not urgent.
    • Value: Medium. It makes the Docker engine testable without a live daemon and closes a validation gap, benefiting all future Docker-related work, but it does not change user-visible behaviour.
    • Effort: High. It requires defining a Protocol, a public install hook, a full in-memory fake covering containers/images/networks/volumes/build-cache, a contract suite against a real SDK client, and migrating 31 existing MagicMocks.
  4. dan-petty commented on Oct 26, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones on the board.

    • Priority: P2-Medium. This change improves testability and maintainability but does not address an urgent issue or bug.
    • Value: High. Improving testability and reducing coupling makes the codebase more robust and easier to work with.
    • Effort: High. Implementing a new fake engine, contract suite, and migrating existing tests requires significant effort.
    • Related: refactor(k8s): KubernetesService is the Kubernetes API seam, with a shared fake cluster and its contract suite #1315, which asks for a different change. Both items are about creating seams with fakes and contract suites; however, the candidate focuses on Docker while #1315 addresses Kubernetes services.
  5. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Status set to Ready at 69636b6 (see proposed design in issue body).

  6. added
    status/ready
    effort/high
    value/high
    on Oct 28, 2026
  7. added a commit that references this issue on Oct 28, 2026
  8. removed
    status/ready
    on Oct 30, 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/highpriority/p2-mediumscope/infratype/refactorvalue/high

    Type

    No type

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions