Refactored code and modified/added tests

pull/16569/head
Judah Nouriyelian 2023-05-31 16:55:54 +00:00
parent c81a3172a4
commit 852d9197a1
2 changed files with 13 additions and 6 deletions

View File

@ -1668,10 +1668,10 @@ func autoSetDriverOptions(cmd *cobra.Command, drvName string) (err error) {
// validateKubernetesVersion ensures that the requested version is reasonable // validateKubernetesVersion ensures that the requested version is reasonable
func validateKubernetesVersion(old *config.ClusterConfig) { func validateKubernetesVersion(old *config.ClusterConfig) {
paramVersion := viper.GetString(kubernetesVersion)
paramVersion = strings.TrimPrefix(strings.ToLower(paramVersion), version.VersionPrefix)
kubernetesVer, err := getKubernetesVersion(old) kubernetesVer, err := getKubernetesVersion(old)
if err != nil { if err != nil {
paramVersion := viper.GetString(kubernetesVersion)
paramVersion = strings.TrimPrefix(strings.ToLower(paramVersion), version.VersionPrefix)
if errors.Is(err, ErrKubernetesPatchNotFound) { if errors.Is(err, ErrKubernetesPatchNotFound) {
exit.Message(reason.PatchNotFound, "Unable to detect the latest patch release for specified major.minor version v{{.majorminor}}", exit.Message(reason.PatchNotFound, "Unable to detect the latest patch release for specified major.minor version v{{.majorminor}}",
out.V{"majorminor": paramVersion}) out.V{"majorminor": paramVersion})
@ -1686,9 +1686,6 @@ func validateKubernetesVersion(old *config.ClusterConfig) {
newestVersion := semver.MustParse(strings.TrimPrefix(constants.NewestKubernetesVersion, version.VersionPrefix)) newestVersion := semver.MustParse(strings.TrimPrefix(constants.NewestKubernetesVersion, version.VersionPrefix))
zeroVersion := semver.MustParse(strings.TrimPrefix(constants.NoKubernetesVersion, version.VersionPrefix)) zeroVersion := semver.MustParse(strings.TrimPrefix(constants.NoKubernetesVersion, version.VersionPrefix))
paramVersion := viper.GetString(kubernetesVersion)
paramVersion = strings.TrimPrefix(strings.ToLower(paramVersion), version.VersionPrefix)
if isTwoDigitSemver(paramVersion) && getLatestPatch(paramVersion) != "" { if isTwoDigitSemver(paramVersion) && getLatestPatch(paramVersion) != "" {
out.Styled(style.Workaround, `Using Kubernetes {{.version}} since patch version was unspecified`, out.V{"version": nvs}) out.Styled(style.Workaround, `Using Kubernetes {{.version}} since patch version was unspecified`, out.V{"version": nvs})
} }

View File

@ -546,13 +546,23 @@ func TestIsTwoDigitSemver(t *testing.T) {
version: "123456789.987654321", version: "123456789.987654321",
expected: true, expected: true,
}, },
{
desc: "a valid two digit version with a version prefix",
version: "v1.26",
expected: false,
},
{
desc: "a valid three digit version with a version prefix",
version: "v1.26.5",
expected: false,
},
} }
for _, tc := range tcs { for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) { t.Run(tc.desc, func(t *testing.T) {
actual := isTwoDigitSemver(tc.version) actual := isTwoDigitSemver(tc.version)
// check whether the function correctly verifies if it is a 2 digit semver // check whether the function correctly verifies if it is a 2 digit semver
if actual != tc.expected { if actual != tc.expected {
t.Fatalf("test failed. Expected version v%s to return %t", tc.version, tc.expected) t.Fatalf("test failed. Expected version %s to return %t", tc.version, tc.expected)
} }
}) })
} }