github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/client/testutil/driver_compatible.go (about)

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