github.com/felipejfc/helm@v2.1.2+incompatible/cmd/helm/downloader/chart_downloader_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 downloader
    17  
    18  import (
    19  	"fmt"
    20  	"io/ioutil"
    21  	"net/http"
    22  	"net/http/httptest"
    23  	"os"
    24  	"path/filepath"
    25  	"testing"
    26  
    27  	"k8s.io/helm/cmd/helm/helmpath"
    28  	"k8s.io/helm/pkg/repo/repotest"
    29  )
    30  
    31  func TestResolveChartRef(t *testing.T) {
    32  	tests := []struct {
    33  		name, ref, expect, version string
    34  		fail                       bool
    35  	}{
    36  		{name: "full URL", ref: "http://example.com/foo-1.2.3.tgz", expect: "http://example.com/foo-1.2.3.tgz"},
    37  		{name: "full URL, HTTPS", ref: "https://example.com/foo-1.2.3.tgz", expect: "https://example.com/foo-1.2.3.tgz"},
    38  		{name: "full URL, HTTPS, irrelevant version", ref: "https://example.com/foo-1.2.3.tgz", version: "0.1.0", expect: "https://example.com/foo-1.2.3.tgz"},
    39  		{name: "reference, testing repo", ref: "testing/alpine", expect: "http://example.com/alpine-1.2.3.tgz"},
    40  		{name: "reference, version, testing repo", ref: "testing/alpine", version: "0.2.0", expect: "http://example.com/alpine-0.2.0.tgz"},
    41  		{name: "full URL, file", ref: "file:///foo-1.2.3.tgz", fail: true},
    42  		{name: "invalid", ref: "invalid-1.2.3", fail: true},
    43  		{name: "not found", ref: "nosuchthing/invalid-1.2.3", fail: true},
    44  	}
    45  
    46  	c := ChartDownloader{
    47  		HelmHome: helmpath.Home("testdata/helmhome"),
    48  		Out:      os.Stderr,
    49  	}
    50  
    51  	for _, tt := range tests {
    52  		u, err := c.ResolveChartVersion(tt.ref, tt.version)
    53  		if err != nil {
    54  			if tt.fail {
    55  				continue
    56  			}
    57  			t.Errorf("%s: failed with error %s", tt.name, err)
    58  			continue
    59  		}
    60  		if got := u.String(); got != tt.expect {
    61  			t.Errorf("%s: expected %s, got %s", tt.name, tt.expect, got)
    62  		}
    63  	}
    64  }
    65  
    66  func TestVerifyChart(t *testing.T) {
    67  	v, err := VerifyChart("testdata/signtest-0.1.0.tgz", "testdata/helm-test-key.pub")
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	// The verification is tested at length in the provenance package. Here,
    72  	// we just want a quick sanity check that the v is not empty.
    73  	if len(v.FileHash) == 0 {
    74  		t.Error("Digest missing")
    75  	}
    76  }
    77  
    78  func TestDownload(t *testing.T) {
    79  	expect := "Call me Ishmael"
    80  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    81  		fmt.Fprint(w, expect)
    82  	}))
    83  	defer srv.Close()
    84  
    85  	got, err := download(srv.URL)
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  
    90  	if got.String() != expect {
    91  		t.Errorf("Expected %q, got %q", expect, got.String())
    92  	}
    93  }
    94  
    95  func TestIsTar(t *testing.T) {
    96  	tests := map[string]bool{
    97  		"foo.tgz":           true,
    98  		"foo/bar/baz.tgz":   true,
    99  		"foo-1.2.3.4.5.tgz": true,
   100  		"foo.tar.gz":        false, // for our purposes
   101  		"foo.tgz.1":         false,
   102  		"footgz":            false,
   103  	}
   104  
   105  	for src, expect := range tests {
   106  		if isTar(src) != expect {
   107  			t.Errorf("%q should be %t", src, expect)
   108  		}
   109  	}
   110  }
   111  
   112  func TestDownloadTo(t *testing.T) {
   113  	hh, err := ioutil.TempDir("", "helm-downloadto-")
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  	defer os.RemoveAll(hh)
   118  
   119  	dest := filepath.Join(hh, "dest")
   120  	os.MkdirAll(dest, 0755)
   121  
   122  	// Set up a fake repo
   123  	srv := repotest.NewServer(hh)
   124  	defer srv.Stop()
   125  	if _, err := srv.CopyCharts("testdata/*.tgz*"); err != nil {
   126  		t.Error(err)
   127  		return
   128  	}
   129  
   130  	c := ChartDownloader{
   131  		HelmHome: helmpath.Home("testdata/helmhome"),
   132  		Out:      os.Stderr,
   133  		Verify:   VerifyAlways,
   134  		Keyring:  "testdata/helm-test-key.pub",
   135  	}
   136  	cname := "/signtest-0.1.0.tgz"
   137  	where, v, err := c.DownloadTo(srv.URL()+cname, "", dest)
   138  	if err != nil {
   139  		t.Error(err)
   140  		return
   141  	}
   142  
   143  	if expect := filepath.Join(dest, cname); where != expect {
   144  		t.Errorf("Expected download to %s, got %s", expect, where)
   145  	}
   146  
   147  	if v.FileHash == "" {
   148  		t.Error("File hash was empty, but verification is required.")
   149  	}
   150  
   151  	if _, err := os.Stat(filepath.Join(dest, cname)); err != nil {
   152  		t.Error(err)
   153  		return
   154  	}
   155  }
   156  
   157  func TestDownloadTo_VerifyLater(t *testing.T) {
   158  	hh, err := ioutil.TempDir("", "helm-downloadto-")
   159  	if err != nil {
   160  		t.Fatal(err)
   161  	}
   162  	defer os.RemoveAll(hh)
   163  
   164  	dest := filepath.Join(hh, "dest")
   165  	os.MkdirAll(dest, 0755)
   166  
   167  	// Set up a fake repo
   168  	srv := repotest.NewServer(hh)
   169  	defer srv.Stop()
   170  	if _, err := srv.CopyCharts("testdata/*.tgz*"); err != nil {
   171  		t.Error(err)
   172  		return
   173  	}
   174  
   175  	c := ChartDownloader{
   176  		HelmHome: helmpath.Home("testdata/helmhome"),
   177  		Out:      os.Stderr,
   178  		Verify:   VerifyLater,
   179  	}
   180  	cname := "/signtest-0.1.0.tgz"
   181  	where, _, err := c.DownloadTo(srv.URL()+cname, "", dest)
   182  	if err != nil {
   183  		t.Error(err)
   184  		return
   185  	}
   186  
   187  	if expect := filepath.Join(dest, cname); where != expect {
   188  		t.Errorf("Expected download to %s, got %s", expect, where)
   189  	}
   190  
   191  	if _, err := os.Stat(filepath.Join(dest, cname)); err != nil {
   192  		t.Error(err)
   193  		return
   194  	}
   195  	if _, err := os.Stat(filepath.Join(dest, cname+".prov")); err != nil {
   196  		t.Error(err)
   197  		return
   198  	}
   199  }