github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+incompatible/cmd/helm/install_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 "fmt" 21 "io" 22 "regexp" 23 "strings" 24 "testing" 25 26 "github.com/spf13/cobra" 27 ) 28 29 func TestInstall(t *testing.T) { 30 tests := []releaseCase{ 31 // Install, base case 32 { 33 name: "basic install", 34 args: []string{"testdata/testcharts/alpine"}, 35 flags: strings.Split("--name aeneas", " "), 36 expected: "aeneas", 37 resp: releaseMock(&releaseOptions{name: "aeneas"}), 38 }, 39 // Install, no hooks 40 { 41 name: "install without hooks", 42 args: []string{"testdata/testcharts/alpine"}, 43 flags: strings.Split("--name aeneas --no-hooks", " "), 44 expected: "juno", 45 resp: releaseMock(&releaseOptions{name: "juno"}), 46 }, 47 // Install, values from cli 48 { 49 name: "install with values", 50 args: []string{"testdata/testcharts/alpine"}, 51 flags: strings.Split("--set foo=bar", " "), 52 resp: releaseMock(&releaseOptions{name: "virgil"}), 53 expected: "virgil", 54 }, 55 // Install, no charts 56 { 57 name: "install with no chart specified", 58 args: []string{}, 59 err: true, 60 }, 61 // Install, re-use name 62 { 63 name: "install and replace release", 64 args: []string{"testdata/testcharts/alpine"}, 65 flags: strings.Split("--name aeneas --replace", " "), 66 expected: "aeneas", 67 resp: releaseMock(&releaseOptions{name: "aeneas"}), 68 }, 69 // Install, using the name-template 70 { 71 name: "install with name-template", 72 args: []string{"testdata/testcharts/alpine"}, 73 flags: []string{"--name-template", "{{upper \"foobar\"}}"}, 74 expected: "FOOBAR", 75 resp: releaseMock(&releaseOptions{name: "FOOBAR"}), 76 }, 77 // Install, perform chart verification along the way. 78 { 79 name: "install with verification, missing provenance", 80 args: []string{"testdata/testcharts/compressedchart-0.1.0.tgz"}, 81 flags: strings.Split("--verify --keyring testdata/helm-test-key.pub", " "), 82 err: true, 83 }, 84 { 85 name: "install with verification, directory instead of file", 86 args: []string{"testdata/testcharts/signtest"}, 87 flags: strings.Split("--verify --keyring testdata/helm-test-key.pub", " "), 88 err: true, 89 }, 90 { 91 name: "install with verification, valid", 92 args: []string{"testdata/testcharts/signtest-0.1.0.tgz"}, 93 flags: strings.Split("--verify --keyring testdata/helm-test-key.pub", " "), 94 }, 95 } 96 97 runReleaseCases(t, tests, func(c *fakeReleaseClient, out io.Writer) *cobra.Command { 98 return newInstallCmd(c, out) 99 }) 100 } 101 102 func TestValues(t *testing.T) { 103 args := "sailor=sinbad,good,port.source=baghdad,port.destination=basrah" 104 vobj := new(values) 105 vobj.Set(args) 106 107 if vobj.Type() != "struct" { 108 t.Fatalf("Expected Type to be struct, got %s", vobj.Type()) 109 } 110 111 vals := vobj.pairs 112 if fmt.Sprint(vals["good"]) != "true" { 113 t.Errorf("Expected good to be true. Got %v", vals["good"]) 114 } 115 116 port := vals["port"].(map[string]interface{}) 117 118 if fmt.Sprint(port["source"]) != "baghdad" { 119 t.Errorf("Expected source to be baghdad. Got %s", port["source"]) 120 } 121 if fmt.Sprint(port["destination"]) != "basrah" { 122 t.Errorf("Expected source to be baghdad. Got %s", port["source"]) 123 } 124 125 y := `good: true 126 port: 127 destination: basrah 128 source: baghdad 129 sailor: sinbad 130 ` 131 out, err := vobj.yaml() 132 if err != nil { 133 t.Fatal(err) 134 } 135 if string(out) != y { 136 t.Errorf("Expected YAML to be \n%s\nGot\n%s\n", y, out) 137 } 138 139 if vobj.String() != y { 140 t.Errorf("Expected String() to be \n%s\nGot\n%s\n", y, out) 141 } 142 143 // Combined case, overriding a property 144 vals["sailor"] = "pisti" 145 updatedYAML := `good: true 146 port: 147 destination: basrah 148 source: baghdad 149 sailor: pisti 150 ` 151 newOut, err := vobj.yaml() 152 if err != nil { 153 t.Fatal(err) 154 } 155 if string(newOut) != updatedYAML { 156 t.Errorf("Expected YAML to be \n%s\nGot\n%s\n", updatedYAML, newOut) 157 } 158 159 } 160 161 type nameTemplateTestCase struct { 162 tpl string 163 expected string 164 expectedErrorStr string 165 } 166 167 func TestNameTemplate(t *testing.T) { 168 testCases := []nameTemplateTestCase{ 169 // Just a straight up nop please 170 { 171 tpl: "foobar", 172 expected: "foobar", 173 expectedErrorStr: "", 174 }, 175 // Random numbers at the end for fun & profit 176 { 177 tpl: "foobar-{{randNumeric 6}}", 178 expected: "foobar-[0-9]{6}$", 179 expectedErrorStr: "", 180 }, 181 // Random numbers in the middle for fun & profit 182 { 183 tpl: "foobar-{{randNumeric 4}}-baz", 184 expected: "foobar-[0-9]{4}-baz$", 185 expectedErrorStr: "", 186 }, 187 // No such function 188 { 189 tpl: "foobar-{{randInt}}", 190 expected: "", 191 expectedErrorStr: "function \"randInt\" not defined", 192 }, 193 // Invalid template 194 { 195 tpl: "foobar-{{", 196 expected: "", 197 expectedErrorStr: "unexpected unclosed action", 198 }, 199 } 200 201 for _, tc := range testCases { 202 203 n, err := generateName(tc.tpl) 204 if err != nil { 205 if tc.expectedErrorStr == "" { 206 t.Errorf("Was not expecting error, but got: %v", err) 207 continue 208 } 209 re, compErr := regexp.Compile(tc.expectedErrorStr) 210 if compErr != nil { 211 t.Errorf("Expected error string failed to compile: %v", compErr) 212 continue 213 } 214 if !re.MatchString(err.Error()) { 215 t.Errorf("Error didn't match for %s expected %s but got %v", tc.tpl, tc.expectedErrorStr, err) 216 continue 217 } 218 } 219 if err == nil && tc.expectedErrorStr != "" { 220 t.Errorf("Was expecting error %s but didn't get an error back", tc.expectedErrorStr) 221 } 222 223 if tc.expected != "" { 224 re, err := regexp.Compile(tc.expected) 225 if err != nil { 226 t.Errorf("Expected string failed to compile: %v", err) 227 continue 228 } 229 if !re.MatchString(n) { 230 t.Errorf("Returned name didn't match for %s expected %s but got %s", tc.tpl, tc.expected, n) 231 } 232 } 233 } 234 }