Skip to content

Commit

Permalink
Merge pull request #108 from kolyshkin/use-t-tempdir
Browse files Browse the repository at this point in the history
mount: trivial test nitpicks
  • Loading branch information
tianon committed Mar 8, 2022
2 parents b8d8fab + fc00352 commit 0335593
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 76 deletions.
39 changes: 9 additions & 30 deletions mount/mount_unix_test.go
Expand Up @@ -4,7 +4,6 @@
package mount

import (
"io/ioutil"
"os"
"path"
"strings"
Expand Down Expand Up @@ -34,13 +33,8 @@ func TestMounted(t *testing.T) {
t.Skip("root required")
}

tmp := path.Join(os.TempDir(), "mount-tests")
if err := os.MkdirAll(tmp, 0o777); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)

var (
tmp = t.TempDir()
sourceDir = path.Join(tmp, "source")
targetDir = path.Join(tmp, "target")
sourcePath = path.Join(sourceDir, "file.txt")
Expand All @@ -54,11 +48,11 @@ func TestMounted(t *testing.T) {
t.Fatal(err)
}

if err := ioutil.WriteFile(sourcePath, []byte("hello"), 0o644); err != nil {
if err := os.WriteFile(sourcePath, []byte("hello"), 0o644); err != nil {
t.Fatal(err)
}

if err := ioutil.WriteFile(targetPath, nil, 0o644); err != nil {
if err := os.WriteFile(targetPath, nil, 0o644); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -104,12 +98,7 @@ func TestMountTmpfsOptions(t *testing.T) {
},
}

target := path.Join(os.TempDir(), "mount-tmpfs-tests-"+t.Name())
if err := os.MkdirAll(target, 0o777); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(target)

target := t.TempDir()
for _, tc := range testCases {
t.Run(tc.opts, func(t *testing.T) {
if err := Mount("tmpfs", target, "tmpfs", tc.opts); err != nil {
Expand Down Expand Up @@ -141,13 +130,8 @@ func TestMountReadonly(t *testing.T) {
t.Skip("root required")
}

tmp := path.Join(os.TempDir(), "mount-tests")
if err := os.MkdirAll(tmp, 0o777); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)

var (
tmp = t.TempDir()
sourceDir = path.Join(tmp, "source")
targetDir = path.Join(tmp, "target")
sourcePath = path.Join(sourceDir, "file.txt")
Expand All @@ -161,11 +145,11 @@ func TestMountReadonly(t *testing.T) {
t.Fatal(err)
}

if err := ioutil.WriteFile(sourcePath, []byte("hello"), 0o644); err != nil {
if err := os.WriteFile(sourcePath, []byte("hello"), 0o644); err != nil {
t.Fatal(err)
}

if err := ioutil.WriteFile(targetPath, nil, 0o644); err != nil {
if err := os.WriteFile(targetPath, nil, 0o644); err != nil {
t.Fatal(err)
}

Expand All @@ -178,7 +162,7 @@ func TestMountReadonly(t *testing.T) {
}
}()

if err := ioutil.WriteFile(targetPath, []byte("hello"), 0o644); err == nil {
if err := os.WriteFile(targetPath, []byte("hello"), 0o644); err == nil {
t.Fatal("Should not be able to open a ro file as rw")
}
}
Expand Down Expand Up @@ -211,12 +195,7 @@ func TestRecursiveUnmountTooGreedy(t *testing.T) {
t.Skip("root required")
}

tmp, err := ioutil.TempDir("", t.Name())
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)

tmp := t.TempDir()
// Create a bunch of tmpfs mounts. Make sure "dir" itself is not
// a mount point, or we'll hit the fast path in RecursiveUnmount.
dirs := []string{"dir-other", "dir/subdir1", "dir/subdir1/subsub", "dir/subdir2/subsub"}
Expand Down
14 changes: 2 additions & 12 deletions mount/mounter_linux_test.go
Expand Up @@ -2,7 +2,6 @@ package mount

import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
Expand All @@ -15,12 +14,7 @@ func TestMount(t *testing.T) {
t.Skip("root required")
}

source, err := ioutil.TempDir("", "mount-test-source-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(source)

source := t.TempDir()
// Ensure we have a known start point by mounting tmpfs with given options
if err := Mount("tmpfs", source, "tmpfs", "private"); err != nil {
t.Fatal(err)
Expand All @@ -31,11 +25,7 @@ func TestMount(t *testing.T) {
t.FailNow()
}

target, err := ioutil.TempDir("", "mount-test-target-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(target)
target := t.TempDir()

tests := []struct {
source string
Expand Down
46 changes: 12 additions & 34 deletions mount/sharedsubtree_linux_test.go
Expand Up @@ -2,7 +2,6 @@ package mount

import (
"errors"
"io/ioutil"
"os"
"path"
"testing"
Expand All @@ -16,13 +15,9 @@ func TestSubtreePrivate(t *testing.T) {
t.Skip("root required")
}

tmp := path.Join(os.TempDir(), "mount-tests")
if err := os.MkdirAll(tmp, 0o777); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)

var (
tmp = t.TempDir()

sourceDir = path.Join(tmp, "source")
targetDir = path.Join(tmp, "target")
outside1Dir = path.Join(tmp, "outside1")
Expand All @@ -49,10 +44,10 @@ func TestSubtreePrivate(t *testing.T) {
t.Fatal(err)
}

if err := createFile(outside1Path); err != nil {
if err := os.WriteFile(outside1Path, []byte("hello"), 0o644); err != nil {
t.Fatal(err)
}
if err := createFile(outside2Path); err != nil {
if err := os.WriteFile(outside2Path, []byte("hello"), 0o644); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -118,13 +113,9 @@ func TestSubtreeShared(t *testing.T) {
t.Skip("root required")
}

tmp := path.Join(os.TempDir(), "mount-tests")
if err := os.MkdirAll(tmp, 0o777); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)

var (
tmp = t.TempDir()

sourceDir = path.Join(tmp, "source")
targetDir = path.Join(tmp, "target")
outsideDir = path.Join(tmp, "outside")
Expand All @@ -143,7 +134,7 @@ func TestSubtreeShared(t *testing.T) {
t.Fatal(err)
}

if err := createFile(outsidePath); err != nil {
if err := os.WriteFile(outsidePath, []byte("hello"), 0o644); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -190,13 +181,9 @@ func TestSubtreeSharedSlave(t *testing.T) {
t.Skip("root required")
}

tmp := path.Join(os.TempDir(), "mount-tests")
if err := os.MkdirAll(tmp, 0o777); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)

var (
tmp = t.TempDir()

sourceDir = path.Join(tmp, "source")
targetDir = path.Join(tmp, "target")
outside1Dir = path.Join(tmp, "outside1")
Expand All @@ -223,10 +210,10 @@ func TestSubtreeSharedSlave(t *testing.T) {
t.Fatal(err)
}

if err := createFile(outside1Path); err != nil {
if err := os.WriteFile(outside1Path, []byte("hello"), 0o644); err != nil {
t.Fatal(err)
}
if err := createFile(outside2Path); err != nil {
if err := os.WriteFile(outside2Path, []byte("hello"), 0o644); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -298,13 +285,8 @@ func TestSubtreeUnbindable(t *testing.T) {
t.Skip("root required")
}

tmp := path.Join(os.TempDir(), "mount-tests")
if err := os.MkdirAll(tmp, 0o777); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)

var (
tmp = t.TempDir()
sourceDir = path.Join(tmp, "source")
targetDir = path.Join(tmp, "target")
)
Expand Down Expand Up @@ -337,7 +319,3 @@ func TestSubtreeUnbindable(t *testing.T) {
}
}()
}

func createFile(path string) error {
return ioutil.WriteFile(path, []byte("hello"), 0o666)
}

0 comments on commit 0335593

Please sign in to comment.