Split the symlink target error cases into for each condition

This commit is contained in:
Shingo Omura
2025-07-22 10:31:51 +09:00
parent a4fb1562f4
commit 31d2de59dd
2 changed files with 20 additions and 4 deletions

View File

@@ -88,8 +88,11 @@ func resolvePCIeRoot(pciBusID string) (string, error) {
// targetAbs must be /sys/devices/pci0000:01/...<intermediate PCI devices>.../0000:00:1f.0
devicePathPrefix := sysfs.devices("pci")
if !strings.HasPrefix(target, devicePathPrefix) || filepath.Base(target) != pciBusID {
return "", fmt.Errorf("invalid symlink target for PCI Bus ID %s: %s", sysBusPath, target)
if !strings.HasPrefix(target, devicePathPrefix) {
return "", fmt.Errorf("symlink target for PCI Bus ID %s is invalid: it must start with %s: %s", pciBusID, devicePathPrefix, target)
}
if filepath.Base(target) != pciBusID {
return "", fmt.Errorf("symlink target for PCI Bus ID %s is invalid: it must end with %s: %s", pciBusID, pciBusID, target)
}
// We need to extract the PCIe Root part, which is the first part of the path after /sys/devices/.

View File

@@ -17,6 +17,7 @@ limitations under the License.
package deviceattribute
import (
"fmt"
"os"
"path/filepath"
"reflect"
@@ -72,7 +73,7 @@ func TestGetPCIeRootBAttributeyPCIBusID(t *testing.T) {
expectsError: true,
expectedErrMsg: "no such file or directory",
},
"invalid symlink": {
"invalid symlink (invalid prefix)": {
mockSysfsSetup: func(t *testing.T, mockSysfs sysfsPath) {
devicePath := mockSysfs.devices(filepath.Join("invalid-pci-root", "0000:00:13.1", pciBusID))
touchFile(t, devicePath)
@@ -82,7 +83,19 @@ func TestGetPCIeRootBAttributeyPCIBusID(t *testing.T) {
address: pciBusID,
expectedAttribute: nil,
expectsError: true,
expectedErrMsg: "invalid symlink target for PCI Bus ID",
expectedErrMsg: fmt.Sprintf("symlink target for PCI Bus ID %s is invalid: it must start with", pciBusID),
},
"invalid symlink (invalid suffix)": {
mockSysfsSetup: func(t *testing.T, mockSysfs sysfsPath) {
devicePath := mockSysfs.devices(filepath.Join(pcieRoot, "0000:00:13.1", "0000:01:02.4")) // different PCI Bus ID
touchFile(t, devicePath)
busPath := mockSysfs.bus(filepath.Join("pci", "devices", pciBusID))
createSymlink(t, devicePath, busPath)
},
address: pciBusID,
expectedAttribute: nil,
expectsError: true,
expectedErrMsg: fmt.Sprintf("symlink target for PCI Bus ID %s is invalid: it must end with", pciBusID),
},
}
for name, test := range tests {