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"
|
|
|
|
"os"
|
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.")
|
|
|
|
}
|