mirror of
https://github.com/helm/helm.git
synced 2026-08-07 16:41:27 +00:00
Merge pull request #32420 from mmorel-35/testifylint-manual-assert-pkg-6
chore(pkg): refactor: convert tests to testify assert/require part 6
This commit is contained in:
@@ -21,6 +21,7 @@ import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"helm.sh/helm/v4/pkg/chart/common"
|
||||
@@ -31,9 +32,7 @@ import (
|
||||
func loadChart(t *testing.T, path string) *chart.Chart {
|
||||
t.Helper()
|
||||
c, err := loader.Load(path)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to load testdata: %s", err)
|
||||
}
|
||||
require.NoError(t, err, "failed to load testdata")
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -44,19 +43,11 @@ func TestLoadDependency(t *testing.T) {
|
||||
}
|
||||
|
||||
check := func(deps []*chart.Dependency) {
|
||||
if len(deps) != 2 {
|
||||
t.Errorf("expected 2 dependencies, got %d", len(deps))
|
||||
}
|
||||
assert.Len(t, deps, 2, "expected 2 dependencies, got %d", len(deps))
|
||||
for i, tt := range tests {
|
||||
if deps[i].Name != tt.Name {
|
||||
t.Errorf("expected dependency named %q, got %q", tt.Name, deps[i].Name)
|
||||
}
|
||||
if deps[i].Version != tt.Version {
|
||||
t.Errorf("expected dependency named %q to have version %q, got %q", tt.Name, tt.Version, deps[i].Version)
|
||||
}
|
||||
if deps[i].Repository != tt.Repository {
|
||||
t.Errorf("expected dependency named %q to have repository %q, got %q", tt.Name, tt.Repository, deps[i].Repository)
|
||||
}
|
||||
assert.Equal(t, tt.Name, deps[i].Name, "expected dependency named %q, got %q", tt.Name, deps[i].Name)
|
||||
assert.Equal(t, tt.Version, deps[i].Version, "expected dependency named %q to have version %q, got %q", tt.Name, tt.Version, deps[i].Version)
|
||||
assert.Equal(t, tt.Repository, deps[i].Repository, "expected dependency named %q to have repository %q, got %q", tt.Name, tt.Repository, deps[i].Repository)
|
||||
}
|
||||
}
|
||||
c := loadChart(t, "testdata/frobnitz")
|
||||
@@ -119,18 +110,12 @@ func TestDependencyEnabled(t *testing.T) {
|
||||
for _, tc := range tests {
|
||||
c := loadChart(t, "testdata/subpop")
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if err := processDependencyEnabled(c, tc.v, ""); err != nil {
|
||||
t.Fatalf("error processing enabled dependencies %v", err)
|
||||
}
|
||||
require.NoErrorf(t, processDependencyEnabled(c, tc.v, ""), "error processing enabled dependencies")
|
||||
|
||||
names := extractChartNames(c)
|
||||
if len(names) != len(tc.e) {
|
||||
t.Fatalf("slice lengths do not match got %v, expected %v", len(names), len(tc.e))
|
||||
}
|
||||
require.Len(t, names, len(tc.e), "slice lengths do not match got %v, expected %v", len(names), len(tc.e))
|
||||
for i := range names {
|
||||
if names[i] != tc.e[i] {
|
||||
t.Fatalf("slice values do not match got %v, expected %v", names, tc.e)
|
||||
}
|
||||
require.Equal(t, tc.e[i], names[i], "slice values do not match got %v, expected %v", names, tc.e)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -221,64 +206,44 @@ func TestProcessDependencyImportValues(t *testing.T) {
|
||||
e["SCBexported2A"] = "blaster"
|
||||
e["global.SC1exported2.all.SC1exported3"] = "SC1expstr"
|
||||
|
||||
if err := processDependencyImportValues(c, false); err != nil {
|
||||
t.Fatalf("processing import values dependencies %v", err)
|
||||
}
|
||||
require.NoErrorf(t, processDependencyImportValues(c, false), "processing import values dependencies")
|
||||
cc := common.Values(c.Values)
|
||||
for kk, vv := range e {
|
||||
pv, err := cc.PathValue(kk)
|
||||
if err != nil {
|
||||
t.Fatalf("retrieving import values table %v %v", kk, err)
|
||||
}
|
||||
require.NoError(t, err, "retrieving import values table %v", kk)
|
||||
|
||||
switch pv := pv.(type) {
|
||||
case float64:
|
||||
if s := strconv.FormatFloat(pv, 'f', -1, 64); s != vv {
|
||||
t.Errorf("failed to match imported float value %v with expected %v for key %q", s, vv, kk)
|
||||
}
|
||||
s := strconv.FormatFloat(pv, 'f', -1, 64)
|
||||
assert.Equalf(t, s, vv, "failed to match imported float value %v with expected %v for key %q", s, vv, kk)
|
||||
case bool:
|
||||
if b := strconv.FormatBool(pv); b != vv {
|
||||
t.Errorf("failed to match imported bool value %v with expected %v for key %q", b, vv, kk)
|
||||
}
|
||||
b := strconv.FormatBool(pv)
|
||||
assert.Equalf(t, b, vv, "failed to match imported bool value %v with expected %v for key %q", b, vv, kk)
|
||||
default:
|
||||
if pv != vv {
|
||||
t.Errorf("failed to match imported string value %q with expected %q for key %q", pv, vv, kk)
|
||||
}
|
||||
assert.Equal(t, vv, pv, "failed to match imported string value %q with expected %q for key %q", pv, vv, kk)
|
||||
}
|
||||
}
|
||||
|
||||
// Since this was processed with coalescing there should be no null values.
|
||||
// Here we verify that.
|
||||
_, err := cc.PathValue("ensurenull")
|
||||
if err == nil {
|
||||
t.Error("expect nil value not found but found it")
|
||||
}
|
||||
require.Error(t, err, "expect nil value not found but found it")
|
||||
var xerr common.ErrNoValue
|
||||
require.ErrorAs(t, err, &xerr, "expected an ErrNoValue")
|
||||
|
||||
c = loadChart(t, "testdata/subpop")
|
||||
if err := processDependencyImportValues(c, true); err != nil {
|
||||
t.Fatalf("processing import values dependencies %v", err)
|
||||
}
|
||||
require.NoErrorf(t, processDependencyImportValues(c, true), "processing import values dependencies")
|
||||
cc = common.Values(c.Values)
|
||||
val, err := cc.PathValue("ensurenull")
|
||||
if err != nil {
|
||||
t.Error("expect value but ensurenull was not found")
|
||||
}
|
||||
if val != nil {
|
||||
t.Errorf("expect nil value but got %q instead", val)
|
||||
}
|
||||
require.NoError(t, err, "expect value but ensurenull was not found")
|
||||
assert.Nil(t, val, "expect nil value but got %q instead", val)
|
||||
}
|
||||
|
||||
func TestProcessDependencyImportValuesFromSharedDependencyToAliases(t *testing.T) {
|
||||
c := loadChart(t, "testdata/chart-with-import-from-aliased-dependencies")
|
||||
|
||||
if err := processDependencyEnabled(c, c.Values, ""); err != nil {
|
||||
t.Fatalf("expected no errors but got %q", err)
|
||||
}
|
||||
if err := processDependencyImportValues(c, true); err != nil {
|
||||
t.Fatalf("processing import values dependencies %v", err)
|
||||
}
|
||||
require.NoErrorf(t, processDependencyEnabled(c, c.Values, ""), "expected no errors")
|
||||
require.NoErrorf(t, processDependencyImportValues(c, true), "processing import values dependencies")
|
||||
e := make(map[string]string)
|
||||
|
||||
e["foo-defaults.defaultValue"] = "42"
|
||||
@@ -293,12 +258,8 @@ func TestProcessDependencyImportValuesFromSharedDependencyToAliases(t *testing.T
|
||||
cValues := common.Values(c.Values)
|
||||
for kk, vv := range e {
|
||||
pv, err := cValues.PathValue(kk)
|
||||
if err != nil {
|
||||
t.Fatalf("retrieving import values table %v %v", kk, err)
|
||||
}
|
||||
if pv != vv {
|
||||
t.Errorf("failed to match imported value %v with expected %v", pv, vv)
|
||||
}
|
||||
require.NoError(t, err, "retrieving import values table %v", kk)
|
||||
assert.Equal(t, vv, pv, "failed to match imported value %v with expected %v", pv, vv)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -325,25 +286,18 @@ func TestProcessDependencyImportValuesMultiLevelPrecedence(t *testing.T) {
|
||||
e["app2.service.port"] = "8080"
|
||||
e["app3.service.port"] = "9090"
|
||||
e["app4.service.port"] = "1234"
|
||||
if err := processDependencyImportValues(c, true); err != nil {
|
||||
t.Fatalf("processing import values dependencies %v", err)
|
||||
}
|
||||
require.NoErrorf(t, processDependencyImportValues(c, true), "processing import values dependencies")
|
||||
cc := common.Values(c.Values)
|
||||
for kk, vv := range e {
|
||||
pv, err := cc.PathValue(kk)
|
||||
if err != nil {
|
||||
t.Fatalf("retrieving import values table %v %v", kk, err)
|
||||
}
|
||||
require.NoError(t, err, "retrieving import values table %v", kk)
|
||||
|
||||
switch pv := pv.(type) {
|
||||
case float64:
|
||||
if s := strconv.FormatFloat(pv, 'f', -1, 64); s != vv {
|
||||
t.Errorf("failed to match imported float value %v with expected %v", s, vv)
|
||||
}
|
||||
s := strconv.FormatFloat(pv, 'f', -1, 64)
|
||||
assert.Equalf(t, s, vv, "failed to match imported float value %v with expected %v", s, vv)
|
||||
default:
|
||||
if pv != vv {
|
||||
t.Errorf("failed to match imported string value %q with expected %q", pv, vv)
|
||||
}
|
||||
assert.Equal(t, vv, pv, "failed to match imported string value %q with expected %q", pv, vv)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -352,127 +306,71 @@ func TestProcessDependencyImportValuesForEnabledCharts(t *testing.T) {
|
||||
c := loadChart(t, "testdata/import-values-from-enabled-subchart/parent-chart")
|
||||
nameOverride := "parent-chart-prod"
|
||||
|
||||
if err := processDependencyImportValues(c, true); err != nil {
|
||||
t.Fatalf("processing import values dependencies %v", err)
|
||||
}
|
||||
|
||||
if len(c.Dependencies()) != 2 {
|
||||
t.Fatalf("expected 2 dependencies for this chart, but got %d", len(c.Dependencies()))
|
||||
}
|
||||
|
||||
if err := processDependencyEnabled(c, c.Values, ""); err != nil {
|
||||
t.Fatalf("expected no errors but got %q", err)
|
||||
}
|
||||
|
||||
if len(c.Dependencies()) != 1 {
|
||||
t.Fatal("expected no changes in dependencies")
|
||||
}
|
||||
|
||||
if len(c.Metadata.Dependencies) != 1 {
|
||||
t.Fatalf("expected 1 dependency specified in Chart.yaml, got %d", len(c.Metadata.Dependencies))
|
||||
}
|
||||
|
||||
require.NoErrorf(t, processDependencyImportValues(c, true), "processing import values dependencies")
|
||||
require.Len(t, c.Dependencies(), 2, "expected 2 dependencies for this chart, but got %d", len(c.Dependencies()))
|
||||
require.NoErrorf(t, processDependencyEnabled(c, c.Values, ""), "expected no errors")
|
||||
require.Len(t, c.Dependencies(), 1, "expected no changes in dependencies")
|
||||
require.Len(t, c.Metadata.Dependencies, 1, "expected 1 dependency specified in Chart.yaml, got %d", len(c.Metadata.Dependencies))
|
||||
prodDependencyValues := c.Dependencies()[0].Values
|
||||
if prodDependencyValues["nameOverride"] != nameOverride {
|
||||
t.Fatalf("dependency chart name should be %s but got %s", nameOverride, prodDependencyValues["nameOverride"])
|
||||
}
|
||||
require.Equal(t, nameOverride, prodDependencyValues["nameOverride"], "dependency chart name should be %s but got %s", nameOverride, prodDependencyValues["nameOverride"])
|
||||
}
|
||||
|
||||
func TestGetAliasDependency(t *testing.T) {
|
||||
c := loadChart(t, "testdata/frobnitz")
|
||||
req := c.Metadata.Dependencies
|
||||
|
||||
if len(req) == 0 {
|
||||
t.Fatal("there are no dependencies to test")
|
||||
}
|
||||
require.NotEmpty(t, req, "there are no dependencies to test")
|
||||
|
||||
// Success case
|
||||
aliasChart := getAliasDependency(c.Dependencies(), req[0])
|
||||
if aliasChart == nil {
|
||||
t.Fatalf("failed to get dependency chart for alias %s", req[0].Name)
|
||||
}
|
||||
require.NotNil(t, aliasChart, "failed to get dependency chart for alias %s", req[0].Name)
|
||||
if req[0].Alias != "" {
|
||||
if aliasChart.Name() != req[0].Alias {
|
||||
t.Fatalf("dependency chart name should be %s but got %s", req[0].Alias, aliasChart.Name())
|
||||
}
|
||||
} else if aliasChart.Name() != req[0].Name {
|
||||
t.Fatalf("dependency chart name should be %s but got %s", req[0].Name, aliasChart.Name())
|
||||
require.Equal(t, req[0].Alias, aliasChart.Name(), "dependency chart name should be %s but got %s", req[0].Alias, aliasChart.Name())
|
||||
} else {
|
||||
require.Equalf(t, aliasChart.Name(), req[0].Name, "dependency chart name should be %s but got %s", req[0].Name, aliasChart.Name())
|
||||
}
|
||||
|
||||
if req[0].Version != "" {
|
||||
if !IsCompatibleRange(req[0].Version, aliasChart.Metadata.Version) {
|
||||
t.Fatal("dependency chart version is not in the compatible range")
|
||||
}
|
||||
require.True(t, IsCompatibleRange(req[0].Version, aliasChart.Metadata.Version), "dependency chart version is not in the compatible range")
|
||||
}
|
||||
|
||||
// Failure case
|
||||
req[0].Name = "something-else"
|
||||
if aliasChart := getAliasDependency(c.Dependencies(), req[0]); aliasChart != nil {
|
||||
t.Fatalf("expected no chart but got %s", aliasChart.Name())
|
||||
}
|
||||
require.Nilf(t, getAliasDependency(c.Dependencies(), req[0]), "expected no chart")
|
||||
|
||||
req[0].Version = "something else which is not in the compatible range"
|
||||
if IsCompatibleRange(req[0].Version, aliasChart.Metadata.Version) {
|
||||
t.Fatal("dependency chart version outside the compatible range should fail, but it succeeded")
|
||||
}
|
||||
require.False(t, IsCompatibleRange(req[0].Version, aliasChart.Metadata.Version), "dependency chart version outside the compatible range should fail, but it succeeded")
|
||||
}
|
||||
|
||||
func TestDependentChartAliases(t *testing.T) {
|
||||
c := loadChart(t, "testdata/dependent-chart-alias")
|
||||
req := c.Metadata.Dependencies
|
||||
|
||||
if len(c.Dependencies()) != 2 {
|
||||
t.Fatalf("expected 2 dependencies for this chart, but got %d", len(c.Dependencies()))
|
||||
}
|
||||
|
||||
if err := processDependencyEnabled(c, c.Values, ""); err != nil {
|
||||
t.Fatalf("expected no errors but got %q", err)
|
||||
}
|
||||
|
||||
if len(c.Dependencies()) != 3 {
|
||||
t.Fatal("expected alias dependencies to be added")
|
||||
}
|
||||
|
||||
if len(c.Dependencies()) != len(c.Metadata.Dependencies) {
|
||||
t.Fatalf("expected number of chart dependencies %d, but got %d", len(c.Metadata.Dependencies), len(c.Dependencies()))
|
||||
}
|
||||
require.Len(t, c.Dependencies(), 2, "expected 2 dependencies for this chart, but got %d", len(c.Dependencies()))
|
||||
require.NoErrorf(t, processDependencyEnabled(c, c.Values, ""), "expected no errors")
|
||||
require.Len(t, c.Dependencies(), 3, "expected alias dependencies to be added")
|
||||
require.Len(t, c.Dependencies(), len(c.Metadata.Dependencies), "expected number of chart dependencies %d, but got %d", len(c.Metadata.Dependencies), len(c.Dependencies()))
|
||||
|
||||
aliasChart := getAliasDependency(c.Dependencies(), req[2])
|
||||
|
||||
if aliasChart == nil {
|
||||
t.Fatalf("failed to get dependency chart for alias %s", req[2].Name)
|
||||
}
|
||||
if aliasChart.Parent() != c {
|
||||
t.Fatalf("dependency chart has wrong parent, expected %s but got %s", c.Name(), aliasChart.Parent().Name())
|
||||
}
|
||||
require.NotNil(t, aliasChart, "failed to get dependency chart for alias %s", req[2].Name)
|
||||
require.Equal(t, c, aliasChart.Parent(), "dependency chart has wrong parent, expected %s but got %s", c.Name(), aliasChart.Parent().Name())
|
||||
if req[2].Alias != "" {
|
||||
if aliasChart.Name() != req[2].Alias {
|
||||
t.Fatalf("dependency chart name should be %s but got %s", req[2].Alias, aliasChart.Name())
|
||||
}
|
||||
} else if aliasChart.Name() != req[2].Name {
|
||||
t.Fatalf("dependency chart name should be %s but got %s", req[2].Name, aliasChart.Name())
|
||||
require.Equal(t, req[2].Alias, aliasChart.Name(), "dependency chart name should be %s but got %s", req[2].Alias, aliasChart.Name())
|
||||
} else {
|
||||
require.Equalf(t, aliasChart.Name(), req[2].Name, "dependency chart name should be %s but got %s", req[2].Name, aliasChart.Name())
|
||||
}
|
||||
|
||||
req[2].Name = "dummy-name"
|
||||
if aliasChart := getAliasDependency(c.Dependencies(), req[2]); aliasChart != nil {
|
||||
t.Fatalf("expected no chart but got %s", aliasChart.Name())
|
||||
}
|
||||
require.Nilf(t, getAliasDependency(c.Dependencies(), req[2]), "expected no chart")
|
||||
}
|
||||
|
||||
func TestDependentChartWithSubChartsAbsentInDependency(t *testing.T) {
|
||||
c := loadChart(t, "testdata/dependent-chart-no-requirements-yaml")
|
||||
|
||||
if len(c.Dependencies()) != 2 {
|
||||
t.Fatalf("expected 2 dependencies for this chart, but got %d", len(c.Dependencies()))
|
||||
}
|
||||
|
||||
if err := processDependencyEnabled(c, c.Values, ""); err != nil {
|
||||
t.Fatalf("expected no errors but got %q", err)
|
||||
}
|
||||
|
||||
if len(c.Dependencies()) != 2 {
|
||||
t.Fatal("expected no changes in dependencies")
|
||||
}
|
||||
require.Len(t, c.Dependencies(), 2, "expected 2 dependencies for this chart, but got %d", len(c.Dependencies()))
|
||||
require.NoErrorf(t, processDependencyEnabled(c, c.Values, ""), "expected no errors")
|
||||
require.Len(t, c.Dependencies(), 2, "expected no changes in dependencies")
|
||||
}
|
||||
|
||||
func TestDependentChartWithSubChartsHelmignore(t *testing.T) {
|
||||
@@ -482,67 +380,37 @@ func TestDependentChartWithSubChartsHelmignore(t *testing.T) {
|
||||
|
||||
func TestDependentChartsWithSubChartsSymlink(t *testing.T) {
|
||||
joonix := filepath.Join("testdata", "joonix")
|
||||
if err := os.Symlink(filepath.Join("..", "..", "frobnitz"), filepath.Join(joonix, "charts", "frobnitz")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, os.Symlink(filepath.Join("..", "..", "frobnitz"), filepath.Join(joonix, "charts", "frobnitz")))
|
||||
defer os.RemoveAll(filepath.Join(joonix, "charts", "frobnitz"))
|
||||
c := loadChart(t, joonix)
|
||||
|
||||
if c.Name() != "joonix" {
|
||||
t.Fatalf("unexpected chart name: %s", c.Name())
|
||||
}
|
||||
if n := len(c.Dependencies()); n != 1 {
|
||||
t.Fatalf("expected 1 dependency for this chart, but got %d", n)
|
||||
}
|
||||
require.Equal(t, "joonix", c.Name(), "unexpected chart name: %s", c.Name())
|
||||
require.Lenf(t, c.Dependencies(), 1, "expected 1 dependency for this chart")
|
||||
}
|
||||
|
||||
func TestDependentChartsWithSubchartsAllSpecifiedInDependency(t *testing.T) {
|
||||
c := loadChart(t, "testdata/dependent-chart-with-all-in-requirements-yaml")
|
||||
|
||||
if len(c.Dependencies()) != 2 {
|
||||
t.Fatalf("expected 2 dependencies for this chart, but got %d", len(c.Dependencies()))
|
||||
}
|
||||
|
||||
if err := processDependencyEnabled(c, c.Values, ""); err != nil {
|
||||
t.Fatalf("expected no errors but got %q", err)
|
||||
}
|
||||
|
||||
if len(c.Dependencies()) != 2 {
|
||||
t.Fatal("expected no changes in dependencies")
|
||||
}
|
||||
|
||||
if len(c.Dependencies()) != len(c.Metadata.Dependencies) {
|
||||
t.Fatalf("expected number of chart dependencies %d, but got %d", len(c.Metadata.Dependencies), len(c.Dependencies()))
|
||||
}
|
||||
require.Len(t, c.Dependencies(), 2, "expected 2 dependencies for this chart, but got %d", len(c.Dependencies()))
|
||||
require.NoErrorf(t, processDependencyEnabled(c, c.Values, ""), "expected no errors")
|
||||
require.Len(t, c.Dependencies(), 2, "expected no changes in dependencies")
|
||||
require.Len(t, c.Dependencies(), len(c.Metadata.Dependencies), "expected number of chart dependencies %d, but got %d", len(c.Metadata.Dependencies), len(c.Dependencies()))
|
||||
}
|
||||
|
||||
func TestDependentChartsWithSomeSubchartsSpecifiedInDependency(t *testing.T) {
|
||||
c := loadChart(t, "testdata/dependent-chart-with-mixed-requirements-yaml")
|
||||
|
||||
if len(c.Dependencies()) != 2 {
|
||||
t.Fatalf("expected 2 dependencies for this chart, but got %d", len(c.Dependencies()))
|
||||
}
|
||||
|
||||
if err := processDependencyEnabled(c, c.Values, ""); err != nil {
|
||||
t.Fatalf("expected no errors but got %q", err)
|
||||
}
|
||||
|
||||
if len(c.Dependencies()) != 2 {
|
||||
t.Fatal("expected no changes in dependencies")
|
||||
}
|
||||
|
||||
if len(c.Metadata.Dependencies) != 1 {
|
||||
t.Fatalf("expected 1 dependency specified in Chart.yaml, got %d", len(c.Metadata.Dependencies))
|
||||
}
|
||||
require.Len(t, c.Dependencies(), 2, "expected 2 dependencies for this chart, but got %d", len(c.Dependencies()))
|
||||
require.NoErrorf(t, processDependencyEnabled(c, c.Values, ""), "expected no errors")
|
||||
require.Len(t, c.Dependencies(), 2, "expected no changes in dependencies")
|
||||
require.Len(t, c.Metadata.Dependencies, 1, "expected 1 dependency specified in Chart.yaml, got %d", len(c.Metadata.Dependencies))
|
||||
}
|
||||
|
||||
func validateDependencyTree(t *testing.T, c *chart.Chart) {
|
||||
t.Helper()
|
||||
for _, dependency := range c.Dependencies() {
|
||||
if dependency.Parent() != c {
|
||||
if dependency.Parent() != c {
|
||||
t.Fatalf("dependency chart %s has wrong parent, expected %s but got %s", dependency.Name(), c.Name(), dependency.Parent().Name())
|
||||
}
|
||||
require.Equal(t, c, dependency.Parent(), "dependency chart %s has wrong parent, expected %s but got %s", dependency.Name(), c.Name(), dependency.Parent().Name())
|
||||
}
|
||||
// recurse entire tree
|
||||
validateDependencyTree(t, dependency)
|
||||
@@ -552,16 +420,9 @@ func validateDependencyTree(t *testing.T, c *chart.Chart) {
|
||||
func TestChartWithDependencyAliasedTwiceAndDoublyReferencedSubDependency(t *testing.T) {
|
||||
c := loadChart(t, "testdata/chart-with-dependency-aliased-twice")
|
||||
|
||||
if len(c.Dependencies()) != 1 {
|
||||
t.Fatalf("expected one dependency for this chart, but got %d", len(c.Dependencies()))
|
||||
}
|
||||
require.Len(t, c.Dependencies(), 1, "expected one dependency for this chart, but got %d", len(c.Dependencies()))
|
||||
require.NoErrorf(t, processDependencyEnabled(c, c.Values, ""), "expected no errors")
|
||||
require.Len(t, c.Dependencies(), 2, "expected two dependencies after processing aliases")
|
||||
|
||||
if err := processDependencyEnabled(c, c.Values, ""); err != nil {
|
||||
t.Fatalf("expected no errors but got %q", err)
|
||||
}
|
||||
|
||||
if len(c.Dependencies()) != 2 {
|
||||
t.Fatal("expected two dependencies after processing aliases")
|
||||
}
|
||||
validateDependencyTree(t, c)
|
||||
}
|
||||
|
||||
@@ -250,5 +250,5 @@ func TestGenerateOCICreatedAnnotations(t *testing.T) {
|
||||
require.NoError(t, err, "%s annotation with value '%s' not in RFC3339 format", ocispec.AnnotationCreated, result[ocispec.AnnotationCreated])
|
||||
|
||||
// Verify creation annotation after (or equals) time test began
|
||||
assert.False(t, nowTime.Before(createdTimeAnnotation), "%s annotation with value '%s' not configured properly. Annotation value is not after %s", ocispec.AnnotationCreated, result[ocispec.AnnotationCreated], nowTimeString)
|
||||
assert.GreaterOrEqual(t, nowTime, createdTimeAnnotation, "%s annotation with value '%s' not configured properly. Annotation value is not after %s", ocispec.AnnotationCreated, result[ocispec.AnnotationCreated], nowTimeString)
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ package repo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@@ -29,6 +28,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"sigs.k8s.io/yaml"
|
||||
|
||||
"helm.sh/helm/v4/pkg/cli"
|
||||
@@ -68,37 +69,25 @@ func TestIndexCustomSchemeDownload(t *testing.T) {
|
||||
Name: repoName,
|
||||
URL: repoURL,
|
||||
}, providers)
|
||||
if err != nil {
|
||||
t.Fatalf("Problem loading chart repository from %s: %v", repoURL, err)
|
||||
}
|
||||
require.NoErrorf(t, err, "Problem loading chart repository from %s", repoURL)
|
||||
repo.CachePath = t.TempDir()
|
||||
|
||||
tempIndexFile, err := os.CreateTemp(t.TempDir(), "test-repo")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp index file: %v", err)
|
||||
}
|
||||
require.NoErrorf(t, err, "Failed to create temp index file")
|
||||
defer os.Remove(tempIndexFile.Name())
|
||||
|
||||
idx, err := repo.DownloadIndexFile()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to download index file to %s: %v", idx, err)
|
||||
}
|
||||
require.NoErrorf(t, err, "Failed to download index file to %s", idx)
|
||||
|
||||
if len(myCustomGetter.repoUrls) != 1 {
|
||||
t.Fatal("Custom Getter.Get should be called once")
|
||||
}
|
||||
require.Len(t, myCustomGetter.repoUrls, 1, "Custom Getter.Get should be called once")
|
||||
|
||||
expectedRepoIndexURL := repoURL + "/index.yaml"
|
||||
if myCustomGetter.repoUrls[0] != expectedRepoIndexURL {
|
||||
t.Fatalf("Custom Getter.Get should be called with %s", expectedRepoIndexURL)
|
||||
}
|
||||
require.Equalf(t, expectedRepoIndexURL, myCustomGetter.repoUrls[0], "Custom Getter.Get should be called with %s", expectedRepoIndexURL)
|
||||
}
|
||||
|
||||
func TestConcurrencyDownloadIndex(t *testing.T) {
|
||||
srv, err := startLocalServerForTests(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
defer srv.Close()
|
||||
|
||||
repo, err := NewChartRepository(&Entry{
|
||||
@@ -106,16 +95,12 @@ func TestConcurrencyDownloadIndex(t *testing.T) {
|
||||
URL: srv.URL,
|
||||
}, getter.All(&cli.EnvSettings{}))
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Problem loading chart repository from %s: %v", srv.URL, err)
|
||||
}
|
||||
require.NoErrorf(t, err, "Problem loading chart repository from %s", srv.URL)
|
||||
repo.CachePath = t.TempDir()
|
||||
|
||||
// initial download index
|
||||
idx, err := repo.DownloadIndexFile()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to download index file to %s: %v", idx, err)
|
||||
}
|
||||
require.NoErrorf(t, err, "Failed to download index file to %s", idx)
|
||||
|
||||
indexFName := filepath.Join(repo.CachePath, helmpath.CacheIndexFile(repo.Config.Name))
|
||||
|
||||
@@ -128,16 +113,12 @@ func TestConcurrencyDownloadIndex(t *testing.T) {
|
||||
for range 150 {
|
||||
wg.Go(func() {
|
||||
idx, err := repo.DownloadIndexFile()
|
||||
if err != nil {
|
||||
t.Errorf("Failed to download index file to %s: %v", idx, err)
|
||||
}
|
||||
assert.NoErrorf(t, err, "Failed to download index file to %s", idx)
|
||||
})
|
||||
|
||||
wg.Go(func() {
|
||||
_, err := LoadIndexFile(indexFName)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to load index file: %v", err)
|
||||
}
|
||||
assert.NoErrorf(t, err, "Failed to load index file")
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
@@ -175,9 +156,7 @@ func startLocalTLSServerForTests(handler http.Handler) (*httptest.Server, error)
|
||||
|
||||
func TestFindChartInAuthAndTLSAndPassRepoURL(t *testing.T) {
|
||||
srv, err := startLocalTLSServerForTests(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
defer srv.Close()
|
||||
|
||||
chartURL, err := FindChartInRepoURL(
|
||||
@@ -186,12 +165,8 @@ func TestFindChartInAuthAndTLSAndPassRepoURL(t *testing.T) {
|
||||
getter.All(&cli.EnvSettings{}),
|
||||
WithInsecureSkipTLSVerify(true),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("%v", err)
|
||||
}
|
||||
if chartURL != "https://charts.helm.sh/stable/nginx-0.2.0.tgz" {
|
||||
t.Errorf("%s is not the valid URL", chartURL)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
assert.Equalf(t, "https://charts.helm.sh/stable/nginx-0.2.0.tgz", chartURL, "%s is not the valid URL", chartURL)
|
||||
|
||||
// If the insecureSkipTLSVerify is false, it will return an error that contains "x509: certificate signed by unknown authority".
|
||||
_, err = FindChartInRepoURL(srv.URL, "nginx", getter.All(&cli.EnvSettings{}), WithChartVersion("0.1.0"))
|
||||
@@ -203,33 +178,23 @@ func TestFindChartInAuthAndTLSAndPassRepoURL(t *testing.T) {
|
||||
if !strings.Contains(err.Error(), "x509: “Acme Co” certificate is not trusted") && !strings.Contains(err.Error(), "x509: certificate signed by unknown authority") {
|
||||
t.Errorf("Expected TLS error for function FindChartInAuthAndTLSAndPassRepoURL not found, but got a different error (%v)", err)
|
||||
}
|
||||
} else if !strings.Contains(err.Error(), "x509: certificate signed by unknown authority") {
|
||||
t.Errorf("Expected TLS error for function FindChartInAuthAndTLSAndPassRepoURL not found, but got a different error (%v)", err)
|
||||
} else {
|
||||
assert.ErrorContainsf(t, err, "x509: certificate signed by unknown authority", "Expected TLS error for function FindChartInAuthAndTLSAndPassRepoURL not found, but got a different error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindChartInRepoURL(t *testing.T) {
|
||||
srv, err := startLocalServerForTests(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
defer srv.Close()
|
||||
|
||||
chartURL, err := FindChartInRepoURL(srv.URL, "nginx", getter.All(&cli.EnvSettings{}))
|
||||
if err != nil {
|
||||
t.Fatalf("%v", err)
|
||||
}
|
||||
if chartURL != "https://charts.helm.sh/stable/nginx-0.2.0.tgz" {
|
||||
t.Errorf("%s is not the valid URL", chartURL)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
assert.Equalf(t, "https://charts.helm.sh/stable/nginx-0.2.0.tgz", chartURL, "%s is not the valid URL", chartURL)
|
||||
|
||||
chartURL, err = FindChartInRepoURL(srv.URL, "nginx", getter.All(&cli.EnvSettings{}), WithChartVersion("0.1.0"))
|
||||
if err != nil {
|
||||
t.Errorf("%s", err)
|
||||
}
|
||||
if chartURL != "https://charts.helm.sh/stable/nginx-0.1.0.tgz" {
|
||||
t.Errorf("%s is not the valid URL", chartURL)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
assert.Equalf(t, "https://charts.helm.sh/stable/nginx-0.1.0.tgz", chartURL, "%s is not the valid URL", chartURL)
|
||||
}
|
||||
|
||||
func TestErrorFindChartInRepoURL(t *testing.T) {
|
||||
@@ -237,38 +202,26 @@ func TestErrorFindChartInRepoURL(t *testing.T) {
|
||||
RepositoryCache: t.TempDir(),
|
||||
})
|
||||
|
||||
if _, err := FindChartInRepoURL("http://someserver/something", "nginx", g); err == nil {
|
||||
t.Error("Expected error for bad chart URL, but did not get any errors")
|
||||
} else if !strings.Contains(err.Error(), `looks like "http://someserver/something" is not a valid chart repository or cannot be reached`) {
|
||||
t.Errorf("Expected error for bad chart URL, but got a different error (%v)", err)
|
||||
}
|
||||
_, err := FindChartInRepoURL("http://someserver/something", "nginx", g)
|
||||
require.Error(t, err, "Expected error for bad chart URL, but did not get any errors")
|
||||
require.ErrorContainsf(t, err, `looks like "http://someserver/something" is not a valid chart repository or cannot be reached`, "Expected error for bad chart URL, but got a different error")
|
||||
|
||||
srv, err := startLocalServerForTests(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
defer srv.Close()
|
||||
|
||||
if _, err = FindChartInRepoURL(srv.URL, "nginx1", g); err == nil {
|
||||
t.Error("Expected error for chart not found, but did not get any errors")
|
||||
} else if err.Error() != `chart "nginx1" not found in `+srv.URL+` repository` {
|
||||
t.Errorf("Expected error for chart not found, but got a different error (%v)", err)
|
||||
}
|
||||
if !errors.Is(err, ChartNotFoundError{}) {
|
||||
t.Error("error is not of correct error type structure")
|
||||
}
|
||||
_, err = FindChartInRepoURL(srv.URL, "nginx1", g)
|
||||
require.Error(t, err, "Expected error for chart not found, but did not get any errors")
|
||||
require.EqualError(t, err, `chart "nginx1" not found in `+srv.URL+` repository`, "Expected error for chart not found, but got a different error")
|
||||
require.ErrorIs(t, err, ChartNotFoundError{}, "error is not of correct error type structure")
|
||||
|
||||
if _, err = FindChartInRepoURL(srv.URL, "nginx1", g, WithChartVersion("0.1.0")); err == nil {
|
||||
t.Error("Expected error for chart not found, but did not get any errors")
|
||||
} else if err.Error() != `chart "nginx1" version "0.1.0" not found in `+srv.URL+` repository` {
|
||||
t.Errorf("Expected error for chart not found, but got a different error (%v)", err)
|
||||
}
|
||||
_, err = FindChartInRepoURL(srv.URL, "nginx1", g, WithChartVersion("0.1.0"))
|
||||
require.Error(t, err, "Expected error for chart not found, but did not get any errors")
|
||||
require.EqualError(t, err, `chart "nginx1" version "0.1.0" not found in `+srv.URL+` repository`, "Expected error for chart not found, but got a different error")
|
||||
|
||||
if _, err = FindChartInRepoURL(srv.URL, "chartWithNoURL", g); err == nil {
|
||||
t.Error("Expected error for no chart URLs available, but did not get any errors")
|
||||
} else if err.Error() != `chart "chartWithNoURL" has no downloadable URLs` {
|
||||
t.Errorf("Expected error for chart not found, but got a different error (%v)", err)
|
||||
}
|
||||
_, err = FindChartInRepoURL(srv.URL, "chartWithNoURL", g)
|
||||
require.Error(t, err, "Expected error for no chart URLs available, but did not get any errors")
|
||||
assert.EqualError(t, err, `chart "chartWithNoURL" has no downloadable URLs`, "Expected error for chart not found, but got a different error")
|
||||
}
|
||||
|
||||
func TestResolveReferenceURL(t *testing.T) {
|
||||
@@ -286,11 +239,7 @@ func TestResolveReferenceURL(t *testing.T) {
|
||||
{"http://localhost:8123/charts?with=queryparameter", "/nginx-0.2.0.tgz", "http://localhost:8123/nginx-0.2.0.tgz?with=queryparameter"},
|
||||
} {
|
||||
chartURL, err := ResolveReferenceURL(tt.baseURL, tt.refURL)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error in ResolveReferenceURL(%q, %q): %s", tt.baseURL, tt.refURL, err)
|
||||
}
|
||||
if chartURL != tt.chartURL {
|
||||
t.Errorf("expected ResolveReferenceURL(%q, %q) to equal %q, got %q", tt.baseURL, tt.refURL, tt.chartURL, chartURL)
|
||||
}
|
||||
require.NoErrorf(t, err, "unexpected error in ResolveReferenceURL(%q, %q)", tt.baseURL, tt.refURL)
|
||||
assert.Equalf(t, chartURL, tt.chartURL, "expected ResolveReferenceURL(%q, %q) to equal %q, got %q", tt.baseURL, tt.refURL, tt.chartURL, chartURL)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user