github.com/amy/helm@v2.7.2+incompatible/cmd/helm/template_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 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 "io" 24 "os" 25 "path/filepath" 26 "strings" 27 "testing" 28 ) 29 30 var chartPath = "./../../pkg/chartutil/testdata/subpop/charts/subchart1" 31 32 func TestTemplateCmd(t *testing.T) { 33 absChartPath, err := filepath.Abs(chartPath) 34 if err != nil { 35 t.Fatal(err) 36 } 37 tests := []struct { 38 name string 39 desc string 40 args []string 41 expectKey string 42 expectValue string 43 }{ 44 { 45 name: "check_name", 46 desc: "check for a known name in chart", 47 args: []string{chartPath}, 48 expectKey: "subchart1/templates/service.yaml", 49 expectValue: "protocol: TCP\n name: nginx", 50 }, 51 { 52 name: "check_set_name", 53 desc: "verify --set values exist", 54 args: []string{chartPath, "-x", "templates/service.yaml", "--set", "service.name=apache"}, 55 expectKey: "subchart1/templates/service.yaml", 56 expectValue: "protocol: TCP\n name: apache", 57 }, 58 { 59 name: "check_execute", 60 desc: "verify --execute single template", 61 args: []string{chartPath, "-x", "templates/service.yaml", "--set", "service.name=apache"}, 62 expectKey: "subchart1/templates/service.yaml", 63 expectValue: "protocol: TCP\n name: apache", 64 }, 65 { 66 name: "check_execute_absolute", 67 desc: "verify --execute single template", 68 args: []string{chartPath, "-x", absChartPath + "/" + "templates/service.yaml", "--set", "service.name=apache"}, 69 expectKey: "subchart1/templates/service.yaml", 70 expectValue: "protocol: TCP\n name: apache", 71 }, 72 { 73 name: "check_namespace", 74 desc: "verify --namespace", 75 args: []string{chartPath, "--namespace", "test"}, 76 expectKey: "subchart1/templates/service.yaml", 77 expectValue: "namespace: \"test\"", 78 }, 79 { 80 name: "check_release_name", 81 desc: "verify --release exists", 82 args: []string{chartPath, "--name", "test"}, 83 expectKey: "subchart1/templates/service.yaml", 84 expectValue: "release-name: \"test\"", 85 }, 86 { 87 name: "check_notes", 88 desc: "verify --notes shows notes", 89 args: []string{chartPath, "--notes", "true"}, 90 expectKey: "subchart1/templates/NOTES.txt", 91 expectValue: "Sample notes for subchart1", 92 }, 93 { 94 name: "check_values_files", 95 desc: "verify --values files values exist", 96 args: []string{chartPath, "--values", chartPath + "/charts/subchartA/values.yaml"}, 97 expectKey: "subchart1/templates/service.yaml", 98 expectValue: "name: apache", 99 }, 100 { 101 name: "check_name_template", 102 desc: "verify --name-template result exists", 103 args: []string{chartPath, "--name-template", "foobar-{{ b64enc \"abc\" }}-baz"}, 104 expectKey: "subchart1/templates/service.yaml", 105 expectValue: "release-name: \"foobar-YWJj-baz\"", 106 }, 107 { 108 name: "check_kube_version", 109 desc: "verify --kube-version overrides the kubernetes version", 110 args: []string{chartPath, "--kube-version", "1.6"}, 111 expectKey: "subchart1/templates/service.yaml", 112 expectValue: "kube-version/major: \"1\"\n kube-version/minor: \"6\"", 113 }, 114 } 115 116 var buf bytes.Buffer 117 for _, tt := range tests { 118 t.Run(tt.name, func(T *testing.T) { 119 // capture stdout 120 old := os.Stdout 121 r, w, _ := os.Pipe() 122 os.Stdout = w 123 // execute template command 124 out := bytes.NewBuffer(nil) 125 cmd := newTemplateCmd(out) 126 cmd.SetArgs(tt.args) 127 err := cmd.Execute() 128 if err != nil { 129 t.Errorf("expected: %v, got %v", tt.expectValue, err) 130 } 131 // restore stdout 132 w.Close() 133 os.Stdout = old 134 var b bytes.Buffer 135 io.Copy(&b, r) 136 r.Close() 137 // scan yaml into map[<path>]yaml 138 scanner := bufio.NewScanner(&b) 139 next := false 140 lastKey := "" 141 m := map[string]string{} 142 for scanner.Scan() { 143 if scanner.Text() == "---" { 144 next = true 145 } else if next { 146 // remove '# Source: ' 147 head := "# Source: " 148 lastKey = scanner.Text()[len(head):] 149 next = false 150 } else { 151 m[lastKey] = m[lastKey] + scanner.Text() + "\n" 152 } 153 } 154 if err := scanner.Err(); err != nil { 155 fmt.Fprintln(os.Stderr, "reading standard input:", err) 156 } 157 if v, ok := m[tt.expectKey]; ok { 158 if strings.Contains(v, tt.expectValue) == false { 159 t.Errorf("failed to match expected value %s in %s", tt.expectValue, v) 160 } 161 } else { 162 t.Errorf("could not find key %s", tt.expectKey) 163 } 164 buf.Reset() 165 }) 166 } 167 }