Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/gpu_plugin/gpu_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ func packedPolicy(req *pluginapi.ContainerPreferredAllocationRequest) []string {
}

func validatePCIDeviceIDs(pciIDList string) error {
if pciIDList == "" {
return nil
}

r := regexp.MustCompile(`^0x[0-9a-f]{4}$`)

for id := range strings.SplitSeq(pciIDList, ",") {
Expand Down
50 changes: 49 additions & 1 deletion cmd/gpu_plugin/gpu_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ func TestParsePCIDeviceIDs(t *testing.T) {
{
name: "empty string",
input: "",
wantError: true,
wantError: false,
},
{
name: "invalid ID format",
Expand Down Expand Up @@ -1193,3 +1193,51 @@ func TestParsePCIDeviceIDs(t *testing.T) {
})
}
}

func TestCheckAllowDenyOptions(t *testing.T) {
tcases := []struct {
name string
options cliOptions
expectReturn bool
}{
{
name: "valid allow IDs",
options: cliOptions{allowIDs: "0x1234,0x5678"},
expectReturn: true,
},
{
name: "valid deny IDs",
options: cliOptions{denyIDs: "0x1234,0x5678"},
expectReturn: true,
},
{
name: "both allow and deny IDs",
options: cliOptions{allowIDs: "0x1234", denyIDs: "0x5678"},
expectReturn: false,
},
{
name: "invalid allow ID format",
options: cliOptions{allowIDs: "0x1234,abcd"},
expectReturn: false,
},
{
name: "invalid deny ID format",
options: cliOptions{denyIDs: "0x1234,abcd"},
expectReturn: false,
},
{
name: "both empty",
options: cliOptions{allowIDs: "", denyIDs: ""},
expectReturn: true,
},
}

for _, tc := range tcases {
t.Run(tc.name, func(t *testing.T) {
ret := checkAllowDenyOptions(tc.options)
if ret != tc.expectReturn {
t.Errorf("checkAllowDenyOptions() return = %v, expected %v", ret, tc.expectReturn)
}
})
}
}