summaryrefslogtreecommitdiff
path: root/pkg/capabilities/caps_test.go
blob: 072f230369cdc466fc98d5b14e92a0066bd823a6 (plain)
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
package capabilities // import "github.com/docker/docker/pkg/capabilities"

import (
	"fmt"
	"testing"
)

func TestMatch(t *testing.T) {
	set := Set{
		"foo": struct{}{},
		"bar": struct{}{},
	}
	type testcase struct {
		caps     [][]string
		expected []string
	}
	var testcases = []testcase{
		// matches
		{
			caps:     [][]string{{}},
			expected: []string{},
		},
		{
			caps:     [][]string{{"foo"}},
			expected: []string{"foo"},
		},
		{
			caps:     [][]string{{"bar"}, {"foo"}},
			expected: []string{"bar"},
		},
		{
			caps:     [][]string{{"foo", "bar"}},
			expected: []string{"foo", "bar"},
		},
		{
			caps:     [][]string{{"qux"}, {"foo"}},
			expected: []string{"foo"},
		},
		{
			caps:     [][]string{{"foo", "bar"}, {"baz"}, {"bar"}},
			expected: []string{"foo", "bar"},
		},

		// non matches
		{caps: nil},
		{caps: [][]string{}},
		{caps: [][]string{{"qux"}}},
		{caps: [][]string{{"foo", "bar", "qux"}}},
		{caps: [][]string{{"qux"}, {"baz"}}},
		{caps: [][]string{{"foo", "baz"}}},
	}

	for _, m := range testcases {
		t.Run(fmt.Sprintf("%v", m.caps), func(t *testing.T) {
			selected := set.Match(m.caps)
			if m.expected == nil || selected == nil {
				if m.expected == nil && selected == nil {
					return
				}
				t.Fatalf("selected = %v, expected = %v", selected, m.expected)
			}
			if len(selected) != len(m.expected) {
				t.Fatalf("len(selected) = %d, len(expected) = %d", len(selected), len(m.expected))
			}
			for i, s := range selected {
				if m.expected[i] != s {
					t.Fatalf("selected[%d] = %s, expected[%d] = %s", i, s, i, m.expected[i])
				}
			}
		})
	}
}