| 1 |
/* |
| 2 |
Copyright 2025 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 compatibility |
| 18 |
|
| 19 |
import ( |
| 20 |
"testing" |
| 21 |
|
| 22 |
"k8s.io/apimachinery/pkg/util/version" |
| 23 |
basecompatibility "k8s.io/component-base/compatibility" |
| 24 |
) |
| 25 |
|
| 26 |
func TestValidateKubeEffectiveVersion(t *testing.T) { |
| 27 |
tests := []struct { |
| 28 |
name string |
| 29 |
emulationVersion string |
| 30 |
minCompatibilityVersion string |
| 31 |
expectErr bool |
| 32 |
}{ |
| 33 |
{ |
| 34 |
name: "valid versions", |
| 35 |
emulationVersion: "v1.32.0", |
| 36 |
minCompatibilityVersion: "v1.31.0", |
| 37 |
expectErr: false, |
| 38 |
}, |
| 39 |
{ |
| 40 |
name: "emulationVersion too low", |
| 41 |
emulationVersion: "v1.30.0", |
| 42 |
minCompatibilityVersion: "v1.31.0", |
| 43 |
expectErr: true, |
| 44 |
}, |
| 45 |
{ |
| 46 |
name: "minCompatibilityVersion too low", |
| 47 |
emulationVersion: "v1.31.0", |
| 48 |
minCompatibilityVersion: "v1.30.0", |
| 49 |
expectErr: true, |
| 50 |
}, |
| 51 |
{ |
| 52 |
name: "both versions too low", |
| 53 |
emulationVersion: "v1.30.0", |
| 54 |
minCompatibilityVersion: "v1.29.0", |
| 55 |
expectErr: true, |
| 56 |
}, |
| 57 |
} |
| 58 |
|
| 59 |
for _, test := range tests { |
| 60 |
t.Run(test.name, func(t *testing.T) { |
| 61 |
binaryVersion := version.MustParseGeneric("1.33") |
| 62 |
versionFloor := kubeEffectiveVersionFloors(binaryVersion) |
| 63 |
effective := basecompatibility.NewEffectiveVersion(binaryVersion, false, versionFloor, versionFloor) |
| 64 |
effective.SetEmulationVersion(version.MustParseGeneric(test.emulationVersion)) |
| 65 |
effective.SetMinCompatibilityVersion(version.MustParseGeneric(test.minCompatibilityVersion)) |
| 66 |
|
| 67 |
err := effective.Validate() |
| 68 |
if test.expectErr && err == nil { |
| 69 |
t.Error("expected error, but got nil") |
| 70 |
} |
| 71 |
if !test.expectErr && err != nil { |
| 72 |
t.Errorf("unexpected error: %v", err) |
| 73 |
} |
| 74 |
}) |
| 75 |
} |
| 76 |
} |