github.com/TarasLykhenko/helm@v3.0.0-beta.3+incompatible/cmd/helm/pull_test.go (about) 1 /* 2 Copyright The Helm Authors. 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 "fmt" 21 "os" 22 "path/filepath" 23 "regexp" 24 "testing" 25 26 "helm.sh/helm/pkg/repo/repotest" 27 ) 28 29 func TestPullCmd(t *testing.T) { 30 srv, err := repotest.NewTempServer("testdata/testcharts/*.tgz*") 31 if err != nil { 32 t.Fatal(err) 33 } 34 defer srv.Stop() 35 36 if err := srv.LinkIndices(); err != nil { 37 t.Fatal(err) 38 } 39 40 // all flags will get "-d outdir" appended. 41 tests := []struct { 42 name string 43 args string 44 wantError bool 45 failExpect string 46 expectFile string 47 expectDir bool 48 expectVerify bool 49 }{ 50 { 51 name: "Basic chart fetch", 52 args: "test/signtest", 53 expectFile: "./signtest-0.1.0.tgz", 54 }, 55 { 56 name: "Chart fetch with version", 57 args: "test/signtest --version=0.1.0", 58 expectFile: "./signtest-0.1.0.tgz", 59 }, 60 { 61 name: "Fail chart fetch with non-existent version", 62 args: "test/signtest --version=99.1.0", 63 wantError: true, 64 failExpect: "no such chart", 65 }, 66 { 67 name: "Fail fetching non-existent chart", 68 args: "test/nosuchthing", 69 failExpect: "Failed to fetch", 70 wantError: true, 71 }, 72 { 73 name: "Fetch and verify", 74 args: "test/signtest --verify --keyring testdata/helm-test-key.pub", 75 expectFile: "./signtest-0.1.0.tgz", 76 expectVerify: true, 77 }, 78 { 79 name: "Fetch and fail verify", 80 args: "test/reqtest --verify --keyring testdata/helm-test-key.pub", 81 failExpect: "Failed to fetch provenance", 82 wantError: true, 83 }, 84 { 85 name: "Fetch and untar", 86 args: "test/signtest --untar --untardir signtest", 87 expectFile: "./signtest", 88 expectDir: true, 89 }, 90 { 91 name: "Fetch, verify, untar", 92 args: "test/signtest --verify --keyring=testdata/helm-test-key.pub --untar --untardir signtest", 93 expectFile: "./signtest", 94 expectDir: true, 95 expectVerify: true, 96 }, 97 { 98 name: "Chart fetch using repo URL", 99 expectFile: "./signtest-0.1.0.tgz", 100 args: "signtest --repo " + srv.URL(), 101 }, 102 { 103 name: "Fail fetching non-existent chart on repo URL", 104 args: "someChart --repo " + srv.URL(), 105 failExpect: "Failed to fetch chart", 106 wantError: true, 107 }, 108 { 109 name: "Specific version chart fetch using repo URL", 110 expectFile: "./signtest-0.1.0.tgz", 111 args: "signtest --version=0.1.0 --repo " + srv.URL(), 112 }, 113 { 114 name: "Specific version chart fetch using repo URL", 115 args: "signtest --version=0.2.0 --repo " + srv.URL(), 116 failExpect: "Failed to fetch chart version", 117 wantError: true, 118 }, 119 } 120 121 for _, tt := range tests { 122 t.Run(tt.name, func(t *testing.T) { 123 outdir := srv.Root() 124 cmd := fmt.Sprintf("fetch %s -d '%s' --repository-config %s --repository-cache %s ", 125 tt.args, 126 outdir, 127 filepath.Join(outdir, "repositories.yaml"), 128 outdir, 129 ) 130 _, out, err := executeActionCommand(cmd) 131 if err != nil { 132 if tt.wantError { 133 return 134 } 135 t.Fatalf("%q reported error: %s", tt.name, err) 136 } 137 138 if tt.expectVerify { 139 pointerAddressPattern := "0[xX][A-Fa-f0-9]+" 140 sha256Pattern := "[A-Fa-f0-9]{64}" 141 verificationRegex := regexp.MustCompile( 142 fmt.Sprintf("Verification: &{%s sha256:%s signtest-0.1.0.tgz}\n", pointerAddressPattern, sha256Pattern)) 143 if !verificationRegex.MatchString(out) { 144 t.Errorf("%q: expected match for regex %s, got %s", tt.name, verificationRegex, out) 145 } 146 } 147 148 ef := filepath.Join(outdir, tt.expectFile) 149 fi, err := os.Stat(ef) 150 if err != nil { 151 t.Errorf("%q: expected a file at %s. %s", tt.name, ef, err) 152 } 153 if fi.IsDir() != tt.expectDir { 154 t.Errorf("%q: expected directory=%t, but it's not.", tt.name, tt.expectDir) 155 } 156 }) 157 } 158 }