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

     1  //go:build freebsd || linux
     2  
     3  /*
     4     Copyright The containerd Authors.
     5  
     6     Licensed under the Apache License, Version 2.0 (the "License");
     7     you may not use this file except in compliance with the License.
     8     You may obtain a copy of the License at
     9  
    10         http://www.apache.org/licenses/LICENSE-2.0
    11  
    12     Unless required by applicable law or agreed to in writing, software
    13     distributed under the License is distributed on an "AS IS" BASIS,
    14     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15     See the License for the specific language governing permissions and
    16     limitations under the License.
    17  */
    18  
    19  package netutil
    20  
    21  import (
    22  	"testing"
    23  
    24  	"github.com/Masterminds/semver/v3"
    25  	"gotest.tools/v3/assert"
    26  )
    27  
    28  func TestGuessFirewallPluginVersion(t *testing.T) {
    29  
    30  	type testCase struct {
    31  		stderr   string
    32  		expected string
    33  		err      string
    34  	}
    35  	testCases := []testCase{
    36  		{
    37  			stderr:   "CNI firewall plugin v1.1.0\n",
    38  			expected: "1.1.0",
    39  		},
    40  		{
    41  			stderr:   "CNI firewall plugin v0.8.0\n",
    42  			expected: "0.8.0",
    43  		},
    44  		{
    45  			stderr:   "Foo\nCNI firewall plugin v123.456.789+beta.10\nBar\n",
    46  			expected: "123.456.789+beta.10",
    47  		},
    48  		{
    49  			stderr: "CNI firewall plugin version unknown\n",
    50  			err:    semver.ErrInvalidSemVer.Error(),
    51  		},
    52  		{
    53  			stderr: "",
    54  			err:    "does not have any line that starts with \"CNI firewall plugin \"",
    55  		},
    56  		{
    57  			stderr: "Foo\nBar\nBaz\n",
    58  			err:    "does not have any line that starts with \"CNI firewall plugin \"",
    59  		},
    60  	}
    61  
    62  	for _, tc := range testCases {
    63  		got, err := guessFirewallPluginVersion(tc.stderr)
    64  		if tc.err == "" {
    65  			assert.NilError(t, err)
    66  			assert.Equal(t, tc.expected, got.String())
    67  		} else {
    68  			assert.ErrorContains(t, err, tc.err)
    69  		}
    70  	}
    71  }