|
| 1 | +/* |
| 2 | + * Copyright (c) NVIDIA CORPORATION. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package device |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + |
| 24 | + "github.com/NVIDIA/go-nvml/pkg/nvml" |
| 25 | + "github.com/NVIDIA/go-nvml/pkg/nvml/mock" |
| 26 | +) |
| 27 | + |
| 28 | +func pciInfoWithBusID(busID string) nvml.PciInfo { |
| 29 | + var info nvml.PciInfo |
| 30 | + for i := 0; i < len(busID) && i < len(info.BusId); i++ { |
| 31 | + info.BusId[i] = uint8(busID[i]) |
| 32 | + } |
| 33 | + return info |
| 34 | +} |
| 35 | + |
| 36 | +func deviceWithPCIBusID(busID string) *device { |
| 37 | + return &device{ |
| 38 | + Device: &mock.Device{ |
| 39 | + GetPciInfoFunc: func() (nvml.PciInfo, nvml.Return) { |
| 40 | + return pciInfoWithBusID(busID), nvml.SUCCESS |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestGetPCIBusID(t *testing.T) { |
| 47 | + testCases := []struct { |
| 48 | + name string |
| 49 | + busIDFromNVML string |
| 50 | + expected string |
| 51 | + }{ |
| 52 | + { |
| 53 | + // Typical legacy NVML 4-digit domain: must not strip "0000". |
| 54 | + name: "four_digit_legacy_domain", |
| 55 | + busIDFromNVML: "0000:0A:00.0", |
| 56 | + expected: "0000:0a:00.0", |
| 57 | + }, |
| 58 | + { |
| 59 | + // Non-zero 4-digit domain: must not trim. |
| 60 | + name: "nonzero_four_digit_domain", |
| 61 | + busIDFromNVML: "0001:03:00.0", |
| 62 | + expected: "0001:03:00.0", |
| 63 | + }, |
| 64 | + { |
| 65 | + // 8-digit domain 00000000: trim prefix "0000". |
| 66 | + name: "eight_digit_domain_padded_with_zeros", |
| 67 | + busIDFromNVML: "00000000:0a:00.0", |
| 68 | + expected: "0000:0a:00.0", |
| 69 | + }, |
| 70 | + { |
| 71 | + // 8-digit domain does not match padded "0000xxxx": leave unchanged. |
| 72 | + name: "eight_digit_domain_id", |
| 73 | + busIDFromNVML: "0001ABCD:03:00.0", |
| 74 | + expected: "0001abcd:03:00.0", |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + for _, tc := range testCases { |
| 79 | + t.Run(tc.name, func(t *testing.T) { |
| 80 | + got, err := deviceWithPCIBusID(tc.busIDFromNVML).GetPCIBusID() |
| 81 | + require.NoError(t, err) |
| 82 | + require.Equal(t, tc.expected, got) |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
0 commit comments