From 1c75c583a74a097b83d01299dc33f0d1eb9b79a1 Mon Sep 17 00:00:00 2001 From: Shubham Bajpai Date: Mon, 14 Jun 2021 16:16:04 +0530 Subject: [PATCH] fix(webhook): fix version comparison of webhook resources (#1807) Signed-off-by: shubham --- pkg/util/util.go | 7 ++++--- pkg/util/util_test.go | 44 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/pkg/util/util.go b/pkg/util/util.go index 34e1574cbe..d3fb9bf242 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -275,9 +275,10 @@ func IsCurrentLessThanNewVersion(old, new string) bool { for i := 0; i < len(oldVersions); i++ { oldVersion, _ := strconv.Atoi(oldVersions[i]) newVersion, _ := strconv.Atoi(newVersions[i]) - if oldVersion > newVersion { - return false + if oldVersion == newVersion { + continue } + return oldVersion < newVersion } - return true + return false } diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go index a7791b3cda..792fb95af1 100644 --- a/pkg/util/util_test.go +++ b/pkg/util/util_test.go @@ -373,3 +373,47 @@ func TestRemoveString(t *testing.T) { }) } } + +func TestIsCurrentLessThanNewVersion(t *testing.T) { + type args struct { + old string + new string + } + tests := []struct { + name string + args args + want bool + }{ + { + name: "old is less than new", + args: args{ + old: "1.12.0", + new: "2.8.0", + }, + want: true, + }, + { + name: "old is greater than new", + args: args{ + old: "2.10.0-RC2", + new: "2.8.0", + }, + want: false, + }, + { + name: "old is same as new", + args: args{ + old: "2.8.0", + new: "2.8.0", + }, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := IsCurrentLessThanNewVersion(tt.args.old, tt.args.new); got != tt.want { + t.Errorf("IsCurrentLessThanNewVersion() = %v, want %v", got, tt.want) + } + }) + } +}