2017-01-13 23:31:21 +00:00
|
|
|
package testutil
|
2017-01-12 01:08:00 +00:00
|
|
|
|
|
|
|
import (
|
2017-01-25 00:10:48 +00:00
|
|
|
"flag"
|
2017-02-03 22:20:05 +00:00
|
|
|
"io/ioutil"
|
2017-01-25 00:10:48 +00:00
|
|
|
"os"
|
2017-02-01 02:04:13 +00:00
|
|
|
"path/filepath"
|
2017-02-03 22:20:05 +00:00
|
|
|
"strconv"
|
2017-01-17 21:26:56 +00:00
|
|
|
"syscall"
|
2017-01-12 01:08:00 +00:00
|
|
|
"testing"
|
2017-01-25 00:10:48 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-01-12 01:08:00 +00:00
|
|
|
)
|
|
|
|
|
2017-01-25 00:10:48 +00:00
|
|
|
var rootEnabled bool
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.BoolVar(&rootEnabled, "test.root", false, "enable tests that require root")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmount unmounts a given mountPoint and sets t.Error if it fails
|
2017-01-13 23:31:21 +00:00
|
|
|
func Unmount(t *testing.T, mountPoint string) {
|
2017-01-12 01:08:00 +00:00
|
|
|
t.Log("unmount", mountPoint)
|
2017-01-17 21:26:56 +00:00
|
|
|
if err := syscall.Unmount(mountPoint, 0); err != nil {
|
2017-01-12 01:08:00 +00:00
|
|
|
t.Error("Could not umount", mountPoint, err)
|
|
|
|
}
|
|
|
|
}
|
2017-01-25 00:10:48 +00:00
|
|
|
|
|
|
|
// RequiresRoot skips tests that require root, unless the test.root flag has
|
|
|
|
// been set
|
|
|
|
func RequiresRoot(t *testing.T) {
|
|
|
|
if !rootEnabled {
|
|
|
|
t.Skip("skipping test that requires root")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Equal(t, 0, os.Getuid(), "This test must be run as root.")
|
|
|
|
}
|
2017-02-01 02:04:13 +00:00
|
|
|
|
|
|
|
// DumpDir will log out all of the contents of the provided directory to
|
|
|
|
// testing logger.
|
|
|
|
//
|
|
|
|
// Use this in a defer statement within tests that may allocate and exercise a
|
|
|
|
// temporary directory. Immensely useful for sanity checking and debugging
|
|
|
|
// failing tests.
|
|
|
|
//
|
|
|
|
// One should still test that contents are as expected. This is only a visual
|
|
|
|
// tool to assist when things don't go your way.
|
|
|
|
func DumpDir(t *testing.T, root string) {
|
|
|
|
if err := filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-03 02:55:53 +00:00
|
|
|
if fi.Mode()&os.ModeSymlink != 0 {
|
|
|
|
target, err := os.Readlink(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.Log(fi.Mode(), path, "->", target)
|
2017-02-03 22:20:05 +00:00
|
|
|
} else if fi.Mode().IsRegular() {
|
|
|
|
p, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
2017-02-10 04:32:11 +00:00
|
|
|
t.Logf("error reading file: %v", err)
|
2017-02-03 22:20:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p) > 64 { // just display a little bit.
|
|
|
|
p = p[:64]
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log(fi.Mode(), path, "[", strconv.Quote(string(p)), "...]")
|
2017-02-03 02:55:53 +00:00
|
|
|
} else {
|
|
|
|
t.Log(fi.Mode(), path)
|
|
|
|
}
|
|
|
|
|
2017-02-01 02:04:13 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("error dumping directory: %v", err)
|
|
|
|
}
|
|
|
|
}
|