|
| 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 statusz |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 24 | + "k8s.io/apimachinery/pkg/util/version" |
| 25 | + "k8s.io/component-base/featuregate" |
| 26 | + utilversion "k8s.io/component-base/version" |
| 27 | +) |
| 28 | + |
| 29 | +func TestBinaryVersion(t *testing.T) { |
| 30 | + componentGlobalsRegistry := featuregate.DefaultComponentGlobalsRegistry |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + setFakeEffectiveVersion bool |
| 34 | + fakeVersion string |
| 35 | + wantBinaryVersion *version.Version |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "binaryVersion with effective version", |
| 39 | + wantBinaryVersion: version.MustParseSemantic("v1.2.3"), |
| 40 | + setFakeEffectiveVersion: true, |
| 41 | + fakeVersion: "1.2.3", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "binaryVersion without effective version", |
| 45 | + wantBinaryVersion: utilversion.DefaultKubeEffectiveVersion().BinaryVersion(), |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + for _, tt := range tests { |
| 50 | + componentGlobalsRegistry.Reset() |
| 51 | + t.Run(tt.name, func(t *testing.T) { |
| 52 | + if tt.setFakeEffectiveVersion { |
| 53 | + verKube := utilversion.NewEffectiveVersion(tt.fakeVersion) |
| 54 | + fg := featuregate.NewVersionedFeatureGate(version.MustParse(tt.fakeVersion)) |
| 55 | + utilruntime.Must(componentGlobalsRegistry.Register(featuregate.DefaultKubeComponent, verKube, fg)) |
| 56 | + } |
| 57 | + |
| 58 | + registry := ®istry{} |
| 59 | + got := registry.binaryVersion() |
| 60 | + assert.Equal(t, tt.wantBinaryVersion, got) |
| 61 | + }) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestEmulationVersion(t *testing.T) { |
| 66 | + componentGlobalsRegistry := featuregate.DefaultComponentGlobalsRegistry |
| 67 | + tests := []struct { |
| 68 | + name string |
| 69 | + setFakeEffectiveVersion bool |
| 70 | + fakeEmulVer string |
| 71 | + wantEmul *version.Version |
| 72 | + }{ |
| 73 | + { |
| 74 | + name: "emulationVersion with effective version", |
| 75 | + fakeEmulVer: "2.3.4", |
| 76 | + setFakeEffectiveVersion: true, |
| 77 | + wantEmul: version.MustParseSemantic("2.3.4"), |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "emulationVersion without effective version", |
| 81 | + wantEmul: nil, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + for _, tt := range tests { |
| 86 | + componentGlobalsRegistry.Reset() |
| 87 | + t.Run(tt.name, func(t *testing.T) { |
| 88 | + if tt.setFakeEffectiveVersion { |
| 89 | + verKube := utilversion.NewEffectiveVersion("0.0.0") |
| 90 | + verKube.SetEmulationVersion(version.MustParse(tt.fakeEmulVer)) |
| 91 | + fg := featuregate.NewVersionedFeatureGate(version.MustParse(tt.fakeEmulVer)) |
| 92 | + utilruntime.Must(componentGlobalsRegistry.Register(featuregate.DefaultKubeComponent, verKube, fg)) |
| 93 | + } |
| 94 | + |
| 95 | + registry := ®istry{} |
| 96 | + got := registry.emulationVersion() |
| 97 | + if tt.wantEmul != nil && got != nil { |
| 98 | + assert.Equal(t, tt.wantEmul.Major(), got.Major()) |
| 99 | + assert.Equal(t, tt.wantEmul.Minor(), got.Minor()) |
| 100 | + } else { |
| 101 | + assert.Equal(t, tt.wantEmul, got) |
| 102 | + } |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
0 commit comments