github.com/strongmonkey/helm@v2.7.2+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  	cleanup := resetEnv()
   147  	defer func() {
   148  		os.Chdir(origDir)
   149  		os.RemoveAll(tmp)
   150  		cleanup()
   151  	}()
   152  
   153  	settings.Home = helmpath.Home(tmp)
   154  
   155  	for _, tt := range tests {
   156  		buf := bytes.NewBuffer(nil)
   157  		c := newPackageCmd(buf)
   158  
   159  		// This is an unfortunate byproduct of the tmpdir
   160  		if v, ok := tt.flags["keyring"]; ok && len(v) > 0 {
   161  			tt.flags["keyring"] = filepath.Join(origDir, v)
   162  		}
   163  
   164  		setFlags(c, tt.flags)
   165  		re := regexp.MustCompile(tt.expect)
   166  
   167  		adjustedArgs := make([]string, len(tt.args))
   168  		for i, f := range tt.args {
   169  			adjustedArgs[i] = filepath.Join(origDir, f)
   170  		}
   171  
   172  		err := c.RunE(c, adjustedArgs)
   173  		if err != nil {
   174  			if tt.err && re.MatchString(err.Error()) {
   175  				continue
   176  			}
   177  			t.Errorf("%q: expected error %q, got %q", tt.name, tt.expect, err)
   178  			continue
   179  		}
   180  
   181  		if !re.Match(buf.Bytes()) {
   182  			t.Errorf("%q: expected output %q, got %q", tt.name, tt.expect, buf.String())
   183  		}
   184  
   185  		if len(tt.hasfile) > 0 {
   186  			if fi, err := os.Stat(tt.hasfile); err != nil {
   187  				t.Errorf("%q: expected file %q, got err %q", tt.name, tt.hasfile, err)
   188  			} else if fi.Size() == 0 {
   189  				t.Errorf("%q: file %q has zero bytes.", tt.name, tt.hasfile)
   190  			}
   191  		}
   192  
   193  		if v, ok := tt.flags["sign"]; ok && v == "1" {
   194  			if fi, err := os.Stat(tt.hasfile + ".prov"); err != nil {
   195  				t.Errorf("%q: expected provenance file", tt.name)
   196  			} else if fi.Size() == 0 {
   197  				t.Errorf("%q: provenance file is empty", tt.name)
   198  			}
   199  		}
   200  	}
   201  }
   202  
   203  func setFlags(cmd *cobra.Command, flags map[string]string) {
   204  	dest := cmd.Flags()
   205  	for f, v := range flags {
   206  		dest.Set(f, v)
   207  	}
   208  }