moby/pkg/capabilities/caps_test.go
Sebastiaan van Stijn 1291bbfa50
pkg/capabilities: format code with gofumpt
Formatting the code with https://github.com/mvdan/gofumpt

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-06-29 00:31:51 +02:00

72 lines
1.6 KiB
Go

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
}
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])
}
}
})
}
}