diff --git a/pkg/manifests/releases/read.go b/pkg/manifests/releases/read.go index 97ca1c3c24c8..11408e1f2a39 100644 --- a/pkg/manifests/releases/read.go +++ b/pkg/manifests/releases/read.go @@ -90,23 +90,24 @@ func ReleaseForVersion(releases *releasev1.Release, version string) (*releasev1. return &release, nil } + // If we are looking for the latest pre-release, we need to compare the build metadata + // Else we continue to look for an exact match. + if !wantLatestPreRelease { + continue + } + releaseVersion, err := semver.New(release.Version) if err != nil { return nil, fmt.Errorf("invalid version for release %d: %v", release.Number, err) } - if semVer.SamePrerelease(releaseVersion) { - if wantLatestPreRelease { - // If we are looking for the latest pre-release, we need to compare the build metadata - // to find the latest one. CompareBuildMetadata will compare the build identifiers - // in order. For example: v0.19.0-dev+build.10 > v0.19.0-dev+build.9 - if latestPreReleaseVersion == nil || releaseVersion.CompareBuildMetadata(latestPreReleaseVersion) > 0 { - latestPreRelease = &release - latestPreReleaseVersion = releaseVersion - } - } else { - return &release, nil - } + if semVer.SamePrerelease(releaseVersion) && + (latestPreReleaseVersion == nil || releaseVersion.CompareBuildMetadata(latestPreReleaseVersion) > 0) { + // If we are looking for the latest pre-release, we need to compare the build metadata + // to find the latest one. CompareBuildMetadata will compare the build identifiers + // in order. For example: v0.19.0-dev+build.10 > v0.19.0-dev+build.9 + latestPreRelease = &release + latestPreReleaseVersion = releaseVersion } } diff --git a/pkg/manifests/releases/read_test.go b/pkg/manifests/releases/read_test.go index 0a47666a2738..abb477f61500 100644 --- a/pkg/manifests/releases/read_test.go +++ b/pkg/manifests/releases/read_test.go @@ -91,7 +91,7 @@ func TestReleaseForVersionSuccess(t *testing.T) { want: &releasev1.EksARelease{Version: "v0.0.1-alpha", Number: 3}, }, { - name: "multiple releases same patch, same prerelease, different build metadata", + name: "version doesn't exist", releases: &releasev1.Release{ Spec: releasev1.ReleaseSpec{ Releases: []releasev1.EksARelease{ @@ -102,38 +102,39 @@ func TestReleaseForVersionSuccess(t *testing.T) { }, }, }, - version: "v0.0.1-alpha", - want: &releasev1.EksARelease{Version: "v0.0.1-alpha+werwe", Number: 1}, + version: "v0.0.2-alpha", + want: nil, }, { - name: "version doesn't exist", + name: "want latest prerelease", releases: &releasev1.Release{ Spec: releasev1.ReleaseSpec{ Releases: []releasev1.EksARelease{ - {Version: "v0.0.1-alpha+werwe", Number: 1}, - {Version: "v0.0.1-alpha+f4fe", Number: 2}, - {Version: "v0.0.1-alpha+f43fs", Number: 3}, - {Version: "v0.0.1-alpha+f234f", Number: 4}, + {Version: "v0.0.1-alpha+build.3", Number: 1}, + {Version: "v0.0.1-alpha+build.1", Number: 2}, + {Version: "v0.0.1-alpha+build.10", Number: 3}, + {Version: "v0.0.1-alpha+build.9", Number: 4}, }, }, }, - version: "v0.0.2-alpha", - want: nil, + version: "v0.0.1-alpha+latest", + want: &releasev1.EksARelease{Version: "v0.0.1-alpha+build.10", Number: 3}, }, { - name: "want latest prerelease", + name: "want exact match with versions in same pre-release", releases: &releasev1.Release{ Spec: releasev1.ReleaseSpec{ Releases: []releasev1.EksARelease{ {Version: "v0.0.1-alpha+build.3", Number: 1}, {Version: "v0.0.1-alpha+build.1", Number: 2}, {Version: "v0.0.1-alpha+build.10", Number: 3}, - {Version: "v0.0.1-alpha+build.9", Number: 4}, + {Version: "v0.0.1-alpha+build.4", Number: 4}, + {Version: "v0.0.1-alpha+build.9", Number: 5}, }, }, }, - version: "v0.0.1-alpha+latest", - want: &releasev1.EksARelease{Version: "v0.0.1-alpha+build.10", Number: 3}, + version: "v0.0.1-alpha+build.4", + want: &releasev1.EksARelease{Version: "v0.0.1-alpha+build.4", Number: 4}, }, } for _, tt := range tests { @@ -166,7 +167,9 @@ func TestReleaseForVersionError(t *testing.T) { }, }, }, - version: "1.1.1", + // We need to ask fo the latest pre-release if we want to trigger + // the version parsing + version: "1.1.1-dev+latest", want: "invalid version for release 2", }, }