Skip to content

Commit 7584d55

Browse files
committed
Create mountertest package for mount test utils
Signed-off-by: Burak Varlı <[email protected]>
1 parent 8adcb20 commit 7584d55

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed

cmd/aws-s3-csi-mounter/csimounter/csimounter_test.go

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package csimounter_test
22

33
import (
4-
"log"
5-
"os"
64
"os/exec"
75
"path/filepath"
8-
"syscall"
96
"testing"
107

118
"github.com/google/go-cmp/cmp/cmpopts"
129

1310
"github.com/awslabs/aws-s3-csi-driver/cmd/aws-s3-csi-mounter/csimounter"
11+
"github.com/awslabs/aws-s3-csi-driver/pkg/driver/node/mounter/mountertest"
1412
"github.com/awslabs/aws-s3-csi-driver/pkg/podmounter/mountoptions"
1513
"github.com/awslabs/aws-s3-csi-driver/pkg/util/testutil/assert"
1614
)
@@ -19,10 +17,10 @@ func TestRunningMountpoint(t *testing.T) {
1917
mountpointPath := filepath.Join(t.TempDir(), "mount-s3")
2018

2119
t.Run("Passes bucket name and FUSE device as mount point", func(t *testing.T) {
22-
dev := openDevNull(t)
20+
dev := mountertest.OpenDevNull(t)
2321

2422
runner := func(c *exec.Cmd) (int, error) {
25-
assertSameFile(t, dev, c.ExtraFiles[0])
23+
mountertest.AssertSameFile(t, dev, c.ExtraFiles[0])
2624
assert.Equals(t, mountpointPath, c.Path)
2725
assert.Equals(t, []string{mountpointPath, "test-bucket", "/dev/fd/3"}, c.Args[:3])
2826
return 0, nil
@@ -50,7 +48,7 @@ func TestRunningMountpoint(t *testing.T) {
5048
exitCode, err := csimounter.Run(csimounter.Options{
5149
MountpointPath: mountpointPath,
5250
MountOptions: mountoptions.Options{
53-
Fd: int(openDevNull(t).Fd()),
51+
Fd: int(mountertest.OpenDevNull(t).Fd()),
5452
BucketName: "test-bucket",
5553
},
5654
CmdRunner: runner,
@@ -70,7 +68,7 @@ func TestRunningMountpoint(t *testing.T) {
7068
exitCode, err := csimounter.Run(csimounter.Options{
7169
MountpointPath: mountpointPath,
7270
MountOptions: mountoptions.Options{
73-
Fd: int(openDevNull(t).Fd()),
71+
Fd: int(mountertest.OpenDevNull(t).Fd()),
7472
Env: env,
7573
},
7674
CmdRunner: runner,
@@ -92,7 +90,7 @@ func TestRunningMountpoint(t *testing.T) {
9290
exitCode, err := csimounter.Run(csimounter.Options{
9391
MountpointPath: mountpointPath,
9492
MountOptions: mountoptions.Options{
95-
Fd: int(openDevNull(t).Fd()),
93+
Fd: int(mountertest.OpenDevNull(t).Fd()),
9694
BucketName: "test-bucket",
9795
},
9896
CmdRunner: runner,
@@ -103,7 +101,7 @@ func TestRunningMountpoint(t *testing.T) {
103101
exitCode, err = csimounter.Run(csimounter.Options{
104102
MountpointPath: mountpointPath,
105103
MountOptions: mountoptions.Options{
106-
Fd: int(openDevNull(t).Fd()),
104+
Fd: int(mountertest.OpenDevNull(t).Fd()),
107105
BucketName: "test-bucket",
108106
Args: []string{"--foreground"},
109107
},
@@ -124,28 +122,3 @@ func TestRunningMountpoint(t *testing.T) {
124122
assert.Equals(t, cmpopts.AnyError, err)
125123
})
126124
}
127-
128-
func openDevNull(t *testing.T) *os.File {
129-
file, err := os.Open(os.DevNull)
130-
assert.NoError(t, err)
131-
t.Cleanup(func() {
132-
err = file.Close()
133-
if err != nil {
134-
log.Printf("Failed to close file handle: %v\n", err)
135-
}
136-
})
137-
return file
138-
}
139-
140-
func assertSameFile(t *testing.T, want *os.File, got *os.File) {
141-
var wantStat = &syscall.Stat_t{}
142-
err := syscall.Fstat(int(want.Fd()), wantStat)
143-
assert.NoError(t, err)
144-
145-
var gotStat = &syscall.Stat_t{}
146-
err = syscall.Fstat(int(got.Fd()), gotStat)
147-
assert.NoError(t, err)
148-
149-
assert.Equals(t, wantStat.Dev, gotStat.Dev)
150-
assert.Equals(t, wantStat.Ino, gotStat.Ino)
151-
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package mountertest
2+
3+
import (
4+
"os"
5+
"syscall"
6+
"testing"
7+
8+
"github.com/awslabs/aws-s3-csi-driver/pkg/util/testutil/assert"
9+
)
10+
11+
// OpenDevNull opens `/dev/null` and returns the file handle.
12+
func OpenDevNull(t *testing.T) *os.File {
13+
file, err := os.Open(os.DevNull)
14+
assert.NoError(t, err)
15+
t.Cleanup(func() {
16+
_ = file.Close()
17+
})
18+
return file
19+
}
20+
21+
// AssertSameFile checks if given file handles points to the same underlying file description.
22+
func AssertSameFile(t *testing.T, want *os.File, got *os.File) {
23+
t.Helper()
24+
25+
var wantStat = &syscall.Stat_t{}
26+
err := syscall.Fstat(int(want.Fd()), wantStat)
27+
assert.NoError(t, err)
28+
29+
var gotStat = &syscall.Stat_t{}
30+
err = syscall.Fstat(int(got.Fd()), gotStat)
31+
assert.NoError(t, err)
32+
33+
assert.Equals(t, wantStat.Dev, gotStat.Dev)
34+
assert.Equals(t, wantStat.Ino, gotStat.Ino)
35+
}

0 commit comments

Comments
 (0)