github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+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  
    29  func TestPackage(t *testing.T) {
    30  
    31  	tests := []struct {
    32  		name    string
    33  		flags   map[string]string
    34  		args    []string
    35  		expect  string
    36  		hasfile string
    37  		err     bool
    38  	}{
    39  		{
    40  			name:   "package without chart path",
    41  			args:   []string{},
    42  			flags:  map[string]string{},
    43  			expect: "This command needs at least one argument, the path to the chart.",
    44  			err:    true,
    45  		},
    46  		{
    47  			name:   "package --sign, no --key",
    48  			args:   []string{"testdata/testcharts/alpine"},
    49  			flags:  map[string]string{"sign": "1"},
    50  			expect: "key is required for signing a package",
    51  			err:    true,
    52  		},
    53  		{
    54  			name:   "package --sign, no --keyring",
    55  			args:   []string{"testdata/testcharts/alpine"},
    56  			flags:  map[string]string{"sign": "1", "key": "nosuchkey", "keyring": ""},
    57  			expect: "keyring is required for signing a package",
    58  			err:    true,
    59  		},
    60  		{
    61  			name:    "package testdata/testcharts/alpine",
    62  			args:    []string{"testdata/testcharts/alpine"},
    63  			expect:  "",
    64  			hasfile: "alpine-0.1.0.tgz",
    65  		},
    66  		{
    67  			name:    "package --sign --key=KEY --keyring=KEYRING testdata/testcharts/alpine",
    68  			args:    []string{"testdata/testcharts/alpine"},
    69  			flags:   map[string]string{"sign": "1", "keyring": "testdata/helm-test-key.secret", "key": "helm-test"},
    70  			expect:  "",
    71  			hasfile: "alpine-0.1.0.tgz",
    72  		},
    73  	}
    74  
    75  	// Because these tests are destructive, we run them in a tempdir.
    76  	origDir, err := os.Getwd()
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  	tmp, err := ioutil.TempDir("", "helm-package-test-")
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  
    85  	t.Logf("Running tests in %s", tmp)
    86  	if err := os.Chdir(tmp); err != nil {
    87  		t.Fatal(err)
    88  	}
    89  
    90  	defer func() {
    91  		os.Chdir(origDir)
    92  		os.RemoveAll(tmp)
    93  	}()
    94  
    95  	for _, tt := range tests {
    96  		buf := bytes.NewBuffer(nil)
    97  		c := newPackageCmd(nil, buf)
    98  
    99  		// This is an unfortunate byproduct of the tmpdir
   100  		if v, ok := tt.flags["keyring"]; ok && len(v) > 0 {
   101  			tt.flags["keyring"] = filepath.Join(origDir, v)
   102  		}
   103  
   104  		setFlags(c, tt.flags)
   105  		re := regexp.MustCompile(tt.expect)
   106  
   107  		adjustedArgs := make([]string, len(tt.args))
   108  		for i, f := range tt.args {
   109  			adjustedArgs[i] = filepath.Join(origDir, f)
   110  		}
   111  
   112  		err := c.RunE(c, adjustedArgs)
   113  		if err != nil {
   114  			if tt.err && re.MatchString(err.Error()) {
   115  				continue
   116  			}
   117  			t.Errorf("%q: expected error %q, got %q", tt.name, tt.expect, err)
   118  			continue
   119  		}
   120  
   121  		if !re.Match(buf.Bytes()) {
   122  			t.Errorf("%q: expected output %q, got %q", tt.name, tt.expect, buf.String())
   123  		}
   124  
   125  		if len(tt.hasfile) > 0 {
   126  			if fi, err := os.Stat(tt.hasfile); err != nil {
   127  				t.Errorf("%q: expected file %q, got err %q", tt.name, tt.hasfile, err)
   128  			} else if fi.Size() == 0 {
   129  				t.Errorf("%q: file %q has zero bytes.", tt.name, tt.hasfile)
   130  			}
   131  		}
   132  
   133  		if v, ok := tt.flags["sign"]; ok && v == "1" {
   134  			if fi, err := os.Stat(tt.hasfile + ".prov"); err != nil {
   135  				t.Errorf("%q: expected provenance file", tt.name)
   136  			} else if fi.Size() == 0 {
   137  				t.Errorf("%q: provenance file is empty", tt.name)
   138  			}
   139  		}
   140  	}
   141  }
   142  
   143  func setFlags(cmd *cobra.Command, flags map[string]string) {
   144  	dest := cmd.Flags()
   145  	for f, v := range flags {
   146  		dest.Set(f, v)
   147  	}
   148  }