From 88fdb7e7eae5ff9957a23850d1963bb1aa8cdd8a Mon Sep 17 00:00:00 2001 From: Evans Mungai Date: Thu, 19 Feb 2026 19:43:13 +0000 Subject: [PATCH] Fix failing test Signed-off-by: Evans Mungai --- pkg/chart/common/util/jsonschema.go | 8 +++----- pkg/cmd/pull_test.go | 14 +++++++++----- pkg/downloader/chart_downloader.go | 7 +++++++ pkg/repo/v1/repotest/server.go | 7 ++++++- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/pkg/chart/common/util/jsonschema.go b/pkg/chart/common/util/jsonschema.go index 6d7f32604..873c08fdd 100644 --- a/pkg/chart/common/util/jsonschema.go +++ b/pkg/chart/common/util/jsonschema.go @@ -83,7 +83,7 @@ func ValidateAgainstSchema(ch chart.Charter, values map[string]interface{}) erro slog.Debug("chart name", "chart-name", chrt.Name()) err := ValidateAgainstSingleSchema(values, chrt.Schema()) if err != nil { - sb.WriteString(fmt.Sprintf("%s:\n", chrt.Name())) + fmt.Fprintf(&sb, "%s:\n", chrt.Name()) sb.WriteString(err.Error()) } } @@ -103,10 +103,8 @@ func ValidateAgainstSchema(ch chart.Charter, values map[string]interface{}) erro subchartValues, ok := raw.(map[string]any) if !ok { - sb.WriteString(fmt.Sprintf( - "%s:\ninvalid type for values: expected object (map), got %T\n", - sub.Name(), raw, - )) + fmt.Fprintf(&sb, "%s:\ninvalid type for values: expected object (map), got %T\n", + sub.Name(), raw) continue } diff --git a/pkg/cmd/pull_test.go b/pkg/cmd/pull_test.go index 1c5af3146..6d5eb1482 100644 --- a/pkg/cmd/pull_test.go +++ b/pkg/cmd/pull_test.go @@ -22,6 +22,7 @@ import ( "net/http/httptest" "os" "path/filepath" + "strings" "testing" "helm.sh/helm/v4/pkg/repo/v1/repotest" @@ -520,7 +521,7 @@ func TestPullOCIWithTagAndDigest(t *testing.T) { if err != nil { t.Fatal(err) } - result := ociSrv.Run(t) + result := ociSrv.RunWithReturn(t) contentCache := t.TempDir() outdir := t.TempDir() @@ -543,12 +544,15 @@ func TestPullOCIWithTagAndDigest(t *testing.T) { } // Verify the file was downloaded - // When digest is present, the filename uses the digest format + // When digest is present, the filename uses the digest format (e.g. chart@sha256-hex.tgz) expectedFile := filepath.Join(outdir, "oci-dependent-chart-0.1.0.tgz") if _, err := os.Stat(expectedFile); err != nil { - // Try the digest-based filename - digestPart := result.PushedChart.Manifest.Digest[7:] // strip "sha256:" - expectedFile = filepath.Join(outdir, fmt.Sprintf("oci-dependent-chart@sha256-%s.tgz", digestPart)) + // Try the digest-based filename; parse algorithm:hex to avoid fixed-offset assumptions + algorithm, digestPart, ok := strings.Cut(result.PushedChart.Manifest.Digest, ":") + if !ok { + t.Fatalf("digest must be in algorithm:hex format, got %q", result.PushedChart.Manifest.Digest) + } + expectedFile = filepath.Join(outdir, fmt.Sprintf("oci-dependent-chart@%s-%s.tgz", algorithm, digestPart)) if _, err := os.Stat(expectedFile); err != nil { t.Errorf("expected chart file not found: %v", err) } diff --git a/pkg/downloader/chart_downloader.go b/pkg/downloader/chart_downloader.go index f9cf93382..d21a79335 100644 --- a/pkg/downloader/chart_downloader.go +++ b/pkg/downloader/chart_downloader.go @@ -130,6 +130,10 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven if err != nil { return "", nil, err } + if len(digest) != 32 { + return "", nil, fmt.Errorf("invalid digest length: %d", len(digest)) + } + copy(digest32[:], digest) if pth, err := c.Cache.Get(digest32, CacheChart); err == nil { fdata, err := os.ReadFile(pth) @@ -237,6 +241,9 @@ func (c *ChartDownloader) DownloadToCache(ref, version string) (string, *provena if err != nil { return "", nil, fmt.Errorf("unable to decode digest: %w", err) } + if digestString != "" && len(digest) != 32 { + return "", nil, fmt.Errorf("invalid digest length: %d", len(digest)) + } var digest32 [32]byte copy(digest32[:], digest) diff --git a/pkg/repo/v1/repotest/server.go b/pkg/repo/v1/repotest/server.go index 6380e1a86..22b4c8060 100644 --- a/pkg/repo/v1/repotest/server.go +++ b/pkg/repo/v1/repotest/server.go @@ -213,7 +213,12 @@ func NewOCIServer(t *testing.T, dir string) (*OCIServer, error) { }, nil } -func (srv *OCIServer) Run(t *testing.T, opts ...OCIServerOpt) *OCIServerRunResult { +func (srv *OCIServer) Run(t *testing.T, opts ...OCIServerOpt) { + t.Helper() + _ = srv.RunWithReturn(t, opts...) +} + +func (srv *OCIServer) RunWithReturn(t *testing.T, opts ...OCIServerOpt) *OCIServerRunResult { t.Helper() cfg := &OCIServerRunConfig{} for _, fn := range opts {