github.com/werf/3p-helm@v2.8.1+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 cleanup := resetEnv() 36 defer func() { 37 os.RemoveAll(hh.String()) 38 cleanup() 39 }() 40 srv := repotest.NewServer(hh.String()) 41 defer srv.Stop() 42 43 settings.Home = hh 44 45 // all flags will get "--home=TMDIR -d outdir" appended. 46 tests := []struct { 47 name string 48 chart string 49 flags []string 50 fail bool 51 failExpect string 52 expectFile string 53 expectDir bool 54 expectVerify bool 55 }{ 56 { 57 name: "Basic chart fetch", 58 chart: "test/signtest", 59 expectFile: "./signtest-0.1.0.tgz", 60 }, 61 { 62 name: "Chart fetch with version", 63 chart: "test/signtest", 64 flags: []string{"--version", "0.1.0"}, 65 expectFile: "./signtest-0.1.0.tgz", 66 }, 67 { 68 name: "Fail chart fetch with non-existent version", 69 chart: "test/signtest", 70 flags: []string{"--version", "99.1.0"}, 71 fail: true, 72 failExpect: "no such chart", 73 }, 74 { 75 name: "Fail fetching non-existent chart", 76 chart: "test/nosuchthing", 77 failExpect: "Failed to fetch", 78 fail: true, 79 }, 80 { 81 name: "Fetch and verify", 82 chart: "test/signtest", 83 flags: []string{"--verify", "--keyring", "testdata/helm-test-key.pub"}, 84 expectFile: "./signtest-0.1.0.tgz", 85 expectVerify: true, 86 }, 87 { 88 name: "Fetch and fail verify", 89 chart: "test/reqtest", 90 flags: []string{"--verify", "--keyring", "testdata/helm-test-key.pub"}, 91 failExpect: "Failed to fetch provenance", 92 fail: true, 93 }, 94 { 95 name: "Fetch and untar", 96 chart: "test/signtest", 97 flags: []string{"--untar", "--untardir", "signtest"}, 98 expectFile: "./signtest", 99 expectDir: true, 100 }, 101 { 102 name: "Fetch, verify, untar", 103 chart: "test/signtest", 104 flags: []string{"--verify", "--keyring", "testdata/helm-test-key.pub", "--untar", "--untardir", "signtest"}, 105 expectFile: "./signtest", 106 expectDir: true, 107 expectVerify: true, 108 }, 109 { 110 name: "Chart fetch using repo URL", 111 chart: "signtest", 112 expectFile: "./signtest-0.1.0.tgz", 113 flags: []string{"--repo", srv.URL()}, 114 }, 115 { 116 name: "Fail fetching non-existent chart on repo URL", 117 chart: "someChart", 118 flags: []string{"--repo", srv.URL()}, 119 failExpect: "Failed to fetch chart", 120 fail: true, 121 }, 122 { 123 name: "Specific version chart fetch using repo URL", 124 chart: "signtest", 125 expectFile: "./signtest-0.1.0.tgz", 126 flags: []string{"--repo", srv.URL(), "--version", "0.1.0"}, 127 }, 128 { 129 name: "Specific version chart fetch using repo URL", 130 chart: "signtest", 131 flags: []string{"--repo", srv.URL(), "--version", "0.2.0"}, 132 failExpect: "Failed to fetch chart version", 133 fail: true, 134 }, 135 } 136 137 if _, err := srv.CopyCharts("testdata/testcharts/*.tgz*"); err != nil { 138 t.Fatal(err) 139 } 140 if err := srv.LinkIndices(); err != nil { 141 t.Fatal(err) 142 } 143 144 for _, tt := range tests { 145 outdir := filepath.Join(hh.String(), "testout") 146 os.RemoveAll(outdir) 147 os.Mkdir(outdir, 0755) 148 149 buf := bytes.NewBuffer(nil) 150 cmd := newFetchCmd(buf) 151 tt.flags = append(tt.flags, "-d", outdir) 152 cmd.ParseFlags(tt.flags) 153 if err := cmd.RunE(cmd, []string{tt.chart}); err != nil { 154 if tt.fail { 155 continue 156 } 157 t.Errorf("%q reported error: %s", tt.name, err) 158 continue 159 } 160 if tt.expectVerify { 161 pointerAddressPattern := "0[xX][A-Fa-f0-9]+" 162 sha256Pattern := "[A-Fa-f0-9]{64}" 163 verificationRegex := regexp.MustCompile( 164 fmt.Sprintf("Verification: &{%s sha256:%s signtest-0.1.0.tgz}\n", pointerAddressPattern, sha256Pattern)) 165 if !verificationRegex.MatchString(buf.String()) { 166 t.Errorf("%q: expected match for regex %s, got %s", tt.name, verificationRegex, buf.String()) 167 } 168 } 169 170 ef := filepath.Join(outdir, tt.expectFile) 171 fi, err := os.Stat(ef) 172 if err != nil { 173 t.Errorf("%q: expected a file at %s. %s", tt.name, ef, err) 174 } 175 if fi.IsDir() != tt.expectDir { 176 t.Errorf("%q: expected directory=%t, but it's not.", tt.name, tt.expectDir) 177 } 178 } 179 }