84 lines
2.5 KiB
Go
84 lines
2.5 KiB
Go
/*
|
|
Copyright 2024 The Cozystack Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"k8s.io/apimachinery/pkg/util/version"
|
|
baseversion "k8s.io/component-base/version"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestCozyEmulationVersionToKubeEmulationVersion(t *testing.T) {
|
|
kubeVer, err := version.ParseSemantic(baseversion.DefaultKubeBinaryVersion)
|
|
if err != nil {
|
|
t.Fatalf("Failed to parse kube version: %v", err)
|
|
}
|
|
|
|
testCases := []struct {
|
|
desc string
|
|
appsEmulationVer *version.Version
|
|
expectedKubeEmulationVer *version.Version
|
|
}{
|
|
{
|
|
desc: "same version as than kube binary",
|
|
appsEmulationVer: version.MajorMinor(1, 2),
|
|
expectedKubeEmulationVer: kubeVer,
|
|
},
|
|
{
|
|
desc: "1 version lower than kube binary",
|
|
appsEmulationVer: version.MajorMinor(1, 1),
|
|
expectedKubeEmulationVer: mustParseVersion(t, kubeVer.Major(), kubeVer.Minor()-1),
|
|
},
|
|
{
|
|
desc: "2 versions lower than kube binary",
|
|
appsEmulationVer: version.MajorMinor(1, 0),
|
|
expectedKubeEmulationVer: mustParseVersion(t, kubeVer.Major(), kubeVer.Minor()-2),
|
|
},
|
|
{
|
|
desc: "capped at kube binary",
|
|
appsEmulationVer: version.MajorMinor(1, 3),
|
|
expectedKubeEmulationVer: kubeVer,
|
|
},
|
|
{
|
|
desc: "no mapping",
|
|
appsEmulationVer: version.MajorMinor(2, 10),
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
mappedKubeEmulationVer := CozyVersionToKubeVersion(tc.appsEmulationVer)
|
|
if tc.expectedKubeEmulationVer == nil {
|
|
assert.Nil(t, mappedKubeEmulationVer)
|
|
} else {
|
|
assert.True(t, mappedKubeEmulationVer.EqualTo(tc.expectedKubeEmulationVer))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func mustParseVersion(t *testing.T, major, minor uint) *version.Version {
|
|
v, err := version.ParseSemantic(fmt.Sprintf("%d.%d.0", major, minor))
|
|
if err != nil {
|
|
t.Fatalf("Failed to parse version: %v", err)
|
|
}
|
|
return v
|
|
}
|