Skip to content

Commit

Permalink
wip some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shoenig committed Mar 17, 2022
1 parent e8e76e4 commit c241a24
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 30 deletions.
3 changes: 1 addition & 2 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/hashicorp/nomad/client/config"
consulApi "github.com/hashicorp/nomad/client/consul"
"github.com/hashicorp/nomad/client/fingerprint"
cstate "github.com/hashicorp/nomad/client/state"
"github.com/hashicorp/nomad/command/agent/consul"
"github.com/hashicorp/nomad/helper/pluginutils/catalog"
"github.com/hashicorp/nomad/helper/pluginutils/singleton"
Expand All @@ -30,8 +31,6 @@ import (
psstructs "github.com/hashicorp/nomad/plugins/shared/structs"
"github.com/hashicorp/nomad/testutil"
"github.com/stretchr/testify/assert"

cstate "github.com/hashicorp/nomad/client/state"
"github.com/stretchr/testify/require"
)

Expand Down
10 changes: 5 additions & 5 deletions client/lib/cgutil/cgutil_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestUtil_GetCgroupParent(t *testing.T) {
ci.Parallel(t)

t.Run("v1", func(t *testing.T) {
testutil.CgroupV1Compatible(t)
testutil.CgroupsCompatibleV1(t)
t.Run("default", func(t *testing.T) {
exp := "/nomad"
parent := GetCgroupParent("")
Expand All @@ -53,7 +53,7 @@ func TestUtil_GetCgroupParent(t *testing.T) {
})

t.Run("v2", func(t *testing.T) {
testutil.CgroupV2Compatible(t)
testutil.CgroupsCompatibleV2(t)
t.Run("default", func(t *testing.T) {
exp := "nomad.slice"
parent := GetCgroupParent("")
Expand All @@ -74,7 +74,7 @@ func TestUtil_CreateCPUSetManager(t *testing.T) {
logger := testlog.HCLogger(t)

t.Run("v1", func(t *testing.T) {
testutil.CgroupV1Compatible(t)
testutil.CgroupsCompatibleV1(t)
parent := "/" + uuid.Short()
manager := CreateCPUSetManager(parent, logger)
err := manager.Init([]uint16{0})
Expand All @@ -83,7 +83,7 @@ func TestUtil_CreateCPUSetManager(t *testing.T) {
})

t.Run("v2", func(t *testing.T) {
testutil.CgroupV2Compatible(t)
testutil.CgroupsCompatibleV2(t)
parent := uuid.Short() + ".slice"
manager := CreateCPUSetManager(parent, logger)
err := manager.Init([]uint16{0})
Expand All @@ -96,7 +96,7 @@ func TestUtil_GetCPUsFromCgroup(t *testing.T) {
ci.Parallel(t)

t.Run("v2", func(t *testing.T) {
testutil.CgroupV2Compatible(t)
testutil.CgroupsCompatibleV2(t)
cpus, err := GetCPUsFromCgroup("system.slice") // thanks, systemd!
require.NoError(t, err)
require.NotEmpty(t, cpus)
Expand Down
10 changes: 5 additions & 5 deletions client/lib/cgutil/cpuset_manager_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func tmpCpusetManagerV1(t *testing.T) (manager *cpusetManagerV1, cleanup func())
}

func TestCpusetManager_V1_Init(t *testing.T) {
testutil.CgroupV1Compatible(t)
testutil.CgroupsCompatibleV1(t)

manager, cleanup := tmpCpusetManagerV1(t)
defer cleanup()
Expand All @@ -55,7 +55,7 @@ func TestCpusetManager_V1_Init(t *testing.T) {
}

func TestCpusetManager_V1_AddAlloc_single(t *testing.T) {
testutil.CgroupV1Compatible(t)
testutil.CgroupsCompatibleV1(t)

manager, cleanup := tmpCpusetManagerV1(t)
defer cleanup()
Expand Down Expand Up @@ -104,13 +104,13 @@ func TestCpusetManager_V1_AddAlloc_single(t *testing.T) {
}

func TestCpusetManager_V1_AddAlloc_subset(t *testing.T) {
testutil.CgroupV1Compatible(t)
testutil.CgroupsCompatibleV1(t)

t.Skip("todo: add test for #11933")
}

func TestCpusetManager_V1_AddAlloc_all(t *testing.T) {
testutil.CgroupV1Compatible(t)
testutil.CgroupsCompatibleV1(t)

// cgroupsv2 changes behavior of writing empty cpuset.cpu, which is what
// happens to the /shared group when one or more allocs consume all available
Expand All @@ -119,7 +119,7 @@ func TestCpusetManager_V1_AddAlloc_all(t *testing.T) {
}

func TestCpusetManager_V1_RemoveAlloc(t *testing.T) {
testutil.CgroupV1Compatible(t)
testutil.CgroupsCompatibleV1(t)

manager, cleanup := tmpCpusetManagerV1(t)
defer cleanup()
Expand Down
4 changes: 2 additions & 2 deletions client/lib/cgutil/cpuset_manager_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
var systemCores = []uint16{0, 1}

func TestCpusetManager_V2_AddAlloc(t *testing.T) {
testutil.CgroupV2Compatible(t)
testutil.CgroupsCompatibleV2(t)

logger := testlog.HCLogger(t)
parent := uuid.Short() + ".scope"
Expand Down Expand Up @@ -62,7 +62,7 @@ func cpusetIs(t *testing.T, exp, parent, allocID, task string) {
}

func TestCpusetManager_V2_RemoveAlloc(t *testing.T) {
testutil.CgroupV2Compatible(t)
testutil.CgroupsCompatibleV2(t)

logger := testlog.HCLogger(t)
parent := uuid.Short() + ".scope"
Expand Down
27 changes: 14 additions & 13 deletions client/testutil/driver_compatible.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func ExecCompatible(t *testing.T) {
t.Skip("Test requires root on Linux")
}

if !cgroupCompatible(t) {
if !CgroupsCompatible(t) {
t.Skip("Test requires cgroup support")
}
}
Expand All @@ -64,7 +64,7 @@ func JavaCompatible(t *testing.T) {
t.Skip("Test requires root on Linux")
}

if !cgroupCompatible(t) {
if !CgroupsCompatible(t) {
t.Skip("Test requires cgroup support")
}
}
Expand All @@ -84,20 +84,21 @@ func QemuCompatible(t *testing.T) {
}
}

func cgroupCompatible(t *testing.T) bool {
return cgroupV1Compatible(t) || cgroupV2Compatible(t)
// CgroupsCompatible returns true if either cgroups.v1 or cgroups.v2 is supported.
func CgroupsCompatible(t *testing.T) bool {
return cgroupsCompatibleV1(t) || cgroupsCompatibleV2(t)
}

// CgroupV1Compatible skips tests unless:
// CgroupsCompatibleV1 skips tests unless:
// - cgroup.v1 mount point is detected
func CgroupV1Compatible(t *testing.T) {
if !cgroupV1Compatible(t) {
func CgroupsCompatibleV1(t *testing.T) {
if !cgroupsCompatibleV1(t) {
t.Skipf("Test requires cgroup.v1 support")
}
}

func cgroupV1Compatible(t *testing.T) bool {
if cgroupV2Compatible(t) {
func cgroupsCompatibleV1(t *testing.T) bool {
if cgroupsCompatibleV2(t) {
t.Log("No cgroup.v1 mount point: running in cgroup.v2 mode")
return false
}
Expand All @@ -113,15 +114,15 @@ func cgroupV1Compatible(t *testing.T) bool {
return true
}

// CgroupV2Compatible skips tests unless:
// CgroupsCompatibleV2 skips tests unless:
// - cgroup.v2 unified mode is detected
func CgroupV2Compatible(t *testing.T) {
if !cgroupV2Compatible(t) {
func CgroupsCompatibleV2(t *testing.T) {
if !cgroupsCompatibleV2(t) {
t.Skip("Test requires cgroup.v2 support")
}
}

func cgroupV2Compatible(t *testing.T) bool {
func cgroupsCompatibleV2(t *testing.T) bool {
if cgroups.IsCgroup2UnifiedMode() {
return true
}
Expand Down
4 changes: 1 addition & 3 deletions drivers/docker/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1507,9 +1507,7 @@ func TestDockerDriver_Init(t *testing.T) {
func TestDockerDriver_CPUSetCPUs(t *testing.T) {
ci.Parallel(t)
testutil.DockerCompatible(t)
if runtime.GOOS == "windows" {
t.Skip("Windows does not support CPUSetCPUs.")
}
testutil.CgroupsCompatible(t)

testCases := []struct {
Name string
Expand Down
36 changes: 36 additions & 0 deletions drivers/docker/reconcile_cpuset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package docker

import (
"testing"

"github.com/hashicorp/nomad/ci"
"github.com/stretchr/testify/require"
)

// Note: see client

func TestCoordinate_NomadCgroup(t *testing.T) {
ci.Parallel(t)

result := (coordinate{
containerID: "c6d05b36f4f56619ca59fbce921115e87dda1661860a4670e3e35ecfa3571ba1",
allocID: "27ee5321-28d6-22d7-9426-4e1888da8e7d",
task: "redis",
path: "/nomad.scope/27ee5321-28d6-22d7-9426-4e1888da8e7d.redis.scope",
}).NomadCgroup()
exp := "/sys/fs/cgroup/nomad.scope/27ee5321-28d6-22d7-9426-4e1888da8e7d.redis.scope"
require.Equal(t, exp, result)
}

func TestCoordinate_DockerCgroup(t *testing.T) {
ci.Parallel(t)

result := (coordinate{
containerID: "c6d05b36f4f56619ca59fbce921115e87dda1661860a4670e3e35ecfa3571ba1",
allocID: "27ee5321-28d6-22d7-9426-4e1888da8e7d",
task: "redis",
path: "/nomad.scope/27ee5321-28d6-22d7-9426-4e1888da8e7d.redis.scope",
}).DockerCgroup()
exp := "/sys/fs/cgroup/nomad.scope/docker-c6d05b36f4f56619ca59fbce921115e87dda1661860a4670e3e35ecfa3571ba1.scope"
require.Equal(t, exp, result)
}

0 comments on commit c241a24

Please sign in to comment.