github.com/manicqin/nomad@v0.9.5/client/testutil/driver_compatible.go (about) 1 package testutil 2 3 import ( 4 "os/exec" 5 "runtime" 6 "sync" 7 "syscall" 8 "testing" 9 10 "github.com/hashicorp/nomad/client/fingerprint" 11 ) 12 13 // RequireRoot skips tests unless running on a Unix as root. 14 func RequireRoot(t *testing.T) { 15 if syscall.Geteuid() != 0 { 16 t.Skip("Must run as root on Unix") 17 } 18 } 19 20 // RequireConsul skips tests unless a Consul binary is available on $PATH. 21 func RequireConsul(t *testing.T) { 22 _, err := exec.Command("consul", "version").CombinedOutput() 23 if err != nil { 24 t.Skipf("Test requires Consul: %v", err) 25 } 26 } 27 28 func ExecCompatible(t *testing.T) { 29 if runtime.GOOS != "linux" || syscall.Geteuid() != 0 { 30 t.Skip("Test only available running as root on linux") 31 } 32 CgroupCompatible(t) 33 } 34 35 func JavaCompatible(t *testing.T) { 36 if runtime.GOOS == "linux" && syscall.Geteuid() != 0 { 37 t.Skip("Test only available when running as root on linux") 38 } 39 } 40 41 func QemuCompatible(t *testing.T) { 42 // Check if qemu exists 43 bin := "qemu-system-x86_64" 44 if runtime.GOOS == "windows" { 45 bin = "qemu-img" 46 } 47 _, err := exec.Command(bin, "--version").CombinedOutput() 48 if err != nil { 49 t.Skip("Must have Qemu installed for Qemu specific tests to run") 50 } 51 } 52 53 func CgroupCompatible(t *testing.T) { 54 mount, err := fingerprint.FindCgroupMountpointDir() 55 if err != nil || mount == "" { 56 t.Skipf("Failed to find cgroup mount: %v %v", mount, err) 57 } 58 } 59 60 var rktExists bool 61 var rktOnce sync.Once 62 63 func RktCompatible(t *testing.T) { 64 if runtime.GOOS != "linux" || syscall.Geteuid() != 0 { 65 t.Skip("Must be root on Linux to run test") 66 } 67 68 // else see if rkt exists 69 rktOnce.Do(func() { 70 _, err := exec.Command("rkt", "version").CombinedOutput() 71 if err == nil { 72 rktExists = true 73 } 74 }) 75 76 if !rktExists { 77 t.Skip("Must have rkt installed for rkt specific tests to run") 78 } 79 } 80 81 func MountCompatible(t *testing.T) { 82 if runtime.GOOS == "windows" { 83 t.Skip("Windows does not support mount") 84 } 85 86 if syscall.Geteuid() != 0 { 87 t.Skip("Must be root to run test") 88 } 89 }