diff --git a/cmd/chart/upgrade.go b/cmd/chart/upgrade.go index 6722e9bbb..413c948cf 100644 --- a/cmd/chart/upgrade.go +++ b/cmd/chart/upgrade.go @@ -162,22 +162,12 @@ func updateImages(iName string, v bool) (bool, string, error) { return false, iName, errors.New("unable to list tags for " + imageName) } - var vs []*semver.Version - for _, r := range ref { - v, err := semver.NewVersion(r) - if err == nil { - vs = append(vs, v) - } - } + latestTag, hasLaterTag := getLatestTag(ref) - if len(vs) == 0 { + if !hasLaterTag { return false, iName, fmt.Errorf("no valid semver tags found for %s", imageName) } - sort.Sort(sort.Reverse(semver.Collection(vs))) - - latestTag := vs[0].Original() - laterVersionB := false // AE: Don't upgrade to an RC tag, even if it's newer. @@ -206,3 +196,22 @@ func tagIsUpgradeable(currentTag, latestTag string) bool { return latestSemVer.Compare(currentSemVer) == 1 && latestSemVer.Prerelease() == currentSemVer.Prerelease() } + +func getLatestTag(discoveredTags []string) (string, bool) { + + var vs []*semver.Version + for _, tag := range discoveredTags { + v, err := semver.NewVersion(tag) + if err == nil { + vs = append(vs, v) + } + } + + if len(vs) > 0 { + sort.Sort(sort.Reverse(semver.Collection(vs))) + return vs[0].Original(), true + } + + return "", false + +} diff --git a/cmd/chart/upgrade_test.go b/cmd/chart/upgrade_test.go index a50ba3541..9faf613c6 100644 --- a/cmd/chart/upgrade_test.go +++ b/cmd/chart/upgrade_test.go @@ -103,3 +103,58 @@ func Test_tagIsUpgradable(t *testing.T) { }) } } + +func TestGetLatestTag(t *testing.T) { + tests := []struct { + name string + discoveredTags []string + expectedTag string + expectedFound bool + }{ + { + name: "Valid semantic tags", + discoveredTags: []string{"v1.0.0", "v2.1.0", "v2.3.4", "v2.3.3"}, + expectedTag: "v2.3.4", + expectedFound: true, + }, + { + name: "No valid semantic tags", + discoveredTags: []string{"invalid", "v.a.b", "xyz"}, + expectedTag: "", + expectedFound: false, + }, + { + name: "Empty list", + discoveredTags: []string{}, + expectedTag: "", + expectedFound: false, + }, + { + name: "Mixed valid and invalid tags", + discoveredTags: []string{"v1.0.0", "invalid", "v2.1.0-beta", "v1.2.3"}, + expectedTag: "v2.1.0-beta", + expectedFound: true, + }, + { + name: "similar tag values", + discoveredTags: []string{"17", "17.0", "17.0.0"}, + expectedTag: "17", + expectedFound: true, + }, + { + name: "similar tag values different arrival order", + discoveredTags: []string{"17.0", "17", "17.0.0"}, + expectedTag: "17.0", + expectedFound: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + tag, found := getLatestTag(tc.discoveredTags) + if tag != tc.expectedTag || found != tc.expectedFound { + t.Errorf("getLatestTag(%v) = (%v, %v), want (%v, %v)", tc.discoveredTags, tag, found, tc.expectedTag, tc.expectedFound) + } + }) + } +}