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