github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/testutil/driver_compatible.go (about)

     1  package testutil
     2  
     3  import (
     4  	"os/exec"
     5  	"runtime"
     6  	"syscall"
     7  	"testing"
     8  )
     9  
    10  // RequireRoot skips tests unless:
    11  // - running as root
    12  func RequireRoot(t *testing.T) {
    13  	if syscall.Geteuid() != 0 {
    14  		t.Skip("Test requires root")
    15  	}
    16  }
    17  
    18  // RequireConsul skips tests unless:
    19  // - "consul" executable is detected 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  // RequireVault skips tests unless:
    28  // - "vault" executable is detected on $PATH
    29  func RequireVault(t *testing.T) {
    30  	_, err := exec.Command("vault", "version").CombinedOutput()
    31  	if err != nil {
    32  		t.Skipf("Test requires Vault: %v", err)
    33  	}
    34  }
    35  
    36  // RequireLinux skips tests unless:
    37  // - running on Linux
    38  func RequireLinux(t *testing.T) {
    39  	if runtime.GOOS != "linux" {
    40  		t.Skip("Test requires Linux")
    41  	}
    42  }
    43  
    44  // ExecCompatible skips tests unless:
    45  // - running as root
    46  // - running on Linux
    47  func ExecCompatible(t *testing.T) {
    48  	if runtime.GOOS != "linux" || syscall.Geteuid() != 0 {
    49  		t.Skip("Test requires root on Linux")
    50  	}
    51  }
    52  
    53  // JavaCompatible skips tests unless:
    54  // - "java" executable is detected on $PATH
    55  // - running as root
    56  // - running on Linux
    57  func JavaCompatible(t *testing.T) {
    58  	_, err := exec.Command("java", "-version").CombinedOutput()
    59  	if err != nil {
    60  		t.Skipf("Test requires Java: %v", err)
    61  	}
    62  
    63  	if runtime.GOOS != "linux" || syscall.Geteuid() != 0 {
    64  		t.Skip("Test requires root on Linux")
    65  	}
    66  }
    67  
    68  // QemuCompatible skips tests unless:
    69  // - "qemu-system-x86_64" executable is detected on $PATH (!windows)
    70  // - "qemu-img" executable is detected on on $PATH (windows)
    71  func QemuCompatible(t *testing.T) {
    72  	// Check if qemu exists
    73  	bin := "qemu-system-x86_64"
    74  	if runtime.GOOS == "windows" {
    75  		bin = "qemu-img"
    76  	}
    77  	_, err := exec.Command(bin, "--version").CombinedOutput()
    78  	if err != nil {
    79  		t.Skipf("Test requires QEMU (%s)", bin)
    80  	}
    81  }
    82  
    83  // MountCompatible skips tests unless:
    84  // - not running as windows
    85  // - running as root
    86  func MountCompatible(t *testing.T) {
    87  	if runtime.GOOS == "windows" {
    88  		t.Skip("Test requires not using Windows")
    89  	}
    90  
    91  	if syscall.Geteuid() != 0 {
    92  		t.Skip("Test requires root")
    93  	}
    94  }
    95  
    96  // MinimumCores skips tests unless:
    97  // - system has at least cores available CPU cores
    98  func MinimumCores(t *testing.T, cores int) {
    99  	available := runtime.NumCPU()
   100  	if available < cores {
   101  		t.Skipf("Test requires at least %d cores, only %d available", cores, available)
   102  	}
   103  }