From db7f1d94ff4706636ad4a82310586993938bb342 Mon Sep 17 00:00:00 2001 From: shinigami-777 Date: Tue, 4 Aug 2026 19:17:00 +0530 Subject: [PATCH 1/2] test: improve test coverage for pkg/device-plugin/nvidiadevice/nvinternal/cdi Signed-off-by: shinigami-777 --- .../nvidiadevice/nvinternal/cdi/cdi_test.go | 61 ++++++++ .../nvidiadevice/nvinternal/cdi/imex_test.go | 75 +++++++++ .../nvidiadevice/nvinternal/cdi/null_test.go | 48 ++++++ .../nvinternal/cdi/options_test.go | 146 ++++++++++++++++++ 4 files changed, 330 insertions(+) create mode 100644 pkg/device-plugin/nvidiadevice/nvinternal/cdi/cdi_test.go create mode 100644 pkg/device-plugin/nvidiadevice/nvinternal/cdi/imex_test.go create mode 100644 pkg/device-plugin/nvidiadevice/nvinternal/cdi/null_test.go create mode 100644 pkg/device-plugin/nvidiadevice/nvinternal/cdi/options_test.go diff --git a/pkg/device-plugin/nvidiadevice/nvinternal/cdi/cdi_test.go b/pkg/device-plugin/nvidiadevice/nvinternal/cdi/cdi_test.go new file mode 100644 index 0000000000..8e9b08d8da --- /dev/null +++ b/pkg/device-plugin/nvidiadevice/nvinternal/cdi/cdi_test.go @@ -0,0 +1,61 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The HAMi Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to NVIDIA CORPORATION under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. NVIDIA CORPORATION licenses this file to you 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. + */ + +/* + * Modifications Copyright The HAMi Authors. See + * GitHub history for details. + */ + +package cdi + +import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi" +) + +func TestQualifiedName(t *testing.T) { + handler := &cdiHandler{ + vendor: "nvidia.com", + } + name := handler.QualifiedName("gpu", "0") + require.Equal(t, "nvidia.com/gpu=0", name) +} + +func TestAdditionalDevices(t *testing.T) { + handler := &cdiHandler{ + vendor: "nvidia.com", + additionalModes: []string{"gdrcopy", "gds"}, + cdilibs: map[string]nvcdi.SpecGenerator{ + "gdrcopy": &imexChannelCDILib{}, + }, + } + devices := handler.AdditionalDevices() + require.Len(t, devices, 1) + require.Equal(t, "nvidia.com/gdrcopy=all", devices[0]) +} diff --git a/pkg/device-plugin/nvidiadevice/nvinternal/cdi/imex_test.go b/pkg/device-plugin/nvidiadevice/nvinternal/cdi/imex_test.go new file mode 100644 index 0000000000..4472fd797f --- /dev/null +++ b/pkg/device-plugin/nvidiadevice/nvinternal/cdi/imex_test.go @@ -0,0 +1,75 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The HAMi Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to NVIDIA CORPORATION under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. NVIDIA CORPORATION licenses this file to you 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. + */ + +/* + * Modifications Copyright The HAMi Authors. See + * GitHub history for details. + */ + +package cdi + +import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/Project-HAMi/HAMi/pkg/device-plugin/nvidiadevice/nvinternal/imex" +) + +func TestImexChannelCDILib_GetSpec(t *testing.T) { + channels := imex.Channels{ + {ID: "channel1", Path: "/dev/imex/channel1", HostPath: "/dev/imex/channel1"}, + {ID: "channel2", Path: "/dev/imex/channel2", HostPath: "/dev/imex/channel2"}, + } + + lib := &imexChannelCDILib{ + vendor: "nvidia.com", + imexChannels: channels, + } + + spec, err := lib.GetSpec() + require.NoError(t, err) + require.NotNil(t, spec) + + raw := spec.Raw() + require.Len(t, raw.Devices, 2) + + found1 := false + found2 := false + for _, dev := range raw.Devices { + if dev.Name == "channel1" { + found1 = true + require.Len(t, dev.ContainerEdits.DeviceNodes, 1) + require.Equal(t, "/dev/imex/channel1", dev.ContainerEdits.DeviceNodes[0].Path) + } else if dev.Name == "channel2" { + found2 = true + require.Len(t, dev.ContainerEdits.DeviceNodes, 1) + require.Equal(t, "/dev/imex/channel2", dev.ContainerEdits.DeviceNodes[0].Path) + } + } + require.True(t, found1) + require.True(t, found2) +} diff --git a/pkg/device-plugin/nvidiadevice/nvinternal/cdi/null_test.go b/pkg/device-plugin/nvidiadevice/nvinternal/cdi/null_test.go new file mode 100644 index 0000000000..dae1407cd2 --- /dev/null +++ b/pkg/device-plugin/nvidiadevice/nvinternal/cdi/null_test.go @@ -0,0 +1,48 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The HAMi Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to NVIDIA CORPORATION under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. NVIDIA CORPORATION licenses this file to you 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. + */ + +/* + * Modifications Copyright The HAMi Authors. See + * GitHub history for details. + */ + +package cdi + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestNullHandler(t *testing.T) { + handler := NewNullHandler() + require.NotNil(t, handler) + + require.Nil(t, handler.AdditionalDevices()) + require.NoError(t, handler.CreateSpecFile()) + require.Equal(t, "", handler.QualifiedName("class", "id")) +} diff --git a/pkg/device-plugin/nvidiadevice/nvinternal/cdi/options_test.go b/pkg/device-plugin/nvidiadevice/nvinternal/cdi/options_test.go new file mode 100644 index 0000000000..fa55da7218 --- /dev/null +++ b/pkg/device-plugin/nvidiadevice/nvinternal/cdi/options_test.go @@ -0,0 +1,146 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The HAMi Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to NVIDIA CORPORATION under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. NVIDIA CORPORATION licenses this file to you 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. + */ + +/* + * Modifications Copyright The HAMi Authors. See + * GitHub history for details. + */ + +package cdi + +import ( + "testing" + + "github.com/stretchr/testify/require" + + spec "github.com/NVIDIA/k8s-device-plugin/api/config/v1" + "github.com/Project-HAMi/HAMi/pkg/device-plugin/nvidiadevice/nvinternal/imex" +) + +func TestOptions(t *testing.T) { + testCases := []struct { + name string + option Option + validate func(*testing.T, *cdiHandler) + }{ + { + name: "WithDeviceListStrategies", + option: WithDeviceListStrategies(spec.DeviceListStrategies{"cdi": true}), + validate: func(t *testing.T, c *cdiHandler) { + require.Equal(t, spec.DeviceListStrategies{"cdi": true}, c.deviceListStrategies) + }, + }, + { + name: "WithDriverRoot", + option: WithDriverRoot("/driver-root"), + validate: func(t *testing.T, c *cdiHandler) { + require.Equal(t, "/driver-root", c.driverRoot) + }, + }, + { + name: "WithDevRoot", + option: WithDevRoot("/dev-root"), + validate: func(t *testing.T, c *cdiHandler) { + require.Equal(t, "/dev-root", c.devRoot) + }, + }, + { + name: "WithTargetDriverRoot", + option: WithTargetDriverRoot("/target-driver-root"), + validate: func(t *testing.T, c *cdiHandler) { + require.Equal(t, "/target-driver-root", c.targetDriverRoot) + }, + }, + { + name: "WithTargetDevRoot", + option: WithTargetDevRoot("/target-dev-root"), + validate: func(t *testing.T, c *cdiHandler) { + require.Equal(t, "/target-dev-root", c.targetDevRoot) + }, + }, + { + name: "WithNvidiaCTKPath", + option: WithNvidiaCTKPath("/nvidia-ctk"), + validate: func(t *testing.T, c *cdiHandler) { + require.Equal(t, "/nvidia-ctk", c.nvidiaCTKPath) + }, + }, + { + name: "WithDeviceIDStrategy", + option: WithDeviceIDStrategy("uuid"), + validate: func(t *testing.T, c *cdiHandler) { + require.Equal(t, "uuid", c.deviceIDStrategy) + }, + }, + { + name: "WithVendor", + option: WithVendor("nvidia.com"), + validate: func(t *testing.T, c *cdiHandler) { + require.Equal(t, "nvidia.com", c.vendor) + }, + }, + { + name: "WithGdrcopyEnabled", + option: WithGdrcopyEnabled(true), + validate: func(t *testing.T, c *cdiHandler) { + require.True(t, c.gdrcopyEnabled) + }, + }, + { + name: "WithGdsEnabled", + option: WithGdsEnabled(true), + validate: func(t *testing.T, c *cdiHandler) { + require.True(t, c.gdsEnabled) + }, + }, + { + name: "WithMofedEnabled", + option: WithMofedEnabled(true), + validate: func(t *testing.T, c *cdiHandler) { + require.True(t, c.mofedEnabled) + }, + }, + { + name: "WithImexChannels", + option: WithImexChannels(imex.Channels{ + {ID: "channel1", Path: "/path1"}, + }), + validate: func(t *testing.T, c *cdiHandler) { + require.Len(t, c.imexChannels, 1) + require.Equal(t, "channel1", c.imexChannels[0].ID) + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + handler := &cdiHandler{} + tc.option(handler) + tc.validate(t, handler) + }) + } +} From 12b5ca52c3b9b8a9a37ce0d9e1d89a73782a5784 Mon Sep 17 00:00:00 2001 From: shinigami-777 Date: Tue, 4 Aug 2026 19:55:35 +0530 Subject: [PATCH 2/2] test: ran goimports on pkg/device-plugin/nvidiadevice/nvinternal/cdi test files Signed-off-by: shinigami-777 --- pkg/device-plugin/nvidiadevice/nvinternal/cdi/cdi_test.go | 4 ++-- pkg/device-plugin/nvidiadevice/nvinternal/cdi/imex_test.go | 3 ++- pkg/device-plugin/nvidiadevice/nvinternal/cdi/options_test.go | 1 + 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/device-plugin/nvidiadevice/nvinternal/cdi/cdi_test.go b/pkg/device-plugin/nvidiadevice/nvinternal/cdi/cdi_test.go index 8e9b08d8da..219aac120c 100644 --- a/pkg/device-plugin/nvidiadevice/nvinternal/cdi/cdi_test.go +++ b/pkg/device-plugin/nvidiadevice/nvinternal/cdi/cdi_test.go @@ -35,8 +35,8 @@ package cdi import ( "testing" - "github.com/stretchr/testify/require" "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi" + "github.com/stretchr/testify/require" ) func TestQualifiedName(t *testing.T) { @@ -49,7 +49,7 @@ func TestQualifiedName(t *testing.T) { func TestAdditionalDevices(t *testing.T) { handler := &cdiHandler{ - vendor: "nvidia.com", + vendor: "nvidia.com", additionalModes: []string{"gdrcopy", "gds"}, cdilibs: map[string]nvcdi.SpecGenerator{ "gdrcopy": &imexChannelCDILib{}, diff --git a/pkg/device-plugin/nvidiadevice/nvinternal/cdi/imex_test.go b/pkg/device-plugin/nvidiadevice/nvinternal/cdi/imex_test.go index 4472fd797f..5a40025281 100644 --- a/pkg/device-plugin/nvidiadevice/nvinternal/cdi/imex_test.go +++ b/pkg/device-plugin/nvidiadevice/nvinternal/cdi/imex_test.go @@ -36,6 +36,7 @@ import ( "testing" "github.com/stretchr/testify/require" + "github.com/Project-HAMi/HAMi/pkg/device-plugin/nvidiadevice/nvinternal/imex" ) @@ -56,7 +57,7 @@ func TestImexChannelCDILib_GetSpec(t *testing.T) { raw := spec.Raw() require.Len(t, raw.Devices, 2) - + found1 := false found2 := false for _, dev := range raw.Devices { diff --git a/pkg/device-plugin/nvidiadevice/nvinternal/cdi/options_test.go b/pkg/device-plugin/nvidiadevice/nvinternal/cdi/options_test.go index fa55da7218..9cd8ba4a1c 100644 --- a/pkg/device-plugin/nvidiadevice/nvinternal/cdi/options_test.go +++ b/pkg/device-plugin/nvidiadevice/nvinternal/cdi/options_test.go @@ -38,6 +38,7 @@ import ( "github.com/stretchr/testify/require" spec "github.com/NVIDIA/k8s-device-plugin/api/config/v1" + "github.com/Project-HAMi/HAMi/pkg/device-plugin/nvidiadevice/nvinternal/imex" )