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>
This commit is contained in:
Sunny 2021-11-18 03:58:54 +05:30 committed by Hidde Beydals
parent 2b8134ce20
commit 37ac5a9679
3 changed files with 63 additions and 4 deletions

View File

@ -17,11 +17,16 @@ limitations under the License.
package chart
import (
"os"
"path/filepath"
"testing"
. "github.com/onsi/gomega"
"github.com/otiai10/copy"
helmchart "helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chartutil"
"github.com/fluxcd/source-controller/internal/helm"
)
var (
@ -126,6 +131,17 @@ func TestOverwriteChartDefaultValues(t *testing.T) {
}
func TestLoadChartMetadataFromDir(t *testing.T) {
g := NewWithT(t)
// Create a chart file that exceeds the max chart file size.
tmpDir, err := os.MkdirTemp("", "load-chart-")
g.Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(tmpDir)
copy.Copy("../testdata/charts/helmchart", tmpDir)
bigRequirementsFile := filepath.Join(tmpDir, "requirements.yaml")
data := make([]byte, helm.MaxChartFileSize+10)
g.Expect(os.WriteFile(bigRequirementsFile, data, 0644)).ToNot(HaveOccurred())
tests := []struct {
name string
dir string
@ -152,6 +168,11 @@ func TestLoadChartMetadataFromDir(t *testing.T) {
dir: "../testdata/charts/",
wantErr: "../testdata/charts/Chart.yaml: no such file or directory",
},
{
name: "Error if file size exceeds max size",
dir: tmpDir,
wantErr: "size of 'requirements.yaml' exceeds",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -176,6 +197,16 @@ func TestLoadChartMetadataFromDir(t *testing.T) {
}
func TestLoadChartMetadataFromArchive(t *testing.T) {
g := NewWithT(t)
// Create a chart archive that exceeds the max chart size.
tmpDir, err := os.MkdirTemp("", "load-chart-")
g.Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(tmpDir)
bigArchiveFile := filepath.Join(tmpDir, "chart.tgz")
data := make([]byte, helm.MaxChartSize+10)
g.Expect(os.WriteFile(bigArchiveFile, data, 0644)).ToNot(HaveOccurred())
tests := []struct {
name string
archive string
@ -207,6 +238,11 @@ func TestLoadChartMetadataFromArchive(t *testing.T) {
archive: "../testdata/charts/empty.tgz",
wantErr: "no 'Chart.yaml' found",
},
{
name: "Error if archive size exceeds max size",
archive: bigArchiveFile,
wantErr: "size of chart 'chart.tgz' exceeds",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

View File

@ -22,8 +22,8 @@ var (
// MaxIndexSize is the max allowed file size in bytes of a ChartRepository.
MaxIndexSize int64 = 50 << 20
// MaxChartSize is the max allowed file size in bytes of a Helm Chart.
MaxChartSize int64 = 2 << 20
MaxChartSize int64 = 10 << 20
// MaxChartFileSize is the max allowed file size in bytes of any arbitrary
// file originating from a chart.
MaxChartFileSize int64 = 2 << 10
MaxChartFileSize int64 = 5 << 20
)

View File

@ -22,9 +22,11 @@ import (
"fmt"
"net/url"
"os"
"path/filepath"
"testing"
"time"
"github.com/fluxcd/source-controller/internal/helm"
. "github.com/onsi/gomega"
"helm.sh/helm/v3/pkg/chart"
helmgetter "helm.sh/helm/v3/pkg/getter"
@ -353,9 +355,20 @@ func TestChartRepository_LoadIndexFromBytes_Unordered(t *testing.T) {
// Index load tests are derived from https://github.com/helm/helm/blob/v3.3.4/pkg/repo/index_test.go#L108
// to ensure parity with Helm behaviour.
func TestChartRepository_LoadIndexFromFile(t *testing.T) {
g := NewWithT(t)
// Create an index file that exceeds the max index size.
tmpDir, err := os.MkdirTemp("", "load-index-")
g.Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(tmpDir)
bigIndexFile := filepath.Join(tmpDir, "index.yaml")
data := make([]byte, helm.MaxIndexSize+10)
g.Expect(os.WriteFile(bigIndexFile, data, 0644)).ToNot(HaveOccurred())
tests := []struct {
name string
filename string
wantErr string
}{
{
name: "regular index file",
@ -365,16 +378,26 @@ func TestChartRepository_LoadIndexFromFile(t *testing.T) {
name: "chartmuseum index file",
filename: chartmuseumTestFile,
},
{
name: "error if index size exceeds max size",
filename: bigIndexFile,
wantErr: "size of index 'index.yaml' exceeds",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
t.Parallel()
r := newChartRepository()
err := r.LoadFromFile(testFile)
err := r.LoadFromFile(tt.filename)
if tt.wantErr != "" {
g.Expect(err).To(HaveOccurred())
g.Expect(err.Error()).To(ContainSubstring(tt.wantErr))
return
}
g.Expect(err).ToNot(HaveOccurred())
verifyLocalIndex(t, r.Index)