Skip to content

Commit

Permalink
fix isLikelyNotMountPointStatx relative path issue
Browse files Browse the repository at this point in the history
Kubernetes-commit: 80964efae9d2786f153ccc055d95fc3476778caa
  • Loading branch information
umagnus authored and k8s-publishing-bot committed Oct 23, 2024
1 parent 9983dfa commit c50f65c
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mount_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ func (*Mounter) List() ([]MountPoint, error) {

func statx(file string) (unix.Statx_t, error) {
var stat unix.Statx_t
if err := unix.Statx(0, file, unix.AT_STATX_DONT_SYNC, 0, &stat); err != nil {
if err := unix.Statx(unix.AT_FDCWD, file, unix.AT_STATX_DONT_SYNC, 0, &stat); err != nil {
if err == unix.ENOSYS {
return stat, errStatxNotSupport
}
Expand Down
115 changes: 115 additions & 0 deletions mount_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"reflect"
"sort"
"strings"
Expand All @@ -36,6 +37,7 @@ import (
"golang.org/x/sys/unix"
utilexec "k8s.io/utils/exec"
testexec "k8s.io/utils/exec/testing"
"k8s.io/utils/ptr"
)

func TestReadProcMountsFrom(t *testing.T) {
Expand Down Expand Up @@ -874,3 +876,116 @@ func makeFakeCommandAction(stdout string, err error, cmdFn func()) testexec.Fake
return testexec.InitFakeCmd(&c, cmd, args...)
}
}

func TestIsLikelyNotMountPoint(t *testing.T) {
mounter := Mounter{"fake/path", ptr.To(true), true, true}

tests := []struct {
fileName string
targetLinkName string
setUp func(base, fileName, targetLinkName string) error
cleanUp func(base, fileName, targetLinkName string) error
expectedResult bool
expectError bool
}{
{
"Dir",
"",
func(base, fileName, targetLinkName string) error {
return os.Mkdir(filepath.Join(base, fileName), 0o750)
},
func(base, fileName, targetLinkName string) error {
return os.Remove(filepath.Join(base, fileName))
},
true,
false,
},
{
"InvalidDir",
"",
func(base, fileName, targetLinkName string) error {
return nil
},
func(base, fileName, targetLinkName string) error {
return nil
},
true,
true,
},
{
"ValidSymLink",
"targetSymLink",
func(base, fileName, targetLinkName string) error {
targeLinkPath := filepath.Join(base, targetLinkName)
if err := os.Mkdir(targeLinkPath, 0o750); err != nil {
return err
}

filePath := filepath.Join(base, fileName)
if err := os.Symlink(targeLinkPath, filePath); err != nil {
return err
}
return nil
},
func(base, fileName, targetLinkName string) error {
if err := os.Remove(filepath.Join(base, fileName)); err != nil {
return err
}
return os.Remove(filepath.Join(base, targetLinkName))
},
true,
false,
},
{
"InvalidSymLink",
"targetSymLink2",
func(base, fileName, targetLinkName string) error {
targeLinkPath := filepath.Join(base, targetLinkName)
if err := os.Mkdir(targeLinkPath, 0o750); err != nil {
return err
}

filePath := filepath.Join(base, fileName)
if err := os.Symlink(targeLinkPath, filePath); err != nil {
return err
}
return os.Remove(targeLinkPath)
},
func(base, fileName, targetLinkName string) error {
return os.Remove(filepath.Join(base, fileName))
},
true,
true,
},
}

for _, test := range tests {
// test with absolute and relative path
baseList := []string{t.TempDir(), "./"}
for _, base := range baseList {
if err := test.setUp(base, test.fileName, test.targetLinkName); err != nil {
t.Fatalf("unexpected error in setUp(%s, %s): %v", test.fileName, test.targetLinkName, err)
}

filePath := filepath.Join(base, test.fileName)
result, err := mounter.IsLikelyNotMountPoint(filePath)
if result != test.expectedResult {
t.Errorf("Expect result not equal with IsLikelyNotMountPoint(%s) return: %t, expected: %t", filePath, result, test.expectedResult)
}

if base == "./" {
if err := test.cleanUp(base, test.fileName, test.targetLinkName); err != nil {
t.Fatalf("unexpected error in cleanUp(%s, %s): %v", test.fileName, test.targetLinkName, err)
}
}

if (err != nil) != test.expectError {
if test.expectError {
t.Errorf("Expect error during IsLikelyNotMountPoint(%s)", filePath)
} else {
t.Errorf("Expect error is nil during IsLikelyNotMountPoint(%s): %v", filePath, err)
}
}
}
}
}

0 comments on commit c50f65c

Please sign in to comment.