k8s.io/apiserver@v0.31.1/pkg/util/version/version_test.go (about)

     1  /*
     2  Copyright 2024 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  	"testing"
    21  
    22  	"k8s.io/apimachinery/pkg/util/version"
    23  )
    24  
    25  func TestValidate(t *testing.T) {
    26  	tests := []struct {
    27  		name                    string
    28  		binaryVersion           string
    29  		emulationVersion        string
    30  		minCompatibilityVersion string
    31  		expectErrors            bool
    32  	}{
    33  		{
    34  			name:                    "patch version diff ok",
    35  			binaryVersion:           "v1.32.2",
    36  			emulationVersion:        "v1.32.1",
    37  			minCompatibilityVersion: "v1.31.5",
    38  		},
    39  		{
    40  			name:                    "emulation version one minor lower than binary ok",
    41  			binaryVersion:           "v1.32.2",
    42  			emulationVersion:        "v1.31.0",
    43  			minCompatibilityVersion: "v1.31.0",
    44  		},
    45  		{
    46  			name:                    "emulation version two minor lower than binary not ok",
    47  			binaryVersion:           "v1.33.2",
    48  			emulationVersion:        "v1.31.0",
    49  			minCompatibilityVersion: "v1.32.0",
    50  			expectErrors:            true,
    51  		},
    52  		{
    53  			name:                    "emulation version one minor higher than binary not ok",
    54  			binaryVersion:           "v1.32.2",
    55  			emulationVersion:        "v1.33.0",
    56  			minCompatibilityVersion: "v1.31.0",
    57  			expectErrors:            true,
    58  		},
    59  		{
    60  			name:                    "emulation version two minor higher than binary not ok",
    61  			binaryVersion:           "v1.32.2",
    62  			emulationVersion:        "v1.34.0",
    63  			minCompatibilityVersion: "v1.31.0",
    64  			expectErrors:            true,
    65  		},
    66  		{
    67  			name:                    "compatibility version same as binary not ok",
    68  			binaryVersion:           "v1.32.2",
    69  			emulationVersion:        "v1.32.0",
    70  			minCompatibilityVersion: "v1.32.0",
    71  			expectErrors:            true,
    72  		},
    73  		{
    74  			name:                    "compatibility version two minor lower than binary not ok",
    75  			binaryVersion:           "v1.32.2",
    76  			emulationVersion:        "v1.32.0",
    77  			minCompatibilityVersion: "v1.30.0",
    78  			expectErrors:            true,
    79  		},
    80  		{
    81  			name:                    "compatibility version one minor higher than binary not ok",
    82  			binaryVersion:           "v1.32.2",
    83  			emulationVersion:        "v1.32.0",
    84  			minCompatibilityVersion: "v1.33.0",
    85  			expectErrors:            true,
    86  		},
    87  	}
    88  
    89  	for _, test := range tests {
    90  		t.Run(test.name, func(t *testing.T) {
    91  			binaryVersion := version.MustParseGeneric(test.binaryVersion)
    92  			effective := &effectiveVersion{}
    93  			emulationVersion := version.MustParseGeneric(test.emulationVersion)
    94  			minCompatibilityVersion := version.MustParseGeneric(test.minCompatibilityVersion)
    95  			effective.Set(binaryVersion, emulationVersion, minCompatibilityVersion)
    96  
    97  			errs := effective.Validate()
    98  			if len(errs) > 0 && !test.expectErrors {
    99  				t.Errorf("expected no errors, errors found %+v", errs)
   100  			}
   101  
   102  			if len(errs) == 0 && test.expectErrors {
   103  				t.Errorf("expected errors, no errors found")
   104  			}
   105  		})
   106  	}
   107  }