From b4b177ded57e229d269e8d03c4cd5b3385cf4dd6 Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Thu, 17 Mar 2022 10:13:19 -0500 Subject: [PATCH] wip some tests --- client/client_test.go | 3 +- client/lib/cgutil/cgutil_linux_test.go | 10 +++--- client/lib/cgutil/cpuset_manager_v1_test.go | 10 +++--- client/lib/cgutil/cpuset_manager_v2_test.go | 4 +-- client/testutil/driver_compatible.go | 27 ++++++++-------- drivers/docker/driver_test.go | 4 +-- drivers/docker/reconcile_cpuset_test.go | 36 +++++++++++++++++++++ 7 files changed, 64 insertions(+), 30 deletions(-) create mode 100644 drivers/docker/reconcile_cpuset_test.go diff --git a/client/client_test.go b/client/client_test.go index bb99e6cc9208..c8cdb0f0a282 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -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" @@ -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" ) diff --git a/client/lib/cgutil/cgutil_linux_test.go b/client/lib/cgutil/cgutil_linux_test.go index b5e3ce2474c8..7fdc492bce3c 100644 --- a/client/lib/cgutil/cgutil_linux_test.go +++ b/client/lib/cgutil/cgutil_linux_test.go @@ -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("") @@ -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("") @@ -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}) @@ -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}) @@ -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) diff --git a/client/lib/cgutil/cpuset_manager_v1_test.go b/client/lib/cgutil/cpuset_manager_v1_test.go index 711a0b254df9..0ca5ee129059 100644 --- a/client/lib/cgutil/cpuset_manager_v1_test.go +++ b/client/lib/cgutil/cpuset_manager_v1_test.go @@ -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() @@ -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() @@ -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 @@ -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() diff --git a/client/lib/cgutil/cpuset_manager_v2_test.go b/client/lib/cgutil/cpuset_manager_v2_test.go index e03dd847a4c6..1aa2e9166fe4 100644 --- a/client/lib/cgutil/cpuset_manager_v2_test.go +++ b/client/lib/cgutil/cpuset_manager_v2_test.go @@ -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" @@ -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" diff --git a/client/testutil/driver_compatible.go b/client/testutil/driver_compatible.go index 67542c4ae846..4bc3253766fc 100644 --- a/client/testutil/driver_compatible.go +++ b/client/testutil/driver_compatible.go @@ -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") } } @@ -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") } } @@ -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 } @@ -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 } diff --git a/drivers/docker/driver_test.go b/drivers/docker/driver_test.go index 26cbf99161d2..cdffca518ae1 100644 --- a/drivers/docker/driver_test.go +++ b/drivers/docker/driver_test.go @@ -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 diff --git a/drivers/docker/reconcile_cpuset_test.go b/drivers/docker/reconcile_cpuset_test.go new file mode 100644 index 000000000000..6032724a9bab --- /dev/null +++ b/drivers/docker/reconcile_cpuset_test.go @@ -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) +}