open-nomad/client/lib/cgutil/cgutil_linux_test.go
Seth Hoenig 51384dd63f client: refactor cpuset manager initialization
This PR refactors the code path in Client startup for setting up the cpuset
cgroup manager (non-linux systems not affected).

Before, there was a logic bug where we would try to read the cpuset.cpus.effective
cgroup interface file before ensuring nomad's parent cgroup existed. Therefor that
file would not exist, and the list of useable cpus would be empty. Tasks started
thereafter would not have a value set for their cpuset.cpus.

The refactoring fixes some less than ideal coding style. Instead we now bootstrap
each cpuset manager type (v1/v2) within its own constructor. If something goes
awry during bootstrap (e.g. cgroups not enabled), the constructor returns the
noop implementation and logs a warning.

Fixes #14229
2022-08-25 11:18:43 -05:00

130 lines
3.2 KiB
Go

//go:build linux
package cgutil
import (
"path/filepath"
"strings"
"testing"
"github.com/hashicorp/nomad/ci"
"github.com/hashicorp/nomad/client/testutil"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/fs2"
"github.com/shoenig/test/must"
"github.com/stretchr/testify/require"
)
func TestUtil_GetCgroupParent(t *testing.T) {
ci.Parallel(t)
t.Run("v1", func(t *testing.T) {
testutil.CgroupsCompatibleV1(t)
t.Run("default", func(t *testing.T) {
exp := "/nomad"
parent := GetCgroupParent("")
require.Equal(t, exp, parent)
})
t.Run("configured", func(t *testing.T) {
exp := "/bar"
parent := GetCgroupParent("/bar")
require.Equal(t, exp, parent)
})
})
t.Run("v2", func(t *testing.T) {
testutil.CgroupsCompatibleV2(t)
t.Run("default", func(t *testing.T) {
exp := "nomad.slice"
parent := GetCgroupParent("")
require.Equal(t, exp, parent)
})
t.Run("configured", func(t *testing.T) {
exp := "abc.slice"
parent := GetCgroupParent("abc.slice")
require.Equal(t, exp, parent)
})
})
}
func TestUtil_CreateCPUSetManager(t *testing.T) {
ci.Parallel(t)
logger := testlog.HCLogger(t)
t.Run("v1", func(t *testing.T) {
testutil.CgroupsCompatibleV1(t)
parent := "/" + uuid.Short()
manager := CreateCPUSetManager(parent, []uint16{0}, logger)
manager.Init()
_, ok := manager.(*cpusetManagerV1)
must.True(t, ok)
must.NoError(t, cgroups.RemovePath(filepath.Join(CgroupRoot, parent)))
})
t.Run("v2", func(t *testing.T) {
testutil.CgroupsCompatibleV2(t)
parent := uuid.Short() + ".slice"
manager := CreateCPUSetManager(parent, []uint16{0}, logger)
manager.Init()
_, ok := manager.(*cpusetManagerV2)
must.True(t, ok)
must.NoError(t, cgroups.RemovePath(filepath.Join(CgroupRoot, parent)))
})
}
func TestUtil_GetCPUsFromCgroup(t *testing.T) {
ci.Parallel(t)
t.Run("v2", func(t *testing.T) {
testutil.CgroupsCompatibleV2(t)
cpus, err := GetCPUsFromCgroup("system.slice") // thanks, systemd!
require.NoError(t, err)
require.NotEmpty(t, cpus)
})
}
func create(t *testing.T, name string) {
mgr, err := fs2.NewManager(nil, filepath.Join(CgroupRoot, name))
require.NoError(t, err)
if err = mgr.Apply(CreationPID); err != nil {
_ = cgroups.RemovePath(name)
t.Fatal("failed to create cgroup for test")
}
}
func cleanup(t *testing.T, name string) {
err := cgroups.RemovePath(name)
require.NoError(t, err)
}
func TestUtil_CopyCpuset(t *testing.T) {
ci.Parallel(t)
t.Run("v2", func(t *testing.T) {
testutil.CgroupsCompatibleV2(t)
source := uuid.Short() + ".scope"
create(t, source)
defer cleanup(t, source)
require.NoError(t, cgroups.WriteFile(filepath.Join(CgroupRoot, source), "cpuset.cpus", "0-1"))
destination := uuid.Short() + ".scope"
create(t, destination)
defer cleanup(t, destination)
err := CopyCpuset(
filepath.Join(CgroupRoot, source),
filepath.Join(CgroupRoot, destination),
)
require.NoError(t, err)
value, readErr := cgroups.ReadFile(filepath.Join(CgroupRoot, destination), "cpuset.cpus")
require.NoError(t, readErr)
require.Equal(t, "0-1", strings.TrimSpace(value))
})
}