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