File: capabilities_linux_test.go

package info (click to toggle)
runc 1.3.3%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,000 kB
  • sloc: sh: 2,298; ansic: 1,125; makefile: 229
file content (79 lines) | stat: -rw-r--r-- 1,771 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package capabilities

import (
	"io"
	"os"
	"testing"

	"github.com/moby/sys/capability"
	"github.com/opencontainers/runc/libcontainer/configs"
	"github.com/sirupsen/logrus"
	"github.com/sirupsen/logrus/hooks/test"
)

var capTypes = []capability.CapType{
	capability.BOUNDING,
	capability.PERMITTED,
	capability.INHERITABLE,
	capability.EFFECTIVE,
	capability.AMBIENT,
}

func TestNew(t *testing.T) {
	cs := []string{"CAP_CHOWN", "CAP_UNKNOWN", "CAP_UNKNOWN2"}
	conf := configs.Capabilities{
		Bounding:    cs,
		Effective:   cs,
		Inheritable: cs,
		Permitted:   cs,
		Ambient:     cs,
	}

	hook := test.NewGlobal()
	defer hook.Reset()

	logrus.SetOutput(io.Discard)
	caps, err := New(&conf)
	logrus.SetOutput(os.Stderr)

	if err != nil {
		t.Error(err)
	}
	e := hook.AllEntries()
	if len(e) != 1 {
		t.Errorf("expected 1 warning, got %d", len(e))
	}

	expectedLogs := logrus.Entry{
		Level:   logrus.WarnLevel,
		Message: "ignoring unknown or unavailable capabilities: [CAP_UNKNOWN CAP_UNKNOWN2]",
	}

	l := hook.LastEntry()
	if l == nil {
		t.Fatal("expected a warning, but got none")
	}
	if l.Level != expectedLogs.Level {
		t.Errorf("expected %q, got %q", expectedLogs.Level, l.Level)
	}
	if l.Message != expectedLogs.Message {
		t.Errorf("expected %q, got %q", expectedLogs.Message, l.Message)
	}

	if len(caps.caps) != len(capTypes) {
		t.Errorf("expected %d capability types, got %d: %v", len(capTypes), len(caps.caps), caps.caps)
	}

	for _, cType := range capTypes {
		if i := len(caps.caps[cType]); i != 1 {
			t.Errorf("expected 1 capability for %s, got %d: %v", cType, i, caps.caps[cType])
			continue
		}
		if caps.caps[cType][0] != capability.CAP_CHOWN {
			t.Errorf("expected CAP_CHOWN, got %s: ", caps.caps[cType][0])
			continue
		}
	}

	hook.Reset()
}