Skip to content

Commit

Permalink
Merge pull request #118761 from TommyStarK/gh_113831
Browse files Browse the repository at this point in the history
move common logic of highestSupportedVersion to util package

Kubernetes-commit: f9f00da6bcc04559b44e0d6e127cb4b0c00b0370
  • Loading branch information
k8s-publishing-bot committed Sep 18, 2023
2 parents 64708d3 + 9e33432 commit a017454
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
42 changes: 42 additions & 0 deletions pkg/util/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package version

import (
"bytes"
"errors"
"fmt"
"regexp"
"strconv"
Expand Down Expand Up @@ -85,6 +86,47 @@ func parse(str string, semver bool) (*Version, error) {
return v, nil
}

// HighestSupportedVersion returns the highest supported version
// This function assumes that the highest supported version must be v1.x.
func HighestSupportedVersion(versions []string) (*Version, error) {
if len(versions) == 0 {
return nil, errors.New("empty array for supported versions")
}

var (
highestSupportedVersion *Version
theErr error
)

for i := len(versions) - 1; i >= 0; i-- {
currentHighestVer, err := ParseGeneric(versions[i])
if err != nil {
theErr = err
continue
}

if currentHighestVer.Major() > 1 {
continue
}

if highestSupportedVersion == nil || highestSupportedVersion.LessThan(currentHighestVer) {
highestSupportedVersion = currentHighestVer
}
}

if highestSupportedVersion == nil {
return nil, fmt.Errorf(
"could not find a highest supported version from versions (%v) reported: %+v",
versions, theErr)
}

if highestSupportedVersion.Major() != 1 {
return nil, fmt.Errorf("highest supported version reported is %v, must be v1.x", highestSupportedVersion)
}

return highestSupportedVersion, nil
}

// ParseGeneric parses a "generic" version string. The version string must consist of two
// or more dot-separated numeric fields (the first of which can't have leading zeroes),
// followed by arbitrary uninterpreted data (which need not be separated from the final
Expand Down
106 changes: 106 additions & 0 deletions pkg/util/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,109 @@ func TestComponents(t *testing.T) {
}
}
}

func TestHighestSupportedVersion(t *testing.T) {
testCases := []struct {
versions []string
expectedHighestSupportedVersion string
shouldFail bool
}{
{
versions: []string{"v1.0.0"},
expectedHighestSupportedVersion: "1.0.0",
shouldFail: false,
},
{
versions: []string{"0.3.0"},
shouldFail: true,
},
{
versions: []string{"0.2.0"},
shouldFail: true,
},
{
versions: []string{"1.0.0"},
expectedHighestSupportedVersion: "1.0.0",
shouldFail: false,
},
{
versions: []string{"v0.3.0"},
shouldFail: true,
},
{
versions: []string{"v0.2.0"},
shouldFail: true,
},
{
versions: []string{"0.2.0", "v0.3.0"},
shouldFail: true,
},
{
versions: []string{"0.2.0", "v1.0.0"},
expectedHighestSupportedVersion: "1.0.0",
shouldFail: false,
},
{
versions: []string{"0.2.0", "v1.2.3"},
expectedHighestSupportedVersion: "1.2.3",
shouldFail: false,
},
{
versions: []string{"v1.2.3", "v0.3.0"},
expectedHighestSupportedVersion: "1.2.3",
shouldFail: false,
},
{
versions: []string{"v1.2.3", "v0.3.0", "2.0.1"},
expectedHighestSupportedVersion: "1.2.3",
shouldFail: false,
},
{
versions: []string{"v1.2.3", "4.9.12", "v0.3.0", "2.0.1"},
expectedHighestSupportedVersion: "1.2.3",
shouldFail: false,
},
{
versions: []string{"4.9.12", "2.0.1"},
expectedHighestSupportedVersion: "",
shouldFail: true,
},
{
versions: []string{"v1.2.3", "boo", "v0.3.0", "2.0.1"},
expectedHighestSupportedVersion: "1.2.3",
shouldFail: false,
},
{
versions: []string{},
expectedHighestSupportedVersion: "",
shouldFail: true,
},
{
versions: []string{"var", "boo", "foo"},
expectedHighestSupportedVersion: "",
shouldFail: true,
},
}

for _, tc := range testCases {
// Arrange & Act
actual, err := HighestSupportedVersion(tc.versions)

// Assert
if tc.shouldFail && err == nil {
t.Fatalf("expecting highestSupportedVersion to fail, but got nil error for testcase: %#v", tc)
}
if !tc.shouldFail && err != nil {
t.Fatalf("unexpected error during ValidatePlugin for testcase: %#v\r\n err:%v", tc, err)
}
if tc.expectedHighestSupportedVersion != "" {
result, err := actual.Compare(tc.expectedHighestSupportedVersion)
if err != nil {
t.Fatalf("comparison failed with %v for testcase %#v", err, tc)
}
if result != 0 {
t.Fatalf("expectedHighestSupportedVersion %v, but got %v for tc: %#v", tc.expectedHighestSupportedVersion, actual, tc)
}
}
}
}

0 comments on commit a017454

Please sign in to comment.