Skip to content

Add unit tests for cmd/gpuop-cfg config validators - #2660

Merged
abrarshivani merged 1 commit into
NVIDIA:mainfrom
abrarshivani:unit-test-cmd-validate
Jul 30, 2026
Merged

abrarshivani merged 1 commit into
NVIDIA:mainfrom
abrarshivani:unit-test-cmd-validate

Conversation

@abrarshivani

@abrarshivani abrarshivani commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Description

Adds hermetic unit tests for the cmd/gpuop-cfg/validate subtree (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 before ManifestGet (control-flow, not just verified behind a dead proxy).

Covered

  • csv.validateALMExample — 100%: first item Kind == 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 item Kind != ClusterPolicy (including when a ClusterPolicy appears later) → "invalid example clusterpolicy". (Note: the validator only checks cpList[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 (via t.TempDir), and the input == "-" stdin branch (via an os.Pipe redirect). Local file/stdin I/O is deterministic and hermetic.
  • validateImage / validateImages (csv and clusterpolicy) — error paths only: an invalid image reference fails at ref.New before any registry call; an empty/zero spec fails at ImagePath before any registry access. Assertions use require.ErrorContains.
  • NewCommand constructors (validate, csv, clusterpolicy) — 100%: command name/usage, subcommand presence (not an exact count), and the input flag located by name with its type (*cli.StringFlag), names, usage, and default "-" asserted.

Deliberately not covered (and why)

  • Success paths of validateImage/validateImages — they construct a regclient internally and call ManifestGet against 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)

  • Guard csv.validateImages against a CSV with no DeploymentSpecs/Containers (currently indexes [0] unchecked → possible panic on a malformed CSV).
  • Decide validateALMExample(nil) handling (currently dereferences csv.Annotations).
  • Inject the registry/manifest getter so success, registry-failure and context-cancellation paths become hermetically testable; consolidate the duplicated validateImage; wrap nested errors with %w.

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 ./cmd/gpuop-cfg/validate/... -covermode=count
golangci-lint run ./cmd/gpuop-cfg/validate/...           # 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.

@tariq1890

Copy link
Copy Markdown
Contributor

Can you rebase this PR and squash the commit history?

Cover the csv and clusterpolicy validators under cmd/gpuop-cfg/validate:
alm-examples parsing (including apiVersion-ignored, empty/null list, and
unmarshal error boundaries), image validation, options load/getContents
(file and stdin), and the flag/subcommand wiring.

Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
@abrarshivani
abrarshivani force-pushed the unit-test-cmd-validate branch from c37738e to 0f5ead4 Compare July 29, 2026 22:59
@abrarshivani

Copy link
Copy Markdown
Contributor Author

@tariq1890 I have rebased and squashed the commits. Can you please take look?

@abrarshivani
abrarshivani enabled auto-merge July 30, 2026 00:02
@abrarshivani
abrarshivani merged commit f947746 into NVIDIA:main Jul 30, 2026
20 checks passed
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.

2 participants