github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/capabilities/caps_test.go (about)

     1  package capabilities // import "github.com/demonoid81/moby/pkg/capabilities"
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func TestMatch(t *testing.T) {
     9  	set := Set{
    10  		"foo": struct{}{},
    11  		"bar": struct{}{},
    12  	}
    13  	type testcase struct {
    14  		caps     [][]string
    15  		expected []string
    16  	}
    17  	var testcases = []testcase{
    18  		// matches
    19  		{
    20  			caps:     [][]string{{}},
    21  			expected: []string{},
    22  		},
    23  		{
    24  			caps:     [][]string{{"foo"}},
    25  			expected: []string{"foo"},
    26  		},
    27  		{
    28  			caps:     [][]string{{"bar"}, {"foo"}},
    29  			expected: []string{"bar"},
    30  		},
    31  		{
    32  			caps:     [][]string{{"foo", "bar"}},
    33  			expected: []string{"foo", "bar"},
    34  		},
    35  		{
    36  			caps:     [][]string{{"qux"}, {"foo"}},
    37  			expected: []string{"foo"},
    38  		},
    39  		{
    40  			caps:     [][]string{{"foo", "bar"}, {"baz"}, {"bar"}},
    41  			expected: []string{"foo", "bar"},
    42  		},
    43  
    44  		// non matches
    45  		{caps: nil},
    46  		{caps: [][]string{}},
    47  		{caps: [][]string{{"qux"}}},
    48  		{caps: [][]string{{"foo", "bar", "qux"}}},
    49  		{caps: [][]string{{"qux"}, {"baz"}}},
    50  		{caps: [][]string{{"foo", "baz"}}},
    51  	}
    52  
    53  	for _, m := range testcases {
    54  		t.Run(fmt.Sprintf("%v", m.caps), func(t *testing.T) {
    55  			selected := set.Match(m.caps)
    56  			if m.expected == nil || selected == nil {
    57  				if m.expected == nil && selected == nil {
    58  					return
    59  				}
    60  				t.Fatalf("selected = %v, expected = %v", selected, m.expected)
    61  			}
    62  			if len(selected) != len(m.expected) {
    63  				t.Fatalf("len(selected) = %d, len(expected) = %d", len(selected), len(m.expected))
    64  			}
    65  			for i, s := range selected {
    66  				if m.expected[i] != s {
    67  					t.Fatalf("selected[%d] = %s, expected[%d] = %s", i, s, i, m.expected[i])
    68  				}
    69  			}
    70  		})
    71  	}
    72  }