Skip to content

Commit

Permalink
refact(version): use range instead of matrix for validation (#369)
Browse files Browse the repository at this point in the history
Signed-off-by: shubham <[email protected]>
  • Loading branch information
shubham14bajpai committed Aug 3, 2021
1 parent f6c4495 commit e56b640
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 7 deletions.
40 changes: 33 additions & 7 deletions pkg/version/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,53 @@ 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
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
}
59 changes: 59 additions & 0 deletions pkg/version/util_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit e56b640

Please sign in to comment.