github.com/cloudposse/helm@v2.2.3+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/cmd/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: "This command needs 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 --sign --key=KEY --keyring=KEYRING testdata/testcharts/alpine", 99 args: []string{"testdata/testcharts/alpine"}, 100 flags: map[string]string{"sign": "1", "keyring": "testdata/helm-test-key.secret", "key": "helm-test"}, 101 expect: "", 102 hasfile: "alpine-0.1.0.tgz", 103 }, 104 { 105 name: "package testdata/testcharts/chart-missing-deps", 106 args: []string{"testdata/testcharts/chart-missing-deps"}, 107 expect: "Warning: reqsubchart2 is in requirements.yaml but not in the charts/ directory!\n", 108 hasfile: "chart-missing-deps-0.1.0.tgz", 109 }, 110 } 111 112 // Because these tests are destructive, we run them in a tempdir. 113 origDir, err := os.Getwd() 114 if err != nil { 115 t.Fatal(err) 116 } 117 tmp, err := ioutil.TempDir("", "helm-package-test-") 118 if err != nil { 119 t.Fatal(err) 120 } 121 122 t.Logf("Running tests in %s", tmp) 123 if err := os.Chdir(tmp); err != nil { 124 t.Fatal(err) 125 } 126 127 ensureTestHome(helmpath.Home(tmp), t) 128 oldhome := homePath() 129 helmHome = tmp 130 defer func() { 131 helmHome = oldhome 132 os.Chdir(origDir) 133 os.RemoveAll(tmp) 134 }() 135 136 for _, tt := range tests { 137 buf := bytes.NewBuffer(nil) 138 c := newPackageCmd(buf) 139 140 // This is an unfortunate byproduct of the tmpdir 141 if v, ok := tt.flags["keyring"]; ok && len(v) > 0 { 142 tt.flags["keyring"] = filepath.Join(origDir, v) 143 } 144 145 setFlags(c, tt.flags) 146 re := regexp.MustCompile(tt.expect) 147 148 adjustedArgs := make([]string, len(tt.args)) 149 for i, f := range tt.args { 150 adjustedArgs[i] = filepath.Join(origDir, f) 151 } 152 153 err := c.RunE(c, adjustedArgs) 154 if err != nil { 155 if tt.err && re.MatchString(err.Error()) { 156 continue 157 } 158 t.Errorf("%q: expected error %q, got %q", tt.name, tt.expect, err) 159 continue 160 } 161 162 if !re.Match(buf.Bytes()) { 163 t.Errorf("%q: expected output %q, got %q", tt.name, tt.expect, buf.String()) 164 } 165 166 if len(tt.hasfile) > 0 { 167 if fi, err := os.Stat(tt.hasfile); err != nil { 168 t.Errorf("%q: expected file %q, got err %q", tt.name, tt.hasfile, err) 169 } else if fi.Size() == 0 { 170 t.Errorf("%q: file %q has zero bytes.", tt.name, tt.hasfile) 171 } 172 } 173 174 if v, ok := tt.flags["sign"]; ok && v == "1" { 175 if fi, err := os.Stat(tt.hasfile + ".prov"); err != nil { 176 t.Errorf("%q: expected provenance file", tt.name) 177 } else if fi.Size() == 0 { 178 t.Errorf("%q: provenance file is empty", tt.name) 179 } 180 } 181 } 182 } 183 184 func setFlags(cmd *cobra.Command, flags map[string]string) { 185 dest := cmd.Flags() 186 for f, v := range flags { 187 dest.Set(f, v) 188 } 189 }