Add unit tests for internal/state - #2658
Open
abrarshivani wants to merge 1 commit into
Open
Conversation
abrarshivani
force-pushed
the
unit-test-state
branch
5 times, most recently
from
July 23, 2026 19:38
6d26bb8 to
dbc251d
Compare
abrarshivani
force-pushed
the
unit-test-state
branch
13 times, most recently
from
July 30, 2026 21:35
91ffc55 to
7fa5f4c
Compare
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>
abrarshivani
force-pushed
the
unit-test-state
branch
from
July 31, 2026 20:35
7fa5f4c to
6c16861
Compare
abrarshivani
marked this pull request as ready for review
July 31, 2026 23:22
abrarshivani
requested review from
cdesiniotis,
karthikvetrivel,
rahulait,
rajathagasthya,
shivamerla and
tariq1890
as code owners
July 31, 2026 23:22
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds unit tests across
internal/state. Statement coverage of the package is 93.6%, measured against the current rebased head (go test ./internal/state/... -covermode=count). Tests only — no production source changed. Tests use the controller-runtime fake client withinterceptor.Funcsto inject Get/List/Create/Update/Delete errors, a fake manager, a fake REST mapper for generic cleanup, the real manifest renderer, and computed DaemonSet hashes — exercising real success and error paths rather than tautologies.Now at 100%:
info_source.goin full, andNewStateDriver,Sync,cleanupStaleDriverDaemonsets,getDaemonsetFromObjects,getObjectOfKind,getDriverName,getDriverSpec,createConfigMapVolumeMounts,buildDriverInstallConfig,renderManifestObjects,getGDSSpec/getGDRCopySpec/getRuntimeSpec,createOrUpdateObjs,isDaemonSetReady,isDeploymentReady,handleStateObjectsDeletion,getSupportedGVKs,newStates, and thestateManagermethods.deleteStateRelatedObjectsreaches 100% (a fake REST mapper covers the non-NoMatchmapping-error branch).Coverage rerun against the rebased head
The branch was rebased onto source that added Deployment readiness (
isDeploymentReady) and generic state-object deletion (handleStateObjectsDeletion/deleteStateRelatedObjects). Those reachable paths are now directly tested: a readiness table (including the stale-generation and still-terminating-replica cases), an exactgetSupportedGVKsset comparison, and deletion coverage for namespaced-vs-cluster scoping, forbidden-list skip, NotFound-on-delete, list/delete error propagation, and already-deleting objects. The remaining uncovered statements are genuinely not exercisable by a unit test:GetWatchSources— the uncovered block is an inlinepredicate.NewTypedPredicateFuncsclosure only invoked by a live controller-runtime informer on a DaemonSet watch event; the outer construction is covered via a fake manager.NewManager/newNVIDIADriverStatessuccess 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 validNVIDIADriverspec (the spec is always populated, so templates render and always yield a DaemonSet); therenderManifestObjectserror itself is covered directly.mergeServiceAccount— twoSetNestedFieldwritebacks that cannot error (single top-level field on a map, value sourced from a validNestedSlice).Behaviors pinned by characterization tests
While maximizing coverage, several blocks turned out to encode questionable or unreachable behavior. Per the tests-only scope these are not changed here; instead they are pinned by tests with comments so any future fix is a conscious change:
isDaemonSetReadynonzero-desired path does not re-checkObservedGeneration, so status from a previous generation can be reported ready right after a spec change (Kubernetes rollout-status gates onObservedGenerationfirst). Pinned byTestIsDaemonSetReadyStaleGeneration.isDeploymentReadydoes not requireStatus.Replicas == desired, so a rollout with a superseded replica still terminating is reported ready (Kubernetes'DeploymentCompleterequires it). Pinned by the "old replica still terminating" case.deleteStateRelatedObjectstreats aForbiddenList as "nothing to clean up", which can leave operands behind if list permission is revoked after creation. Pinned byTestDeleteStateRelatedObjectsForbiddenListSkipped.driver_volumes.go(~L224-243) — the SUSE/lib/modulesmount for precompiled drivers is nested insideif !cr.Spec.UsePrecompiledDrivers()yet guarded byif cr.Spec.UsePrecompiledDrivers(), a contradiction, so it can never execute (likely a real bug).nodepool.go(~L102-104) — the error branch forgetOSTagis dead becausegetOSTagunconditionally returns a nil error.Happy to address any of these in a separate production PR if desired.
Checklist
make lint)make validate-generated-assets)make validate-modules)Testing