Skip tests when running helm template

Signed-off-by: Torsten Walter <mail@torstenwalter.de>
pull/8703/head
Torsten Walter 4 years ago
parent 3d66daeb55
commit 9ea10ef04a
No known key found for this signature in database
GPG Key ID: F2774C4D575466C7

@ -27,6 +27,8 @@ import (
"sort" "sort"
"strings" "strings"
"helm.sh/helm/v3/pkg/release"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"helm.sh/helm/v3/cmd/helm/require" "helm.sh/helm/v3/cmd/helm/require"
@ -68,7 +70,6 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
client.ClientOnly = !validate client.ClientOnly = !validate
client.APIVersions = chartutil.VersionSet(extraAPIs) client.APIVersions = chartutil.VersionSet(extraAPIs)
client.IncludeCRDs = includeCrds client.IncludeCRDs = includeCrds
client.SkipTests = skipTests
rel, err := runInstall(args, client, valueOpts, out) rel, err := runInstall(args, client, valueOpts, out)
if err != nil && !settings.Debug { if err != nil && !settings.Debug {
@ -86,6 +87,9 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
if !client.DisableHooks { if !client.DisableHooks {
fileWritten := make(map[string]bool) fileWritten := make(map[string]bool)
for _, m := range rel.Hooks { for _, m := range rel.Hooks {
if skipTests && isTestHook(m) {
continue
}
if client.OutputDir == "" { if client.OutputDir == "" {
fmt.Fprintf(&manifests, "---\n# Source: %s\n%s\n", m.Path, m.Manifest) fmt.Fprintf(&manifests, "---\n# Source: %s\n%s\n", m.Path, m.Manifest)
} else { } else {
@ -165,7 +169,7 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
f.StringVar(&client.OutputDir, "output-dir", "", "writes the executed templates to files in output-dir instead of stdout") f.StringVar(&client.OutputDir, "output-dir", "", "writes the executed templates to files in output-dir instead of stdout")
f.BoolVar(&validate, "validate", false, "validate your manifests against the Kubernetes cluster you are currently pointing at. This is the same validation performed on an install") f.BoolVar(&validate, "validate", false, "validate your manifests against the Kubernetes cluster you are currently pointing at. This is the same validation performed on an install")
f.BoolVar(&includeCrds, "include-crds", false, "include CRDs in the templated output") f.BoolVar(&includeCrds, "include-crds", false, "include CRDs in the templated output")
f.BoolVar(&skipTests, "skip-tests", false, "skip tests and manifests in tests directories from templated output") f.BoolVar(&skipTests, "skip-tests", false, "skip tests from templated output")
f.BoolVar(&client.IsUpgrade, "is-upgrade", false, "set .Release.IsUpgrade instead of .Release.IsInstall") f.BoolVar(&client.IsUpgrade, "is-upgrade", false, "set .Release.IsUpgrade instead of .Release.IsInstall")
f.StringArrayVarP(&extraAPIs, "api-versions", "a", []string{}, "Kubernetes api versions used for Capabilities.APIVersions") f.StringArrayVarP(&extraAPIs, "api-versions", "a", []string{}, "Kubernetes api versions used for Capabilities.APIVersions")
f.BoolVar(&client.UseReleaseName, "release-name", false, "use release name in the output-dir path.") f.BoolVar(&client.UseReleaseName, "release-name", false, "use release name in the output-dir path.")
@ -174,6 +178,15 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
return cmd return cmd
} }
func isTestHook(h *release.Hook) bool {
for _, e := range h.Events {
if e == release.HookTest {
return true
}
}
return false
}
// The following functions (writeToFile, createOrOpenFile, and ensureDirectoryForFile) // The following functions (writeToFile, createOrOpenFile, and ensureDirectoryForFile)
// are coppied from the actions package. This is part of a change to correct a // are coppied from the actions package. This is part of a change to correct a
// bug introduced by #8156. As part of the todo to refactor renderResources // bug introduced by #8156. As part of the todo to refactor renderResources

@ -122,9 +122,9 @@ func TestTemplateCmd(t *testing.T) {
golden: "output/template-with-invalid-yaml-debug.txt", golden: "output/template-with-invalid-yaml-debug.txt",
}, },
{ {
name: "template with skip-tests", name: "template skip-tests",
cmd: fmt.Sprintf(`template '%s' --skip-tests`, chartPath), cmd: fmt.Sprintf(`template '%s' --skip-tests`, chartPath),
golden: "output/template-no-tests.txt", golden: "output/template-skip-tests.txt",
}, },
} }
runTestCmd(t, tests) runTestCmd(t, tests)

@ -5,14 +5,6 @@ kind: ServiceAccount
metadata: metadata:
name: subchart-sa name: subchart-sa
--- ---
# Source: subchart/templates/tests/test-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: "foobar-YWJj-baz-testconfig"
data:
message: Hello World
---
# Source: subchart/templates/subdir/role.yaml # Source: subchart/templates/subdir/role.yaml
apiVersion: rbac.authorization.k8s.io/v1 apiVersion: rbac.authorization.k8s.io/v1
kind: Role kind: Role
@ -92,6 +84,16 @@ spec:
selector: selector:
app.kubernetes.io/name: subchart app.kubernetes.io/name: subchart
--- ---
# Source: subchart/templates/tests/test-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: "foobar-YWJj-baz-testconfig"
annotations:
"helm.sh/hook": test
data:
message: Hello World
---
# Source: subchart/templates/tests/test-nothing.yaml # Source: subchart/templates/tests/test-nothing.yaml
apiVersion: v1 apiVersion: v1
kind: Pod kind: Pod

@ -5,14 +5,6 @@ kind: ServiceAccount
metadata: metadata:
name: subchart-sa name: subchart-sa
--- ---
# Source: subchart/templates/tests/test-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: "RELEASE-NAME-testconfig"
data:
message: Hello World
---
# Source: subchart/templates/subdir/role.yaml # Source: subchart/templates/subdir/role.yaml
apiVersion: rbac.authorization.k8s.io/v1 apiVersion: rbac.authorization.k8s.io/v1
kind: Role kind: Role
@ -92,6 +84,16 @@ spec:
selector: selector:
app.kubernetes.io/name: subchart app.kubernetes.io/name: subchart
--- ---
# Source: subchart/templates/tests/test-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: "RELEASE-NAME-testconfig"
annotations:
"helm.sh/hook": test
data:
message: Hello World
---
# Source: subchart/templates/tests/test-nothing.yaml # Source: subchart/templates/tests/test-nothing.yaml
apiVersion: v1 apiVersion: v1
kind: Pod kind: Pod

@ -84,22 +84,3 @@ spec:
name: nginx name: nginx
selector: selector:
app.kubernetes.io/name: subchart app.kubernetes.io/name: subchart
---
# Source: subchart/templates/tests/test-nothing.yaml
apiVersion: v1
kind: Pod
metadata:
name: "RELEASE-NAME-test"
annotations:
"helm.sh/hook": test
spec:
containers:
- name: test
image: "alpine:latest"
envFrom:
- configMapRef:
name: "RELEASE-NAME-testconfig"
command:
- echo
- "$message"
restartPolicy: Never

@ -5,14 +5,6 @@ kind: ServiceAccount
metadata: metadata:
name: subchart-sa name: subchart-sa
--- ---
# Source: subchart/templates/tests/test-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: "RELEASE-NAME-testconfig"
data:
message: Hello World
---
# Source: subchart/templates/subdir/role.yaml # Source: subchart/templates/subdir/role.yaml
apiVersion: rbac.authorization.k8s.io/v1 apiVersion: rbac.authorization.k8s.io/v1
kind: Role kind: Role
@ -92,6 +84,16 @@ spec:
selector: selector:
app.kubernetes.io/name: subchart app.kubernetes.io/name: subchart
--- ---
# Source: subchart/templates/tests/test-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: "RELEASE-NAME-testconfig"
annotations:
"helm.sh/hook": test
data:
message: Hello World
---
# Source: subchart/templates/tests/test-nothing.yaml # Source: subchart/templates/tests/test-nothing.yaml
apiVersion: v1 apiVersion: v1
kind: Pod kind: Pod

@ -5,14 +5,6 @@ kind: ServiceAccount
metadata: metadata:
name: subchart-sa name: subchart-sa
--- ---
# Source: subchart/templates/tests/test-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: "RELEASE-NAME-testconfig"
data:
message: Hello World
---
# Source: subchart/templates/subdir/role.yaml # Source: subchart/templates/subdir/role.yaml
apiVersion: rbac.authorization.k8s.io/v1 apiVersion: rbac.authorization.k8s.io/v1
kind: Role kind: Role
@ -93,6 +85,16 @@ spec:
selector: selector:
app.kubernetes.io/name: subchart app.kubernetes.io/name: subchart
--- ---
# Source: subchart/templates/tests/test-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: "RELEASE-NAME-testconfig"
annotations:
"helm.sh/hook": test
data:
message: Hello World
---
# Source: subchart/templates/tests/test-nothing.yaml # Source: subchart/templates/tests/test-nothing.yaml
apiVersion: v1 apiVersion: v1
kind: Pod kind: Pod

@ -22,14 +22,6 @@ kind: ServiceAccount
metadata: metadata:
name: subchart-sa name: subchart-sa
--- ---
# Source: subchart/templates/tests/test-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: "RELEASE-NAME-testconfig"
data:
message: Hello World
---
# Source: subchart/templates/subdir/role.yaml # Source: subchart/templates/subdir/role.yaml
apiVersion: rbac.authorization.k8s.io/v1 apiVersion: rbac.authorization.k8s.io/v1
kind: Role kind: Role
@ -110,6 +102,16 @@ spec:
selector: selector:
app.kubernetes.io/name: subchart app.kubernetes.io/name: subchart
--- ---
# Source: subchart/templates/tests/test-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: "RELEASE-NAME-testconfig"
annotations:
"helm.sh/hook": test
data:
message: Hello World
---
# Source: subchart/templates/tests/test-nothing.yaml # Source: subchart/templates/tests/test-nothing.yaml
apiVersion: v1 apiVersion: v1
kind: Pod kind: Pod

@ -5,14 +5,6 @@ kind: ServiceAccount
metadata: metadata:
name: subchart-sa name: subchart-sa
--- ---
# Source: subchart/templates/tests/test-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: "RELEASE-NAME-testconfig"
data:
message: Hello World
---
# Source: subchart/templates/subdir/role.yaml # Source: subchart/templates/subdir/role.yaml
apiVersion: rbac.authorization.k8s.io/v1 apiVersion: rbac.authorization.k8s.io/v1
kind: Role kind: Role
@ -92,6 +84,16 @@ spec:
selector: selector:
app.kubernetes.io/name: subchart app.kubernetes.io/name: subchart
--- ---
# Source: subchart/templates/tests/test-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: "RELEASE-NAME-testconfig"
annotations:
"helm.sh/hook": test
data:
message: Hello World
---
# Source: subchart/templates/tests/test-nothing.yaml # Source: subchart/templates/tests/test-nothing.yaml
apiVersion: v1 apiVersion: v1
kind: Pod kind: Pod

@ -2,5 +2,7 @@ apiVersion: v1
kind: ConfigMap kind: ConfigMap
metadata: metadata:
name: "{{ .Release.Name }}-testconfig" name: "{{ .Release.Name }}-testconfig"
annotations:
"helm.sh/hook": test
data: data:
message: Hello World message: Hello World

@ -101,7 +101,7 @@ type Configuration struct {
// TODO: This function is badly in need of a refactor. // TODO: This function is badly in need of a refactor.
// TODO: As part of the refactor the duplicate code in cmd/helm/template.go should be removed // TODO: As part of the refactor the duplicate code in cmd/helm/template.go should be removed
// This code has to do with writing files to disk. // This code has to do with writing files to disk.
func (c *Configuration) renderResources(ch *chart.Chart, values chartutil.Values, releaseName, outputDir string, subNotes, useReleaseName, includeCrds bool, skipTests bool, pr postrender.PostRenderer, dryRun bool) ([]*release.Hook, *bytes.Buffer, string, error) { func (c *Configuration) renderResources(ch *chart.Chart, values chartutil.Values, releaseName, outputDir string, subNotes, useReleaseName, includeCrds bool, pr postrender.PostRenderer, dryRun bool) ([]*release.Hook, *bytes.Buffer, string, error) {
hs := []*release.Hook{} hs := []*release.Hook{}
b := bytes.NewBuffer(nil) b := bytes.NewBuffer(nil)
@ -194,7 +194,7 @@ func (c *Configuration) renderResources(ch *chart.Chart, values chartutil.Values
} }
} }
for _, m := range filterManifests(manifests, skipTests) { for _, m := range manifests {
if outputDir == "" { if outputDir == "" {
fmt.Fprintf(b, "---\n# Source: %s\n%s\n", m.Name, m.Content) fmt.Fprintf(b, "---\n# Source: %s\n%s\n", m.Name, m.Content)
} else { } else {
@ -224,19 +224,6 @@ func (c *Configuration) renderResources(ch *chart.Chart, values chartutil.Values
return hs, b, notes, nil return hs, b, notes, nil
} }
func filterManifests(manifests []releaseutil.Manifest, skipTests bool) []releaseutil.Manifest {
if skipTests {
var manifestsWithoutTests []releaseutil.Manifest
for _, m := range manifests {
if !strings.Contains(m.Name, "tests/") {
manifestsWithoutTests = append(manifestsWithoutTests, m)
}
}
return manifestsWithoutTests
}
return manifests
}
// RESTClientGetter gets the rest client // RESTClientGetter gets the rest client
type RESTClientGetter interface { type RESTClientGetter interface {
ToRESTConfig() (*rest.Config, error) ToRESTConfig() (*rest.Config, error)

@ -91,7 +91,6 @@ type Install struct {
SubNotes bool SubNotes bool
DisableOpenAPIValidation bool DisableOpenAPIValidation bool
IncludeCRDs bool IncludeCRDs bool
SkipTests bool
// APIVersions allows a manual set of supported API Versions to be passed // APIVersions allows a manual set of supported API Versions to be passed
// (for things like templating). These are ignored if ClientOnly is false // (for things like templating). These are ignored if ClientOnly is false
APIVersions chartutil.VersionSet APIVersions chartutil.VersionSet
@ -237,7 +236,7 @@ func (i *Install) Run(chrt *chart.Chart, vals map[string]interface{}) (*release.
rel := i.createRelease(chrt, vals) rel := i.createRelease(chrt, vals)
var manifestDoc *bytes.Buffer var manifestDoc *bytes.Buffer
rel.Hooks, manifestDoc, rel.Info.Notes, err = i.cfg.renderResources(chrt, valuesToRender, i.ReleaseName, i.OutputDir, i.SubNotes, i.UseReleaseName, i.IncludeCRDs, i.SkipTests, i.PostRenderer, i.DryRun) rel.Hooks, manifestDoc, rel.Info.Notes, err = i.cfg.renderResources(chrt, valuesToRender, i.ReleaseName, i.OutputDir, i.SubNotes, i.UseReleaseName, i.IncludeCRDs, i.PostRenderer, i.DryRun)
// Even for errors, attach this if available // Even for errors, attach this if available
if manifestDoc != nil { if manifestDoc != nil {
rel.Manifest = manifestDoc.String() rel.Manifest = manifestDoc.String()

@ -223,7 +223,7 @@ func (u *Upgrade) prepareUpgrade(name string, chart *chart.Chart, vals map[strin
return nil, nil, err return nil, nil, err
} }
hooks, manifestDoc, notesTxt, err := u.cfg.renderResources(chart, valuesToRender, "", "", u.SubNotes, false, false, false, u.PostRenderer, u.DryRun) hooks, manifestDoc, notesTxt, err := u.cfg.renderResources(chart, valuesToRender, "", "", u.SubNotes, false, false, u.PostRenderer, u.DryRun)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }

Loading…
Cancel
Save