github.com/sdbaiguanghe/helm@v2.16.7+incompatible/cmd/helm/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 main
    18  
    19  import (
    20  	"bufio"
    21  	"bytes"
    22  	"fmt"
    23  	"os"
    24  	"path/filepath"
    25  	"strings"
    26  	"testing"
    27  )
    28  
    29  var (
    30  	subchart1ChartPath = "./../../pkg/chartutil/testdata/subpop/charts/subchart1"
    31  	frobnitzChartPath  = "./../../pkg/chartutil/testdata/frobnitz"
    32  )
    33  
    34  func TestTemplateCmd(t *testing.T) {
    35  	subchart1AbsChartPath, err := filepath.Abs(subchart1ChartPath)
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	tests := []struct {
    40  		name        string
    41  		desc        string
    42  		args        []string
    43  		expectKey   string
    44  		expectValue string
    45  		expectError string
    46  	}{
    47  		{
    48  			name:        "check_name",
    49  			desc:        "check for a known name in chart",
    50  			args:        []string{subchart1ChartPath},
    51  			expectKey:   "subchart1/templates/service.yaml",
    52  			expectValue: "protocol: TCP\n    name: nginx",
    53  		},
    54  		{
    55  			name:        "check_set_name",
    56  			desc:        "verify --set values exist",
    57  			args:        []string{subchart1ChartPath, "-x", "templates/service.yaml", "--set", "service.name=apache"},
    58  			expectKey:   "subchart1/templates/service.yaml",
    59  			expectValue: "protocol: TCP\n    name: apache",
    60  		},
    61  		{
    62  			name:        "check_execute",
    63  			desc:        "verify --execute single template",
    64  			args:        []string{subchart1ChartPath, "-x", "templates/service.yaml", "--set", "service.name=apache"},
    65  			expectKey:   "subchart1/templates/service.yaml",
    66  			expectValue: "protocol: TCP\n    name: apache",
    67  		},
    68  		{
    69  			name:        "check_execute_non_existent",
    70  			desc:        "verify --execute fails on a template that doesn't exist",
    71  			args:        []string{subchart1ChartPath, "-x", "templates/thisdoesn'texist.yaml"},
    72  			expectError: "could not find template",
    73  		},
    74  		{
    75  			name:        "check_execute_absolute",
    76  			desc:        "verify --execute single template",
    77  			args:        []string{subchart1ChartPath, "-x", filepath.Join(subchart1AbsChartPath, "templates", "service.yaml"), "--set", "service.name=apache"},
    78  			expectKey:   "subchart1/templates/service.yaml",
    79  			expectValue: "protocol: TCP\n    name: apache",
    80  		},
    81  		{
    82  			name:        "check_execute_subchart_template",
    83  			desc:        "verify --execute single template on a subchart template",
    84  			args:        []string{subchart1ChartPath, "-x", "charts/subcharta/templates/service.yaml", "--set", "subcharta.service.name=foobar"},
    85  			expectKey:   "subchart1/charts/subcharta/templates/service.yaml",
    86  			expectValue: "protocol: TCP\n    name: foobar",
    87  		},
    88  		{
    89  			name:        "check_execute_subchart_template_for_tgz_subchart",
    90  			desc:        "verify --execute single template on a subchart template where the subchart is a .tgz in the chart directory",
    91  			args:        []string{frobnitzChartPath, "-x", "charts/mariner/templates/placeholder.tpl", "--set", "mariner.name=moon"},
    92  			expectKey:   "frobnitz/charts/mariner/templates/placeholder.tpl",
    93  			expectValue: "Goodbye moon",
    94  		},
    95  		{
    96  			name:        "check_namespace",
    97  			desc:        "verify --namespace",
    98  			args:        []string{subchart1ChartPath, "--namespace", "test"},
    99  			expectKey:   "subchart1/templates/service.yaml",
   100  			expectValue: "namespace: \"test\"",
   101  		},
   102  		{
   103  			name:        "check_release_name",
   104  			desc:        "verify --release exists",
   105  			args:        []string{subchart1ChartPath, "--name", "test"},
   106  			expectKey:   "subchart1/templates/service.yaml",
   107  			expectValue: "release-name: \"test\"",
   108  		},
   109  		{
   110  			name:        "check_invalid_name_uppercase",
   111  			desc:        "verify the release name using capitals is invalid",
   112  			args:        []string{subchart1ChartPath, "--name", "FOO"},
   113  			expectKey:   "subchart1/templates/service.yaml",
   114  			expectError: "is invalid",
   115  		},
   116  		{
   117  			name:        "check_invalid_name_uppercase",
   118  			desc:        "verify the release name using periods is invalid",
   119  			args:        []string{subchart1ChartPath, "--name", "foo.bar"},
   120  			expectKey:   "subchart1/templates/service.yaml",
   121  			expectValue: "release-name: \"foo.bar\"",
   122  		},
   123  		{
   124  			name:        "check_invalid_name_uppercase",
   125  			desc:        "verify the release name using underscores is invalid",
   126  			args:        []string{subchart1ChartPath, "--name", "foo_bar"},
   127  			expectKey:   "subchart1/templates/service.yaml",
   128  			expectError: "is invalid",
   129  		},
   130  		{
   131  			name:        "check_release_is_install",
   132  			desc:        "verify --is-upgrade toggles .Release.IsInstall",
   133  			args:        []string{subchart1ChartPath, "--is-upgrade=false"},
   134  			expectKey:   "subchart1/templates/service.yaml",
   135  			expectValue: "release-is-install: \"true\"",
   136  		},
   137  		{
   138  			name:        "check_release_is_upgrade",
   139  			desc:        "verify --is-upgrade toggles .Release.IsUpgrade",
   140  			args:        []string{subchart1ChartPath, "--is-upgrade", "true"},
   141  			expectKey:   "subchart1/templates/service.yaml",
   142  			expectValue: "release-is-upgrade: \"true\"",
   143  		},
   144  		{
   145  			name:        "check_notes",
   146  			desc:        "verify --notes shows notes",
   147  			args:        []string{subchart1ChartPath, "--notes", "true"},
   148  			expectKey:   "subchart1/templates/NOTES.txt",
   149  			expectValue: "Sample notes for subchart1",
   150  		},
   151  		{
   152  			name:        "check_values_files",
   153  			desc:        "verify --values files values exist",
   154  			args:        []string{subchart1ChartPath, "--values", subchart1ChartPath + "/charts/subchartA/values.yaml"},
   155  			expectKey:   "subchart1/templates/service.yaml",
   156  			expectValue: "name: apache",
   157  		},
   158  		{
   159  			name:        "check_invalid_name_template",
   160  			desc:        "verify the release name generate by template is invalid",
   161  			args:        []string{subchart1ChartPath, "--name-template", "foobar-{{ b64enc \"abc\" }}-baz"},
   162  			expectError: "is invalid",
   163  		},
   164  		{
   165  			name:        "check_name_template",
   166  			desc:        "verify --name-template result exists",
   167  			args:        []string{subchart1ChartPath, "--name-template", "foobar-{{ lower \"ABC\" }}-baz"},
   168  			expectKey:   "subchart1/templates/service.yaml",
   169  			expectValue: "release-name: \"foobar-abc-baz\"",
   170  		},
   171  		{
   172  			name:        "check_kube_version",
   173  			desc:        "verify --kube-version overrides the kubernetes version",
   174  			args:        []string{subchart1ChartPath, "--kube-version", "1.6"},
   175  			expectKey:   "subchart1/templates/service.yaml",
   176  			expectValue: "kube-version/major: \"1\"\n    kube-version/minor: \"6\"\n    kube-version/gitversion: \"v1.6.0\"",
   177  		},
   178  		{
   179  			name:        "check_kube_api_versions",
   180  			desc:        "verify --api-versions overrides kubernetes api versions",
   181  			args:        []string{subchart1ChartPath, "--api-versions", "helm.k8s.io/test"},
   182  			expectKey:   "subchart1/templates/service.yaml",
   183  			expectValue: "kube-api-version/test: v1",
   184  		},
   185  	}
   186  
   187  	for _, tt := range tests {
   188  		tt := tt
   189  		t.Run(tt.name, func(t *testing.T) {
   190  			// execute template command
   191  			out := bytes.NewBuffer(nil)
   192  			cmd := newTemplateCmd(out)
   193  			cmd.SetArgs(tt.args)
   194  			err := cmd.Execute()
   195  
   196  			if tt.expectError != "" {
   197  				if err == nil {
   198  					t.Errorf("expected err: %s, but no error occurred", tt.expectError)
   199  				}
   200  				// non nil error, check if it contains the expected error
   201  				if strings.Contains(err.Error(), tt.expectError) {
   202  					// had the error we were looking for, this test case is
   203  					// done
   204  					return
   205  				}
   206  				t.Fatalf("expected err: %q, got: %q", tt.expectError, err)
   207  			} else if err != nil {
   208  				t.Errorf("expected no error, got %v", err)
   209  			}
   210  			// scan yaml into map[<path>]yaml
   211  			scanner := bufio.NewScanner(out)
   212  			next := false
   213  			lastKey := ""
   214  			m := map[string]string{}
   215  			for scanner.Scan() {
   216  				if scanner.Text() == "---" {
   217  					next = true
   218  				} else if next {
   219  					// remove '# Source: '
   220  					head := "# Source: "
   221  					lastKey = scanner.Text()[len(head):]
   222  					next = false
   223  				} else {
   224  					m[lastKey] = m[lastKey] + scanner.Text() + "\n"
   225  				}
   226  			}
   227  			if err := scanner.Err(); err != nil {
   228  				fmt.Fprintln(os.Stderr, "reading standard input:", err)
   229  			}
   230  			if v, ok := m[tt.expectKey]; ok {
   231  				if !strings.Contains(v, tt.expectValue) {
   232  					t.Errorf("failed to match expected value %s in %s", tt.expectValue, v)
   233  				}
   234  			} else {
   235  				t.Errorf("could not find key %s", tt.expectKey)
   236  			}
   237  		})
   238  	}
   239  }