github.com/valdemarpavesi/helm@v2.9.1+incompatible/cmd/helm/package_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package main 17 18 import ( 19 "bytes" 20 "io/ioutil" 21 "os" 22 "path/filepath" 23 "regexp" 24 "testing" 25 26 "github.com/spf13/cobra" 27 28 "k8s.io/helm/pkg/chartutil" 29 "k8s.io/helm/pkg/helm/helmpath" 30 "k8s.io/helm/pkg/proto/hapi/chart" 31 ) 32 33 func TestSetVersion(t *testing.T) { 34 c := &chart.Chart{ 35 Metadata: &chart.Metadata{ 36 Name: "prow", 37 Version: "0.0.1", 38 }, 39 } 40 expect := "1.2.3-beta.5" 41 if err := setVersion(c, expect); err != nil { 42 t.Fatal(err) 43 } 44 45 if c.Metadata.Version != expect { 46 t.Errorf("Expected %q, got %q", expect, c.Metadata.Version) 47 } 48 49 if err := setVersion(c, "monkeyface"); err == nil { 50 t.Error("Expected bogus version to return an error.") 51 } 52 } 53 54 func TestPackage(t *testing.T) { 55 56 tests := []struct { 57 name string 58 flags map[string]string 59 args []string 60 expect string 61 hasfile string 62 err bool 63 }{ 64 { 65 name: "package without chart path", 66 args: []string{}, 67 flags: map[string]string{}, 68 expect: "need at least one argument, the path to the chart", 69 err: true, 70 }, 71 { 72 name: "package --sign, no --key", 73 args: []string{"testdata/testcharts/alpine"}, 74 flags: map[string]string{"sign": "1"}, 75 expect: "key is required for signing a package", 76 err: true, 77 }, 78 { 79 name: "package --sign, no --keyring", 80 args: []string{"testdata/testcharts/alpine"}, 81 flags: map[string]string{"sign": "1", "key": "nosuchkey", "keyring": ""}, 82 expect: "keyring is required for signing a package", 83 err: true, 84 }, 85 { 86 name: "package testdata/testcharts/alpine, no save", 87 args: []string{"testdata/testcharts/alpine"}, 88 flags: map[string]string{"save": "0"}, 89 expect: "", 90 hasfile: "alpine-0.1.0.tgz", 91 }, 92 { 93 name: "package testdata/testcharts/alpine", 94 args: []string{"testdata/testcharts/alpine"}, 95 expect: "", 96 hasfile: "alpine-0.1.0.tgz", 97 }, 98 { 99 name: "package --destination toot", 100 args: []string{"testdata/testcharts/alpine"}, 101 flags: map[string]string{"destination": "toot"}, 102 expect: "", 103 hasfile: "toot/alpine-0.1.0.tgz", 104 }, 105 { 106 name: "package --destination does-not-exist", 107 args: []string{"testdata/testcharts/alpine"}, 108 flags: map[string]string{"destination": "does-not-exist"}, 109 expect: "stat does-not-exist: no such file or directory", 110 err: true, 111 }, 112 { 113 name: "package --sign --key=KEY --keyring=KEYRING testdata/testcharts/alpine", 114 args: []string{"testdata/testcharts/alpine"}, 115 flags: map[string]string{"sign": "1", "keyring": "testdata/helm-test-key.secret", "key": "helm-test"}, 116 expect: "", 117 hasfile: "alpine-0.1.0.tgz", 118 }, 119 { 120 name: "package testdata/testcharts/chart-missing-deps", 121 args: []string{"testdata/testcharts/chart-missing-deps"}, 122 hasfile: "chart-missing-deps-0.1.0.tgz", 123 err: true, 124 }, 125 } 126 127 // Because these tests are destructive, we run them in a tempdir. 128 origDir, err := os.Getwd() 129 if err != nil { 130 t.Fatal(err) 131 } 132 tmp, err := ioutil.TempDir("", "helm-package-test-") 133 if err != nil { 134 t.Fatal(err) 135 } 136 137 t.Logf("Running tests in %s", tmp) 138 if err := os.Chdir(tmp); err != nil { 139 t.Fatal(err) 140 } 141 142 if err := os.Mkdir("toot", 0777); err != nil { 143 t.Fatal(err) 144 } 145 146 ensureTestHome(helmpath.Home(tmp), t) 147 cleanup := resetEnv() 148 defer func() { 149 os.Chdir(origDir) 150 os.RemoveAll(tmp) 151 cleanup() 152 }() 153 154 settings.Home = helmpath.Home(tmp) 155 156 for _, tt := range tests { 157 buf := bytes.NewBuffer(nil) 158 c := newPackageCmd(buf) 159 160 // This is an unfortunate byproduct of the tmpdir 161 if v, ok := tt.flags["keyring"]; ok && len(v) > 0 { 162 tt.flags["keyring"] = filepath.Join(origDir, v) 163 } 164 165 setFlags(c, tt.flags) 166 re := regexp.MustCompile(tt.expect) 167 168 adjustedArgs := make([]string, len(tt.args)) 169 for i, f := range tt.args { 170 adjustedArgs[i] = filepath.Join(origDir, f) 171 } 172 173 err := c.RunE(c, adjustedArgs) 174 if err != nil { 175 if tt.err && re.MatchString(err.Error()) { 176 continue 177 } 178 t.Errorf("%q: expected error %q, got %q", tt.name, tt.expect, err) 179 continue 180 } 181 182 if !re.Match(buf.Bytes()) { 183 t.Errorf("%q: expected output %q, got %q", tt.name, tt.expect, buf.String()) 184 } 185 186 if len(tt.hasfile) > 0 { 187 if fi, err := os.Stat(tt.hasfile); err != nil { 188 t.Errorf("%q: expected file %q, got err %q", tt.name, tt.hasfile, err) 189 } else if fi.Size() == 0 { 190 t.Errorf("%q: file %q has zero bytes.", tt.name, tt.hasfile) 191 } 192 } 193 194 if v, ok := tt.flags["sign"]; ok && v == "1" { 195 if fi, err := os.Stat(tt.hasfile + ".prov"); err != nil { 196 t.Errorf("%q: expected provenance file", tt.name) 197 } else if fi.Size() == 0 { 198 t.Errorf("%q: provenance file is empty", tt.name) 199 } 200 } 201 } 202 } 203 204 func TestSetAppVersion(t *testing.T) { 205 var ch *chart.Chart 206 expectedAppVersion := "app-version-foo" 207 tmp, _ := ioutil.TempDir("", "helm-package-app-version-") 208 209 thome, err := tempHelmHome(t) 210 if err != nil { 211 t.Fatal(err) 212 } 213 cleanup := resetEnv() 214 defer func() { 215 os.RemoveAll(tmp) 216 os.RemoveAll(thome.String()) 217 cleanup() 218 }() 219 220 settings.Home = helmpath.Home(thome) 221 222 c := newPackageCmd(&bytes.Buffer{}) 223 flags := map[string]string{ 224 "destination": tmp, 225 "app-version": expectedAppVersion, 226 } 227 setFlags(c, flags) 228 err = c.RunE(c, []string{"testdata/testcharts/alpine"}) 229 if err != nil { 230 t.Errorf("unexpected error %q", err) 231 } 232 233 chartPath := filepath.Join(tmp, "alpine-0.1.0.tgz") 234 if fi, err := os.Stat(chartPath); err != nil { 235 t.Errorf("expected file %q, got err %q", chartPath, err) 236 } else if fi.Size() == 0 { 237 t.Errorf("file %q has zero bytes.", chartPath) 238 } 239 ch, err = chartutil.Load(chartPath) 240 if err != nil { 241 t.Errorf("unexpected error loading packaged chart: %v", err) 242 } 243 if ch.Metadata.AppVersion != expectedAppVersion { 244 t.Errorf("expected app-version %q, found %q", expectedAppVersion, ch.Metadata.AppVersion) 245 } 246 } 247 248 func setFlags(cmd *cobra.Command, flags map[string]string) { 249 dest := cmd.Flags() 250 for f, v := range flags { 251 dest.Set(f, v) 252 } 253 }