Skip to content

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

Draft
abrarshivani wants to merge 6 commits into
NVIDIA:mainfrom
abrarshivani:unit-test-cmd-validate
Draft

Add unit tests for cmd/gpuop-cfg config validators#2660
abrarshivani wants to merge 6 commits 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.

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
abrarshivani force-pushed the unit-test-cmd-validate branch from 884cf22 to 1a54638 Compare July 23, 2026 19:38
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>
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.

1 participant