Merge pull request #32428 from mmorel-35/testifylint-manual-assert-pkg-13

chore(pkg): refactor: convert tests to testify assert/require part 13
pull/32453/head
Terry Howe 2 months ago committed by GitHub
commit da9e2b6bab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -48,9 +48,7 @@ func TestPullSetRegistryClient(t *testing.T) {
func TestPullRun_ChartNotFound(t *testing.T) { func TestPullRun_ChartNotFound(t *testing.T) {
srv, err := startLocalServerForTests(t, nil) srv, err := startLocalServerForTests(t, nil)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
defer srv.Close() defer srv.Close()
config := actionConfigFixture(t) config := actionConfigFixture(t)

@ -20,9 +20,11 @@ import (
"errors" "errors"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
chart "helm.sh/helm/v4/pkg/chart/v2" chart "helm.sh/helm/v4/pkg/chart/v2"
"helm.sh/helm/v4/pkg/chart/v2/lint/support" "helm.sh/helm/v4/pkg/chart/v2/lint/support"
chartutil "helm.sh/helm/v4/pkg/chart/v2/util" chartutil "helm.sh/helm/v4/pkg/chart/v2/util"
@ -47,35 +49,17 @@ var badChartName, _ = chartutil.LoadChartfile(badChartNamePath)
func TestValidateChartYamlNotDirectory(t *testing.T) { func TestValidateChartYamlNotDirectory(t *testing.T) {
_ = os.Mkdir(nonExistingChartFilePath, os.ModePerm) _ = os.Mkdir(nonExistingChartFilePath, os.ModePerm)
defer os.Remove(nonExistingChartFilePath) defer os.Remove(nonExistingChartFilePath)
assert.Error(t, validateChartYamlNotDirectory(nonExistingChartFilePath), "validateChartYamlNotDirectory to return a linter error, got no error")
err := validateChartYamlNotDirectory(nonExistingChartFilePath)
if err == nil {
t.Error("validateChartYamlNotDirectory to return a linter error, got no error")
}
} }
func TestValidateChartYamlFormat(t *testing.T) { func TestValidateChartYamlFormat(t *testing.T) {
err := validateChartYamlFormat(errors.New("Read error")) require.Error(t, validateChartYamlFormat(errors.New("Read error")), "validateChartYamlFormat to return a linter error, got no error")
if err == nil { assert.NoError(t, validateChartYamlFormat(nil), "validateChartYamlFormat to return no error, got a linter error")
t.Error("validateChartYamlFormat to return a linter error, got no error")
}
err = validateChartYamlFormat(nil)
if err != nil {
t.Error("validateChartYamlFormat to return no error, got a linter error")
}
} }
func TestValidateChartName(t *testing.T) { func TestValidateChartName(t *testing.T) {
err := validateChartName(badChart) require.Error(t, validateChartName(badChart), "validateChartName to return a linter error, got no error")
if err == nil { assert.Error(t, validateChartName(badChartName), "expected validateChartName to return a linter error for an invalid name, got no error")
t.Error("validateChartName to return a linter error, got no error")
}
err = validateChartName(badChartName)
if err == nil {
t.Error("expected validateChartName to return a linter error for an invalid name, got no error")
}
} }
func TestValidateChartVersion(t *testing.T) { func TestValidateChartVersion(t *testing.T) {
@ -93,18 +77,12 @@ func TestValidateChartVersion(t *testing.T) {
for _, test := range failTest { for _, test := range failTest {
badChart.Version = test.Version badChart.Version = test.Version
err := validateChartVersion(badChart) require.ErrorContainsf(t, validateChartVersion(badChart), test.ErrorMsg, "validateChartVersion(%s) to return \"%s\", got no error", test.Version, test.ErrorMsg)
if err == nil || !strings.Contains(err.Error(), test.ErrorMsg) {
t.Errorf("validateChartVersion(%s) to return \"%s\", got no error", test.Version, test.ErrorMsg)
}
} }
for _, version := range successTest { for _, version := range successTest {
badChart.Version = version badChart.Version = version
err := validateChartVersion(badChart) assert.NoError(t, validateChartVersion(badChart), "validateChartVersion(%s) to return no error, got a linter error", version)
if err != nil {
t.Errorf("validateChartVersion(%s) to return no error, got a linter error", version)
}
} }
} }
@ -122,18 +100,12 @@ func TestValidateChartVersionStrictSemVerV2(t *testing.T) {
for _, test := range failTest { for _, test := range failTest {
badChart.Version = test.Version badChart.Version = test.Version
err := validateChartVersionStrictSemVerV2(badChart) require.ErrorContainsf(t, validateChartVersionStrictSemVerV2(badChart), test.ErrorMsg, "validateChartVersionStrictSemVerV2(%s) to return \"%s\", got no error", test.Version, test.ErrorMsg)
if err == nil || !strings.Contains(err.Error(), test.ErrorMsg) {
t.Errorf("validateChartVersionStrictSemVerV2(%s) to return \"%s\", got no error", test.Version, test.ErrorMsg)
}
} }
for _, version := range successTest { for _, version := range successTest {
badChart.Version = version badChart.Version = version
err := validateChartVersionStrictSemVerV2(badChart) assert.NoError(t, validateChartVersionStrictSemVerV2(badChart), "validateChartVersionStrictSemVerV2(%s) to return no error, got a linter error", version)
if err != nil {
t.Errorf("validateChartVersionStrictSemVerV2(%s) to return no error, got a linter error", version)
}
} }
} }
@ -158,29 +130,17 @@ func TestValidateChartMaintainer(t *testing.T) {
for _, test := range failTest { for _, test := range failTest {
badChart.Maintainers = []*chart.Maintainer{{Name: test.Name, Email: test.Email}} badChart.Maintainers = []*chart.Maintainer{{Name: test.Name, Email: test.Email}}
err := validateChartMaintainer(badChart) require.ErrorContainsf(t, validateChartMaintainer(badChart), test.ErrorMsg, "validateChartMaintainer(%s, %s) to return \"%s\", got no error", test.Name, test.Email, test.ErrorMsg)
if err == nil || !strings.Contains(err.Error(), test.ErrorMsg) {
t.Errorf("validateChartMaintainer(%s, %s) to return \"%s\", got no error", test.Name, test.Email, test.ErrorMsg)
}
} }
for _, test := range successTest { for _, test := range successTest {
badChart.Maintainers = []*chart.Maintainer{{Name: test.Name, Email: test.Email}} badChart.Maintainers = []*chart.Maintainer{{Name: test.Name, Email: test.Email}}
err := validateChartMaintainer(badChart) require.NoError(t, validateChartMaintainer(badChart), "validateChartMaintainer(%s, %s)", test.Name, test.Email)
if err != nil {
t.Errorf("validateChartMaintainer(%s, %s) to return no error, got %s", test.Name, test.Email, err.Error())
}
} }
// Testing for an empty maintainer // Testing for an empty maintainer
badChart.Maintainers = []*chart.Maintainer{nil} badChart.Maintainers = []*chart.Maintainer{nil}
err := validateChartMaintainer(badChart) assert.EqualError(t, validateChartMaintainer(badChart), "a maintainer entry is empty")
if err == nil {
t.Error("validateChartMaintainer did not return error for nil maintainer as expected")
}
if err.Error() != "a maintainer entry is empty" {
t.Errorf("validateChartMaintainer returned unexpected error for nil maintainer: %s", err.Error())
}
} }
func TestValidateChartSources(t *testing.T) { func TestValidateChartSources(t *testing.T) {
@ -188,18 +148,12 @@ func TestValidateChartSources(t *testing.T) {
var successTest = []string{"http://riverrun.io", "https://riverrun.io", "https://riverrun.io/blackfish"} var successTest = []string{"http://riverrun.io", "https://riverrun.io", "https://riverrun.io/blackfish"}
for _, test := range failTest { for _, test := range failTest {
badChart.Sources = []string{test} badChart.Sources = []string{test}
err := validateChartSources(badChart) require.ErrorContainsf(t, validateChartSources(badChart), "invalid source URL", "validateChartSources(%s) to return \"invalid source URL\", got no error", test)
if err == nil || !strings.Contains(err.Error(), "invalid source URL") {
t.Errorf("validateChartSources(%s) to return \"invalid source URL\", got no error", test)
}
} }
for _, test := range successTest { for _, test := range successTest {
badChart.Sources = []string{test} badChart.Sources = []string{test}
err := validateChartSources(badChart) assert.NoError(t, validateChartSources(badChart), "validateChartSources(%s) to return no error", test)
if err != nil {
t.Errorf("validateChartSources(%s) to return no error, got %s", test, err.Error())
}
} }
} }
@ -209,24 +163,13 @@ func TestValidateChartIconPresence(t *testing.T) {
Icon: "", Icon: "",
} }
err := validateChartIconPresence(testChart) assert.ErrorContainsf(t, validateChartIconPresence(testChart), "icon is recommended", "expected %q", "icon is recommended")
if err == nil {
t.Error("validateChartIconPresence to return a linter error, got no error")
} else if !strings.Contains(err.Error(), "icon is recommended") {
t.Errorf("expected %q, got %q", "icon is recommended", err.Error())
}
}) })
t.Run("Icon present", func(t *testing.T) { t.Run("Icon present", func(t *testing.T) {
testChart := &chart.Metadata{ testChart := &chart.Metadata{
Icon: "http://example.org/icon.png", Icon: "http://example.org/icon.png",
} }
assert.NoError(t, validateChartIconPresence(testChart))
err := validateChartIconPresence(testChart)
if err != nil {
t.Errorf("Unexpected error: %q", err.Error())
}
}) })
} }
@ -235,18 +178,12 @@ func TestValidateChartIconURL(t *testing.T) {
var successTest = []string{"http://riverrun.io", "https://riverrun.io", "https://riverrun.io/blackfish.png"} var successTest = []string{"http://riverrun.io", "https://riverrun.io", "https://riverrun.io/blackfish.png"}
for _, test := range failTest { for _, test := range failTest {
badChart.Icon = test badChart.Icon = test
err := validateChartIconURL(badChart) require.ErrorContainsf(t, validateChartIconURL(badChart), "invalid icon URL", "validateChartIconURL(%s) to return \"invalid icon URL\", got no error", test)
if err == nil || !strings.Contains(err.Error(), "invalid icon URL") {
t.Errorf("validateChartIconURL(%s) to return \"invalid icon URL\", got no error", test)
}
} }
for _, test := range successTest { for _, test := range successTest {
badChart.Icon = test badChart.Icon = test
err := validateChartSources(badChart) assert.NoError(t, validateChartSources(badChart), "validateChartIconURL(%s) to return no error", test)
if err != nil {
t.Errorf("validateChartIconURL(%s) to return no error, got %s", test, err.Error())
}
} }
} }
@ -257,37 +194,15 @@ func TestChartfile(t *testing.T) {
msgs := linter.Messages msgs := linter.Messages
expectedNumberOfErrorMessages := 7 expectedNumberOfErrorMessages := 7
if len(msgs) != expectedNumberOfErrorMessages { require.Lenf(t, msgs, expectedNumberOfErrorMessages, "Expected %d errors, got %d", expectedNumberOfErrorMessages, len(msgs))
t.Errorf("Expected %d errors, got %d", expectedNumberOfErrorMessages, len(msgs))
return
}
if !strings.Contains(msgs[0].Err.Error(), "name is required") {
t.Errorf("Unexpected message 0: %s", msgs[0].Err)
}
if !strings.Contains(msgs[1].Err.Error(), "apiVersion is required. The value must be either \"v1\" or \"v2\"") {
t.Errorf("Unexpected message 1: %s", msgs[1].Err)
}
if !strings.Contains(msgs[2].Err.Error(), "version '0.0.0.0' is not a valid SemVer") {
t.Errorf("Unexpected message 2: %s", msgs[2].Err)
}
if !strings.Contains(msgs[3].Err.Error(), "icon is recommended") {
t.Errorf("Unexpected message 3: %s", msgs[3].Err)
}
if !strings.Contains(msgs[4].Err.Error(), "chart type is not valid in apiVersion") {
t.Errorf("Unexpected message 4: %s", msgs[4].Err)
}
if !strings.Contains(msgs[5].Err.Error(), "dependencies are not valid in the Chart file with apiVersion") { require.ErrorContains(t, msgs[0].Err, "name is required", "Unexpected message 0: %s", msgs[0].Err)
t.Errorf("Unexpected message 5: %s", msgs[5].Err) require.ErrorContains(t, msgs[1].Err, "apiVersion is required. The value must be either \"v1\" or \"v2\"", "Unexpected message 1: %s", msgs[1].Err)
} require.ErrorContains(t, msgs[2].Err, "version '0.0.0.0' is not a valid SemVer", "Unexpected message 2: %s", msgs[2].Err)
if !strings.Contains(msgs[6].Err.Error(), "version '0.0.0.0' is not a valid SemVerV2") { require.ErrorContains(t, msgs[3].Err, "icon is recommended", "Unexpected message 3: %s", msgs[3].Err)
t.Errorf("Unexpected message 6: %s", msgs[6].Err) require.ErrorContains(t, msgs[4].Err, "chart type is not valid in apiVersion", "Unexpected message 4: %s", msgs[4].Err)
} require.ErrorContains(t, msgs[5].Err, "dependencies are not valid in the Chart file with apiVersion", "Unexpected message 5: %s", msgs[5].Err)
assert.ErrorContains(t, msgs[6].Err, "version '0.0.0.0' is not a valid SemVerV2", "Unexpected message 6: %s", msgs[6].Err)
}) })
t.Run("Chart.yaml validity issues due to type mismatch", func(t *testing.T) { t.Run("Chart.yaml validity issues due to type mismatch", func(t *testing.T) {
@ -301,19 +216,9 @@ func TestChartfile(t *testing.T) {
return return
} }
if !strings.Contains(msgs[0].Err.Error(), "version should be of type string") { require.ErrorContains(t, msgs[0].Err, "version should be of type string", "Unexpected message 0: %s", msgs[0].Err)
t.Errorf("Unexpected message 0: %s", msgs[0].Err) require.ErrorContains(t, msgs[1].Err, "version '7.2445e+06' is not a valid SemVer", "Unexpected message 1: %s", msgs[1].Err)
} require.ErrorContains(t, msgs[2].Err, "appVersion should be of type string", "Unexpected message 2: %s", msgs[2].Err)
assert.ErrorContains(t, msgs[3].Err, "version '7.2445e+06' is not a valid SemVerV2", "Unexpected message 3: %s", msgs[3].Err)
if !strings.Contains(msgs[1].Err.Error(), "version '7.2445e+06' is not a valid SemVer") {
t.Errorf("Unexpected message 1: %s", msgs[1].Err)
}
if !strings.Contains(msgs[2].Err.Error(), "appVersion should be of type string") {
t.Errorf("Unexpected message 2: %s", msgs[2].Err)
}
if !strings.Contains(msgs[3].Err.Error(), "version '7.2445e+06' is not a valid SemVerV2") {
t.Errorf("Unexpected message 3: %s", msgs[3].Err)
}
}) })
} }

@ -20,6 +20,9 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
chart "helm.sh/helm/v4/pkg/chart/v2" chart "helm.sh/helm/v4/pkg/chart/v2"
"helm.sh/helm/v4/pkg/repo/v1" "helm.sh/helm/v4/pkg/repo/v1"
) )
@ -40,24 +43,16 @@ func TestSortScore(t *testing.T) {
// Test Score // Test Score
for i := range expectScore { for i := range expectScore {
if expectScore[i] != in[i].Score { assert.Equalf(t, expectScore[i], in[i].Score, "Sort error on index %d: expected %d, got %d", i, expectScore[i], in[i].Score)
t.Errorf("Sort error on index %d: expected %d, got %d", i, expectScore[i], in[i].Score)
}
} }
// Test Name // Test Name
for i := range expect { for i := range expect {
if expect[i] != in[i].Name { assert.Equalf(t, expect[i], in[i].Name, "Sort error: expected %s, got %s", expect[i], in[i].Name)
t.Errorf("Sort error: expected %s, got %s", expect[i], in[i].Name)
}
} }
// Test version of last two items // Test version of last two items
if in[5].Chart.Version != "1.2.4" { assert.Equalf(t, "1.2.4", in[5].Chart.Version, "Expected 1.2.4, got %s", in[5].Chart.Version)
t.Errorf("Expected 1.2.4, got %s", in[5].Chart.Version) assert.Equal(t, "1.2.3", in[6].Chart.Version, "Expected 1.2.3 to be last")
}
if in[6].Chart.Version != "1.2.3" {
t.Error("Expected 1.2.3 to be last")
}
} }
var indexfileEntries = map[string]repo.ChartVersions{ var indexfileEntries = map[string]repo.ChartVersions{
@ -122,30 +117,22 @@ func loadTestIndex(_ *testing.T, all bool) *Index {
func TestAll(t *testing.T) { func TestAll(t *testing.T) {
i := loadTestIndex(t, false) i := loadTestIndex(t, false)
all := i.All() all := i.All()
if len(all) != 4 { assert.Lenf(t, all, 4, "Expected 4 entries, got %d", len(all))
t.Errorf("Expected 4 entries, got %d", len(all))
}
i = loadTestIndex(t, true) i = loadTestIndex(t, true)
all = i.All() all = i.All()
if len(all) != 5 { assert.Lenf(t, all, 5, "Expected 5 entries, got %d", len(all))
t.Errorf("Expected 5 entries, got %d", len(all))
}
} }
func TestAddRepo_Sort(t *testing.T) { func TestAddRepo_Sort(t *testing.T) {
i := loadTestIndex(t, true) i := loadTestIndex(t, true)
sr, err := i.Search("TESTING/SANTA-MARIA", 100, false) sr, err := i.Search("TESTING/SANTA-MARIA", 100, false)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
SortScore(sr) SortScore(sr)
ch := sr[0] ch := sr[0]
expect := "1.2.3" expect := "1.2.3"
if ch.Chart.Version != expect { assert.Equalf(t, ch.Chart.Version, expect, "Expected %q, got %q", expect, ch.Chart.Version)
t.Errorf("Expected %q, got %q", expect, ch.Chart.Version)
}
} }
func TestSearchByName(t *testing.T) { func TestSearchByName(t *testing.T) {
@ -247,9 +234,7 @@ func TestSearchByName(t *testing.T) {
charts, err := i.Search(tt.query, 100, tt.regexp) charts, err := i.Search(tt.query, 100, tt.regexp)
if err != nil { if err != nil {
if tt.fail { if tt.fail {
if !strings.Contains(err.Error(), tt.failMsg) { require.ErrorContains(t, err, tt.failMsg)
t.Fatalf("Unexpected error message: %s", err)
}
return return
} }
t.Fatalf("%s: %s", tt.name, err) t.Fatalf("%s: %s", tt.name, err)
@ -258,9 +243,7 @@ func TestSearchByName(t *testing.T) {
SortScore(charts) SortScore(charts)
l := len(charts) l := len(charts)
if l != len(tt.expect) { require.Lenf(t, tt.expect, l, "Expected %d result, got %d", len(tt.expect), l)
t.Fatalf("Expected %d result, got %d", len(tt.expect), l)
}
// For empty result sets, just keep going. // For empty result sets, just keep going.
if l == 0 { if l == 0 {
return return
@ -268,9 +251,7 @@ func TestSearchByName(t *testing.T) {
for i, got := range charts { for i, got := range charts {
ex := tt.expect[i] ex := tt.expect[i]
if got.Name != ex.Name { assert.Equalf(t, got.Name, ex.Name, "[%d]: Expected name %q, got %q", i, ex.Name, got.Name)
t.Errorf("[%d]: Expected name %q, got %q", i, ex.Name, got.Name)
}
} }
}) })
} }
@ -280,12 +261,8 @@ func TestSearchByNameAll(t *testing.T) {
// Test with the All bit turned on. // Test with the All bit turned on.
i := loadTestIndex(t, true) i := loadTestIndex(t, true)
cs, err := i.Search("santa-maria", 100, false) cs, err := i.Search("santa-maria", 100, false)
if err != nil { require.NoError(t, err)
t.Fatal(err) assert.Lenf(t, cs, 2, "expected 2 charts, got %d", len(cs))
}
if len(cs) != 2 {
t.Errorf("expected 2 charts, got %d", len(cs))
}
} }
func TestCalcScore(t *testing.T) { func TestCalcScore(t *testing.T) {
@ -293,16 +270,12 @@ func TestCalcScore(t *testing.T) {
fields := []string{"aaa", "bbb", "ccc", "ddd"} fields := []string{"aaa", "bbb", "ccc", "ddd"}
matchline := strings.Join(fields, sep) matchline := strings.Join(fields, sep)
if r := i.calcScore(2, matchline); r != 0 { r := i.calcScore(2, matchline)
t.Errorf("Expected 0, got %d", r) assert.Equalf(t, 0, r, "Expected 0, got %d", r)
} r = i.calcScore(5, matchline)
if r := i.calcScore(5, matchline); r != 1 { assert.Equalf(t, 1, r, "Expected 1, got %d", r)
t.Errorf("Expected 1, got %d", r) r = i.calcScore(10, matchline)
} assert.Equalf(t, 2, r, "Expected 2, got %d", r)
if r := i.calcScore(10, matchline); r != 2 { r = i.calcScore(14, matchline)
t.Errorf("Expected 2, got %d", r) assert.Equalf(t, 3, r, "Expected 3, got %d", r)
}
if r := i.calcScore(14, matchline); r != 3 {
t.Errorf("Expected 3, got %d", r)
}
} }

@ -21,9 +21,9 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"helm.sh/helm/v4/pkg/registry" "helm.sh/helm/v4/pkg/registry"
@ -31,13 +31,10 @@ import (
func TestNewOCIPusher(t *testing.T) { func TestNewOCIPusher(t *testing.T) {
p, err := NewOCIPusher() p, err := NewOCIPusher()
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
if _, ok := p.(*OCIPusher); !ok { _, ok := p.(*OCIPusher)
t.Fatal("Expected NewOCIPusher to produce an *OCIPusher") require.True(t, ok, "Expected NewOCIPusher to produce an *OCIPusher")
}
cd := "../../testdata" cd := "../../testdata"
join := filepath.Join join := filepath.Join
@ -51,55 +48,28 @@ func TestNewOCIPusher(t *testing.T) {
WithInsecureSkipTLSVerify(insecureSkipTLSVerify), WithInsecureSkipTLSVerify(insecureSkipTLSVerify),
WithPlainHTTP(plainHTTP), WithPlainHTTP(plainHTTP),
) )
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
op, ok := p.(*OCIPusher) op, ok := p.(*OCIPusher)
if !ok { require.True(t, ok, "Expected NewOCIPusher to produce an *OCIPusher")
t.Fatal("Expected NewOCIPusher to produce an *OCIPusher") assert.Equal(t, pub, op.opts.certFile, "Expected NewOCIPusher to contain %q as the public key file, got %q", pub, op.opts.certFile)
} assert.Equal(t, priv, op.opts.keyFile, "Expected NewOCIPusher to contain %q as the private key file, got %q", priv, op.opts.keyFile)
assert.Equal(t, ca, op.opts.caFile, "Expected NewOCIPusher to contain %q as the CA file, got %q", ca, op.opts.caFile)
if op.opts.certFile != pub { assert.Equal(t, plainHTTP, op.opts.plainHTTP, "Expected NewOCIPusher to have plainHTTP as %t, got %t", plainHTTP, op.opts.plainHTTP)
t.Errorf("Expected NewOCIPusher to contain %q as the public key file, got %q", pub, op.opts.certFile) assert.Equal(t, insecureSkipTLSVerify, op.opts.insecureSkipTLSVerify, "Expected NewOCIPusher to have insecureSkipVerifyTLS as %t, got %t", insecureSkipTLSVerify, op.opts.insecureSkipTLSVerify)
}
if op.opts.keyFile != priv {
t.Errorf("Expected NewOCIPusher to contain %q as the private key file, got %q", priv, op.opts.keyFile)
}
if op.opts.caFile != ca {
t.Errorf("Expected NewOCIPusher to contain %q as the CA file, got %q", ca, op.opts.caFile)
}
if op.opts.plainHTTP != plainHTTP {
t.Errorf("Expected NewOCIPusher to have plainHTTP as %t, got %t", plainHTTP, op.opts.plainHTTP)
}
if op.opts.insecureSkipTLSVerify != insecureSkipTLSVerify {
t.Errorf("Expected NewOCIPusher to have insecureSkipVerifyTLS as %t, got %t", insecureSkipTLSVerify, op.opts.insecureSkipTLSVerify)
}
// Test if setting registryClient is being passed to the ops // Test if setting registryClient is being passed to the ops
registryClient, err := registry.NewClient() registryClient, err := registry.NewClient()
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
p, err = NewOCIPusher( p, err = NewOCIPusher(
WithRegistryClient(registryClient), WithRegistryClient(registryClient),
) )
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
op, ok = p.(*OCIPusher)
if !ok {
t.Fatal("expected NewOCIPusher to produce an *OCIPusher")
}
if op.opts.registryClient != registryClient { op, ok = p.(*OCIPusher)
t.Errorf("Expected NewOCIPusher to contain %p as RegistryClient, got %p", registryClient, op.opts.registryClient) require.True(t, ok, "expected NewOCIPusher to produce an *OCIPusher")
} assert.Equal(t, registryClient, op.opts.registryClient, "Expected NewOCIPusher to contain %p as RegistryClient, got %p", registryClient, op.opts.registryClient)
} }
func TestOCIPusher_Push_ErrorHandling(t *testing.T) { func TestOCIPusher_Push_ErrorHandling(t *testing.T) {
@ -127,23 +97,13 @@ func TestOCIPusher_Push_ErrorHandling(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
pusher, err := NewOCIPusher() pusher, err := NewOCIPusher()
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
chartRef := tt.chartRef chartRef := tt.chartRef
if tt.setupFunc != nil { if tt.setupFunc != nil {
chartRef = tt.setupFunc() chartRef = tt.setupFunc()
} }
assert.ErrorContains(t, pusher.Push(chartRef, "oci://localhost:5000/test"), tt.expectedError)
err = pusher.Push(chartRef, "oci://localhost:5000/test")
if err == nil {
t.Fatal("Expected error but got none")
}
if !strings.Contains(err.Error(), tt.expectedError) {
t.Errorf("Expected error containing %q, got %q", tt.expectedError, err.Error())
}
}) })
} }
} }
@ -227,30 +187,20 @@ func TestOCIPusher_newRegistryClient(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
pusher, err := NewOCIPusher(tt.opts...) pusher, err := NewOCIPusher(tt.opts...)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
op, ok := pusher.(*OCIPusher) op, ok := pusher.(*OCIPusher)
if !ok { require.True(t, ok, "Expected *OCIPusher")
t.Fatal("Expected *OCIPusher")
}
client, err := op.newRegistryClient() client, err := op.newRegistryClient()
if tt.expectError { if tt.expectError {
if err == nil { require.Error(t, err, "Expected error but got none")
t.Fatal("Expected error but got none") if tt.errorContains != "" {
} require.ErrorContainsf(t, err, tt.errorContains, "Expected error containing %q, got %q", tt.errorContains, err.Error())
if tt.errorContains != "" && !strings.Contains(err.Error(), tt.errorContains) {
t.Errorf("Expected error containing %q, got %q", tt.errorContains, err.Error())
} }
} else { } else {
if err != nil { require.NoError(t, err)
t.Fatalf("Unexpected error: %v", err) require.NotNil(t, client, "Expected non-nil registry client")
}
if client == nil {
t.Fatal("Expected non-nil registry client")
}
} }
}) })
} }
@ -287,25 +237,18 @@ func TestOCIPusher_Push_ChartOperations(t *testing.T) {
// Copy a valid chart // Copy a valid chart
src, err := os.Open(chartPath) src, err := os.Open(chartPath)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
defer src.Close() defer src.Close()
dst, err := os.Create(tempChart) dst, err := os.Create(tempChart)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
if _, err := io.Copy(dst, src); err != nil { _, err = io.Copy(dst, src)
t.Fatal(err) require.NoError(t, err)
}
dst.Close() dst.Close()
// Make the file unreadable // Make the file unreadable
if err := os.Chmod(tempChart, 0o000); err != nil { require.NoError(t, os.Chmod(tempChart, 0o000))
t.Fatal(err)
}
return tempChart, func() { return tempChart, func() {
os.Chmod(tempChart, 0o644) // Restore permissions for cleanup os.Chmod(tempChart, 0o644) // Restore permissions for cleanup
@ -328,25 +271,18 @@ func TestOCIPusher_Push_ChartOperations(t *testing.T) {
// Copy chart file // Copy chart file
src, err := os.Open(chartWithProvPath) src, err := os.Open(chartWithProvPath)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
defer src.Close() defer src.Close()
dst, err := os.Create(tempChart) dst, err := os.Create(tempChart)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
if _, err := io.Copy(dst, src); err != nil { _, err = io.Copy(dst, src)
t.Fatal(err) require.NoError(t, err)
}
dst.Close() dst.Close()
// Create provenance file // Create provenance file
if err := os.WriteFile(tempProv, []byte("test provenance data"), 0o644); err != nil { require.NoError(t, os.WriteFile(tempProv, []byte("test provenance data"), 0o644))
t.Fatal(err)
}
return tempChart, func() {} return tempChart, func() {}
}, },
@ -373,9 +309,7 @@ func TestOCIPusher_Push_ChartOperations(t *testing.T) {
} }
pusher, err := NewOCIPusher(tt.options...) pusher, err := NewOCIPusher(tt.options...)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
err = pusher.Push(chartRef, tt.href) err = pusher.Push(chartRef, tt.href)
@ -400,27 +334,17 @@ func TestOCIPusher_Push_MultipleOptions(t *testing.T) {
} }
pusher, err := NewOCIPusher() pusher, err := NewOCIPusher()
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
// Test that multiple options are applied correctly // Test that multiple options are applied correctly
err = pusher.Push(chartPath, "oci://localhost:5000/test", // We expect an error since we're not actually pushing to a registry
require.Error(t, pusher.Push(chartPath, "oci://localhost:5000/test",
WithPlainHTTP(true), WithPlainHTTP(true),
WithInsecureSkipTLSVerify(true), WithInsecureSkipTLSVerify(true),
) ), "Expected error when pushing without a valid registry")
// We expect an error since we're not actually pushing to a registry
if err == nil {
t.Fatal("Expected error when pushing without a valid registry")
}
// Verify options were applied // Verify options were applied
op := pusher.(*OCIPusher) op := pusher.(*OCIPusher)
if !op.opts.plainHTTP { assert.True(t, op.opts.plainHTTP, "Expected plainHTTP option to be applied")
t.Error("Expected plainHTTP option to be applied") assert.True(t, op.opts.insecureSkipTLSVerify, "Expected insecureSkipTLSVerify option to be applied")
}
if !op.opts.insecureSkipTLSVerify {
t.Error("Expected insecureSkipTLSVerify option to be applied")
}
} }

Loading…
Cancel
Save