From f6efe2f0c18637d39b2068cf9625a269cfd6aff8 Mon Sep 17 00:00:00 2001 From: Martin Hickey Date: Wed, 17 Oct 2018 22:10:46 +0100 Subject: [PATCH] Add test template example for helm create (#4524) * Add test template example for helm create As helm create ganerates an nginx chart, the example is based on https://github.com/helm/helm/blob/master/docs/examples/nginx/templates/ service-test.yaml Signed-off-by: Martin Hickey * Update after review - https://github.com/helm/helm/pull/4524#pullrequestreview-155183390 - https://github.com/helm/helm/pull/4524#pullrequestreview-155379922 - Fix where app.kubernetes.io/instance was incorrect in service selector Signed-off-by: Martin Hickey * Add doc update Signed-off-by: Martin Hickey * Update GO formatting Signed-off-by: Martin Hickey --- cmd/helm/create.go | 13 +++++++------ cmd/helm/create_test.go | 4 ++-- docs/helm/helm_create.md | 14 ++++++++------ pkg/chartutil/create.go | 33 +++++++++++++++++++++++++++++++-- pkg/chartutil/create_test.go | 8 ++++++++ pkg/chartutil/save.go | 2 +- 6 files changed, 57 insertions(+), 17 deletions(-) diff --git a/cmd/helm/create.go b/cmd/helm/create.go index eaea81e5b..7f0f99af8 100644 --- a/cmd/helm/create.go +++ b/cmd/helm/create.go @@ -38,15 +38,17 @@ something like this: foo/ | - |- .helmignore # Contains patterns to ignore when packaging Helm charts. + |- .helmignore # Contains patterns to ignore when packaging Helm charts. | - |- Chart.yaml # Information about your chart + |- Chart.yaml # Information about your chart | - |- values.yaml # The default values for your templates + |- values.yaml # The default values for your templates | - |- charts/ # Charts that this chart depends on + |- charts/ # Charts that this chart depends on | - |- templates/ # The template files + |- templates/ # The template files + | + |- templates/tests/ # The test files 'helm create' takes a path for an argument. If directories in the given path do not exist, Helm will attempt to create them as it goes. If the given @@ -84,7 +86,6 @@ func newCreateCmd(out io.Writer) *cobra.Command { func (c *createCmd) run() error { fmt.Fprintf(c.out, "Creating %s\n", c.name) - chartname := filepath.Base(c.name) cfile := &chart.Metadata{ Name: chartname, diff --git a/cmd/helm/create_test.go b/cmd/helm/create_test.go index 214432b83..3cdf5ebf8 100644 --- a/cmd/helm/create_test.go +++ b/cmd/helm/create_test.go @@ -143,8 +143,8 @@ func TestCreateStarterCmd(t *testing.T) { t.Errorf("Wrong API version: %q", c.Metadata.ApiVersion) } - if l := len(c.Templates); l != 6 { - t.Errorf("Expected 5 templates, got %d", l) + if l := len(c.Templates); l != 7 { + t.Errorf("Expected 6 templates, got %d", l) } found := false diff --git a/docs/helm/helm_create.md b/docs/helm/helm_create.md index 0e20d9860..2dc45a77c 100644 --- a/docs/helm/helm_create.md +++ b/docs/helm/helm_create.md @@ -13,15 +13,17 @@ something like this: foo/ | - |- .helmignore # Contains patterns to ignore when packaging Helm charts. + |- .helmignore # Contains patterns to ignore when packaging Helm charts. | - |- Chart.yaml # Information about your chart + |- Chart.yaml # Information about your chart | - |- values.yaml # The default values for your templates + |- values.yaml # The default values for your templates | - |- charts/ # Charts that this chart depends on + |- charts/ # Charts that this chart depends on | - |- templates/ # The template files + |- templates/ # The template files + | + |- templates/tests/ # The test files 'helm create' takes a path for an argument. If directories in the given path do not exist, Helm will attempt to create them as it goes. If the given @@ -56,4 +58,4 @@ helm create NAME [flags] * [helm](helm.md) - The Helm package manager for Kubernetes. -###### Auto generated by spf13/cobra on 1-Aug-2018 +###### Auto generated by spf13/cobra on 18-Sep-2018 diff --git a/pkg/chartutil/create.go b/pkg/chartutil/create.go index 046155f71..875083f7e 100644 --- a/pkg/chartutil/create.go +++ b/pkg/chartutil/create.go @@ -44,8 +44,12 @@ const ( ServiceName = "service.yaml" // NotesName is the name of the example NOTES.txt file. NotesName = "NOTES.txt" - // HelpersName is the name of the example NOTES.txt file. + // HelpersName is the name of the example helpers file. HelpersName = "_helpers.tpl" + // TemplatesTestsDir is the relative directory name for templates tests. + TemplatesTestsDir = "templates/tests" + // TestConnectionName is the name of the example connection test file. + TestConnectionName = "test-connection.yaml" ) const defaultValues = `# Default values for %s. @@ -295,6 +299,26 @@ Create chart name and version as used by the chart label. {{- end -}} ` +const defaultTestConnection = `apiVersion: v1 +kind: Pod +metadata: + name: "{{ include ".fullname" . }}-test-connection" + labels: + app.kubernetes.io/name: {{ include ".name" . }} + helm.sh/chart: {{ include ".chart" . }} + app.kubernetes.io/instance: {{ .Release.Name }} + app.kubernetes.io/managed-by: {{ .Release.Service }} + annotations: + "helm.sh/hook": test-success +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include ".fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never +` + // CreateFrom creates a new chart, but scaffolds it from the src chart. func CreateFrom(chartfile *chart.Metadata, dest string, src string) error { schart, err := Load(src) @@ -359,7 +383,7 @@ func Create(chartfile *chart.Metadata, dir string) (string, error) { } } - for _, d := range []string{TemplatesDir, ChartsDir} { + for _, d := range []string{TemplatesDir, TemplatesTestsDir, ChartsDir} { if err := os.MkdirAll(filepath.Join(cdir, d), 0755); err != nil { return cdir, err } @@ -404,6 +428,11 @@ func Create(chartfile *chart.Metadata, dir string) (string, error) { path: filepath.Join(cdir, TemplatesDir, HelpersName), content: Transform(defaultHelpers, "", chartfile.Name), }, + { + // test-connection.yaml + path: filepath.Join(cdir, TemplatesTestsDir, TestConnectionName), + content: Transform(defaultTestConnection, "", chartfile.Name), + }, } for _, file := range files { diff --git a/pkg/chartutil/create_test.go b/pkg/chartutil/create_test.go index 96c467e7e..a0ddf8fa2 100644 --- a/pkg/chartutil/create_test.go +++ b/pkg/chartutil/create_test.go @@ -75,6 +75,14 @@ func TestCreate(t *testing.T) { } } + for _, f := range []string{TestConnectionName} { + if fi, err := os.Stat(filepath.Join(dir, TemplatesTestsDir, f)); err != nil { + t.Errorf("Expected %s file: %s", f, err) + } else if fi.IsDir() { + t.Errorf("Expected %s to be a file.", f) + } + } + } func TestCreateFrom(t *testing.T) { diff --git a/pkg/chartutil/save.go b/pkg/chartutil/save.go index 9b85d0714..5d60485bf 100644 --- a/pkg/chartutil/save.go +++ b/pkg/chartutil/save.go @@ -54,7 +54,7 @@ func SaveDir(c *chart.Chart, dest string) error { } } - for _, d := range []string{TemplatesDir, ChartsDir} { + for _, d := range []string{TemplatesDir, ChartsDir, TemplatesTestsDir} { if err := os.MkdirAll(filepath.Join(outdir, d), 0755); err != nil { return err }