github.com/caicloud/helm@v2.5.0+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/helm/helmpath" 29 "k8s.io/helm/pkg/proto/hapi/chart" 30 ) 31 32 func TestSetVersion(t *testing.T) { 33 c := &chart.Chart{ 34 Metadata: &chart.Metadata{ 35 Name: "prow", 36 Version: "0.0.1", 37 }, 38 } 39 expect := "1.2.3-beta.5" 40 if err := setVersion(c, expect); err != nil { 41 t.Fatal(err) 42 } 43 44 if c.Metadata.Version != expect { 45 t.Errorf("Expected %q, got %q", expect, c.Metadata.Version) 46 } 47 48 if err := setVersion(c, "monkeyface"); err == nil { 49 t.Error("Expected bogus version to return an error.") 50 } 51 } 52 53 func TestPackage(t *testing.T) { 54 55 tests := []struct { 56 name string 57 flags map[string]string 58 args []string 59 expect string 60 hasfile string 61 err bool 62 }{ 63 { 64 name: "package without chart path", 65 args: []string{}, 66 flags: map[string]string{}, 67 expect: "need at least one argument, the path to the chart", 68 err: true, 69 }, 70 { 71 name: "package --sign, no --key", 72 args: []string{"testdata/testcharts/alpine"}, 73 flags: map[string]string{"sign": "1"}, 74 expect: "key is required for signing a package", 75 err: true, 76 }, 77 { 78 name: "package --sign, no --keyring", 79 args: []string{"testdata/testcharts/alpine"}, 80 flags: map[string]string{"sign": "1", "key": "nosuchkey", "keyring": ""}, 81 expect: "keyring is required for signing a package", 82 err: true, 83 }, 84 { 85 name: "package testdata/testcharts/alpine, no save", 86 args: []string{"testdata/testcharts/alpine"}, 87 flags: map[string]string{"save": "0"}, 88 expect: "", 89 hasfile: "alpine-0.1.0.tgz", 90 }, 91 { 92 name: "package testdata/testcharts/alpine", 93 args: []string{"testdata/testcharts/alpine"}, 94 expect: "", 95 hasfile: "alpine-0.1.0.tgz", 96 }, 97 { 98 name: "package --destination toot", 99 args: []string{"testdata/testcharts/alpine"}, 100 flags: map[string]string{"destination": "toot"}, 101 expect: "", 102 hasfile: "toot/alpine-0.1.0.tgz", 103 }, 104 { 105 name: "package --destination does-not-exist", 106 args: []string{"testdata/testcharts/alpine"}, 107 flags: map[string]string{"destination": "does-not-exist"}, 108 expect: "stat does-not-exist: no such file or directory", 109 err: true, 110 }, 111 { 112 name: "package --sign --key=KEY --keyring=KEYRING testdata/testcharts/alpine", 113 args: []string{"testdata/testcharts/alpine"}, 114 flags: map[string]string{"sign": "1", "keyring": "testdata/helm-test-key.secret", "key": "helm-test"}, 115 expect: "", 116 hasfile: "alpine-0.1.0.tgz", 117 }, 118 { 119 name: "package testdata/testcharts/chart-missing-deps", 120 args: []string{"testdata/testcharts/chart-missing-deps"}, 121 hasfile: "chart-missing-deps-0.1.0.tgz", 122 err: true, 123 }, 124 } 125 126 // Because these tests are destructive, we run them in a tempdir. 127 origDir, err := os.Getwd() 128 if err != nil { 129 t.Fatal(err) 130 } 131 tmp, err := ioutil.TempDir("", "helm-package-test-") 132 if err != nil { 133 t.Fatal(err) 134 } 135 136 t.Logf("Running tests in %s", tmp) 137 if err := os.Chdir(tmp); err != nil { 138 t.Fatal(err) 139 } 140 141 if err := os.Mkdir("toot", 0777); err != nil { 142 t.Fatal(err) 143 } 144 145 ensureTestHome(helmpath.Home(tmp), t) 146 oldhome := settings.Home 147 settings.Home = helmpath.Home(tmp) 148 defer func() { 149 settings.Home = oldhome 150 os.Chdir(origDir) 151 os.RemoveAll(tmp) 152 }() 153 154 for _, tt := range tests { 155 buf := bytes.NewBuffer(nil) 156 c := newPackageCmd(buf) 157 158 // This is an unfortunate byproduct of the tmpdir 159 if v, ok := tt.flags["keyring"]; ok && len(v) > 0 { 160 tt.flags["keyring"] = filepath.Join(origDir, v) 161 } 162 163 setFlags(c, tt.flags) 164 re := regexp.MustCompile(tt.expect) 165 166 adjustedArgs := make([]string, len(tt.args)) 167 for i, f := range tt.args { 168 adjustedArgs[i] = filepath.Join(origDir, f) 169 } 170 171 err := c.RunE(c, adjustedArgs) 172 if err != nil { 173 if tt.err && re.MatchString(err.Error()) { 174 continue 175 } 176 t.Errorf("%q: expected error %q, got %q", tt.name, tt.expect, err) 177 continue 178 } 179 180 if !re.Match(buf.Bytes()) { 181 t.Errorf("%q: expected output %q, got %q", tt.name, tt.expect, buf.String()) 182 } 183 184 if len(tt.hasfile) > 0 { 185 if fi, err := os.Stat(tt.hasfile); err != nil { 186 t.Errorf("%q: expected file %q, got err %q", tt.name, tt.hasfile, err) 187 } else if fi.Size() == 0 { 188 t.Errorf("%q: file %q has zero bytes.", tt.name, tt.hasfile) 189 } 190 } 191 192 if v, ok := tt.flags["sign"]; ok && v == "1" { 193 if fi, err := os.Stat(tt.hasfile + ".prov"); err != nil { 194 t.Errorf("%q: expected provenance file", tt.name) 195 } else if fi.Size() == 0 { 196 t.Errorf("%q: provenance file is empty", tt.name) 197 } 198 } 199 } 200 } 201 202 func setFlags(cmd *cobra.Command, flags map[string]string) { 203 dest := cmd.Flags() 204 for f, v := range flags { 205 dest.Set(f, v) 206 } 207 }