github.com/codefresh-io/kcfi@v0.0.0-20230301195427-c1578715cc46/cmd/kcfi/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 "os" 21 "path/filepath" 22 "regexp" 23 "testing" 24 25 "github.com/spf13/cobra" 26 27 "github.com/codefresh-io/kcfi/pkg/helm-internal/test/ensure" 28 "helm.sh/helm/v3/pkg/chart" 29 "helm.sh/helm/v3/pkg/chart/loader" 30 ) 31 32 func TestPackage(t *testing.T) { 33 tests := []struct { 34 name string 35 flags map[string]string 36 args []string 37 expect string 38 hasfile string 39 err bool 40 }{ 41 { 42 name: "package without chart path", 43 args: []string{}, 44 flags: map[string]string{}, 45 expect: "need at least one argument, the path to the chart", 46 err: true, 47 }, 48 { 49 name: "package --sign, no --key", 50 args: []string{"testdata/testcharts/alpine"}, 51 flags: map[string]string{"sign": "1"}, 52 expect: "key is required for signing a package", 53 err: true, 54 }, 55 { 56 name: "package --sign, no --keyring", 57 args: []string{"testdata/testcharts/alpine"}, 58 flags: map[string]string{"sign": "1", "key": "nosuchkey", "keyring": ""}, 59 expect: "keyring is required for signing a package", 60 err: true, 61 }, 62 { 63 name: "package testdata/testcharts/alpine, no save", 64 args: []string{"testdata/testcharts/alpine"}, 65 flags: map[string]string{"save": "0"}, 66 expect: "", 67 hasfile: "alpine-0.1.0.tgz", 68 }, 69 { 70 name: "package testdata/testcharts/alpine", 71 args: []string{"testdata/testcharts/alpine"}, 72 expect: "", 73 hasfile: "alpine-0.1.0.tgz", 74 }, 75 { 76 name: "package testdata/testcharts/issue1979", 77 args: []string{"testdata/testcharts/issue1979"}, 78 expect: "", 79 hasfile: "alpine-0.1.0.tgz", 80 }, 81 { 82 name: "package --destination toot", 83 args: []string{"testdata/testcharts/alpine"}, 84 flags: map[string]string{"destination": "toot"}, 85 expect: "", 86 hasfile: "toot/alpine-0.1.0.tgz", 87 }, 88 { 89 name: "package --sign --key=KEY --keyring=KEYRING testdata/testcharts/alpine", 90 args: []string{"testdata/testcharts/alpine"}, 91 flags: map[string]string{"sign": "1", "keyring": "testdata/helm-test-key.secret", "key": "helm-test"}, 92 expect: "", 93 hasfile: "alpine-0.1.0.tgz", 94 }, 95 { 96 name: "package testdata/testcharts/chart-missing-deps", 97 args: []string{"testdata/testcharts/chart-missing-deps"}, 98 hasfile: "chart-missing-deps-0.1.0.tgz", 99 err: true, 100 }, 101 { 102 name: "package testdata/testcharts/chart-bad-type", 103 args: []string{"testdata/testcharts/chart-bad-type"}, 104 err: true, 105 }, 106 } 107 108 origDir, err := os.Getwd() 109 if err != nil { 110 t.Fatal(err) 111 } 112 113 for _, tt := range tests { 114 t.Run(tt.name, func(t *testing.T) { 115 cachePath := ensure.TempDir(t) 116 defer testChdir(t, cachePath)() 117 118 if err := os.MkdirAll("toot", 0777); err != nil { 119 t.Fatal(err) 120 } 121 var buf bytes.Buffer 122 c := newPackageCmd(&buf) 123 124 // This is an unfortunate byproduct of the tmpdir 125 if v, ok := tt.flags["keyring"]; ok && len(v) > 0 { 126 tt.flags["keyring"] = filepath.Join(origDir, v) 127 } 128 129 setFlags(c, tt.flags) 130 re := regexp.MustCompile(tt.expect) 131 132 adjustedArgs := make([]string, len(tt.args)) 133 for i, f := range tt.args { 134 adjustedArgs[i] = filepath.Join(origDir, f) 135 } 136 137 err := c.RunE(c, adjustedArgs) 138 if err != nil { 139 if tt.err && re.MatchString(err.Error()) { 140 return 141 } 142 t.Fatalf("%q: expected error %q, got %q", tt.name, tt.expect, err) 143 } 144 145 if !re.Match(buf.Bytes()) { 146 t.Errorf("%q: expected output %q, got %q", tt.name, tt.expect, buf.String()) 147 } 148 149 if len(tt.hasfile) > 0 { 150 if fi, err := os.Stat(tt.hasfile); err != nil { 151 t.Errorf("%q: expected file %q, got err %q", tt.name, tt.hasfile, err) 152 } else if fi.Size() == 0 { 153 t.Errorf("%q: file %q has zero bytes.", tt.name, tt.hasfile) 154 } 155 } 156 157 if v, ok := tt.flags["sign"]; ok && v == "1" { 158 if fi, err := os.Stat(tt.hasfile + ".prov"); err != nil { 159 t.Errorf("%q: expected provenance file", tt.name) 160 } else if fi.Size() == 0 { 161 t.Errorf("%q: provenance file is empty", tt.name) 162 } 163 } 164 }) 165 } 166 } 167 168 func TestSetAppVersion(t *testing.T) { 169 var ch *chart.Chart 170 expectedAppVersion := "app-version-foo" 171 172 dir := ensure.TempDir(t) 173 174 c := newPackageCmd(&bytes.Buffer{}) 175 flags := map[string]string{ 176 "destination": dir, 177 "app-version": expectedAppVersion, 178 } 179 setFlags(c, flags) 180 if err := c.RunE(c, []string{"testdata/testcharts/alpine"}); err != nil { 181 t.Errorf("unexpected error %q", err) 182 } 183 184 chartPath := filepath.Join(dir, "alpine-0.1.0.tgz") 185 if fi, err := os.Stat(chartPath); err != nil { 186 t.Errorf("expected file %q, got err %q", chartPath, err) 187 } else if fi.Size() == 0 { 188 t.Errorf("file %q has zero bytes.", chartPath) 189 } 190 ch, err := loader.Load(chartPath) 191 if err != nil { 192 t.Fatalf("unexpected error loading packaged chart: %v", err) 193 } 194 if ch.Metadata.AppVersion != expectedAppVersion { 195 t.Errorf("expected app-version %q, found %q", expectedAppVersion, ch.Metadata.AppVersion) 196 } 197 } 198 199 func setFlags(cmd *cobra.Command, flags map[string]string) { 200 dest := cmd.Flags() 201 for f, v := range flags { 202 dest.Set(f, v) 203 } 204 }