From 1bc91955307c78b0f4a010a4208b149e5e1e8422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Varl=C4=B1?= Date: Fri, 31 Jan 2025 20:20:55 +0100 Subject: [PATCH] Create `mountertest` package for mount test utils (#367) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These utils will be reused in upcoming `PodMounter` therefore moved into a separate package to make it reusable. --- By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice. Signed-off-by: Burak Varlı --- .../csimounter/csimounter_test.go | 41 ++++--------------- .../node/mounter/mountertest/mounter.go | 35 ++++++++++++++++ 2 files changed, 42 insertions(+), 34 deletions(-) create mode 100644 pkg/driver/node/mounter/mountertest/mounter.go diff --git a/cmd/aws-s3-csi-mounter/csimounter/csimounter_test.go b/cmd/aws-s3-csi-mounter/csimounter/csimounter_test.go index e3d7ba09..39c0fd93 100644 --- a/cmd/aws-s3-csi-mounter/csimounter/csimounter_test.go +++ b/cmd/aws-s3-csi-mounter/csimounter/csimounter_test.go @@ -1,16 +1,14 @@ package csimounter_test import ( - "log" - "os" "os/exec" "path/filepath" - "syscall" "testing" "github.com/google/go-cmp/cmp/cmpopts" "github.com/awslabs/aws-s3-csi-driver/cmd/aws-s3-csi-mounter/csimounter" + "github.com/awslabs/aws-s3-csi-driver/pkg/driver/node/mounter/mountertest" "github.com/awslabs/aws-s3-csi-driver/pkg/podmounter/mountoptions" "github.com/awslabs/aws-s3-csi-driver/pkg/util/testutil/assert" ) @@ -19,10 +17,10 @@ func TestRunningMountpoint(t *testing.T) { mountpointPath := filepath.Join(t.TempDir(), "mount-s3") t.Run("Passes bucket name and FUSE device as mount point", func(t *testing.T) { - dev := openDevNull(t) + dev := mountertest.OpenDevNull(t) runner := func(c *exec.Cmd) (int, error) { - assertSameFile(t, dev, c.ExtraFiles[0]) + mountertest.AssertSameFile(t, dev, c.ExtraFiles[0]) assert.Equals(t, mountpointPath, c.Path) assert.Equals(t, []string{mountpointPath, "test-bucket", "/dev/fd/3"}, c.Args[:3]) return 0, nil @@ -50,7 +48,7 @@ func TestRunningMountpoint(t *testing.T) { exitCode, err := csimounter.Run(csimounter.Options{ MountpointPath: mountpointPath, MountOptions: mountoptions.Options{ - Fd: int(openDevNull(t).Fd()), + Fd: int(mountertest.OpenDevNull(t).Fd()), BucketName: "test-bucket", }, CmdRunner: runner, @@ -70,7 +68,7 @@ func TestRunningMountpoint(t *testing.T) { exitCode, err := csimounter.Run(csimounter.Options{ MountpointPath: mountpointPath, MountOptions: mountoptions.Options{ - Fd: int(openDevNull(t).Fd()), + Fd: int(mountertest.OpenDevNull(t).Fd()), Env: env, }, CmdRunner: runner, @@ -92,7 +90,7 @@ func TestRunningMountpoint(t *testing.T) { exitCode, err := csimounter.Run(csimounter.Options{ MountpointPath: mountpointPath, MountOptions: mountoptions.Options{ - Fd: int(openDevNull(t).Fd()), + Fd: int(mountertest.OpenDevNull(t).Fd()), BucketName: "test-bucket", }, CmdRunner: runner, @@ -103,7 +101,7 @@ func TestRunningMountpoint(t *testing.T) { exitCode, err = csimounter.Run(csimounter.Options{ MountpointPath: mountpointPath, MountOptions: mountoptions.Options{ - Fd: int(openDevNull(t).Fd()), + Fd: int(mountertest.OpenDevNull(t).Fd()), BucketName: "test-bucket", Args: []string{"--foreground"}, }, @@ -124,28 +122,3 @@ func TestRunningMountpoint(t *testing.T) { assert.Equals(t, cmpopts.AnyError, err) }) } - -func openDevNull(t *testing.T) *os.File { - file, err := os.Open(os.DevNull) - assert.NoError(t, err) - t.Cleanup(func() { - err = file.Close() - if err != nil { - log.Printf("Failed to close file handle: %v\n", err) - } - }) - return file -} - -func assertSameFile(t *testing.T, want *os.File, got *os.File) { - var wantStat = &syscall.Stat_t{} - err := syscall.Fstat(int(want.Fd()), wantStat) - assert.NoError(t, err) - - var gotStat = &syscall.Stat_t{} - err = syscall.Fstat(int(got.Fd()), gotStat) - assert.NoError(t, err) - - assert.Equals(t, wantStat.Dev, gotStat.Dev) - assert.Equals(t, wantStat.Ino, gotStat.Ino) -} diff --git a/pkg/driver/node/mounter/mountertest/mounter.go b/pkg/driver/node/mounter/mountertest/mounter.go new file mode 100644 index 00000000..b90f875f --- /dev/null +++ b/pkg/driver/node/mounter/mountertest/mounter.go @@ -0,0 +1,35 @@ +package mountertest + +import ( + "os" + "syscall" + "testing" + + "github.com/awslabs/aws-s3-csi-driver/pkg/util/testutil/assert" +) + +// OpenDevNull opens `/dev/null` and returns the file handle. +func OpenDevNull(t *testing.T) *os.File { + file, err := os.Open(os.DevNull) + assert.NoError(t, err) + t.Cleanup(func() { + _ = file.Close() + }) + return file +} + +// AssertSameFile checks if given file handles points to the same underlying file description. +func AssertSameFile(t *testing.T, want *os.File, got *os.File) { + t.Helper() + + var wantStat = &syscall.Stat_t{} + err := syscall.Fstat(int(want.Fd()), wantStat) + assert.NoError(t, err) + + var gotStat = &syscall.Stat_t{} + err = syscall.Fstat(int(got.Fd()), gotStat) + assert.NoError(t, err) + + assert.Equals(t, wantStat.Dev, gotStat.Dev) + assert.Equals(t, wantStat.Ino, gotStat.Ino) +}