Skip to content

docker: add unit tests, reach 100% coverage, migrate to moby/moby client - #2147

Open
Seanstoppable wants to merge 3 commits into
wtfutil:trunkfrom
Seanstoppable:seanstoppable-docker-module-unit-tests
Open

docker: add unit tests, reach 100% coverage, migrate to moby/moby client#2147
Seanstoppable wants to merge 3 commits into
wtfutil:trunkfrom
Seanstoppable:seanstoppable-docker-module-unit-tests

Conversation

@Seanstoppable

Copy link
Copy Markdown
Collaborator

Summary

  • Added unit tests for modules/docker covering container-state formatting, system-info display, and settings, using manually constructed structs so tests don't require a running docker daemon. Table-driven throughout.
  • Introduced a dockerAPIClient interface (and an injectable newDockerClient constructor) so NewWidget/Refresh/getSystemInfo/getContainerStates can be tested with a fake client, bringing coverage from 0% → 100%.
  • Migrated off github.com/docker/docker (the full moby monolith, +incompatible) onto the split, properly-versioned github.com/moby/moby/client + github.com/moby/moby/api modules for a smaller dependency footprint. Adapted to the redesigned client API (wrapped result types, pre-aggregated disk-usage totals), which also simplified formatSystemInfo — it no longer manually sums container/image/volume sizes.

Coverage

Stage Coverage
Before 0.0%
After initial test additions 64.2%
After dockerAPIClient interface refactor 100.0%
After moby/moby client migration 100.0% (maintained)

Details

  • client.go / utils.go / widget.go: extracted pure formatting functions (formatContainerStates, formatSystemInfo) from daemon-calling code, added the dockerAPIClient interface, and made client construction injectable via a package-level newDockerClient var.
  • New test files: utils_test.go, container_states_test.go, system_info_test.go, settings_test.go, widget_test.go, fake_client_test.go, client_test.go, new_widget_test.go.
  • go.mod: removed github.com/docker/docker; added github.com/moby/moby/client and github.com/moby/moby/api; ran go mod tidy (dropped several now-unused transitive deps).

Validation

  • go build ./... — clean
  • go vet ./... — clean
  • go test ./modules/docker/... -cover — 100.0% of statements, all tests passing
  • golangci-lint run ./modules/docker/... — clean (one pre-existing, unrelated gofmt issue in settings.go predates this change and was left untouched per scope)

- Extract pure formatConversion functions formatContainerStates and
  formatSystemInfo from client.go so container/system formatting can be
  tested without a docker daemon.
- Add table-driven tests for padSlice, formatContainerStates,
  formatSystemInfo, Settings (NewSettingsFromYAML), and Widget.display.
- Coverage for modules/docker: 0.0%% -> 64.2%%.
- Introduce dockerAPIClient interface (Info/DiskUsage/ContainerList)
  implemented by *client.Client, so Widget.cli can be swapped for a
  fake in tests instead of requiring a real docker daemon.
- Make client construction (newDockerClient var) injectable so
  NewWidget's success/error paths are testable, while still covering
  the real client.NewClientWithOpts call directly (no daemon needed
  to construct the client object).
- Add fakeDockerClient test double plus table-driven tests for
  getSystemInfo, getContainerStates, refreshDisplayBuffer, Refresh,
  and NewWidget covering success and error paths.
- Coverage: 64.2%% -> 100%%.
Replace github.com/docker/docker (the full moby monolith,
+incompatible) with the split github.com/moby/moby/client and
github.com/moby/moby/api modules, which have proper go.mod
versioning and a much smaller transitive dependency graph.

The new client redesigns Info/DiskUsage/ContainerList to return
wrapped result types and pre-aggregated disk-usage totals, so
formatSystemInfo no longer needs to manually sum SizeRw/Size/
UsageData.Size across container/image/volume slices - it now takes
the already-computed per-category totals directly.

go.mod: github.com/docker/docker removed; github.com/moby/moby/client
and github.com/moby/moby/api added (go mod tidy also dropped several
now-unused transitive deps).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant