github.com/canthefason/helm@v2.2.1-0.20170221172616-16b043b8d505+incompatible/cmd/helm/fetch_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "bytes" 21 "fmt" 22 "os" 23 "path/filepath" 24 "regexp" 25 "testing" 26 27 "k8s.io/helm/pkg/repo/repotest" 28 ) 29 30 func TestFetchCmd(t *testing.T) { 31 hh, err := tempHelmHome(t) 32 if err != nil { 33 t.Fatal(err) 34 } 35 old := homePath() 36 helmHome = hh 37 defer func() { 38 helmHome = old 39 os.RemoveAll(hh) 40 }() 41 42 // all flags will get "--home=TMDIR -d outdir" appended. 43 tests := []struct { 44 name string 45 chart string 46 flags []string 47 fail bool 48 failExpect string 49 expectFile string 50 expectDir bool 51 expectVerify bool 52 }{ 53 { 54 name: "Basic chart fetch", 55 chart: "test/signtest", 56 expectFile: "./signtest-0.1.0.tgz", 57 }, 58 { 59 name: "Chart fetch with version", 60 chart: "test/signtest", 61 flags: []string{"--version", "0.1.0"}, 62 expectFile: "./signtest-0.1.0.tgz", 63 }, 64 { 65 name: "Fail chart fetch with non-existent version", 66 chart: "test/signtest", 67 flags: []string{"--version", "99.1.0"}, 68 fail: true, 69 failExpect: "no such chart", 70 }, 71 { 72 name: "Fail fetching non-existent chart", 73 chart: "test/nosuchthing", 74 failExpect: "Failed to fetch", 75 fail: true, 76 }, 77 { 78 name: "Fetch and verify", 79 chart: "test/signtest", 80 flags: []string{"--verify", "--keyring", "testdata/helm-test-key.pub"}, 81 expectFile: "./signtest-0.1.0.tgz", 82 expectVerify: true, 83 }, 84 { 85 name: "Fetch and fail verify", 86 chart: "test/reqtest", 87 flags: []string{"--verify", "--keyring", "testdata/helm-test-key.pub"}, 88 failExpect: "Failed to fetch provenance", 89 fail: true, 90 }, 91 { 92 name: "Fetch and untar", 93 chart: "test/signtest", 94 flags: []string{"--untar", "--untardir", "signtest"}, 95 expectFile: "./signtest", 96 expectDir: true, 97 }, 98 { 99 name: "Fetch, verify, untar", 100 chart: "test/signtest", 101 flags: []string{"--verify", "--keyring", "testdata/helm-test-key.pub", "--untar", "--untardir", "signtest"}, 102 expectFile: "./signtest", 103 expectDir: true, 104 expectVerify: true, 105 }, 106 } 107 108 srv := repotest.NewServer(hh) 109 defer srv.Stop() 110 111 if _, err := srv.CopyCharts("testdata/testcharts/*.tgz*"); err != nil { 112 t.Fatal(err) 113 } 114 if err := srv.LinkIndices(); err != nil { 115 t.Fatal(err) 116 } 117 118 for _, tt := range tests { 119 outdir := filepath.Join(hh, "testout") 120 os.RemoveAll(outdir) 121 os.Mkdir(outdir, 0755) 122 123 buf := bytes.NewBuffer(nil) 124 cmd := newFetchCmd(buf) 125 tt.flags = append(tt.flags, "-d", outdir) 126 cmd.ParseFlags(tt.flags) 127 if err := cmd.RunE(cmd, []string{tt.chart}); err != nil { 128 if tt.fail { 129 continue 130 } 131 t.Errorf("%q reported error: %s", tt.name, err) 132 continue 133 } 134 if tt.expectVerify { 135 pointerAddressPattern := "0[xX][A-Fa-f0-9]+" 136 sha256Pattern := "[A-Fa-f0-9]{64}" 137 verificationRegex := regexp.MustCompile( 138 fmt.Sprintf("Verification: &{%s sha256:%s signtest-0.1.0.tgz}\n", pointerAddressPattern, sha256Pattern)) 139 if !verificationRegex.MatchString(buf.String()) { 140 t.Errorf("%q: expected match for regex %s, got %s", tt.name, verificationRegex, buf.String()) 141 } 142 } 143 144 ef := filepath.Join(outdir, tt.expectFile) 145 fi, err := os.Stat(ef) 146 if err != nil { 147 t.Errorf("%q: expected a file at %s. %s", tt.name, ef, err) 148 } 149 if fi.IsDir() != tt.expectDir { 150 t.Errorf("%q: expected directory=%t, but it's not.", tt.name, tt.expectDir) 151 } 152 } 153 }