tests: use unmount wrapper in ResetTmpDir

This should make it work on OSX.

Also, split unmount into two functions. Depending on what you
want,

* UnmountErr returns the resulting error
* UnmountPanic panics if the error was not nil
This commit is contained in:
Jakob Unterwurzacher 2016-07-11 20:31:36 +02:00
parent 621cbad5e1
commit 1c54fcd04b
5 changed files with 38 additions and 27 deletions

View File

@ -7,6 +7,8 @@ package example_filesystems
// "-openssl=true".
import (
"flag"
"fmt"
"os"
"testing"
@ -18,7 +20,12 @@ const statusTxtContent = "It works!\n"
var opensslOpt string
func TestMain(m *testing.M) {
// Make "testing.Verbose()" return the correct value
flag.Parse()
for _, opensslOpt = range []string{"-openssl=false", "-openssl=true"} {
if testing.Verbose() {
fmt.Printf("TestMain: testing with %q\n", opensslOpt)
}
test_helpers.ResetTmpDir(true)
r := m.Run()
if r != 0 {
@ -80,12 +87,12 @@ func TestExampleFSv07(t *testing.T) {
}
test_helpers.MountOrFatal(t, cDir, pDir, "-extpass", "echo test", opensslOpt)
checkExampleFS(t, pDir, true)
test_helpers.Unmount(pDir)
test_helpers.UnmountPanic(pDir)
test_helpers.MountOrFatal(t, cDir, pDir, "-masterkey",
"ed7f6d83-40cce86c-0e7d79c2-a9438710-575221bf-30a0eb60-2821fa8f-7f3123bf",
opensslOpt)
checkExampleFS(t, pDir, true)
test_helpers.Unmount(pDir)
test_helpers.UnmountPanic(pDir)
}
// gocryptfs v0.7 filesystem created with "-plaintextnames"
@ -95,7 +102,7 @@ func TestExampleFSv07PlaintextNames(t *testing.T) {
test_helpers.MountOrFatal(t, cDir, pDir, "-extpass", "echo test", opensslOpt)
checkExampleFS(t, pDir, true)
test_helpers.Unmount(pDir)
test_helpers.UnmountPanic(pDir)
// The actual unmount takes some time, this causes weird problems. Just don't
// reuse the mountpoint.
pDir = pDir + ".2"
@ -103,7 +110,7 @@ func TestExampleFSv07PlaintextNames(t *testing.T) {
"6d96397b-585631e1-c7cba69d-61e738b6-4d5ad2c2-e21f0fb3-52f60d3a-b08526f7",
opensslOpt)
checkExampleFS(t, pDir, true)
test_helpers.Unmount(pDir)
test_helpers.UnmountPanic(pDir)
}
// Test example_filesystems/v0.9
@ -117,11 +124,11 @@ func TestExampleFSv09(t *testing.T) {
}
test_helpers.MountOrFatal(t, cDir, pDir, "-extpass", "echo test", opensslOpt)
checkExampleFSLongnames(t, pDir)
test_helpers.Unmount(pDir)
test_helpers.UnmountPanic(pDir)
pDir = pDir + ".2"
test_helpers.MountOrFatal(t, cDir, pDir, "-masterkey",
"1cafe3f4-bc316466-2214c47c-ecd89bf3-4e078fe4-f5faeea7-8b7cab02-884f5e1c",
opensslOpt)
checkExampleFSLongnames(t, pDir)
test_helpers.Unmount(pDir)
test_helpers.UnmountPanic(pDir)
}

View File

@ -45,7 +45,7 @@ func TestMain(m *testing.M) {
opts = append(opts, fmt.Sprintf("-plaintextnames=%v", plaintextnames))
test_helpers.MountOrExit(test_helpers.DefaultCipherDir, test_helpers.DefaultPlainDir, opts...)
r := m.Run()
test_helpers.Unmount(test_helpers.DefaultPlainDir)
test_helpers.UnmountPanic(test_helpers.DefaultPlainDir)
if r != 0 {
os.Exit(r)
}

View File

@ -17,7 +17,7 @@ func TestMain(m *testing.M) {
test_helpers.ResetTmpDir(false)
test_helpers.MountOrExit(test_helpers.DefaultCipherDir, test_helpers.DefaultPlainDir, "--zerokey")
r := m.Run()
test_helpers.Unmount(test_helpers.DefaultPlainDir)
test_helpers.UnmountPanic(test_helpers.DefaultPlainDir)
os.Exit(r)
}
@ -91,7 +91,7 @@ func TestRo(t *testing.T) {
dir := test_helpers.InitFS(t)
mnt := dir + ".mnt"
test_helpers.MountOrFatal(t, dir, mnt, "-ro", "-extpass=echo test")
defer test_helpers.Unmount(mnt)
defer test_helpers.UnmountPanic(mnt)
file := mnt + "/file"
err := os.Mkdir(file, 0777)

View File

@ -21,7 +21,7 @@ func TestMain(m *testing.M) {
pDir = cDir + ".mnt"
test_helpers.MountOrExit(cDir, pDir, "-extpass", "echo test")
r := m.Run()
test_helpers.Unmount(pDir)
test_helpers.UnmountPanic(pDir)
os.Exit(r)
}

View File

@ -11,7 +11,6 @@ import (
"runtime"
"syscall"
"testing"
"time"
"github.com/rfjakob/gocryptfs/internal/nametransform"
)
@ -50,9 +49,11 @@ func ResetTmpDir(plaintextNames bool) {
d := filepath.Join(TmpDir, e.Name())
err = os.Remove(d)
if err != nil {
fu := exec.Command("fusermount", "-z", "-u", d)
fu.Run()
os.RemoveAll(d)
UnmountErr(d)
err = os.RemoveAll(d)
if err != nil {
panic(err)
}
}
}
}
@ -141,23 +142,26 @@ func MountOrFatal(t *testing.T, c string, p string, extraArgs ...string) {
}
}
// Unmount PLAINDIR "p"
func Unmount(p string) error {
var cmd *exec.Cmd
if runtime.GOOS == "darwin" {
cmd = exec.Command("umount", p)
} else {
cmd = exec.Command("fusermount", "-u", "-z", p)
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
// UnmountPanic tries to umount "dir" and panics on error.
func UnmountPanic(dir string) {
err := UnmountErr(dir)
if err != nil {
fmt.Println(err)
panic(err)
}
time.Sleep(10 * time.Millisecond)
return err
}
// UnmountError tries to unmount "dir" and returns the resulting error.
func UnmountErr(dir string) error {
var cmd *exec.Cmd
if runtime.GOOS == "darwin" {
cmd = exec.Command("umount", dir)
} else {
cmd = exec.Command("fusermount", "-u", "-z", dir)
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// Return md5 string for file "filename"