2015-09-23 00:10:03 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
2015-10-07 22:24:16 +00:00
|
|
|
"os/exec"
|
2015-09-23 00:10:03 +00:00
|
|
|
"runtime"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
2018-05-30 17:02:30 +00:00
|
|
|
|
2021-04-08 05:04:47 +00:00
|
|
|
"github.com/hashicorp/nomad/client/lib/cgutil"
|
2015-09-23 00:10:03 +00:00
|
|
|
)
|
|
|
|
|
2017-12-13 00:58:27 +00:00
|
|
|
// RequireRoot skips tests unless running on a Unix as root.
|
|
|
|
func RequireRoot(t *testing.T) {
|
|
|
|
if syscall.Geteuid() != 0 {
|
|
|
|
t.Skip("Must run as root on Unix")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 22:41:39 +00:00
|
|
|
// RequireConsul skips tests unless a Consul binary is available on $PATH.
|
|
|
|
func RequireConsul(t *testing.T) {
|
|
|
|
_, err := exec.Command("consul", "version").CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Skipf("Test requires Consul: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 00:10:03 +00:00
|
|
|
func ExecCompatible(t *testing.T) {
|
2015-10-29 00:22:04 +00:00
|
|
|
if runtime.GOOS != "linux" || syscall.Geteuid() != 0 {
|
|
|
|
t.Skip("Test only available running as root on linux")
|
2015-09-23 00:10:03 +00:00
|
|
|
}
|
2018-05-30 17:02:30 +00:00
|
|
|
CgroupCompatible(t)
|
2015-09-23 00:10:03 +00:00
|
|
|
}
|
2015-09-23 04:56:29 +00:00
|
|
|
|
2015-11-03 20:47:48 +00:00
|
|
|
func JavaCompatible(t *testing.T) {
|
|
|
|
if runtime.GOOS == "linux" && syscall.Geteuid() != 0 {
|
|
|
|
t.Skip("Test only available when running as root on linux")
|
2015-09-23 00:10:03 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-23 04:56:29 +00:00
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
func QemuCompatible(t *testing.T) {
|
2015-10-27 22:27:11 +00:00
|
|
|
// Check if qemu exists
|
2015-10-28 17:28:53 +00:00
|
|
|
bin := "qemu-system-x86_64"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
bin = "qemu-img"
|
|
|
|
}
|
|
|
|
_, err := exec.Command(bin, "--version").CombinedOutput()
|
2015-10-23 07:27:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Skip("Must have Qemu installed for Qemu specific tests to run")
|
2015-09-25 23:49:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:02:30 +00:00
|
|
|
func CgroupCompatible(t *testing.T) {
|
2021-04-08 05:04:47 +00:00
|
|
|
mount, err := cgutil.FindCgroupMountpointDir()
|
2018-05-30 17:02:30 +00:00
|
|
|
if err != nil || mount == "" {
|
|
|
|
t.Skipf("Failed to find cgroup mount: %v %v", mount, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 04:56:29 +00:00
|
|
|
func MountCompatible(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("Windows does not support mount")
|
|
|
|
}
|
|
|
|
|
|
|
|
if syscall.Geteuid() != 0 {
|
|
|
|
t.Skip("Must be root to run test")
|
|
|
|
}
|
|
|
|
}
|