diff --git a/pkg/version/util.go b/pkg/version/util.go index 968cafa6..a9c48ed1 100644 --- a/pkg/version/util.go +++ b/pkg/version/util.go @@ -15,23 +15,21 @@ limitations under the License. package version import ( + "strconv" "strings" ) var ( - validCurrentVersions = map[string]bool{ - "1.10.0": true, "1.11.0": true, "1.12.0": true, - "2.0.0": true, "2.1.0": true, "2.2.0": true, "2.3.0": true, - "2.4.0": true, "2.4.1": true, "2.5.0": true, "2.6.0": true, "2.7.0": true, - "2.8.0": true, "2.9.0": true, "2.10.0": true, "2.11.0": true, - } + minCurrentVersion = "1.10.0" validDesiredVersion = strings.Split(GetVersion(), "-")[0] + // these are the versions used in various pipelines for ci testing + exceptions = []string{"master"} ) // IsCurrentVersionValid verifies if the current version is valid or not func IsCurrentVersionValid(v string) bool { currentVersion := strings.Split(v, "-")[0] - return validCurrentVersions[currentVersion] + return CanCurrentVersionBeUpgraded(currentVersion) } // IsDesiredVersionValid verifies the desired version is valid or not @@ -39,3 +37,31 @@ func IsDesiredVersionValid(v string) bool { desiredVersion := strings.Split(v, "-")[0] return validDesiredVersion == desiredVersion } + +// CanCurrentVersionBeUpgraded determines whether the current version +// is within the range of minCurrentVersion and validDesiredVersion +func CanCurrentVersionBeUpgraded(version string) bool { + return IsOldLessThanOrEqualNewVersion(minCurrentVersion, version) && + IsOldLessThanOrEqualNewVersion(version, validDesiredVersion) +} + +// IsOldLessThanOrEqualNewVersion compares old and new version and returns true +// if old version is less `<` or equal then new version +func IsOldLessThanOrEqualNewVersion(old, new string) bool { + oldVersions := strings.Split(strings.Split(old, "-")[0], ".") + newVersions := strings.Split(strings.Split(new, "-")[0], ".") + for _, exception := range exceptions { + if newVersions[0] == exception { + return true + } + } + for i := 0; i < len(oldVersions); i++ { + oldVersion, _ := strconv.Atoi(oldVersions[i]) + newVersion, _ := strconv.Atoi(newVersions[i]) + if oldVersion == newVersion { + continue + } + return oldVersion < newVersion + } + return true +} diff --git a/pkg/version/util_test.go b/pkg/version/util_test.go new file mode 100644 index 00000000..84f8da68 --- /dev/null +++ b/pkg/version/util_test.go @@ -0,0 +1,59 @@ +/* +Copyright 2021 The OpenEBS 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 version + +import "testing" + +func TestIsCurrentVersionValid(t *testing.T) { + // setting the variable for test + validDesiredVersion = "2.9.0" + type args struct { + v string + } + tests := []struct { + name string + args args + want bool + }{ + { + name: "Valid Current Version", + args: args{ + v: "1.12.0", + }, + want: true, + }, + { + name: "Less than Min Current Version", + args: args{ + v: "1.9.0", + }, + want: false, + }, + { + name: "More than Valid Desired Version", + args: args{ + v: "2.13.0", + }, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := IsCurrentVersionValid(tt.args.v); got != tt.want { + t.Errorf("IsCurrentVersionValid() = %v, want %v", got, tt.want) + } + }) + } +}