Skip to content

service: add unit tests for LCOW v2 shim service layer#2649

Open
shreyanshjain7174 wants to merge 1 commit intomicrosoft:mainfrom
shreyanshjain7174:service/unit-tests
Open

service: add unit tests for LCOW v2 shim service layer#2649
shreyanshjain7174 wants to merge 1 commit intomicrosoft:mainfrom
shreyanshjain7174:service/unit-tests

Conversation

@shreyanshjain7174
Copy link
Copy Markdown
Contributor

@shreyanshjain7174 shreyanshjain7174 commented Mar 26, 2026

Adds unit tests for the LCOW v2 shim service layer. The service orchestrates sandbox lifecycle (create / start / stop / wait / shutdown / status / metrics) by delegating to vm.Controller; without these tests every code change had to be validated against a live HCS environment.

Design

The Service struct holds a single vmController interface field (defined in types.go). Production passes *vm.Controller; tests pass a generated mock. The interface is a superset that includes the methods pod.New needs (Guest(), SCSIController(), Plan9Controller(), NetworkController() etc.) so the same field satisfies both Service's direct calls and pod.New's narrower interface via Go structural typing - no concrete-pointer leakage. This mirrors the merged internal/controller/pod pattern.

Coverage

49 tests across three files matching the production layout:

  • service_sandbox_internal_test.go (23): duplicate sandbox rejection, missing config.json, VM create / start failure, sandbox-id mismatch guards, stop success / failure / idempotency, wait clean / error / wait-failure / exit-status-failure exits, status mapping for every state, ping not-implemented, shutdown idempotency / running-VM termination / terminate-error swallowed, metrics success and stats failure.
  • service_shimdiag_internal_test.go (10): pid passthrough, exec-in-host success / failure, tasks / share / stacks state guards, share missing host-path validation, stacks dump-failure and success.
  • service_task_internal_test.go (16): consolidated state-guard and unknown-container guard tables, not-implemented surfaces, shutdown no-op, update validation (nil resources rejected, dispatch by resource type, per-resource failure surfaces), enrichNotFoundError pass-through and ErrNotFound preservation.

Out of scope

  • The full CreateSandbox -> BuildSandboxConfig -> CreateVM path is exercised by internal/builder/vm/lcow/ tests and the test/parity/vm/ parity suite; the service-level surface is the orchestration logic only.
  • The confidential StartSandbox path reads boot files from disk; belongs in functional tagged tests.

Mocks

mocks/mock_service.go is checked in. Standardising mock generation (directive in test file + CI drift validation) is tracked in #2707.

Comment thread cmd/containerd-shim-lcow-v2/service/service.go Outdated
Defines a narrow vmController interface in the service package and threads
it through Service in place of the concrete *vm.Controller field. Production
passes vm.New(); tests pass a generated gomock. The interface includes the
methods pod.New requires (Guest, SCSIController, VPCIController,
Plan9Controller, NetworkController) so the same field satisfies both
Service's direct calls and pod.New's narrower interface via Go structural
typing.

Tests cover the production failure paths through the service RPC surface:

Sandbox API (23 tests):
- duplicate Create rejection, missing config.json, VM create / start failure
- sandbox-id mismatch guards
- stop success / failure / idempotency
- wait clean / error / wait-failure / exit-status-failure exits
- status mapping for every state, ping not-implemented
- shutdown idempotency, running-VM termination, terminate-error swallowed
- metrics success and stats failure

Shimdiag API (10 tests):
- pid passthrough, exec-in-host success / failure
- tasks / share / stacks state guards
- share missing host-path validation
- stacks dump-failure and success

Task API (16 tests):
- consolidated state-guard and unknown-container guard tables
- not-implemented surfaces, shutdown no-op
- update validation: nil resources rejected, dispatch by resource type,
  per-resource failure surfaces
- enrichNotFoundError pass-through and ErrNotFound preservation

Mocks committed at mocks/mock_service.go with the standard build tag
(windows && lcow). Standardising mock generation across controllers is
tracked in microsoft#2707.

Signed-off-by: Shreyansh Sancheti <shsancheti@microsoft.com>
// vmController is responsible for managing the lifecycle of the underlying utility VM and its associated resources.
vmController *vm.Controller
// vmController is responsible for managing the lifecycle of the underlying
// utility VM. Tests substitute a mock; production passes [*vm.Controller].
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Please remove this statement Tests substitute a mock; production passes [*vm.Controller].

)

// vmController is the subset of the VM controller that [Service] depends on.
// Implemented by [*vm.Controller]. Tests substitute a mock.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

vmController vmController

// podControllers maps podID -> PodController for each active pod.
podControllers map[string]*pod.Controller
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we create similar interface for pod.Controller too?

"github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/stats"
"github.com/Microsoft/hcsshim/internal/builder/vm/lcow"
"github.com/Microsoft/hcsshim/internal/controller/vm"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please format the imports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants