Add unit tests for internal packages - #2656
Conversation
aa9c69d to
88f0709
Compare
88f0709 to
b6fafd7
Compare
|
Can you condense the commit history and split it by the package instead? For eg:- If you have two or more commits writing tests for code in the same package, those commits can be merged to one. |
|
@tariq1890 Sure, will segregate each package tests into its own PR. |
|
Thanks @abrarshivani. To clarify, it doesn't have to be separate PRs. I just meant that we reorganize this into commits per package. If you want to make it a separate PR for each commit, that's fine too. I'll leave that for you to decide. |
|
Oh, ok. I was thinking about this from a reviewer's perspective. I can also group the commits by package to make it easier to review. |
0f65be4 to
f897418
Compare
| // Stop at the first matching suffix so a file that matches | ||
| // more than one of the provided suffixes is returned only once. | ||
| break |
There was a problem hiding this comment.
Are we sure that this is correct? Won't this lead to a bug as we need to collect multiple yaml files under a state- directory for template rendering?
There was a problem hiding this comment.
Also, even if this change is valid, it warrants its own PR IMO. We shouldn't slip in any behavioural change in a PR that claims addition of unit tests.
There was a problem hiding this comment.
Agreed, even though it looks safe, this behavioral change should be in a separate PR
There was a problem hiding this comment.
Let me move the production code-related changes to another PR.
Cover GetVersionParts and GetVersionString: with and without gitCommit, empty and whitespace-only values, variadic joining with empty elements, and the fresh-slice / no-mutation guarantees. internal/info reaches 100% statement coverage. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
Cover every path in the ClusterPolicy and NVIDIADriver updaters: SetConditionsReady/SetConditionsError success cases, the type-assertion guard for foreign objects, the Ready->Error transition, 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. Conditions are compared with a single cmp.Diff against the expected slice (ignoring server-set LastTransitionTime/ObservedGeneration) rather than field by field, and each test builds a fresh runtime.NewScheme() instead of mutating the global client-go scheme. This promotes go-cmp to a direct dependency and vendors cmp/cmpopts. clusterpolicy.go and nvidiadriver.go both 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 reaches 100% statement coverage. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
Cover the driver-list failure, the GPU-node-list failure, and the owner-label patch failures for both the add/update and removal cases, using fake-client interceptors. internal/nvidiadriver reaches 98.6% statement coverage; 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>
f897418 to
0afb86b
Compare
Cover GetFilesWithSuffix and the object-hash helpers. 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, using fake-client interceptors. internal/validator reaches 100% statement coverage. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
0afb86b to
a674f06
Compare
Description
Adds unit tests to raise coverage across the
internal/packages. This PR is tests-only — it contains no production changes. Each package was driven to full statement coverage of every reachable path it owns (both success and error paths), verified withgo test -covermode=count.The branch is organized as one commit per package, so each can be reviewed independently.
Packages covered
Add unit tests for internal/infointernal/infoAdd unit tests for internal/conditionsinternal/conditionsclusterpolicy.go100%,nvidiadriver.go100%Add unit tests for internal/nodeinfointernal/nodeinfoAdd unit tests for internal/nvidiadriver ...internal/nvidiadriverAdd unit tests for internal/utilsinternal/utilsGetFilesWithSuffix+ object-hash helpers 100%Add unit tests for internal/validator ...internal/validatorinternal/info—GetVersionParts/GetVersionString: with/withoutgitCommit, empty/whitespace values, variadic joining, fresh-slice / no-mutation guarantees.internal/conditions— ClusterPolicy and NVIDIADriver updaters: Ready/Error success, type-assertion guard, failed-Get, unknown-status-type default branch, retry-on-conflict, non-conflict update-error propagation, andstatus.statedefaulting/preservation. Uses the controller-runtime fake client + interceptors; condition assertions usecmp.Diff(ignoring server-setLastTransitionTime/ObservedGeneration); schemes are built per-test rather than mutating the global client-go scheme. This promotesgithub.com/google/go-cmpto a direct dependency (a test dependency) and vendorscmp/cmpopts.internal/nodeinfo— the previously untestedResetmethods on both filter builders.internal/nvidiadriver—AssignOwnerserror paths (driver-list, node-list, owner-label patch add/remove). The one uncovered line is a defensive nil-Labelsguard, unreachable because nodes are listed by the GPU-present label and therefore always carry labels.internal/utils—GetFilesWithSuffixand the object-hash helpers.internal/validator—Validateerror paths (driver-list failure, invalid listed selector, node-list failure).Out of scope
Two packages touched here also gained new, untested code on
mainafter this branch was opened. Covering it is left to follow-up PRs, so the package-level numbers are below 100%:internal/conditionsis 64.6% overall —gpucluster.go(added by Add GPUCluster CRD and controller for DRA-based stack #2571) is untested.internal/utilsis 67.2% overall —PrependPathListEnvvar,SetEnvVar, andWriteFileAtomicallyare untested.GetStringHashis at 75%: itspanicon ahasher.Writeerror is a deliberate assertion of an invariant and is unreachable by design, so it is left as-is.The
internal/imagetests that previously lived in this PR have been dropped: theImagePathwork was split into #2682, which has since merged and already bringsinternal/imageto 100%.Production changes split out
The small
internal/utilsproduction cleanups that previously lived here (theGetFilesWithSuffixdedupebreakand theGetObjectHashIgnoreEmptyKeysprecondition doc) have been moved to their own PR, #2690, so this PR stays tests-only.utils.gohere is unchanged frommain.Checklist
make lint)make validate-generated-assets)make validate-modules)Testing
Every commit was verified to build and pass independently (
git rebase --exec), so the branch is bisect-clean:Full-branch checks: