Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add RenderTemplateAndGetStdOutErrE & RunHelmCommandAndGetStdOutErrE #1526

Merged
merged 2 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions modules/helm/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ func RunHelmCommandAndGetStdOutE(t testing.TestingT, options *Options, cmd strin
return shell.RunCommandAndGetStdOutE(t, helmCmd)
}

// RunHelmCommandAndGetStdOutErrE runs helm with the given arguments and options and returns stdout and stderr separately.
func RunHelmCommandAndGetStdOutErrE(t testing.TestingT, options *Options, cmd string, additionalArgs ...string) (string, string, error) {
helmCmd := prepareHelmCommand(t, options, cmd, additionalArgs...)
return shell.RunCommandAndGetStdOutErrE(t, helmCmd)
}

func prepareHelmCommand(t testing.TestingT, options *Options, cmd string, additionalArgs ...string) shell.Command {
args := []string{cmd}
args = getCommonArgs(options, args...)
Expand Down
37 changes: 29 additions & 8 deletions modules/helm/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,42 @@ func RenderTemplate(t testing.TestingT, options *Options, chartDir string, relea
// RenderTemplateE runs `helm template` to render the template given the provided options and returns stdout/stderr from
// the template command. If you pass in templateFiles, this will only render those templates.
func RenderTemplateE(t testing.TestingT, options *Options, chartDir string, releaseName string, templateFiles []string, extraHelmArgs ...string) (string, error) {
// Get render arguments
args, err := getRenderArgs(t, options, chartDir, releaseName, templateFiles, extraHelmArgs...)
if err != nil {
return "", err
}

// Finally, call out to helm template command
return RunHelmCommandAndGetStdOutE(t, options, "template", args...)
}

// RenderTemplateAndGetStdOutErrE runs `helm template` to render the template given the provided options and returns stdout and stderr separately from
// the template command. If you pass in templateFiles, this will only render those templates.
func RenderTemplateAndGetStdOutErrE(t testing.TestingT, options *Options, chartDir string, releaseName string, templateFiles []string, extraHelmArgs ...string) (string, string, error) {
args, err := getRenderArgs(t, options, chartDir, releaseName, templateFiles, extraHelmArgs...)
if err != nil {
return "", "", err
}

// Finally, call out to helm template command
return RunHelmCommandAndGetStdOutErrE(t, options, "template", args...)
}

func getRenderArgs(t testing.TestingT, options *Options, chartDir string, releaseName string, templateFiles []string, extraHelmArgs ...string) ([]string, error) {
// First, verify the charts dir exists
absChartDir, err := filepath.Abs(chartDir)
if err != nil {
return "", errors.WithStackTrace(err)
return nil, errors.WithStackTrace(err)
}
if !files.FileExists(chartDir) {
return "", errors.WithStackTrace(ChartNotFoundError{chartDir})
return nil, errors.WithStackTrace(ChartNotFoundError{chartDir})
}

// check chart dependencies
if options.BuildDependencies {
if _, err := RunHelmCommandAndGetOutputE(t, options, "dependency", "build", chartDir); err != nil {
return "", errors.WithStackTrace(err)
return nil, errors.WithStackTrace(err)
}
}

Expand All @@ -54,13 +77,13 @@ func RenderTemplateE(t testing.TestingT, options *Options, chartDir string, rele
}
args, err = getValuesArgsE(t, options, args...)
if err != nil {
return "", err
return nil, err
}
for _, templateFile := range templateFiles {
// validate this is a valid template file
absTemplateFile := filepath.Join(absChartDir, templateFile)
if !strings.HasPrefix(templateFile, "charts") && !files.FileExists(absTemplateFile) {
return "", errors.WithStackTrace(TemplateFileNotFoundError{Path: templateFile, ChartDir: absChartDir})
return nil, errors.WithStackTrace(TemplateFileNotFoundError{Path: templateFile, ChartDir: absChartDir})
}

// Note: we only get the abs template file path to check it actually exists, but the `helm template` command
Expand All @@ -72,9 +95,7 @@ func RenderTemplateE(t testing.TestingT, options *Options, chartDir string, rele

// ... and add the name and chart at the end as the command expects
args = append(args, releaseName, chartDir)

// Finally, call out to helm template command
return RunHelmCommandAndGetStdOutE(t, options, "template", args...)
return args, nil
}

// RenderRemoteTemplate runs `helm template` to render a *remote* chart given the provided options and returns stdout/stderr from
Expand Down
24 changes: 15 additions & 9 deletions modules/helm/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package helm
import (
"fmt"
"os"
"regexp"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -164,12 +164,18 @@ func TestUnmarshall(t *testing.T) {
assert.Equal(t, deployment[0], deployment[1])
}
})
t.Run("Invalid", func(t *testing.T) {
b, err := os.ReadFile("testdata/invalid-duplicate.yaml")
require.NoError(t, err)
var deployment appsv1.Deployment
err = UnmarshalK8SYamlE(t, string(b), &deployment)
assert.Error(t, err)
assert.Regexp(t, regexp.MustCompile(`mapping key ".+" already defined at line \d+`), err.Error())
})
}

func TestRenderWarning(t *testing.T) {
chart, err := filepath.Abs("testdata/deprecated-chart")
require.NoError(t, err)

stdout, stderr, err := RenderTemplateAndGetStdOutErrE(t, &Options{}, chart, "test", nil)
require.NoError(t, err)

assert.Contains(t, stderr, "WARNING:")

var deployment appsv1.Deployment
UnmarshalK8SYaml(t, string(stdout), &deployment)
assert.Equal(t, deployment.Name, "nginx-deployment")
}
4 changes: 4 additions & 0 deletions modules/helm/testdata/deprecated-chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: test
version: 0.1.0
apiVersion: v1
deprecated: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80