mirror of https://github.com/helm/helm
Merge cf811bb11f
into 6717ea5e3c
commit
26189f5955
@ -0,0 +1,59 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package registry // import "helm.sh/helm/v4/pkg/registry"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
)
|
||||
|
||||
func GetTagMatchingVersionOrConstraint(tags []string, versionString string) (string, error) {
|
||||
var constraint *semver.Constraints
|
||||
if versionString == "" {
|
||||
// If string is empty, set wildcard constraint
|
||||
constraint, _ = semver.NewConstraint("*")
|
||||
} else {
|
||||
// when customer inputs specific version, check whether there's an exact match first
|
||||
for _, v := range tags {
|
||||
if versionString == v {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise set constraint to the string given
|
||||
var err error
|
||||
constraint, err = semver.NewConstraint(versionString)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise try to find the first available version matching the string,
|
||||
// in case it is a constraint
|
||||
for _, v := range tags {
|
||||
test, err := semver.NewVersion(v)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if constraint.Check(test) {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("could not locate a version matching provided version string %s", versionString)
|
||||
}
|
@ -0,0 +1,122 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package registry
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetTagMatchingVersionOrConstraint_ExactMatch(t *testing.T) {
|
||||
tags := []string{"1.0.0", "1.2.3", "2.0.0"}
|
||||
got, err := GetTagMatchingVersionOrConstraint(tags, "1.2.3")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "1.2.3" {
|
||||
t.Fatalf("expected exact match '1.2.3', got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTagMatchingVersionOrConstraint_EmptyVersionWildcard(t *testing.T) {
|
||||
// Includes a non-semver tag which should be skipped
|
||||
tags := []string{"latest", "0.9.0", "1.0.0"}
|
||||
got, err := GetTagMatchingVersionOrConstraint(tags, "")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
// Should pick the first valid semver tag in order, which is 0.9.0
|
||||
if got != "0.9.0" {
|
||||
t.Fatalf("expected '0.9.0', got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTagMatchingVersionOrConstraint_ConstraintRange(t *testing.T) {
|
||||
tags := []string{"0.5.0", "1.0.0", "1.1.0", "2.0.0"}
|
||||
|
||||
// Caret range
|
||||
got, err := GetTagMatchingVersionOrConstraint(tags, "^1.0.0")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "1.0.0" { // first match in order
|
||||
t.Fatalf("expected '1.0.0', got %q", got)
|
||||
}
|
||||
|
||||
// Compound range
|
||||
got, err = GetTagMatchingVersionOrConstraint(tags, ">=1.0.0 <2.0.0")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "1.0.0" {
|
||||
t.Fatalf("expected '1.0.0', got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTagMatchingVersionOrConstraint_InvalidConstraint(t *testing.T) {
|
||||
tags := []string{"1.0.0"}
|
||||
_, err := GetTagMatchingVersionOrConstraint(tags, ">a1")
|
||||
if err == nil {
|
||||
t.Fatalf("expected error for invalid constraint")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTagMatchingVersionOrConstraint_NoMatches(t *testing.T) {
|
||||
tags := []string{"0.1.0", "0.2.0"}
|
||||
_, err := GetTagMatchingVersionOrConstraint(tags, ">=1.0.0")
|
||||
if err == nil {
|
||||
t.Fatalf("expected error when no tags match")
|
||||
}
|
||||
if !strings.Contains(err.Error(), ">=1.0.0") {
|
||||
t.Fatalf("expected error to contain version string, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTagMatchingVersionOrConstraint_SkipsNonSemverTags(t *testing.T) {
|
||||
tags := []string{"alpha", "1.0.0", "beta", "1.1.0"}
|
||||
got, err := GetTagMatchingVersionOrConstraint(tags, ">=1.0.0 <2.0.0")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "1.0.0" {
|
||||
t.Fatalf("expected '1.0.0', got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTagMatchingVersionOrConstraint_OrderMatters_FirstMatchReturned(t *testing.T) {
|
||||
// Both 1.2.0 and 1.3.0 satisfy >=1.2.0 <2.0.0, but the function returns the first in input order
|
||||
tags := []string{"1.3.0", "1.2.0"}
|
||||
got, err := GetTagMatchingVersionOrConstraint(tags, ">=1.2.0 <2.0.0")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "1.3.0" {
|
||||
t.Fatalf("expected '1.3.0' (first satisfying tag), got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTagMatchingVersionOrConstraint_ExactMatchHasPrecedence(t *testing.T) {
|
||||
// Exact match should be returned even if another earlier tag would match the parsed constraint
|
||||
tags := []string{"1.3.0", "1.2.3"}
|
||||
got, err := GetTagMatchingVersionOrConstraint(tags, "1.2.3")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "1.2.3" {
|
||||
t.Fatalf("expected exact match '1.2.3', got %q", got)
|
||||
}
|
||||
}
|
Loading…
Reference in new issue