Skip to content

Commit 37ac5a9

Browse files
darkowlzzhiddeco
authored andcommitted
internal/helm: test load funcs for max size cases
This includes a change of the defaults to more acceptible (higher) values. Signed-off-by: Sunny <darkowlzz@protonmail.com>
1 parent 2b8134c commit 37ac5a9

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

internal/helm/chart/metadata_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ limitations under the License.
1717
package chart
1818

1919
import (
20+
"os"
21+
"path/filepath"
2022
"testing"
2123

2224
. "github.com/onsi/gomega"
25+
"github.com/otiai10/copy"
2326
helmchart "helm.sh/helm/v3/pkg/chart"
2427
"helm.sh/helm/v3/pkg/chartutil"
28+
29+
"github.com/fluxcd/source-controller/internal/helm"
2530
)
2631

2732
var (
@@ -126,6 +131,17 @@ func TestOverwriteChartDefaultValues(t *testing.T) {
126131
}
127132

128133
func TestLoadChartMetadataFromDir(t *testing.T) {
134+
g := NewWithT(t)
135+
136+
// Create a chart file that exceeds the max chart file size.
137+
tmpDir, err := os.MkdirTemp("", "load-chart-")
138+
g.Expect(err).ToNot(HaveOccurred())
139+
defer os.RemoveAll(tmpDir)
140+
copy.Copy("../testdata/charts/helmchart", tmpDir)
141+
bigRequirementsFile := filepath.Join(tmpDir, "requirements.yaml")
142+
data := make([]byte, helm.MaxChartFileSize+10)
143+
g.Expect(os.WriteFile(bigRequirementsFile, data, 0644)).ToNot(HaveOccurred())
144+
129145
tests := []struct {
130146
name string
131147
dir string
@@ -152,6 +168,11 @@ func TestLoadChartMetadataFromDir(t *testing.T) {
152168
dir: "../testdata/charts/",
153169
wantErr: "../testdata/charts/Chart.yaml: no such file or directory",
154170
},
171+
{
172+
name: "Error if file size exceeds max size",
173+
dir: tmpDir,
174+
wantErr: "size of 'requirements.yaml' exceeds",
175+
},
155176
}
156177
for _, tt := range tests {
157178
t.Run(tt.name, func(t *testing.T) {
@@ -176,6 +197,16 @@ func TestLoadChartMetadataFromDir(t *testing.T) {
176197
}
177198

178199
func TestLoadChartMetadataFromArchive(t *testing.T) {
200+
g := NewWithT(t)
201+
202+
// Create a chart archive that exceeds the max chart size.
203+
tmpDir, err := os.MkdirTemp("", "load-chart-")
204+
g.Expect(err).ToNot(HaveOccurred())
205+
defer os.RemoveAll(tmpDir)
206+
bigArchiveFile := filepath.Join(tmpDir, "chart.tgz")
207+
data := make([]byte, helm.MaxChartSize+10)
208+
g.Expect(os.WriteFile(bigArchiveFile, data, 0644)).ToNot(HaveOccurred())
209+
179210
tests := []struct {
180211
name string
181212
archive string
@@ -207,6 +238,11 @@ func TestLoadChartMetadataFromArchive(t *testing.T) {
207238
archive: "../testdata/charts/empty.tgz",
208239
wantErr: "no 'Chart.yaml' found",
209240
},
241+
{
242+
name: "Error if archive size exceeds max size",
243+
archive: bigArchiveFile,
244+
wantErr: "size of chart 'chart.tgz' exceeds",
245+
},
210246
}
211247
for _, tt := range tests {
212248
t.Run(tt.name, func(t *testing.T) {

internal/helm/helm.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ var (
2222
// MaxIndexSize is the max allowed file size in bytes of a ChartRepository.
2323
MaxIndexSize int64 = 50 << 20
2424
// MaxChartSize is the max allowed file size in bytes of a Helm Chart.
25-
MaxChartSize int64 = 2 << 20
25+
MaxChartSize int64 = 10 << 20
2626
// MaxChartFileSize is the max allowed file size in bytes of any arbitrary
2727
// file originating from a chart.
28-
MaxChartFileSize int64 = 2 << 10
28+
MaxChartFileSize int64 = 5 << 20
2929
)

internal/helm/repository/chart_repository_test.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ import (
2222
"fmt"
2323
"net/url"
2424
"os"
25+
"path/filepath"
2526
"testing"
2627
"time"
2728

29+
"github.com/fluxcd/source-controller/internal/helm"
2830
. "github.com/onsi/gomega"
2931
"helm.sh/helm/v3/pkg/chart"
3032
helmgetter "helm.sh/helm/v3/pkg/getter"
@@ -353,9 +355,20 @@ func TestChartRepository_LoadIndexFromBytes_Unordered(t *testing.T) {
353355
// Index load tests are derived from https://github.com/helm/helm/blob/v3.3.4/pkg/repo/index_test.go#L108
354356
// to ensure parity with Helm behaviour.
355357
func TestChartRepository_LoadIndexFromFile(t *testing.T) {
358+
g := NewWithT(t)
359+
360+
// Create an index file that exceeds the max index size.
361+
tmpDir, err := os.MkdirTemp("", "load-index-")
362+
g.Expect(err).ToNot(HaveOccurred())
363+
defer os.RemoveAll(tmpDir)
364+
bigIndexFile := filepath.Join(tmpDir, "index.yaml")
365+
data := make([]byte, helm.MaxIndexSize+10)
366+
g.Expect(os.WriteFile(bigIndexFile, data, 0644)).ToNot(HaveOccurred())
367+
356368
tests := []struct {
357369
name string
358370
filename string
371+
wantErr string
359372
}{
360373
{
361374
name: "regular index file",
@@ -365,16 +378,26 @@ func TestChartRepository_LoadIndexFromFile(t *testing.T) {
365378
name: "chartmuseum index file",
366379
filename: chartmuseumTestFile,
367380
},
381+
{
382+
name: "error if index size exceeds max size",
383+
filename: bigIndexFile,
384+
wantErr: "size of index 'index.yaml' exceeds",
385+
},
368386
}
369387

370388
for _, tt := range tests {
371389
tt := tt
372390
t.Run(tt.name, func(t *testing.T) {
373391
g := NewWithT(t)
374-
t.Parallel()
375392

376393
r := newChartRepository()
377-
err := r.LoadFromFile(testFile)
394+
err := r.LoadFromFile(tt.filename)
395+
if tt.wantErr != "" {
396+
g.Expect(err).To(HaveOccurred())
397+
g.Expect(err.Error()).To(ContainSubstring(tt.wantErr))
398+
return
399+
}
400+
378401
g.Expect(err).ToNot(HaveOccurred())
379402

380403
verifyLocalIndex(t, r.Index)

0 commit comments

Comments
 (0)