From 2bc45ca466625797ff1346b5ebf2b2f7f4f06e92 Mon Sep 17 00:00:00 2001 From: Shingo Omura Date: Mon, 30 Jun 2025 16:09:41 +0900 Subject: [PATCH] Make test only helpers private (sysfspath methods, touchFile, createSymlink) Signed-off-by: Shingo Omura --- .../deviceattribute/pci_linux.go | 6 +++--- .../deviceattribute/pci_linux_test.go | 20 +++++++++---------- .../deviceattribute/sysfs_linux.go | 16 +++++++-------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/pci_linux.go b/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/pci_linux.go index c37feae2f7f..4d4bddd2611 100644 --- a/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/pci_linux.go +++ b/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/pci_linux.go @@ -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/....../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/....../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) } diff --git a/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/pci_linux_test.go b/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/pci_linux_test.go index 866b918525d..41bc9dfa658 100644 --- a/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/pci_linux_test.go +++ b/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/pci_linux_test.go @@ -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) diff --git a/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/sysfs_linux.go b/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/sysfs_linux.go index 0c81f72998d..89053276a38 100644 --- a/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/sysfs_linux.go +++ b/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/sysfs_linux.go @@ -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) }