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

     1  //go:build linux
     2  
     3  package testutil
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/opencontainers/runc/libcontainer/cgroups"
     9  )
    10  
    11  // CgroupsCompatible returns true if either cgroups.v1 or cgroups.v2 is supported.
    12  func CgroupsCompatible(t *testing.T) bool {
    13  	return cgroupsCompatibleV1(t) || cgroupsCompatibleV2(t)
    14  }
    15  
    16  // CgroupsCompatibleV1 skips tests unless:
    17  // - cgroup.v1 mount point is detected
    18  func CgroupsCompatibleV1(t *testing.T) {
    19  	if !cgroupsCompatibleV1(t) {
    20  		t.Skipf("Test requires cgroup.v1 support")
    21  	}
    22  }
    23  
    24  func cgroupsCompatibleV1(t *testing.T) bool {
    25  	// build tags mean this will never run outside of linux
    26  
    27  	if cgroupsCompatibleV2(t) {
    28  		t.Log("No cgroup.v1 mount point: running in cgroup.v2 mode")
    29  		return false
    30  	}
    31  	mount, err := cgroups.GetCgroupMounts(false)
    32  	if err != nil {
    33  		t.Logf("Unable to detect cgroup.v1 mount point: %v", err)
    34  		return false
    35  	}
    36  	if len(mount) == 0 {
    37  		t.Logf("No cgroup.v1 mount point: empty path")
    38  		return false
    39  	}
    40  	return true
    41  }
    42  
    43  // CgroupsCompatibleV2 skips tests unless:
    44  // - cgroup.v2 unified mode is detected
    45  func CgroupsCompatibleV2(t *testing.T) {
    46  	if !cgroupsCompatibleV2(t) {
    47  		t.Skip("Test requires cgroup.v2 support")
    48  	}
    49  }
    50  
    51  func cgroupsCompatibleV2(t *testing.T) bool {
    52  	return cgroups.IsCgroup2UnifiedMode()
    53  }