github.com/containerd/nerdctl@v1.7.7/pkg/netutil/nettype/nettype_test.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package nettype
    18  
    19  import (
    20  	"testing"
    21  
    22  	"gotest.tools/v3/assert"
    23  )
    24  
    25  func TestDetect(t *testing.T) {
    26  	type testCase struct {
    27  		names    []string
    28  		expected Type
    29  		err      string
    30  	}
    31  	testCases := []testCase{
    32  		{
    33  			names:    nil,
    34  			expected: CNI,
    35  		},
    36  		{
    37  			names:    []string{"none"},
    38  			expected: None,
    39  		},
    40  		{
    41  			names:    []string{"host"},
    42  			expected: Host,
    43  		},
    44  		{
    45  			names:    []string{"bridge"},
    46  			expected: CNI,
    47  		},
    48  		{
    49  			names:    []string{"foo", "bar"},
    50  			expected: CNI,
    51  		},
    52  		{
    53  			names:    []string{"foo", "bar", "bridge"},
    54  			expected: CNI,
    55  		},
    56  		{
    57  			names: []string{"none", "host"},
    58  			err:   "mixed network types",
    59  		},
    60  		{
    61  			names: []string{"none", "bridge"},
    62  			err:   "mixed network types",
    63  		},
    64  		{
    65  			names: []string{"host", "foo"},
    66  			err:   "mixed network types",
    67  		},
    68  	}
    69  
    70  	for _, tc := range testCases {
    71  		got, err := Detect(tc.names)
    72  		if tc.err == "" {
    73  			assert.NilError(t, err)
    74  			assert.Equal(t, tc.expected, got)
    75  		} else {
    76  			assert.ErrorContains(t, err, tc.err)
    77  		}
    78  	}
    79  }