...

Source file src/k8s.io/kubectl/pkg/cmd/version/skew_warning_test.go

Documentation: k8s.io/kubectl/pkg/cmd/version

     1  /*
     2  Copyright 2021 The Kubernetes 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 version
    18  
    19  import (
    20  	"bytes"
    21  	apimachineryversion "k8s.io/apimachinery/pkg/version"
    22  	"testing"
    23  )
    24  
    25  func TestPrintVersionSkewWarning(t *testing.T) {
    26  	output := &bytes.Buffer{}
    27  
    28  	testCases := []struct {
    29  		name              string
    30  		clientVersion     apimachineryversion.Info
    31  		serverVersion     apimachineryversion.Info
    32  		isWarningExpected bool
    33  	}{
    34  		{
    35  			name:              "Should not warn if server and client versions are same",
    36  			clientVersion:     apimachineryversion.Info{GitVersion: "v1.19.1"},
    37  			serverVersion:     apimachineryversion.Info{GitVersion: "v1.19.1"},
    38  			isWarningExpected: false,
    39  		},
    40  		{
    41  			name:              "Should not warn if server and client versions are same and server is alpha",
    42  			clientVersion:     apimachineryversion.Info{GitVersion: "v1.19.1"},
    43  			serverVersion:     apimachineryversion.Info{GitVersion: "v1.19.7-alpha"},
    44  			isWarningExpected: false,
    45  		},
    46  		{
    47  			name:              "Should not warn if server and client versions are same and server is beta",
    48  			clientVersion:     apimachineryversion.Info{GitVersion: "v1.19.1"},
    49  			serverVersion:     apimachineryversion.Info{GitVersion: "v1.19.7-beta"},
    50  			isWarningExpected: false,
    51  		},
    52  		{
    53  			name:              "Should not warn if server is 1 minor version ahead of client",
    54  			clientVersion:     apimachineryversion.Info{GitVersion: "v1.18.5"},
    55  			serverVersion:     apimachineryversion.Info{GitVersion: "v1.19.1"},
    56  			isWarningExpected: false,
    57  		},
    58  		{
    59  			name:              "Should not warn if server is 1 minor version behind client",
    60  			clientVersion:     apimachineryversion.Info{GitVersion: "v1.19.1"},
    61  			serverVersion:     apimachineryversion.Info{GitVersion: "v1.18.5"},
    62  			isWarningExpected: false,
    63  		},
    64  		{
    65  			name:              "Should warn if server is 2 minor versions ahead of client",
    66  			clientVersion:     apimachineryversion.Info{GitVersion: "v1.17.7"},
    67  			serverVersion:     apimachineryversion.Info{GitVersion: "v1.19.1"},
    68  			isWarningExpected: true,
    69  		},
    70  		{
    71  			name:              "Should warn if server is 2 minor versions behind client",
    72  			clientVersion:     apimachineryversion.Info{GitVersion: "v1.19.1"},
    73  			serverVersion:     apimachineryversion.Info{GitVersion: "v1.17.7"},
    74  			isWarningExpected: true,
    75  		},
    76  		{
    77  			name:              "Should warn if major versions are not equal",
    78  			clientVersion:     apimachineryversion.Info{GitVersion: "v1.19.1"},
    79  			serverVersion:     apimachineryversion.Info{GitVersion: "v2.19.1"},
    80  			isWarningExpected: true,
    81  		},
    82  	}
    83  	for _, tc := range testCases {
    84  		t.Run(tc.name, func(t *testing.T) {
    85  			output.Reset()
    86  
    87  			printVersionSkewWarning(output, tc.clientVersion, tc.serverVersion)
    88  
    89  			if tc.isWarningExpected && output.Len() == 0 {
    90  				t.Error("warning was expected, but not written to the output")
    91  			} else if !tc.isWarningExpected && output.Len() > 0 {
    92  				t.Errorf("warning was not expected, but was written to the output: %s", output.String())
    93  			}
    94  		})
    95  	}
    96  }
    97  

View as plain text