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

View File

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

View File

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

View File

@ -11,7 +11,6 @@ import (
"runtime" "runtime"
"syscall" "syscall"
"testing" "testing"
"time"
"github.com/rfjakob/gocryptfs/internal/nametransform" "github.com/rfjakob/gocryptfs/internal/nametransform"
) )
@ -50,9 +49,11 @@ func ResetTmpDir(plaintextNames bool) {
d := filepath.Join(TmpDir, e.Name()) d := filepath.Join(TmpDir, e.Name())
err = os.Remove(d) err = os.Remove(d)
if err != nil { if err != nil {
fu := exec.Command("fusermount", "-z", "-u", d) UnmountErr(d)
fu.Run() err = os.RemoveAll(d)
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" // UnmountPanic tries to umount "dir" and panics on error.
func Unmount(p string) error { func UnmountPanic(dir string) {
var cmd *exec.Cmd err := UnmountErr(dir)
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()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
panic(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" // Return md5 string for file "filename"