github.com/codefresh-io/kcfi@v0.0.0-20230301195427-c1578715cc46/cmd/kcfi/dependency_update_test.go (about) 1 /* 2 Copyright The Helm Authors. 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 "fmt" 20 "io/ioutil" 21 "os" 22 "path/filepath" 23 "strings" 24 "testing" 25 26 "github.com/codefresh-io/kcfi/pkg/helm-internal/test/ensure" 27 "helm.sh/helm/v3/pkg/chart" 28 "helm.sh/helm/v3/pkg/chartutil" 29 "helm.sh/helm/v3/pkg/helmpath" 30 "helm.sh/helm/v3/pkg/provenance" 31 "helm.sh/helm/v3/pkg/repo" 32 "helm.sh/helm/v3/pkg/repo/repotest" 33 ) 34 35 func TestDependencyUpdateCmd(t *testing.T) { 36 srv, err := repotest.NewTempServer("testdata/testcharts/*.tgz") 37 if err != nil { 38 t.Fatal(err) 39 } 40 defer srv.Stop() 41 t.Logf("Listening on directory %s", srv.Root()) 42 43 if err := srv.LinkIndices(); err != nil { 44 t.Fatal(err) 45 } 46 47 dir := func(p ...string) string { 48 return filepath.Join(append([]string{srv.Root()}, p...)...) 49 } 50 51 chartname := "depup" 52 ch := createTestingMetadata(chartname, srv.URL()) 53 md := ch.Metadata 54 if err := chartutil.SaveDir(ch, dir()); err != nil { 55 t.Fatal(err) 56 } 57 58 _, out, err := executeActionCommand( 59 fmt.Sprintf("dependency update '%s' --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()), 60 ) 61 if err != nil { 62 t.Logf("Output: %s", out) 63 t.Fatal(err) 64 } 65 66 // This is written directly to stdout, so we have to capture as is. 67 if !strings.Contains(out, `update from the "test" chart repository`) { 68 t.Errorf("Repo did not get updated\n%s", out) 69 } 70 71 // Make sure the actual file got downloaded. 72 expect := dir(chartname, "charts/reqtest-0.1.0.tgz") 73 if _, err := os.Stat(expect); err != nil { 74 t.Fatal(err) 75 } 76 77 hash, err := provenance.DigestFile(expect) 78 if err != nil { 79 t.Fatal(err) 80 } 81 82 i, err := repo.LoadIndexFile(dir(helmpath.CacheIndexFile("test"))) 83 if err != nil { 84 t.Fatal(err) 85 } 86 87 reqver := i.Entries["reqtest"][0] 88 if h := reqver.Digest; h != hash { 89 t.Errorf("Failed hash match: expected %s, got %s", hash, h) 90 } 91 92 // Now change the dependencies and update. This verifies that on update, 93 // old dependencies are cleansed and new dependencies are added. 94 md.Dependencies = []*chart.Dependency{ 95 {Name: "reqtest", Version: "0.1.0", Repository: srv.URL()}, 96 {Name: "compressedchart", Version: "0.3.0", Repository: srv.URL()}, 97 } 98 if err := chartutil.SaveChartfile(dir(chartname, "Chart.yaml"), md); err != nil { 99 t.Fatal(err) 100 } 101 102 _, out, err = executeActionCommand(fmt.Sprintf("dependency update '%s' --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir())) 103 if err != nil { 104 t.Logf("Output: %s", out) 105 t.Fatal(err) 106 } 107 108 // In this second run, we should see compressedchart-0.3.0.tgz, and not 109 // the 0.1.0 version. 110 expect = dir(chartname, "charts/compressedchart-0.3.0.tgz") 111 if _, err := os.Stat(expect); err != nil { 112 t.Fatalf("Expected %q: %s", expect, err) 113 } 114 unexpected := dir(chartname, "charts/compressedchart-0.1.0.tgz") 115 if _, err := os.Stat(unexpected); err == nil { 116 t.Fatalf("Unexpected %q", unexpected) 117 } 118 } 119 120 func TestDependencyUpdateCmd_DontDeleteOldChartsOnError(t *testing.T) { 121 defer resetEnv()() 122 defer ensure.HelmHome(t)() 123 124 srv, err := repotest.NewTempServer("testdata/testcharts/*.tgz") 125 if err != nil { 126 t.Fatal(err) 127 } 128 defer srv.Stop() 129 t.Logf("Listening on directory %s", srv.Root()) 130 131 if err := srv.LinkIndices(); err != nil { 132 t.Fatal(err) 133 } 134 135 chartname := "depupdelete" 136 137 dir := func(p ...string) string { 138 return filepath.Join(append([]string{srv.Root()}, p...)...) 139 } 140 createTestingChart(t, dir(), chartname, srv.URL()) 141 142 _, output, err := executeActionCommand(fmt.Sprintf("dependency update %s --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir())) 143 if err != nil { 144 t.Logf("Output: %s", output) 145 t.Fatal(err) 146 } 147 148 // Chart repo is down 149 srv.Stop() 150 151 _, output, err = executeActionCommand(fmt.Sprintf("dependency update %s --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir())) 152 if err == nil { 153 t.Logf("Output: %s", output) 154 t.Fatal("Expected error, got nil") 155 } 156 157 // Make sure charts dir still has dependencies 158 files, err := ioutil.ReadDir(filepath.Join(dir(chartname), "charts")) 159 if err != nil { 160 t.Fatal(err) 161 } 162 dependencies := []string{"compressedchart-0.1.0.tgz", "reqtest-0.1.0.tgz"} 163 164 if len(dependencies) != len(files) { 165 t.Fatalf("Expected %d chart dependencies, got %d", len(dependencies), len(files)) 166 } 167 for index, file := range files { 168 if dependencies[index] != file.Name() { 169 t.Fatalf("Chart dependency %s not matching %s", dependencies[index], file.Name()) 170 } 171 } 172 173 // Make sure tmpcharts is deleted 174 if _, err := os.Stat(filepath.Join(dir(chartname), "tmpcharts")); !os.IsNotExist(err) { 175 t.Fatalf("tmpcharts dir still exists") 176 } 177 } 178 179 // createTestingMetadata creates a basic chart that depends on reqtest-0.1.0 180 // 181 // The baseURL can be used to point to a particular repository server. 182 func createTestingMetadata(name, baseURL string) *chart.Chart { 183 return &chart.Chart{ 184 Metadata: &chart.Metadata{ 185 APIVersion: chart.APIVersionV2, 186 Name: name, 187 Version: "1.2.3", 188 Dependencies: []*chart.Dependency{ 189 {Name: "reqtest", Version: "0.1.0", Repository: baseURL}, 190 {Name: "compressedchart", Version: "0.1.0", Repository: baseURL}, 191 }, 192 }, 193 } 194 } 195 196 // createTestingChart creates a basic chart that depends on reqtest-0.1.0 197 // 198 // The baseURL can be used to point to a particular repository server. 199 func createTestingChart(t *testing.T, dest, name, baseURL string) { 200 t.Helper() 201 cfile := createTestingMetadata(name, baseURL) 202 if err := chartutil.SaveDir(cfile, dest); err != nil { 203 t.Fatal(err) 204 } 205 }