mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-07-24 03:40:56 +00:00
Make test only helpers private (sysfspath methods, touchFile, createSymlink)
Signed-off-by: Shingo Omura <everpeace@gmail.com>
This commit is contained in:
@@ -49,7 +49,7 @@ func GetPCIeRootAttributeByPCIBusID(pciBusID string) (DeviceAttribute, error) {
|
||||
return DeviceAttribute{}, fmt.Errorf("failed to resolve sysfs path for PCI Bus ID %s: %w", pciBusID, err)
|
||||
}
|
||||
|
||||
pciRootPart := strings.Split(strings.TrimPrefix(sysDevicesPath, sysfs.Devices("")+"/"), "/")[0]
|
||||
pciRootPart := strings.Split(strings.TrimPrefix(sysDevicesPath, sysfs.devices("")+"/"), "/")[0]
|
||||
|
||||
return DeviceAttribute{
|
||||
Name: StandardDeviceAttributePCIeRoot,
|
||||
@@ -73,7 +73,7 @@ func GetPCIeRootAttributeByPCIBusID(pciBusID string) (DeviceAttribute, error) {
|
||||
// /sys/devices/pci0000:01/...<intermediate PCI devices>.../0000:00:1f.0,
|
||||
func resolveSysDevicesPath(pciBusID string) (string, error) {
|
||||
// e.g. /sys/bus/pci/devices/0000:00:1f.0
|
||||
sysBusPath := sysfs.Bus(filepath.Join("pci", "devices", pciBusID))
|
||||
sysBusPath := sysfs.bus(filepath.Join("pci", "devices", pciBusID))
|
||||
|
||||
targetRelative, err := os.Readlink(sysBusPath)
|
||||
if err != nil {
|
||||
@@ -88,7 +88,7 @@ func resolveSysDevicesPath(pciBusID string) (string, error) {
|
||||
}
|
||||
|
||||
// targetAbs must be /sys/devices/pci0000:01/...<intermediate PCI devices>.../0000:00:1f.0
|
||||
devicePathPrefix := sysfs.Devices("pci")
|
||||
devicePathPrefix := sysfs.devices("pci")
|
||||
if !strings.HasPrefix(targetAbs, devicePathPrefix) || filepath.Base(targetAbs) != pciBusID {
|
||||
return "", fmt.Errorf("invalid symlink target for PCI Bus ID %s: %s", sysBusPath, targetAbs)
|
||||
}
|
||||
|
||||
@@ -45,10 +45,10 @@ func TestGetPCIeRootBAttributeyPCIBusID(t *testing.T) {
|
||||
}{
|
||||
"valid": {
|
||||
mockSysfsSetup: func(t *testing.T, mockSysfs sysfsPath) {
|
||||
devicePath := mockSysfs.Devices(filepath.Join(pcieRoot, "0000:00:13.1", pciBusID))
|
||||
TouchFile(t, devicePath)
|
||||
busPath := mockSysfs.Bus(filepath.Join("pci", "devices", pciBusID))
|
||||
CreateSymlink(t, devicePath, busPath)
|
||||
devicePath := mockSysfs.devices(filepath.Join(pcieRoot, "0000:00:13.1", pciBusID))
|
||||
touchFile(t, devicePath)
|
||||
busPath := mockSysfs.bus(filepath.Join("pci", "devices", pciBusID))
|
||||
createSymlink(t, devicePath, busPath)
|
||||
},
|
||||
address: pciBusID,
|
||||
expectedAttribute: &expectedAttribute,
|
||||
@@ -74,10 +74,10 @@ func TestGetPCIeRootBAttributeyPCIBusID(t *testing.T) {
|
||||
},
|
||||
"invalid symlink": {
|
||||
mockSysfsSetup: func(t *testing.T, mockSysfs sysfsPath) {
|
||||
devicePath := mockSysfs.Devices(filepath.Join("invalid-pci-root", "0000:00:13.1", pciBusID))
|
||||
TouchFile(t, devicePath)
|
||||
busPath := mockSysfs.Bus(filepath.Join("pci", "devices", pciBusID))
|
||||
CreateSymlink(t, devicePath, busPath)
|
||||
devicePath := mockSysfs.devices(filepath.Join("invalid-pci-root", "0000:00:13.1", pciBusID))
|
||||
touchFile(t, devicePath)
|
||||
busPath := mockSysfs.bus(filepath.Join("pci", "devices", pciBusID))
|
||||
createSymlink(t, devicePath, busPath)
|
||||
},
|
||||
address: pciBusID,
|
||||
expectedAttribute: nil,
|
||||
@@ -122,7 +122,7 @@ func TestGetPCIeRootBAttributeyPCIBusID(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TouchFile(t *testing.T, path string) {
|
||||
func touchFile(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
||||
t.Fatalf("Failed to create directory %s: %v", filepath.Dir(path), err)
|
||||
@@ -132,7 +132,7 @@ func TouchFile(t *testing.T, path string) {
|
||||
}
|
||||
}
|
||||
|
||||
func CreateSymlink(t *testing.T, target, link string) {
|
||||
func createSymlink(t *testing.T, target, link string) {
|
||||
t.Helper()
|
||||
if err := os.MkdirAll(filepath.Dir(link), 0755); err != nil {
|
||||
t.Fatalf("Failed to create directory for symlink %s: %v", filepath.Dir(link), err)
|
||||
|
||||
@@ -31,33 +31,33 @@ var (
|
||||
// and can be replaced with a mock in tests.
|
||||
type sysfsPath string
|
||||
|
||||
func (s sysfsPath) Devices(path string) string {
|
||||
func (s sysfsPath) devices(path string) string {
|
||||
return filepath.Join(string(s), "devices", path)
|
||||
}
|
||||
|
||||
func (s sysfsPath) Bus(path string) string {
|
||||
func (s sysfsPath) bus(path string) string {
|
||||
return filepath.Join(string(s), "bus", path)
|
||||
}
|
||||
|
||||
func (s sysfsPath) Block(path string) string {
|
||||
func (s sysfsPath) block(path string) string {
|
||||
return filepath.Join(string(s), "block", path)
|
||||
}
|
||||
|
||||
func (s sysfsPath) Class(path string) string {
|
||||
func (s sysfsPath) class(path string) string {
|
||||
return filepath.Join(string(s), "class", path)
|
||||
}
|
||||
|
||||
func (s sysfsPath) Dev(path string) string {
|
||||
func (s sysfsPath) dev(path string) string {
|
||||
return filepath.Join(string(s), "dev", path)
|
||||
}
|
||||
|
||||
func (s sysfsPath) Firmware(path string) string {
|
||||
func (s sysfsPath) firmware(path string) string {
|
||||
return filepath.Join(string(s), "firmware", path)
|
||||
}
|
||||
func (s sysfsPath) Kernel(path string) string {
|
||||
func (s sysfsPath) kernel(path string) string {
|
||||
return filepath.Join(string(s), "kernel", path)
|
||||
}
|
||||
|
||||
func (s sysfsPath) Module(path string) string {
|
||||
func (s sysfsPath) module(path string) string {
|
||||
return filepath.Join(string(s), "module", path)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user