-
Notifications
You must be signed in to change notification settings - Fork 644
test: add unit tests for nvinternal info, mig, and watch packages #1893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hami-robot
merged 4 commits into
Project-HAMi:master
from
pmady:test/add-info-version-tests
May 26, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a8d5267
test: add unit tests for nvinternal info, mig, and watch packages
pmady 5ac539b
test: fix copyright year to 2026 in test files
pmady 033e361
test(watch): use t.TempDir() instead of hardcoded /tmp path
pmady 274481f
test(mig): skip capability test when nvidia-caps exists on host
pmady File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
pkg/device-plugin/nvidiadevice/nvinternal/info/version_test.go
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| /* | ||
| Copyright 2026 The HAMi Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package info | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestGetVersion(t *testing.T) { | ||
| original := version | ||
| defer func() { version = original }() | ||
|
|
||
| version = "v2.5.0" | ||
| if got := GetVersion(); got != "v2.5.0" { | ||
| t.Errorf("GetVersion() = %q, want %q", got, "v2.5.0") | ||
| } | ||
| } | ||
|
|
||
| func TestGetVersion_default(t *testing.T) { | ||
| original := version | ||
| defer func() { version = original }() | ||
|
|
||
| version = "unknown" | ||
| if got := GetVersion(); got != "unknown" { | ||
| t.Errorf("GetVersion() = %q, want %q", got, "unknown") | ||
| } | ||
| } | ||
|
|
||
| func TestGetVersionParts_withCommit(t *testing.T) { | ||
| origVersion := version | ||
| origCommit := gitCommit | ||
| defer func() { | ||
| version = origVersion | ||
| gitCommit = origCommit | ||
| }() | ||
|
|
||
| version = "v2.5.0" | ||
| gitCommit = "abc123" | ||
|
|
||
| parts := GetVersionParts() | ||
| if len(parts) != 2 { | ||
| t.Fatalf("GetVersionParts() returned %d parts, want 2", len(parts)) | ||
| } | ||
| if parts[0] != "v2.5.0" { | ||
| t.Errorf("parts[0] = %q, want %q", parts[0], "v2.5.0") | ||
| } | ||
| if parts[1] != "commit: abc123" { | ||
| t.Errorf("parts[1] = %q, want %q", parts[1], "commit: abc123") | ||
| } | ||
| } | ||
|
|
||
| func TestGetVersionParts_noCommit(t *testing.T) { | ||
| origVersion := version | ||
| origCommit := gitCommit | ||
| defer func() { | ||
| version = origVersion | ||
| gitCommit = origCommit | ||
| }() | ||
|
|
||
| version = "v2.5.0" | ||
| gitCommit = "" | ||
|
|
||
| parts := GetVersionParts() | ||
| if len(parts) != 1 { | ||
| t.Fatalf("GetVersionParts() returned %d parts, want 1", len(parts)) | ||
| } | ||
| if parts[0] != "v2.5.0" { | ||
| t.Errorf("parts[0] = %q, want %q", parts[0], "v2.5.0") | ||
| } | ||
| } | ||
|
|
||
| func TestGetVersionString_basic(t *testing.T) { | ||
| origVersion := version | ||
| origCommit := gitCommit | ||
| defer func() { | ||
| version = origVersion | ||
| gitCommit = origCommit | ||
| }() | ||
|
|
||
| version = "v2.5.0" | ||
| gitCommit = "abc123" | ||
|
|
||
| got := GetVersionString() | ||
| if !strings.Contains(got, "v2.5.0") { | ||
| t.Errorf("GetVersionString() missing version: %q", got) | ||
| } | ||
| if !strings.Contains(got, "abc123") { | ||
| t.Errorf("GetVersionString() missing commit: %q", got) | ||
| } | ||
| } | ||
|
|
||
| func TestGetVersionString_withExtra(t *testing.T) { | ||
| origVersion := version | ||
| origCommit := gitCommit | ||
| defer func() { | ||
| version = origVersion | ||
| gitCommit = origCommit | ||
| }() | ||
|
|
||
| version = "v2.5.0" | ||
| gitCommit = "" | ||
|
|
||
| got := GetVersionString("go1.25", "linux/amd64") | ||
| parts := strings.Split(got, "\n") | ||
| if len(parts) != 3 { | ||
| t.Fatalf("GetVersionString() returned %d lines, want 3, got: %q", len(parts), got) | ||
| } | ||
| if parts[0] != "v2.5.0" { | ||
| t.Errorf("parts[0] = %q, want %q", parts[0], "v2.5.0") | ||
| } | ||
| if parts[1] != "go1.25" { | ||
| t.Errorf("parts[1] = %q, want %q", parts[1], "go1.25") | ||
| } | ||
| if parts[2] != "linux/amd64" { | ||
| t.Errorf("parts[2] = %q, want %q", parts[2], "linux/amd64") | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| Copyright 2026 The HAMi Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package mig | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestGetMigCapabilityDevicePaths_noFile(t *testing.T) { | ||
| if _, err := os.Stat(nvcapsMigMinorsPath); err == nil { | ||
| t.Skipf("skipping: %s exists on this machine", nvcapsMigMinorsPath) | ||
| } | ||
| result, err := GetMigCapabilityDevicePaths() | ||
| if err != nil { | ||
| t.Fatalf("GetMigCapabilityDevicePaths() unexpected error: %v", err) | ||
| } | ||
| if result != nil { | ||
| t.Fatalf("GetMigCapabilityDevicePaths() = %v, want nil on non-MIG machine", result) | ||
| } | ||
| } | ||
58 changes: 58 additions & 0 deletions
58
pkg/device-plugin/nvidiadevice/nvinternal/watch/watchers_test.go
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| Copyright 2026 The HAMi Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
| package watch | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestFiles_valid(t *testing.T) { | ||
| tmp := t.TempDir() | ||
| f := filepath.Join(tmp, "test.txt") | ||
| if err := os.WriteFile(f, []byte("x"), 0644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| w, err := Files(f) | ||
| if err != nil { | ||
| t.Fatalf("Files() error: %v", err) | ||
| } | ||
| defer w.Close() | ||
| } | ||
|
|
||
| func TestFiles_nonexistent(t *testing.T) { | ||
| _, err := Files(filepath.Join(t.TempDir(), "nonexistent")) | ||
| if err == nil { | ||
| t.Fatal("Files() expected error for nonexistent path, got nil") | ||
| } | ||
| } | ||
|
|
||
| func TestFiles_empty(t *testing.T) { | ||
| w, err := Files() | ||
| if err != nil { | ||
| t.Fatalf("Files() error on empty input: %v", err) | ||
| } | ||
| defer w.Close() | ||
| } | ||
|
|
||
| func TestSignals(t *testing.T) { | ||
| ch := Signals(os.Interrupt) | ||
| if ch == nil { | ||
| t.Fatal("Signals() returned nil channel") | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.