diff --git a/pkg/server/container_create_test.go b/pkg/server/container_create_test.go index 8df30a7fd..c769ddbce 100644 --- a/pkg/server/container_create_test.go +++ b/pkg/server/container_create_test.go @@ -1021,3 +1021,18 @@ func TestHostname(t *testing.T) { assert.Contains(t, spec.Process.Env, test.expectedEnv) } } + +func TestDisableCgroup(t *testing.T) { + config, sandboxConfig, imageConfig, _ := getCreateContainerTestData() + c := newTestCRIService() + c.config.DisableCgroup = true + spec, err := c.generateContainerSpec("test-id", "sandbox-id", 1234, config, sandboxConfig, imageConfig, nil) + require.NoError(t, err) + + t.Log("resource limit should not be set") + assert.Nil(t, spec.Linux.Resources.Memory) + assert.Nil(t, spec.Linux.Resources.CPU) + + t.Log("cgroup path should be empty") + assert.Empty(t, spec.Linux.CgroupsPath) +} diff --git a/pkg/server/helpers_test.go b/pkg/server/helpers_test.go index b7246c0b7..fb27803ee 100644 --- a/pkg/server/helpers_test.go +++ b/pkg/server/helpers_test.go @@ -304,3 +304,20 @@ systemd_cgroup = true }) } } + +func TestRestrictOOMScoreAdj(t *testing.T) { + current, err := getCurrentOOMScoreAdj() + require.NoError(t, err) + + got, err := restrictOOMScoreAdj(current - 1) + require.NoError(t, err) + assert.Equal(t, got, current) + + got, err = restrictOOMScoreAdj(current) + require.NoError(t, err) + assert.Equal(t, got, current) + + got, err = restrictOOMScoreAdj(current + 1) + require.NoError(t, err) + assert.Equal(t, got, current+1) +} diff --git a/pkg/server/sandbox_run_test.go b/pkg/server/sandbox_run_test.go index c8cb72883..2c9d4a056 100644 --- a/pkg/server/sandbox_run_test.go +++ b/pkg/server/sandbox_run_test.go @@ -691,5 +691,20 @@ func TestGetSandboxRuntime(t *testing.T) { } } +func TestSandboxDisableCgroup(t *testing.T) { + config, imageConfig, _ := getRunPodSandboxTestData() + c := newTestCRIService() + c.config.DisableCgroup = true + spec, err := c.generateSandboxContainerSpec("test-id", config, imageConfig, "test-cni") + require.NoError(t, err) + + t.Log("resource limit should not be set") + assert.Nil(t, spec.Linux.Resources.Memory) + assert.Nil(t, spec.Linux.Resources.CPU) + + t.Log("cgroup path should be empty") + assert.Empty(t, spec.Linux.CgroupsPath) +} + // TODO(random-liu): [P1] Add unit test for different error cases to make sure // the function cleans up on error properly.