Skip to content

Commit

Permalink
tests: add test for latest tag selection
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Gee <richard@technologee.co.uk>
  • Loading branch information
rgee0 committed Oct 31, 2024
1 parent 7645319 commit 009ca5d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 12 deletions.
33 changes: 21 additions & 12 deletions cmd/chart/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

}
55 changes: 55 additions & 0 deletions cmd/chart/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit 009ca5d

Please sign in to comment.