diff --git a/go.sum b/go.sum index a515ab4..1acb589 100644 --- a/go.sum +++ b/go.sum @@ -3,7 +3,6 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1 github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= -github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= @@ -21,16 +20,12 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= -github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8= golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY= -golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/mount_linux.go b/mount_linux.go index abc7715..439d505 100644 --- a/mount_linux.go +++ b/mount_linux.go @@ -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 } diff --git a/mount_linux_test.go b/mount_linux_test.go index 3c0842b..b77d072 100644 --- a/mount_linux_test.go +++ b/mount_linux_test.go @@ -24,6 +24,7 @@ import ( "fmt" "os" "os/exec" + "path/filepath" "reflect" "sort" "strings" @@ -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) { @@ -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) + } + } + } + } +}