Skip to content

Commit

Permalink
libct/cg: add Resources=nil unit test
Browse files Browse the repository at this point in the history
Cgroup controllers should never panic, and yet sometimes they do.

Add a unit test to check that controllers never panic when called with
nil arguments and/or resources, and fix a few found cases.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Sep 22, 2021
1 parent f303c8d commit 08b0751
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions libcontainer/cgroups/fs2/fs2.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ func (m *manager) GetStats() (*cgroups.Stats, error) {
}

func (m *manager) Freeze(state configs.FreezerState) error {
if m.config.Resources == nil {
return errors.New("cannot toggle freezer: cgroups not configured for container")
}
if err := setFreezer(m.dirPath, state); err != nil {
return err
}
Expand All @@ -145,6 +148,9 @@ func (m *manager) Path(_ string) string {
}

func (m *manager) Set(r *configs.Resources) error {
if r == nil {
return nil
}
if err := m.getControllers(); err != nil {
return err
}
Expand Down
44 changes: 44 additions & 0 deletions libcontainer/cgroups/manager/manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package manager

import (
"testing"

"github.com/opencontainers/runc/libcontainer/configs"
)

// TestNilResources checks that a cgroup manager do not panic when
// config.Resources is nil. While it does not make sense to use a
// manager with no resources, it should not result in a panic.
//
// This tests either v1 or v2 managers (both fs and systemd),
// depending on what cgroup version is available on the host.
func TestNilResources(t *testing.T) {
for _, sd := range []bool{false, true} {
cg := &configs.Cgroup{} // .Resources is nil
cg.Systemd = sd
mgr, err := New(cg)
if err != nil {
// Some managers require non-nil Resources during
// instantiation -- provide and retry. In such case
// we're mostly testing Set(nil) below.
cg.Resources = &configs.Resources{}
mgr, err = New(cg)
if err != nil {
t.Error(err)
continue
}
}
_ = mgr.Apply(-1)
_ = mgr.Set(nil)
_ = mgr.Freeze(configs.Thawed)
_ = mgr.Exists()
_, _ = mgr.GetAllPids()
_, _ = mgr.GetCgroups()
_, _ = mgr.GetFreezerState()
_ = mgr.Path("")
_ = mgr.GetPaths()
_, _ = mgr.GetStats()
_, _ = mgr.OOMKillCount()
_ = mgr.Destroy()
}
}
3 changes: 3 additions & 0 deletions libcontainer/cgroups/systemd/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ func (m *legacyManager) freezeBeforeSet(unitName string, r *configs.Resources) (
}

func (m *legacyManager) Set(r *configs.Resources) error {
if r == nil {
return nil
}
if r.Unified != nil {
return cgroups.ErrV1NoUnified
}
Expand Down
3 changes: 3 additions & 0 deletions libcontainer/cgroups/systemd/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ func (m *unifiedManager) GetStats() (*cgroups.Stats, error) {
}

func (m *unifiedManager) Set(r *configs.Resources) error {
if r == nil {
return nil
}
properties, err := genV2ResourcesProperties(r, m.dbus)
if err != nil {
return err
Expand Down

0 comments on commit 08b0751

Please sign in to comment.