github.com/Racer159/helm-experiment@v0.0.0-20230822001441-1eb31183f614/src/template_test.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cmd
    18  
    19  import (
    20  	"fmt"
    21  	"path/filepath"
    22  	"testing"
    23  )
    24  
    25  var chartPath = "testdata/testcharts/subchart"
    26  
    27  func TestTemplateCmd(t *testing.T) {
    28  	tests := []cmdTestCase{
    29  		{
    30  			name:   "check name",
    31  			cmd:    fmt.Sprintf("template '%s'", chartPath),
    32  			golden: "output/template.txt",
    33  		},
    34  		{
    35  			name:   "check set name",
    36  			cmd:    fmt.Sprintf("template '%s' --set service.name=apache", chartPath),
    37  			golden: "output/template-set.txt",
    38  		},
    39  		{
    40  			name:   "check values files",
    41  			cmd:    fmt.Sprintf("template '%s' --values '%s'", chartPath, filepath.Join(chartPath, "/charts/subchartA/values.yaml")),
    42  			golden: "output/template-values-files.txt",
    43  		},
    44  		{
    45  			name:   "check name template",
    46  			cmd:    fmt.Sprintf(`template '%s' --name-template='foobar-{{ b64enc "abc" | lower }}-baz'`, chartPath),
    47  			golden: "output/template-name-template.txt",
    48  		},
    49  		{
    50  			name:      "check no args",
    51  			cmd:       "template",
    52  			wantError: true,
    53  			golden:    "output/template-no-args.txt",
    54  		},
    55  		{
    56  			name:      "check library chart",
    57  			cmd:       fmt.Sprintf("template '%s'", "testdata/testcharts/lib-chart"),
    58  			wantError: true,
    59  			golden:    "output/template-lib-chart.txt",
    60  		},
    61  		{
    62  			name:      "check chart bad type",
    63  			cmd:       fmt.Sprintf("template '%s'", "testdata/testcharts/chart-bad-type"),
    64  			wantError: true,
    65  			golden:    "output/template-chart-bad-type.txt",
    66  		},
    67  		{
    68  			name:   "check chart with dependency which is an app chart acting as a library chart",
    69  			cmd:    fmt.Sprintf("template '%s'", "testdata/testcharts/chart-with-template-lib-dep"),
    70  			golden: "output/template-chart-with-template-lib-dep.txt",
    71  		},
    72  		{
    73  			name:   "check chart with dependency which is an app chart archive acting as a library chart",
    74  			cmd:    fmt.Sprintf("template '%s'", "testdata/testcharts/chart-with-template-lib-archive-dep"),
    75  			golden: "output/template-chart-with-template-lib-archive-dep.txt",
    76  		},
    77  		{
    78  			name:   "check kube version",
    79  			cmd:    fmt.Sprintf("template --kube-version 1.16.0 '%s'", chartPath),
    80  			golden: "output/template-with-kube-version.txt",
    81  		},
    82  		{
    83  			name:   "check kube api versions",
    84  			cmd:    fmt.Sprintf("template --api-versions helm.k8s.io/test '%s'", chartPath),
    85  			golden: "output/template-with-api-version.txt",
    86  		},
    87  		{
    88  			name:   "template with CRDs",
    89  			cmd:    fmt.Sprintf("template '%s' --include-crds", chartPath),
    90  			golden: "output/template-with-crds.txt",
    91  		},
    92  		{
    93  			name:   "template with show-only one",
    94  			cmd:    fmt.Sprintf("template '%s' --show-only templates/service.yaml", chartPath),
    95  			golden: "output/template-show-only-one.txt",
    96  		},
    97  		{
    98  			name:   "template with show-only multiple",
    99  			cmd:    fmt.Sprintf("template '%s' --show-only templates/service.yaml --show-only charts/subcharta/templates/service.yaml", chartPath),
   100  			golden: "output/template-show-only-multiple.txt",
   101  		},
   102  		{
   103  			name:   "template with show-only glob",
   104  			cmd:    fmt.Sprintf("template '%s' --show-only templates/subdir/role*", chartPath),
   105  			golden: "output/template-show-only-glob.txt",
   106  			// Repeat to ensure manifest ordering regressions are caught
   107  			repeat: 10,
   108  		},
   109  		{
   110  			name:   "sorted output of manifests (order of filenames, then order of objects within each YAML file)",
   111  			cmd:    fmt.Sprintf("template '%s'", "testdata/testcharts/object-order"),
   112  			golden: "output/object-order.txt",
   113  			// Helm previously used random file order. Repeat the test so we
   114  			// don't accidentally get the expected result.
   115  			repeat: 10,
   116  		},
   117  		{
   118  			name:      "chart with template with invalid yaml",
   119  			cmd:       fmt.Sprintf("template '%s'", "testdata/testcharts/chart-with-template-with-invalid-yaml"),
   120  			wantError: true,
   121  			golden:    "output/template-with-invalid-yaml.txt",
   122  		},
   123  		{
   124  			name:      "chart with template with invalid yaml (--debug)",
   125  			cmd:       fmt.Sprintf("template '%s' --debug", "testdata/testcharts/chart-with-template-with-invalid-yaml"),
   126  			wantError: true,
   127  			golden:    "output/template-with-invalid-yaml-debug.txt",
   128  		},
   129  		{
   130  			name:   "template skip-tests",
   131  			cmd:    fmt.Sprintf(`template '%s' --skip-tests`, chartPath),
   132  			golden: "output/template-skip-tests.txt",
   133  		},
   134  	}
   135  	runTestCmd(t, tests)
   136  }
   137  
   138  func TestTemplateVersionCompletion(t *testing.T) {
   139  	repoFile := "testdata/helmhome/helm/repositories.yaml"
   140  	repoCache := "testdata/helmhome/helm/repository"
   141  
   142  	repoSetup := fmt.Sprintf("--repository-config %s --repository-cache %s", repoFile, repoCache)
   143  
   144  	tests := []cmdTestCase{{
   145  		name:   "completion for template version flag with release name",
   146  		cmd:    fmt.Sprintf("%s __complete template releasename testing/alpine --version ''", repoSetup),
   147  		golden: "output/version-comp.txt",
   148  	}, {
   149  		name:   "completion for template version flag with generate-name",
   150  		cmd:    fmt.Sprintf("%s __complete template --generate-name testing/alpine --version ''", repoSetup),
   151  		golden: "output/version-comp.txt",
   152  	}, {
   153  		name:   "completion for template version flag too few args",
   154  		cmd:    fmt.Sprintf("%s __complete template testing/alpine --version ''", repoSetup),
   155  		golden: "output/version-invalid-comp.txt",
   156  	}, {
   157  		name:   "completion for template version flag too many args",
   158  		cmd:    fmt.Sprintf("%s __complete template releasename testing/alpine badarg --version ''", repoSetup),
   159  		golden: "output/version-invalid-comp.txt",
   160  	}, {
   161  		name:   "completion for template version flag invalid chart",
   162  		cmd:    fmt.Sprintf("%s __complete template releasename invalid/invalid --version ''", repoSetup),
   163  		golden: "output/version-invalid-comp.txt",
   164  	}}
   165  	runTestCmd(t, tests)
   166  }
   167  
   168  func TestTemplateFileCompletion(t *testing.T) {
   169  	checkFileCompletion(t, "template", false)
   170  	checkFileCompletion(t, "template --generate-name", true)
   171  	checkFileCompletion(t, "template myname", true)
   172  	checkFileCompletion(t, "template myname mychart", false)
   173  }