Skip to content

test(device-plugin): add unit tests for imex and cdi packages - #2355

Closed
Labreo wants to merge 1 commit into
Project-HAMi:masterfrom
Labreo:test/add-imex-cdi-unit-tests
Closed

test(device-plugin): add unit tests for imex and cdi packages#2355
Labreo wants to merge 1 commit into
Project-HAMi:masterfrom
Labreo:test/add-imex-cdi-unit-tests

Conversation

@Labreo

@Labreo Labreo commented Aug 4, 2026

Copy link
Copy Markdown

What type of PR is this?

/kind cleanup

What this PR does / why we need it:
This PR adds comprehensive unit test coverage for the imex (pkg/device-plugin/nvidiadevice/nvinternal/imex) and cdi (pkg/device-plugin/nvidiadevice/nvinternal/cdi) packages in the NVIDIA device plugin internal implementation. Previously, both packages had zero unit test coverage (coverage: 0.0% of statements).

Specifically, this PR introduces:

  • pkg/device-plugin/nvidiadevice/nvinternal/imex/imex_test.go: Tests GetChannels() for empty channel lists, non-required channel skips, required channel error handling, and character device file mode verification (83.3% statement coverage).
  • pkg/device-plugin/nvidiadevice/nvinternal/cdi/null_test.go: Tests NewNullHandler() no-op CDI handler logic (AdditionalDevices, CreateSpecFile, QualifiedName).
  • pkg/device-plugin/nvidiadevice/nvinternal/cdi/options_test.go: Tests option configuration setters (WithDeviceListStrategies, WithDriverRoot, WithDevRoot, WithTargetDriverRoot, WithTargetDevRoot, WithNvidiaCTKPath, WithDeviceIDStrategy, WithVendor, WithGdrcopyEnabled, WithGdsEnabled, WithMofedEnabled, WithImexChannels).
  • pkg/device-plugin/nvidiadevice/nvinternal/cdi/imex_test.go: Tests IMEX channel CDI spec generation (newImexChannelSpecGenerator() and GetSpec()).

Which issue(s) this PR fixes:
Fixes #2354

Special notes for your reviewer:

  • All added files have Apache 2.0 license headers and pass ./hack/verify-license.sh.
  • Tests pass via go test -v -cover ./pkg/device-plugin/nvidiadevice/nvinternal/imex/... ./pkg/device-plugin/nvidiadevice/nvinternal/cdi/....
  • AI assistance disclosure: I used an AI tool for codebase navigation, unit test generation, and review support.

Does this PR introduce a user-facing change?:
NONE

Summary by CodeRabbit

  • Tests
    • Added coverage for IMEX channel CDI specification generation.
    • Added validation for null CDI handler behavior and configuration options.
    • Added tests for IMEX channel discovery, including missing channels and invalid device paths.
    • Improved verification of successful and error-handling scenarios across CDI and IMEX functionality.

Signed-off-by: Labreo <74888611+Labreo@users.noreply.github.com>
@hami-robot

hami-robot Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Labreo
Once this PR has been reviewed and has the lgtm label, please assign dsfans2014 for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@hami-robot

hami-robot Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Welcome @Labreo! It looks like this is your first PR to Project-HAMi/HAMi 🎉

@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The pull request adds unit tests for the NVIDIA device plugin’s imex and cdi internal packages. The tests cover channel validation, null CDI behavior, option setters, and IMEX CDI spec generation.

Changes

NVIDIA internal package tests

Layer / File(s) Summary
IMEX channel retrieval tests
pkg/device-plugin/nvidiadevice/nvinternal/imex/imex_test.go
Tests empty input, optional missing channels, required missing channels, and regular-file channel paths.
CDI handler and spec tests
pkg/device-plugin/nvidiadevice/nvinternal/cdi/*_test.go
Tests IMEX CDI spec generation, NullHandler methods, and 12 CDI option setters.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: archlitchi

Poem

A rabbit checks each channel path,
And tests the CDI trail.
Null handlers rest with empty hands,
While option setters sail.
IMEX specs now prove their shape.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the addition of unit tests for the NVIDIA device plugin IMEX and CDI packages.
Linked Issues check ✅ Passed The tests address issue #2354 by covering IMEX handling, character-device validation, CDI no-ops, options, and spec generation.
Out of Scope Changes check ✅ Passed All changes add tests within the packages identified by issue #2354, with no unrelated code or user-facing changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai
coderabbitai Bot requested a review from archlitchi August 4, 2026 15:00

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
pkg/device-plugin/nvidiadevice/nvinternal/cdi/imex_test.go (1)

58-65: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Assert the generated device-node paths.

The test supplies Path and HostPath, but it does not check ContainerEdits.DeviceNodes. A change that drops or changes either path in GetSpec will pass this test. Assert one device node and compare both fields.

Proposed test extension
 if rawSpec.Devices[0].Name != "0" {
 	t.Errorf("expected device name '0', got %s", rawSpec.Devices[0].Name)
 }
+deviceNodes := rawSpec.Devices[0].ContainerEdits.DeviceNodes
+if len(deviceNodes) != 1 {
+	t.Fatalf("expected 1 device node, got %d", len(deviceNodes))
+}
+if deviceNodes[0].Path != channels[0].Path {
+	t.Errorf("expected device path %q, got %q", channels[0].Path, deviceNodes[0].Path)
+}
+if deviceNodes[0].HostPath != channels[0].HostPath {
+	t.Errorf("expected host path %q, got %q", channels[0].HostPath, deviceNodes[0].HostPath)
+}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/device-plugin/nvidiadevice/nvinternal/cdi/imex_test.go` around lines 58 -
65, Extend the test assertions after validating rawSpec.Devices to inspect
rawSpec.ContainerEdits.DeviceNodes: require exactly one device node, then
compare its Path and HostPath against the input values supplied by the test.
Keep the existing Kind and device-name assertions unchanged.
pkg/device-plugin/nvidiadevice/nvinternal/imex/imex_test.go (1)

28-110: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚖️ Poor tradeoff

Consider adding a happy-path test for an existing character device channel.

Every subtest here exercises an empty list or a failure branch (missing channel, non-character file). None confirms that GetChannels appends a channel to the result when the underlying path is a valid character device. Creating a real character device in a test typically requires syscall.Mknod with elevated privileges, which likely explains the omission, so this is a good-to-have rather than a blocking gap.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/device-plugin/nvidiadevice/nvinternal/imex/imex_test.go` around lines 28
- 110, Add a happy-path subtest to TestGetChannels that provides an existing
valid character-device channel and verifies GetChannels returns it without
error. Use an appropriate test setup or mockable filesystem mechanism consistent
with the implementation, and assert the returned channel collection contains the
expected channel while preserving the existing failure-case tests.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@pkg/device-plugin/nvidiadevice/nvinternal/cdi/imex_test.go`:
- Around line 58-65: Extend the test assertions after validating rawSpec.Devices
to inspect rawSpec.ContainerEdits.DeviceNodes: require exactly one device node,
then compare its Path and HostPath against the input values supplied by the
test. Keep the existing Kind and device-name assertions unchanged.

In `@pkg/device-plugin/nvidiadevice/nvinternal/imex/imex_test.go`:
- Around line 28-110: Add a happy-path subtest to TestGetChannels that provides
an existing valid character-device channel and verifies GetChannels returns it
without error. Use an appropriate test setup or mockable filesystem mechanism
consistent with the implementation, and assert the returned channel collection
contains the expected channel while preserving the existing failure-case tests.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 4e196ed2-3fa0-44a6-98ec-aae209296808

📥 Commits

Reviewing files that changed from the base of the PR and between 4f8a82f and f73e72d.

📒 Files selected for processing (4)
  • pkg/device-plugin/nvidiadevice/nvinternal/cdi/imex_test.go
  • pkg/device-plugin/nvidiadevice/nvinternal/cdi/null_test.go
  • pkg/device-plugin/nvidiadevice/nvinternal/cdi/options_test.go
  • pkg/device-plugin/nvidiadevice/nvinternal/imex/imex_test.go

@mesutoezdil

Copy link
Copy Markdown
Contributor

duplicate #2351

@mesutoezdil mesutoezdil closed this Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Missing unit test coverage for nvinternal/imex and nvinternal/cdi packages

2 participants