diff --git a/operator/internal/version/version.go b/operator/internal/version/version.go index 0114174e2..7635a92e5 100644 --- a/operator/internal/version/version.go +++ b/operator/internal/version/version.go @@ -39,8 +39,12 @@ func IsValid(version string) bool { return semver.IsValid(version) } -// Compare compares two versions and returns 0 if they are equal, 1 if version1 is greater than version2, -1 if version1 is less than version2 +// Compare compares two versions and returns 0 if they are equal, 1 if version1 is greater than version2, -1 if version1 is less than version2, +// or -2 if either version is invalid or empty. func Compare(version1, version2 string) int { + if !IsValid(version1) || !IsValid(version2) { + return -2 + } if version1[0] != 'v' { version1 = "v" + version1 } diff --git a/operator/internal/version/version_test.go b/operator/internal/version/version_test.go index 47294e5cf..ca63023db 100644 --- a/operator/internal/version/version_test.go +++ b/operator/internal/version/version_test.go @@ -66,6 +66,11 @@ var _ = Describe("version", func() { Entry("left is older", "v1.2.3", "v1.3.0", -1), Entry("left is newer", "v2.0.0", "v1.9.9", 1), Entry("prerelease sorts before its release", "v1.2.3-rc.1", "v1.2.3", -1), + Entry("empty on the right returns -2", "v1.2.3", "", -2), + Entry("empty on the left returns -2", "", "v1.2.3", -2), + Entry("empty on both sides returns -2", "", "", -2), + Entry("invalid on the left returns -2", "dev", "v1.2.3", -2), + Entry("invalid on the right returns -2", "v1.2.3", "dev", -2), ) })