Skip to content

Add unit tests for internal/state#2658

Draft
abrarshivani wants to merge 1 commit into
NVIDIA:mainfrom
abrarshivani:unit-test-state
Draft

Add unit tests for internal/state#2658
abrarshivani wants to merge 1 commit into
NVIDIA:mainfrom
abrarshivani:unit-test-state

Conversation

@abrarshivani

@abrarshivani abrarshivani commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Description

Raises internal/state statement coverage from 37.8% to 97.2%. Tests only — no production source changed. Tests use the controller-runtime fake client with interceptor.Funcs to inject Get/List/Create/Update errors, a fake manager, the real manifest renderer, and computed DaemonSet hashes — exercising real success and error paths rather than tautologies.

Now at 100%: info_source.go in full, and NewStateDriver, Sync, cleanupStaleDriverDaemonsets, getDaemonsetFromObjects, getObjectOfKind, getDriverName, getDriverSpec, createConfigMapVolumeMounts, buildDriverInstallConfig, renderManifestObjects, getGDSSpec/getGDRCopySpec/getRuntimeSpec, createOrUpdateObjs, isDaemonSetReady, newStates, and the stateManager methods.

The remaining ~2.8% is the reachable ceiling

Every remaining uncovered statement is genuinely not exercisable by a unit test, verified against the source:

  • GetWatchSources — the uncovered block is an inline predicate.NewTypedPredicateFuncs closure only invoked by a live controller-runtime informer on a DaemonSet watch event; the outer construction is covered via a fake manager.
  • NewManager / newNVIDIADriverStates success returns — require loading the hardcoded on-disk path /opt/gpu-operator/manifests/state-driver (a string literal, not an overridable var). All error paths are covered.
  • getManifestObjects / handleDefaultImagesInObjects — in-loop error branches unreachable with a valid NVIDIADriver spec (the spec is always populated, so templates render and always yield a DaemonSet); the renderManifestObjects error itself is covered directly.
  • mergeServiceAccount — two SetNestedField writebacks that cannot error (single top-level field on a map, value sourced from a valid NestedSlice).

Two genuine dead-code findings (not just coverage gaps)

While maximizing coverage, two blocks turned out to be unreachable due to source-level issues, worth flagging:

  • driver_volumes.go (~L224-243) — the SUSE /lib/modules mount for precompiled drivers is guarded by if cr.Spec.UsePrecompiledDrivers() && ... but nested inside the enclosing if !cr.Spec.UsePrecompiledDrivers() block, a logical contradiction. This code can never execute; it looks like a real bug (precompiled-driver SUSE nodes never get /lib/modules mounted).
  • nodepool.go (~L102-104) — the error branch for getOSTag, but getOSTag unconditionally returns a nil error, so it is dead.

These are left as-is in this tests-only PR; happy to address them separately if desired.

Checklist

  • No secrets, sensitive information, or unrelated changes
  • Lint checks passing (make lint)
  • Generated assets in-sync (make validate-generated-assets)
  • Go mod artifacts in-sync (make validate-modules)
  • Test cases are added for new code paths

Testing

go test ./internal/state/... -covermode=count    # coverage: 97.2% of statements
golangci-lint run ./internal/state/...            # 0 issues (GOOS=linux)

@copy-pr-bot

copy-pr-bot Bot commented Jul 22, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@abrarshivani
abrarshivani force-pushed the unit-test-state branch 4 times, most recently from d763877 to 6d26bb8 Compare July 22, 2026 04:39
Raise internal/state statement coverage from 37.8% to 97.2% with tests only.
Cover info_source (100%), manager SyncState/GetWatchSources/newStates error
paths, state_skel create/update/merge/getSyncState/isDaemonSetReady, driver
Sync/getManifestObjects/handleDefaultImagesInObjects/cleanupStaleDriverDaemonsets,
the GDS/GDRCopy/runtime spec helpers, driver spec/name/volume-config helpers,
and nodepool selection, using the controller-runtime fake client with
interceptors for injected errors and a fake manager where needed.

The ~2.8% that remains is the reachable ceiling: each uncovered statement is
either dead code (the getOSTag error branch that never fires; the SUSE
/lib/modules block gated by contradictory UsePrecompiledDrivers conditions),
a success return that loads the hardcoded on-disk path
/opt/gpu-operator/manifests/state-driver, a live-informer predicate closure in
GetWatchSources, or a provably-impossible SetNestedField error.

Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
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.

1 participant