github.com/canthefason/helm@v2.2.1-0.20170221172616-16b043b8d505+incompatible/cmd/helm/dependency_update_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 main 17 18 import ( 19 "bytes" 20 "io/ioutil" 21 "os" 22 "path/filepath" 23 "strings" 24 "testing" 25 26 "github.com/ghodss/yaml" 27 28 "k8s.io/helm/cmd/helm/helmpath" 29 "k8s.io/helm/pkg/chartutil" 30 "k8s.io/helm/pkg/proto/hapi/chart" 31 "k8s.io/helm/pkg/provenance" 32 "k8s.io/helm/pkg/repo" 33 "k8s.io/helm/pkg/repo/repotest" 34 ) 35 36 func TestDependencyUpdateCmd(t *testing.T) { 37 // Set up a testing helm home 38 oldhome := helmHome 39 hh, err := tempHelmHome(t) 40 if err != nil { 41 t.Fatal(err) 42 } 43 helmHome = hh 44 defer func() { 45 os.RemoveAll(hh) 46 helmHome = oldhome 47 }() 48 49 srv := repotest.NewServer(hh) 50 defer srv.Stop() 51 copied, err := srv.CopyCharts("testdata/testcharts/*.tgz") 52 if err != nil { 53 t.Fatal(err) 54 } 55 t.Logf("Copied charts:\n%s", strings.Join(copied, "\n")) 56 t.Logf("Listening on directory %s", srv.Root()) 57 58 chartname := "depup" 59 if err := createTestingChart(hh, chartname, srv.URL()); err != nil { 60 t.Fatal(err) 61 } 62 63 out := bytes.NewBuffer(nil) 64 duc := &dependencyUpdateCmd{out: out} 65 duc.helmhome = helmpath.Home(hh) 66 duc.chartpath = filepath.Join(hh, chartname) 67 68 if err := duc.run(); err != nil { 69 output := out.String() 70 t.Logf("Output: %s", output) 71 t.Fatal(err) 72 } 73 74 output := out.String() 75 // This is written directly to stdout, so we have to capture as is. 76 if !strings.Contains(output, `update from the "test" chart repository`) { 77 t.Errorf("Repo did not get updated\n%s", output) 78 } 79 80 // Make sure the actual file got downloaded. 81 expect := filepath.Join(hh, chartname, "charts/reqtest-0.1.0.tgz") 82 if _, err := os.Stat(expect); err != nil { 83 t.Fatal(err) 84 } 85 86 hash, err := provenance.DigestFile(expect) 87 if err != nil { 88 t.Fatal(err) 89 } 90 91 i, err := repo.LoadIndexFile(duc.helmhome.CacheIndex("test")) 92 if err != nil { 93 t.Fatal(err) 94 } 95 96 reqver := i.Entries["reqtest"][0] 97 if h := reqver.Digest; h != hash { 98 t.Errorf("Failed hash match: expected %s, got %s", hash, h) 99 } 100 101 // Now change the dependencies and update. This verifies that on update, 102 // old dependencies are cleansed and new dependencies are added. 103 reqfile := &chartutil.Requirements{ 104 Dependencies: []*chartutil.Dependency{ 105 {Name: "reqtest", Version: "0.1.0", Repository: srv.URL()}, 106 {Name: "compressedchart", Version: "0.3.0", Repository: srv.URL()}, 107 }, 108 } 109 dir := filepath.Join(hh, chartname) 110 if err := writeRequirements(dir, reqfile); err != nil { 111 t.Fatal(err) 112 } 113 if err := duc.run(); err != nil { 114 output := out.String() 115 t.Logf("Output: %s", output) 116 t.Fatal(err) 117 } 118 119 // In this second run, we should see compressedchart-0.3.0.tgz, and not 120 // the 0.1.0 version. 121 expect = filepath.Join(hh, chartname, "charts/compressedchart-0.3.0.tgz") 122 if _, err := os.Stat(expect); err != nil { 123 t.Fatalf("Expected %q: %s", expect, err) 124 } 125 dontExpect := filepath.Join(hh, chartname, "charts/compressedchart-0.1.0.tgz") 126 if _, err := os.Stat(dontExpect); err == nil { 127 t.Fatalf("Unexpected %q", dontExpect) 128 } 129 } 130 131 // createTestingChart creates a basic chart that depends on reqtest-0.1.0 132 // 133 // The baseURL can be used to point to a particular repository server. 134 func createTestingChart(dest, name, baseURL string) error { 135 cfile := &chart.Metadata{ 136 Name: name, 137 Version: "1.2.3", 138 } 139 dir := filepath.Join(dest, name) 140 _, err := chartutil.Create(cfile, dest) 141 if err != nil { 142 return err 143 } 144 req := &chartutil.Requirements{ 145 Dependencies: []*chartutil.Dependency{ 146 {Name: "reqtest", Version: "0.1.0", Repository: baseURL}, 147 {Name: "compressedchart", Version: "0.1.0", Repository: baseURL}, 148 }, 149 } 150 return writeRequirements(dir, req) 151 } 152 153 func writeRequirements(dir string, req *chartutil.Requirements) error { 154 data, err := yaml.Marshal(req) 155 if err != nil { 156 return err 157 } 158 159 return ioutil.WriteFile(filepath.Join(dir, "requirements.yaml"), data, 0655) 160 }