open-nomad/lib/cpuset/cpuset_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

238 lines
4.4 KiB
Go
Raw Normal View History

package cpuset
import (
"testing"
"github.com/hashicorp/nomad/ci"
"github.com/stretchr/testify/require"
)
func TestCPUSet_Size(t *testing.T) {
ci.Parallel(t)
set := New(0, 1, 2, 3)
require.Equal(t, 4, set.Size())
require.Equal(t, 0, New().Size())
}
func TestCPUSet_ToSlice(t *testing.T) {
ci.Parallel(t)
cases := []struct {
desc string
in CPUSet
out []uint16
}{
{
"empty cpuset",
New(),
[]uint16{},
},
{
"in order",
New(0, 1, 2, 3, 4, 5, 6, 7),
[]uint16{0, 1, 2, 3, 4, 5, 6, 7},
},
{
"out of order",
New(3, 1, 2, 0),
[]uint16{0, 1, 2, 3},
},
}
for _, c := range cases {
require.Exactly(t, c.out, c.in.ToSlice(), c.desc)
}
}
func TestCPUSet_Equal(t *testing.T) {
ci.Parallel(t)
cases := []struct {
a CPUSet
b CPUSet
shouldEqual bool
}{
{New(), New(), true},
{New(5), New(5), true},
{New(1, 2, 3, 4, 5), New(1, 2, 3, 4, 5), true},
{New(), New(5), false},
{New(5), New(), false},
{New(), New(1, 2, 3, 4, 5), false},
{New(1, 2, 3, 4, 5), New(), false},
{New(5), New(1, 2, 3, 4, 5), false},
{New(1, 2, 3, 4, 5), New(5), false},
}
for _, c := range cases {
require.Equal(t, c.shouldEqual, c.a.Equal(c.b))
}
}
func TestCPUSet_Union(t *testing.T) {
ci.Parallel(t)
cases := []struct {
a CPUSet
b CPUSet
expected CPUSet
}{
{New(), New(), New()},
{New(), New(0), New(0)},
{New(0), New(), New(0)},
{New(0), New(0), New(0)},
{New(), New(0, 1, 2, 3), New(0, 1, 2, 3)},
{New(0, 1), New(0, 1, 2, 3), New(0, 1, 2, 3)},
{New(2, 3), New(4, 5), New(2, 3, 4, 5)},
{New(3, 4), New(0, 1, 2, 3), New(0, 1, 2, 3, 4)},
}
for _, c := range cases {
require.Exactly(t, c.expected.ToSlice(), c.a.Union(c.b).ToSlice())
}
}
func TestCPUSet_Difference(t *testing.T) {
ci.Parallel(t)
cases := []struct {
a CPUSet
b CPUSet
expected CPUSet
}{
{New(), New(), New()},
{New(), New(0), New()},
{New(0), New(), New(0)},
{New(0), New(0), New()},
{New(0, 1), New(0, 1, 2, 3), New()},
{New(2, 3), New(4, 5), New(2, 3)},
{New(3, 4), New(0, 1, 2, 3), New(4)},
}
for _, c := range cases {
require.Exactly(t, c.expected.ToSlice(), c.a.Difference(c.b).ToSlice())
}
}
func TestCPUSet_IsSubsetOf(t *testing.T) {
ci.Parallel(t)
cases := []struct {
a CPUSet
b CPUSet
isSubset bool
}{
{New(0), New(0), true},
{New(), New(0), true},
{New(0), New(), false},
{New(1, 2), New(0, 1, 2, 3), true},
{New(2, 1), New(0, 1, 2, 3), true},
{New(3, 4), New(0, 1, 2, 3), false},
}
for _, c := range cases {
require.Equal(t, c.isSubset, c.a.IsSubsetOf(c.b))
}
}
func TestCPUSet_IsSupersetOf(t *testing.T) {
ci.Parallel(t)
cases := []struct {
a CPUSet
b CPUSet
isSuperset bool
}{
{New(0), New(0), true},
{New(0), New(), true},
{New(), New(0), false},
{New(0, 1, 2, 3), New(0), true},
{New(0, 1, 2, 3), New(2, 3), true},
{New(0, 1, 2, 3), New(2, 3, 4), false},
}
for _, c := range cases {
require.Equal(t, c.isSuperset, c.a.IsSupersetOf(c.b))
}
}
func TestCPUSet_ContainsAny(t *testing.T) {
ci.Parallel(t)
cases := []struct {
a CPUSet
b CPUSet
containsAny bool
}{
{New(0), New(0), true},
{New(0), New(), false},
{New(), New(0), false},
{New(0, 1, 2, 3), New(0), true},
{New(0, 1, 2, 3), New(2, 3), true},
{New(0, 1, 2, 3), New(2, 3, 4), true},
}
for _, c := range cases {
require.Equal(t, c.containsAny, c.a.ContainsAny(c.b))
}
}
func TestParse(t *testing.T) {
ci.Parallel(t)
cases := []struct {
cpuset string
expected CPUSet
}{
{"", New()},
{"\n", New()},
{"1", New(1)},
{"1\n", New(1)},
{"0,1,2,3", New(0, 1, 2, 3)},
{"0-3", New(0, 1, 2, 3)},
{"0,2-3,5", New(0, 2, 3, 5)},
}
for _, c := range cases {
result, err := Parse(c.cpuset)
require.NoError(t, err)
require.True(t, result.Equal(c.expected))
}
}
func TestCPUSet_String(t *testing.T) {
ci.Parallel(t)
client: enable support for cgroups v2 This PR introduces support for using Nomad on systems with cgroups v2 [1] enabled as the cgroups controller mounted on /sys/fs/cgroups. Newer Linux distros like Ubuntu 21.10 are shipping with cgroups v2 only, causing problems for Nomad users. Nomad mostly "just works" with cgroups v2 due to the indirection via libcontainer, but not so for managing cpuset cgroups. Before, Nomad has been making use of a feature in v1 where a PID could be a member of more than one cgroup. In v2 this is no longer possible, and so the logic around computing cpuset values must be modified. When Nomad detects v2, it manages cpuset values in-process, rather than making use of cgroup heirarchy inheritence via shared/reserved parents. Nomad will only activate the v2 logic when it detects cgroups2 is mounted at /sys/fs/cgroups. This means on systems running in hybrid mode with cgroups2 mounted at /sys/fs/cgroups/unified (as is typical) Nomad will continue to use the v1 logic, and should operate as before. Systems that do not support cgroups v2 are also not affected. When v2 is activated, Nomad will create a parent called nomad.slice (unless otherwise configured in Client conifg), and create cgroups for tasks using naming convention <allocID>-<task>.scope. These follow the naming convention set by systemd and also used by Docker when cgroups v2 is detected. Client nodes now export a new fingerprint attribute, unique.cgroups.version which will be set to 'v1' or 'v2' to indicate the cgroups regime in use by Nomad. The new cpuset management strategy fixes #11705, where docker tasks that spawned processes on startup would "leak". In cgroups v2, the PIDs are started in the cgroup they will always live in, and thus the cause of the leak is eliminated. [1] https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html Closes #11289 Fixes #11705 #11773 #11933
2022-02-28 22:24:01 +00:00
cases := []struct {
cpuset CPUSet
expected string
}{
{New(), ""},
{New(0, 1, 2, 3), "0-3"},
{New(1, 3), "1,3"},
{New(0, 2, 3, 5), "0,2-3,5"},
}
for _, c := range cases {
require.Equal(t, c.expected, c.cpuset.String())
}
}
client: enable support for cgroups v2 This PR introduces support for using Nomad on systems with cgroups v2 [1] enabled as the cgroups controller mounted on /sys/fs/cgroups. Newer Linux distros like Ubuntu 21.10 are shipping with cgroups v2 only, causing problems for Nomad users. Nomad mostly "just works" with cgroups v2 due to the indirection via libcontainer, but not so for managing cpuset cgroups. Before, Nomad has been making use of a feature in v1 where a PID could be a member of more than one cgroup. In v2 this is no longer possible, and so the logic around computing cpuset values must be modified. When Nomad detects v2, it manages cpuset values in-process, rather than making use of cgroup heirarchy inheritence via shared/reserved parents. Nomad will only activate the v2 logic when it detects cgroups2 is mounted at /sys/fs/cgroups. This means on systems running in hybrid mode with cgroups2 mounted at /sys/fs/cgroups/unified (as is typical) Nomad will continue to use the v1 logic, and should operate as before. Systems that do not support cgroups v2 are also not affected. When v2 is activated, Nomad will create a parent called nomad.slice (unless otherwise configured in Client conifg), and create cgroups for tasks using naming convention <allocID>-<task>.scope. These follow the naming convention set by systemd and also used by Docker when cgroups v2 is detected. Client nodes now export a new fingerprint attribute, unique.cgroups.version which will be set to 'v1' or 'v2' to indicate the cgroups regime in use by Nomad. The new cpuset management strategy fixes #11705, where docker tasks that spawned processes on startup would "leak". In cgroups v2, the PIDs are started in the cgroup they will always live in, and thus the cause of the leak is eliminated. [1] https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html Closes #11289 Fixes #11705 #11773 #11933
2022-02-28 22:24:01 +00:00
func TestCPUSet_Copy(t *testing.T) {
ci.Parallel(t)
original := New(1, 2, 3, 4, 5)
copied := original.Copy()
require.True(t, original.Equal(copied))
client: enable support for cgroups v2 This PR introduces support for using Nomad on systems with cgroups v2 [1] enabled as the cgroups controller mounted on /sys/fs/cgroups. Newer Linux distros like Ubuntu 21.10 are shipping with cgroups v2 only, causing problems for Nomad users. Nomad mostly "just works" with cgroups v2 due to the indirection via libcontainer, but not so for managing cpuset cgroups. Before, Nomad has been making use of a feature in v1 where a PID could be a member of more than one cgroup. In v2 this is no longer possible, and so the logic around computing cpuset values must be modified. When Nomad detects v2, it manages cpuset values in-process, rather than making use of cgroup heirarchy inheritence via shared/reserved parents. Nomad will only activate the v2 logic when it detects cgroups2 is mounted at /sys/fs/cgroups. This means on systems running in hybrid mode with cgroups2 mounted at /sys/fs/cgroups/unified (as is typical) Nomad will continue to use the v1 logic, and should operate as before. Systems that do not support cgroups v2 are also not affected. When v2 is activated, Nomad will create a parent called nomad.slice (unless otherwise configured in Client conifg), and create cgroups for tasks using naming convention <allocID>-<task>.scope. These follow the naming convention set by systemd and also used by Docker when cgroups v2 is detected. Client nodes now export a new fingerprint attribute, unique.cgroups.version which will be set to 'v1' or 'v2' to indicate the cgroups regime in use by Nomad. The new cpuset management strategy fixes #11705, where docker tasks that spawned processes on startup would "leak". In cgroups v2, the PIDs are started in the cgroup they will always live in, and thus the cause of the leak is eliminated. [1] https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html Closes #11289 Fixes #11705 #11773 #11933
2022-02-28 22:24:01 +00:00
delete(copied.cpus, 3)
require.False(t, original.Equal(copied))
require.True(t, original.Equal(New(1, 2, 3, 4, 5)))
require.True(t, copied.Equal(New(1, 2, 4, 5)))
client: enable support for cgroups v2 This PR introduces support for using Nomad on systems with cgroups v2 [1] enabled as the cgroups controller mounted on /sys/fs/cgroups. Newer Linux distros like Ubuntu 21.10 are shipping with cgroups v2 only, causing problems for Nomad users. Nomad mostly "just works" with cgroups v2 due to the indirection via libcontainer, but not so for managing cpuset cgroups. Before, Nomad has been making use of a feature in v1 where a PID could be a member of more than one cgroup. In v2 this is no longer possible, and so the logic around computing cpuset values must be modified. When Nomad detects v2, it manages cpuset values in-process, rather than making use of cgroup heirarchy inheritence via shared/reserved parents. Nomad will only activate the v2 logic when it detects cgroups2 is mounted at /sys/fs/cgroups. This means on systems running in hybrid mode with cgroups2 mounted at /sys/fs/cgroups/unified (as is typical) Nomad will continue to use the v1 logic, and should operate as before. Systems that do not support cgroups v2 are also not affected. When v2 is activated, Nomad will create a parent called nomad.slice (unless otherwise configured in Client conifg), and create cgroups for tasks using naming convention <allocID>-<task>.scope. These follow the naming convention set by systemd and also used by Docker when cgroups v2 is detected. Client nodes now export a new fingerprint attribute, unique.cgroups.version which will be set to 'v1' or 'v2' to indicate the cgroups regime in use by Nomad. The new cpuset management strategy fixes #11705, where docker tasks that spawned processes on startup would "leak". In cgroups v2, the PIDs are started in the cgroup they will always live in, and thus the cause of the leak is eliminated. [1] https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html Closes #11289 Fixes #11705 #11773 #11933
2022-02-28 22:24:01 +00:00
}