mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-07-22 17:51:28 +00:00
Split the symlink target error cases into for each condition
This commit is contained in:
@@ -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/.
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user