diff --git a/pkg/cli/environment_test.go b/pkg/cli/environment_test.go index 18cb8bc59..8413da83a 100644 --- a/pkg/cli/environment_test.go +++ b/pkg/cli/environment_test.go @@ -373,6 +373,8 @@ func TestUserAgentHeaderInK8sRESTClientConfig(t *testing.T) { } func TestQuantityBytesValue(t *testing.T) { + // This test only verifies that the pflag.Value wrapper correctly propagates + // values and errors. Comprehensive parsing logic is tested in TestEnvInt64OrQuantityBytes. tests := []struct { name string input string @@ -380,35 +382,15 @@ func TestQuantityBytesValue(t *testing.T) { expectError bool }{ { - name: "plain int64", - input: "12345", - expected: 12345, - }, - { - name: "quantity Mi", + name: "valid quantity sets value", input: "256Mi", expected: 256 * 1024 * 1024, }, { - name: "quantity Gi", - input: "1Gi", - expected: 1 * 1024 * 1024 * 1024, - }, - { - name: "quantity with whitespace", - input: " 512Mi ", - expected: 512 * 1024 * 1024, - }, - { - name: "invalid value", + name: "invalid value propagates error", input: "not-a-number", expectError: true, }, - { - name: "lowercase suffix rejected", - input: "1gi", - expectError: true, - }, } for _, tt := range tests { diff --git a/pkg/cmd/install_test.go b/pkg/cmd/install_test.go index 2ae791ba7..0ade6ce86 100644 --- a/pkg/cmd/install_test.go +++ b/pkg/cmd/install_test.go @@ -23,6 +23,7 @@ import ( "path/filepath" "testing" + "helm.sh/helm/v4/pkg/cli" "helm.sh/helm/v4/pkg/repo/v1/repotest" ) @@ -285,6 +286,56 @@ func TestInstall(t *testing.T) { runTestCmd(t, tests) } +func TestInstallWithEnvVars(t *testing.T) { + tests := []struct { + name string + cmd string + envVars map[string]string + wantError bool + golden string + }{ + { + name: "install with HELM_MAX_CHART_SIZE env var with bytes", + cmd: "install too-big testdata/testcharts/compressedchart-0.1.0.tgz", + envVars: map[string]string{ + "HELM_MAX_CHART_SIZE": "42", + }, + wantError: true, + golden: "output/install-with-restricted-chart-size.txt", + }, + { + name: "install with HELM_MAX_FILE_SIZE env var with Quantity suffix", + cmd: "install test-max-file testdata/testcharts/bigchart-0.1.0.tgz", + envVars: map[string]string{ + "HELM_MAX_FILE_SIZE": "1Ki", + }, + wantError: true, + golden: "output/install-with-restricted-file-size.txt", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + defer resetEnv()() + + for k, v := range tt.envVars { + t.Setenv(k, v) + } + // Reset settings to pick up env vars + settings = cli.New() + + test := cmdTestCase{ + name: tt.name, + cmd: tt.cmd, + golden: tt.golden, + wantError: tt.wantError, + } + + runTestCmd(t, []cmdTestCase{test}) + }) + } +} + func TestInstallOutputCompletion(t *testing.T) { outputFlagCompletionTest(t, "install") } diff --git a/pkg/cmd/testdata/output/install-with-restricted-file-size.txt b/pkg/cmd/testdata/output/install-with-restricted-file-size.txt new file mode 100644 index 000000000..f5792e6a3 --- /dev/null +++ b/pkg/cmd/testdata/output/install-with-restricted-file-size.txt @@ -0,0 +1 @@ +Error: INSTALLATION FAILED: unable to load chart archive: decompressed chart file "bigchart/Chart.yaml" is larger than the maximum file size 1Ki diff --git a/pkg/cmd/testdata/output/upgrade-with-restricted-chart-size-env.txt b/pkg/cmd/testdata/output/upgrade-with-restricted-chart-size-env.txt new file mode 100644 index 000000000..f11530e94 --- /dev/null +++ b/pkg/cmd/testdata/output/upgrade-with-restricted-chart-size-env.txt @@ -0,0 +1 @@ +Error: unable to load chart archive: decompressed chart is larger than the maximum size 10 diff --git a/pkg/cmd/testdata/output/upgrade-with-restricted-file-size-env.txt b/pkg/cmd/testdata/output/upgrade-with-restricted-file-size-env.txt new file mode 100644 index 000000000..f85050f65 --- /dev/null +++ b/pkg/cmd/testdata/output/upgrade-with-restricted-file-size-env.txt @@ -0,0 +1 @@ +Error: unable to load chart archive: decompressed chart file "bigchart/values.yaml" is larger than the maximum file size 2Ki diff --git a/pkg/cmd/upgrade_test.go b/pkg/cmd/upgrade_test.go index 656c2bb8c..b3ce12b55 100644 --- a/pkg/cmd/upgrade_test.go +++ b/pkg/cmd/upgrade_test.go @@ -29,6 +29,7 @@ import ( chart "helm.sh/helm/v4/pkg/chart/v2" "helm.sh/helm/v4/pkg/chart/v2/loader" chartutil "helm.sh/helm/v4/pkg/chart/v2/util" + "helm.sh/helm/v4/pkg/cli" rcommon "helm.sh/helm/v4/pkg/release/common" release "helm.sh/helm/v4/pkg/release/v1" ) @@ -201,6 +202,56 @@ func TestUpgradeCmd(t *testing.T) { runTestCmd(t, tests) } +func TestUpgradeWithEnvVars(t *testing.T) { + tests := []struct { + name string + cmd string + envVars map[string]string + wantError bool + golden string + }{ + { + name: "upgrade with HELM_MAX_CHART_SIZE env var with bytes", + cmd: "upgrade too-big testdata/testcharts/compressedchart-0.1.0.tgz", + envVars: map[string]string{ + "HELM_MAX_CHART_SIZE": "10", + }, + wantError: true, + golden: "output/upgrade-with-restricted-chart-size-env.txt", + }, + { + name: "upgrade with HELM_MAX_FILE_SIZE env var with Quantity suffix", + cmd: "upgrade test-max-file testdata/testcharts/bigchart-0.1.0.tgz", + envVars: map[string]string{ + "HELM_MAX_FILE_SIZE": "2Ki", + }, + wantError: true, + golden: "output/upgrade-with-restricted-file-size-env.txt", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + defer resetEnv()() + + for k, v := range tt.envVars { + t.Setenv(k, v) + } + // Reset settings to pick up env vars + settings = cli.New() + + test := cmdTestCase{ + name: tt.name, + cmd: tt.cmd, + golden: tt.golden, + wantError: tt.wantError, + } + + runTestCmd(t, []cmdTestCase{test}) + }) + } +} + func TestUpgradeWithValue(t *testing.T) { releaseName := "funny-bunny-v2" relMock, ch, chartPath := prepareMockRelease(t, releaseName)