Add unit tests for cmd/gpuop-cfg config validators - #2660
Draft
abrarshivani wants to merge 6 commits into
Draft
Conversation
Add hermetic unit tests (no network/registry) for the gpuop-cfg validate subtree: - validateALMExample: full coverage (valid example, malformed JSON, missing/ empty annotation, empty list, wrong Kind). - validateImage/validateImages (csv and clusterpolicy): cover the pre-network error paths (invalid image reference, empty spec producing an image-path error). The registry happy paths are intentionally not unit-tested. - NewCommand constructors (validate, csv, clusterpolicy): full coverage of the command wiring (name, usage, subcommands, flags). Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
abrarshivani
force-pushed
the
unit-test-cmd-validate
branch
from
July 23, 2026 19:38
884cf22 to
1a54638
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 only the error path is hermetic, and why certain inputs deterministically fail before any registry access). Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
Following review feedback that such context belongs in the commit description rather than inline comments, remove the remaining explanatory comments. For the record: - validateImage/validateImages are only unit-tested on their hermetic error paths; the happy path performs a real registry manifest lookup, so it is not exercised here. - csv: an invalid RelatedImage fails before any deployment/registry access, so no network call or nil-index panic on the empty DeploymentSpecs slice. - clusterpolicy: DRIVER_IMAGE is cleared so a zero-value Driver spec fails to produce an image path; and Image="@@bad::ref" makes ref.New fail (after the os-tag is appended) before any registry access. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
- Find the input flag by name instead of indexing cmd.Flags[0], and assert its type, names, usage and default value. - Assert subcommand presence (csv, clusterpolicy) instead of an exact count. - Assert error substrings for the malformed/missing/empty/nil-annotation, empty-list and wrong-kind alm-examples cases, and add multi-entry cases documenting that only the first example is checked. - Use require.ErrorContains in the image reference tests. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
- Test options.load and getContents (both packages): valid manifest, missing file, malformed yaml, empty file, and the stdin (input == "-") branch, using t.TempDir and an os.Pipe redirect. Local file/stdin I/O is deterministic and hermetic. - alm-examples: rename the success case to reflect that only the first item's kind is checked, and add null and JSON-object (non-array) boundary cases. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
Add an alm-examples case with a ClusterPolicy kind but a wrong apiVersion that still returns nil, making explicit that the validator only checks the first item's Kind. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
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 hermetic unit tests for the
cmd/gpuop-cfg/validatesubtree (previously 0% covered). Tests only — no production source changed. No test reaches registry access: every image case fails during image-path construction or reference parsing beforeManifestGet(control-flow, not just verified behind a dead proxy).Covered
csv.validateALMExample— 100%: first itemKind == ClusterPolicy→ nil; malformed JSON → "invalid character"; missing/empty/null/nil-annotation → unmarshal or "no example clusterpolicy found"; JSON object instead of array → "cannot unmarshal object"; empty list → "no example clusterpolicy found"; first itemKind != ClusterPolicy(including when a ClusterPolicy appears later) → "invalid example clusterpolicy". (Note: the validator only checkscpList[0].Kind; the multi-entry cases document that contract.)options.load/options.getContents(csv and clusterpolicy) — 100%: valid manifest, missing file, malformed YAML, empty file (viat.TempDir), and theinput == "-"stdin branch (via anos.Piperedirect). Local file/stdin I/O is deterministic and hermetic.validateImage/validateImages(csv and clusterpolicy) — error paths only: an invalid image reference fails atref.Newbefore any registry call; an empty/zero spec fails atImagePathbefore any registry access. Assertions userequire.ErrorContains.NewCommandconstructors (validate, csv, clusterpolicy) — 100%: command name/usage, subcommand presence (not an exact count), and theinputflag located by name with its type (*cli.StringFlag), names, usage, and default"-"asserted.Deliberately not covered (and why)
validateImage/validateImages— they construct aregclientinternally and callManifestGetagainst a real container registry, so they aren't hermetically unit-testable without a production refactor (injectable manifest getter). Tracked as follow-up.The subtree's package totals are modest by design (validate 100%, csv 55.4%, clusterpolicy 26.9%); the uncovered remainder is the registry/entry-point code above.
Follow-ups (production changes, out of scope for this tests-only PR)
csv.validateImagesagainst a CSV with noDeploymentSpecs/Containers(currently indexes[0]unchecked → possible panic on a malformed CSV).validateALMExample(nil)handling (currently dereferencescsv.Annotations).validateImage; wrap nested errors with%w.Checklist
make lint)make validate-generated-assets)make validate-modules)Testing