Add unit tests for internal packages - #2656
Open
abrarshivani wants to merge 13 commits into
Open
Conversation
abrarshivani
marked this pull request as ready for review
July 22, 2026 20:50
abrarshivani
requested review from
cdesiniotis,
karthikvetrivel,
rahulait,
rajathagasthya,
shivamerla and
tariq1890
as code owners
July 22, 2026 20:50
Add table-driven unit tests covering the previously untested internal/image (ImagePath) and internal/info (GetVersionParts, GetVersionString) packages. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
abrarshivani
force-pushed
the
unit-test-internal
branch
from
July 23, 2026 19:38
aa9c69d to
88f0709
Compare
tariq1890
reviewed
Jul 27, 2026
5 tasks
Cover positive and negative edge cases: - internal/image: tag vs digest selection (including empty digest, missing colon, and case-sensitive sha256 prefix), kbld passthrough, partial-CR paths taking priority over env, env fallback, no-input-validation edges, and the error path with its message contents. - internal/info: empty/whitespace version and commit, variadic joining with empty elements, and fresh-slice / no-mutation guarantees. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
Cover every path in clusterpolicy.go: SetConditionsReady/SetConditionsError success cases, the type-assertion guard for non-ClusterPolicy objects, the Ready->Error transition, the failed-Get path, the unknown-status-type default branch, retry-on-conflict, and non-conflict status-update error propagation. clusterpolicy.go reaches 100% statement coverage. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
Cover the paths in nvidiadriver.go not exercised by the existing tests: the type-assertion guard for non-NVIDIADriver objects, the failed-Get path, the unknown-status-type default branch, retry-on-conflict, non-conflict status-update error propagation, and the status.state defaulting/preservation logic in the Error case. The internal/conditions package now reaches 100% statement coverage. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
Cover the previously untested GetFilesWithSuffix: recursive matching across subdirectories, multiple suffixes, no-match and no-suffix cases, the traversal-error path for a missing directory, an empty directory, a file passed as the base path, and a file matching more than one suffix. The only remaining uncovered line in the package is the defensive panic in GetStringHash, which is unreachable: fnv.New32a's Write never returns an error and the hasher is constructed internally. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
fnv.New32a's Write never returns an error (guaranteed by the hash.Hash contract: "Write ... never returns an error"), so the error check and the panic it guarded were dead code that could never execute. Drop them and ignore the always-nil error explicitly. This makes internal/utils reach 100% statement coverage. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
Cover the previously untested Reset methods on NodeLabelFilterBuilder and NodeLabelNoValFilterBuilder, verifying that Reset clears accumulated label criteria. internal/nodeinfo now reaches 100% statement coverage. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
Cover the driver-list failure, GPU-node-list failure, and owner-label patch failures for both the add/update and removal cases, using fake-client interceptors. The only remaining uncovered line is the defensive nil-Labels guard, which is unreachable in AssignOwners because nodes are listed by the GPU-present label and therefore always carry labels. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
Cover the driver-list failure, an invalid nodeSelector on a listed driver, and the node-list failure. internal/validator now reaches 100% statement coverage. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
Address maintainer naming preference (avoid abbreviations, per review feedback on prior unit-test PRs): rename the local cp variable to clusterPolicy in clusterpolicy_test.go. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
Adopt Kubernetes-idiomatic testing patterns in the conditions and validator tests: - Replace field-by-field metav1.Condition assertions with a single cmp.Diff against the expected condition slice (ignoring server-set LastTransitionTime/ObservedGeneration), which reads clearer and prints a precise diff on failure. - Build a fresh runtime.NewScheme() per test instead of mutating the global client-go scheme.Scheme, keeping the tests hermetic. Promotes github.com/google/go-cmp to a direct dependency and vendors cmp/cmpopts. Coverage unchanged (100%). Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
GetFilesWithSuffix appended a path once per matching suffix, so a file that matched more than one of the provided suffixes was returned multiple times. Break after the first matching suffix so each file is returned at most once; this is behavior-identical for non-overlapping suffixes (the only current caller passes yaml/yml/json). Update the test to assert the single result. Also document that GetObjectHashIgnoreEmptyKeys requires a struct (or pointer to struct) and panics on other kinds, matching what all callers already pass. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
abrarshivani
force-pushed
the
unit-test-internal
branch
from
July 27, 2026 18:29
88f0709 to
b6fafd7
Compare
Remove comments that merely restate what the code and test/case names already convey, keeping only the few that explain non-obvious context (why a default branch is reached via the unexported setConditions, why a bare trailing colon is the expected value, the append-aliasing regression a test guards against, and why certain node setups drive specific patch branches). Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
tariq1890
reviewed
Jul 27, 2026
| panic(err) | ||
| } | ||
| // hash.Hash's Write never returns an error, per its documented contract. | ||
| _, _ = hasher.Write([]byte(s)) |
Contributor
There was a problem hiding this comment.
I would revert this change. We know that the hasher.Write will never return an error which is why we panic should err != nil ever evaluate to true. One of the purposes of panic is to throw an error when a fundamental assumption is broken or when we operate under an inconceivable scenario.
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 to raise coverage across the
internal/packages, plus a few small production cleanups noted below. Each package was driven to full statement coverage of every reachable path (both success and error paths), verified withgo test -covermode=count.Packages covered
internal/image—ImagePath: tag vssha256:digest joining, kbld image pass-through, CR-over-env priority, env fallback, and the empty-CR error path. 100%internal/info—GetVersionParts/GetVersionString: with/withoutgitCommit, empty/whitespace values, variadic joining, fresh-slice / no-mutation guarantees. 100%internal/conditions— ClusterPolicy and NVIDIADriver updaters: Ready/Error success, type-assertion guard, failed-Get, unknown-status-type default, retry-on-conflict, non-conflict update-error propagation, andstatus.statedefaulting/preservation. Uses the controller-runtime fake client + interceptors; condition assertions usecmp.Diff; schemes are built per-test. 100%internal/utils—GetFilesWithSuffixand the object-hash helpers. 100%internal/nodeinfo— the previously untestedResetmethods on both filter builders. 100%internal/validator—Validateerror paths (driver-list failure, invalid listed selector, node-list failure). 100%internal/nvidiadriver—AssignOwnerserror paths (driver-list, node-list, owner-label patch add/remove). 98.5% (one unreachable nil-map guard).Production changes (small cleanups)
internal/utils/utils.go—GetStringHash: removed an unreachable error branch.fnv.New32a'sWritenever returns an error (per thehash.Hashcontract), so the error check and thepanicit guarded were dead code.internal/utils/utils.go—GetFilesWithSuffix: deduplicate results. The function appended a path once per matching suffix, so a file matching more than one of the provided suffixes was returned multiple times. It nowbreaks after the first matching suffix (behavior-identical for the only current caller, which passes the non-overlappingyaml/yml/json).internal/utils/utils.go—GetObjectHashIgnoreEmptyKeys: documented (no behavior change) that the argument must be a struct or pointer-to-struct and that other kinds/nil panic.The
ImagePathincomplete-spec validation (production behavior change) and its tests were moved to #2682.Checklist
make lint)make validate-generated-assets)make validate-modules)Testing