github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/cli/opts/capabilities_test.go (about)

     1  package opts
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  
     7  	"gotest.tools/v3/assert"
     8  )
     9  
    10  func TestNormalizeCapability(t *testing.T) {
    11  	tests := []struct{ in, out string }{
    12  		{in: "ALL", out: "ALL"},
    13  		{in: "FOO", out: "CAP_FOO"},
    14  		{in: "CAP_FOO", out: "CAP_FOO"},
    15  		{in: "CAPFOO", out: "CAP_CAPFOO"},
    16  
    17  		// case-insensitive handling
    18  		{in: "aLl", out: "ALL"},
    19  		{in: "foO", out: "CAP_FOO"},
    20  		{in: "cAp_foO", out: "CAP_FOO"},
    21  
    22  		// white space handling. strictly, these could be considered "invalid",
    23  		// but are a likely situation, so handling these for now.
    24  		{in: "  ALL  ", out: "ALL"},
    25  		{in: "  FOO  ", out: "CAP_FOO"},
    26  		{in: "  CAP_FOO  ", out: "CAP_FOO"},
    27  		{in: " 	 ALL 	 ", out: "ALL"},
    28  		{in: " 	 FOO 	 ", out: "CAP_FOO"},
    29  		{in: " 	 CAP_FOO 	 ", out: "CAP_FOO"},
    30  
    31  		// weird values: no validation takes place currently, so these
    32  		// are handled same as values above; we could consider not accepting
    33  		// these in future
    34  		{in: "SOME CAP", out: "CAP_SOME CAP"},
    35  		{in: "_FOO", out: "CAP__FOO"},
    36  	}
    37  
    38  	for _, tc := range tests {
    39  		tc := tc
    40  		t.Run(tc.in, func(t *testing.T) {
    41  			assert.Equal(t, NormalizeCapability(tc.in), tc.out)
    42  		})
    43  	}
    44  }
    45  
    46  func TestEffectiveCapAddCapDrop(t *testing.T) {
    47  	type caps struct {
    48  		add, drop []string
    49  	}
    50  
    51  	tests := []struct {
    52  		in, out caps
    53  	}{
    54  		{
    55  			in: caps{
    56  				add:  []string{"one", "two"},
    57  				drop: []string{"one", "two"},
    58  			},
    59  			out: caps{
    60  				add: []string{"CAP_ONE", "CAP_TWO"},
    61  			},
    62  		},
    63  		{
    64  			in: caps{
    65  				add:  []string{"CAP_ONE", "cap_one", "CAP_TWO"},
    66  				drop: []string{"one", "cap_two"},
    67  			},
    68  			out: caps{
    69  				add: []string{"CAP_ONE", "CAP_TWO"},
    70  			},
    71  		},
    72  		{
    73  			in: caps{
    74  				add:  []string{"CAP_ONE", "CAP_TWO"},
    75  				drop: []string{"CAP_ONE", "CAP_THREE"},
    76  			},
    77  			out: caps{
    78  				add:  []string{"CAP_ONE", "CAP_TWO"},
    79  				drop: []string{"CAP_THREE"},
    80  			},
    81  		},
    82  		{
    83  			in: caps{
    84  				add:  []string{"ALL"},
    85  				drop: []string{"CAP_ONE", "CAP_TWO"},
    86  			},
    87  			out: caps{
    88  				add:  []string{"ALL"},
    89  				drop: []string{"CAP_ONE", "CAP_TWO"},
    90  			},
    91  		},
    92  		{
    93  			in: caps{
    94  				add: []string{"ALL", "CAP_ONE"},
    95  			},
    96  			out: caps{
    97  				add: []string{"ALL"},
    98  			},
    99  		},
   100  		{
   101  			in: caps{
   102  				drop: []string{"ALL", "CAP_ONE"},
   103  			},
   104  			out: caps{
   105  				drop: []string{"ALL"},
   106  			},
   107  		},
   108  	}
   109  
   110  	for i, tc := range tests {
   111  		tc := tc
   112  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   113  			add, drop := EffectiveCapAddCapDrop(tc.in.add, tc.in.drop)
   114  			assert.DeepEqual(t, add, tc.out.add)
   115  			assert.DeepEqual(t, drop, tc.out.drop)
   116  
   117  		})
   118  	}
   119  }