-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Strip extra data from versions before comparison (#1013)
* Strip extra data from versions before comparison This allows other non vanilla distributions which ship with a modified version of Kubernetes to still be used, example, OKD / OpenShift 9 returns a Kubernetes server version of "v1.22.1-1829+2cb60681522d23-dirty". Since this method is used to check the Kubernetes API version, we strip out the extra version data. * Move to use blang/semver Semver deems that preleases are lesser than the full release so this _technically_ would return -1 on 1.22.0-123+123-dirty when compared to 1.22, it's at-least more compliant than the previous code. * Add back accidental removal of the regexp package. * Remove accidental leftover debug * Add copyright header * Correct the year I forgot we're now in 2022 :) * Fix dependency issue * Fix linting issue
- Loading branch information
Showing
4 changed files
with
93 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (C) 2022, MinIO, Inc. | ||
// | ||
// This code is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License, version 3, | ||
// as published by the Free Software Foundation. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License, version 3, | ||
// along with this program. If not, see <http://www.gnu.org/licenses/> | ||
|
||
package cluster | ||
|
||
import "testing" | ||
|
||
func Test_versionCompare(t *testing.T) { | ||
type args struct { | ||
v1 string | ||
v2 string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want int | ||
}{ | ||
{ | ||
name: "v1 == v2", | ||
args: args{ | ||
v1: "v1.0.0", | ||
v2: "v1.0.0", | ||
}, | ||
want: 0, | ||
}, | ||
{ | ||
name: "v1 > v2", | ||
args: args{ | ||
v1: "v1.1.0", | ||
v2: "v1.0.0", | ||
}, | ||
want: 1, | ||
}, | ||
{ | ||
name: "v2 > v1", | ||
args: args{ | ||
v1: "v1.0.0", | ||
v2: "v1.1.0", | ||
}, | ||
want: -1, | ||
}, | ||
{ | ||
name: "v1 > v2", | ||
args: args{ | ||
v1: "v1.1.1-123+123-dirty", | ||
v2: "v1.1.0", | ||
}, | ||
want: 1, | ||
}, | ||
{ | ||
name: "-1 if invalid", | ||
args: args{ | ||
v1: "a-2.c", | ||
v2: "v1.1.0", | ||
}, | ||
want: -1, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := versionCompare(tt.args.v1, tt.args.v2) | ||
if got != tt.want { | ||
t.Errorf("versionCompare() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |