Skip to content

Commit 48e4344

Browse files
committed
fix isLikelyNotMountPointStatx relative path issue
1 parent 5ffb052 commit 48e4344

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

staging/src/k8s.io/mount-utils/mount_linux.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ func (*Mounter) List() ([]MountPoint, error) {
439439

440440
func statx(file string) (unix.Statx_t, error) {
441441
var stat unix.Statx_t
442-
if err := unix.Statx(0, file, unix.AT_STATX_DONT_SYNC, 0, &stat); err != nil {
442+
if err := unix.Statx(unix.AT_FDCWD, file, unix.AT_STATX_DONT_SYNC, 0, &stat); err != nil {
443443
if err == unix.ENOSYS {
444444
return stat, errStatxNotSupport
445445
}

staging/src/k8s.io/mount-utils/mount_linux_test.go

+115
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"fmt"
2525
"os"
2626
"os/exec"
27+
"path/filepath"
2728
"reflect"
2829
"sort"
2930
"strings"
@@ -36,6 +37,7 @@ import (
3637
"golang.org/x/sys/unix"
3738
utilexec "k8s.io/utils/exec"
3839
testexec "k8s.io/utils/exec/testing"
40+
"k8s.io/utils/ptr"
3941
)
4042

4143
func TestReadProcMountsFrom(t *testing.T) {
@@ -874,3 +876,116 @@ func makeFakeCommandAction(stdout string, err error, cmdFn func()) testexec.Fake
874876
return testexec.InitFakeCmd(&c, cmd, args...)
875877
}
876878
}
879+
880+
func TestIsLikelyNotMountPoint(t *testing.T) {
881+
mounter := Mounter{"fake/path", ptr.To(true), true, true}
882+
883+
tests := []struct {
884+
fileName string
885+
targetLinkName string
886+
setUp func(base, fileName, targetLinkName string) error
887+
cleanUp func(base, fileName, targetLinkName string) error
888+
expectedResult bool
889+
expectError bool
890+
}{
891+
{
892+
"Dir",
893+
"",
894+
func(base, fileName, targetLinkName string) error {
895+
return os.Mkdir(filepath.Join(base, fileName), 0o750)
896+
},
897+
func(base, fileName, targetLinkName string) error {
898+
return os.Remove(filepath.Join(base, fileName))
899+
},
900+
true,
901+
false,
902+
},
903+
{
904+
"InvalidDir",
905+
"",
906+
func(base, fileName, targetLinkName string) error {
907+
return nil
908+
},
909+
func(base, fileName, targetLinkName string) error {
910+
return nil
911+
},
912+
true,
913+
true,
914+
},
915+
{
916+
"ValidSymLink",
917+
"targetSymLink",
918+
func(base, fileName, targetLinkName string) error {
919+
targeLinkPath := filepath.Join(base, targetLinkName)
920+
if err := os.Mkdir(targeLinkPath, 0o750); err != nil {
921+
return err
922+
}
923+
924+
filePath := filepath.Join(base, fileName)
925+
if err := os.Symlink(targeLinkPath, filePath); err != nil {
926+
return err
927+
}
928+
return nil
929+
},
930+
func(base, fileName, targetLinkName string) error {
931+
if err := os.Remove(filepath.Join(base, fileName)); err != nil {
932+
return err
933+
}
934+
return os.Remove(filepath.Join(base, targetLinkName))
935+
},
936+
true,
937+
false,
938+
},
939+
{
940+
"InvalidSymLink",
941+
"targetSymLink2",
942+
func(base, fileName, targetLinkName string) error {
943+
targeLinkPath := filepath.Join(base, targetLinkName)
944+
if err := os.Mkdir(targeLinkPath, 0o750); err != nil {
945+
return err
946+
}
947+
948+
filePath := filepath.Join(base, fileName)
949+
if err := os.Symlink(targeLinkPath, filePath); err != nil {
950+
return err
951+
}
952+
return os.Remove(targeLinkPath)
953+
},
954+
func(base, fileName, targetLinkName string) error {
955+
return os.Remove(filepath.Join(base, fileName))
956+
},
957+
true,
958+
true,
959+
},
960+
}
961+
962+
for _, test := range tests {
963+
// test with absolute and relative path
964+
baseList := []string{t.TempDir(), "./"}
965+
for _, base := range baseList {
966+
if err := test.setUp(base, test.fileName, test.targetLinkName); err != nil {
967+
t.Fatalf("unexpected error in setUp(%s, %s): %v", test.fileName, test.targetLinkName, err)
968+
}
969+
970+
filePath := filepath.Join(base, test.fileName)
971+
result, err := mounter.IsLikelyNotMountPoint(filePath)
972+
if result != test.expectedResult {
973+
t.Errorf("Expect result not equal with IsLikelyNotMountPoint(%s) return: %t, expected: %t", filePath, result, test.expectedResult)
974+
}
975+
976+
if base == "./" {
977+
if err := test.cleanUp(base, test.fileName, test.targetLinkName); err != nil {
978+
t.Fatalf("unexpected error in cleanUp(%s, %s): %v", test.fileName, test.targetLinkName, err)
979+
}
980+
}
981+
982+
if (err != nil) != test.expectError {
983+
if test.expectError {
984+
t.Errorf("Expect error during IsLikelyNotMountPoint(%s)", filePath)
985+
} else {
986+
t.Errorf("Expect error is nil during IsLikelyNotMountPoint(%s): %v", filePath, err)
987+
}
988+
}
989+
}
990+
}
991+
}

0 commit comments

Comments
 (0)