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 <martin.hickey@ie.ibm.com>

* 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 <martin.hickey@ie.ibm.com>

* Add doc update

Signed-off-by: Martin Hickey <martin.hickey@ie.ibm.com>

* Update GO formatting

Signed-off-by: Martin Hickey <martin.hickey@ie.ibm.com>
pull/4799/merge
Martin Hickey 6 years ago committed by Matthew Fisher
parent 1e26b5300b
commit f6efe2f0c1

@ -47,6 +47,8 @@ something like this:
|- charts/ # Charts that this chart depends on
|
|- 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,

@ -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

@ -22,6 +22,8 @@ something like this:
|- charts/ # Charts that this chart depends on
|
|- 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

@ -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 "<CHARTNAME>.fullname" . }}-test-connection"
labels:
app.kubernetes.io/name: {{ include "<CHARTNAME>.name" . }}
helm.sh/chart: {{ include "<CHARTNAME>.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 "<CHARTNAME>.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, "<CHARTNAME>", chartfile.Name),
},
{
// test-connection.yaml
path: filepath.Join(cdir, TemplatesTestsDir, TestConnectionName),
content: Transform(defaultTestConnection, "<CHARTNAME>", chartfile.Name),
},
}
for _, file := range files {

@ -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) {

@ -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
}

Loading…
Cancel
Save