github.com/opencontainers/runc@v1.2.0-rc.1.0.20240520010911-492dc558cdd6/libcontainer/capabilities/capabilities_linux_test.go (about) 1 package capabilities 2 3 import ( 4 "io" 5 "os" 6 "testing" 7 8 "github.com/opencontainers/runc/libcontainer/configs" 9 "github.com/sirupsen/logrus" 10 "github.com/sirupsen/logrus/hooks/test" 11 "github.com/syndtr/gocapability/capability" 12 ) 13 14 func TestNew(t *testing.T) { 15 cs := []string{"CAP_CHOWN", "CAP_UNKNOWN", "CAP_UNKNOWN2"} 16 conf := configs.Capabilities{ 17 Bounding: cs, 18 Effective: cs, 19 Inheritable: cs, 20 Permitted: cs, 21 Ambient: cs, 22 } 23 24 hook := test.NewGlobal() 25 defer hook.Reset() 26 27 logrus.SetOutput(io.Discard) 28 caps, err := New(&conf) 29 logrus.SetOutput(os.Stderr) 30 31 if err != nil { 32 t.Error(err) 33 } 34 e := hook.AllEntries() 35 if len(e) != 1 { 36 t.Errorf("expected 1 warning, got %d", len(e)) 37 } 38 39 expectedLogs := logrus.Entry{ 40 Level: logrus.WarnLevel, 41 Message: "ignoring unknown or unavailable capabilities: [CAP_UNKNOWN CAP_UNKNOWN2]", 42 } 43 44 l := hook.LastEntry() 45 if l == nil { 46 t.Fatal("expected a warning, but got none") 47 } 48 if l.Level != expectedLogs.Level { 49 t.Errorf("expected %q, got %q", expectedLogs.Level, l.Level) 50 } 51 if l.Message != expectedLogs.Message { 52 t.Errorf("expected %q, got %q", expectedLogs.Message, l.Message) 53 } 54 55 if len(caps.caps) != len(capTypes) { 56 t.Errorf("expected %d capability types, got %d: %v", len(capTypes), len(caps.caps), caps.caps) 57 } 58 59 for _, cType := range capTypes { 60 if i := len(caps.caps[cType]); i != 1 { 61 t.Errorf("expected 1 capability for %s, got %d: %v", cType, i, caps.caps[cType]) 62 continue 63 } 64 if caps.caps[cType][0] != capability.CAP_CHOWN { 65 t.Errorf("expected CAP_CHOWN, got %s: ", caps.caps[cType][0]) 66 continue 67 } 68 } 69 70 hook.Reset() 71 }