github.com/lyanchih/helm@v2.2.3+incompatible/cmd/helm/dependency_build_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 "os" 21 "path/filepath" 22 "strings" 23 "testing" 24 25 "k8s.io/helm/cmd/helm/helmpath" 26 "k8s.io/helm/pkg/provenance" 27 "k8s.io/helm/pkg/repo" 28 "k8s.io/helm/pkg/repo/repotest" 29 ) 30 31 func TestDependencyBuildCmd(t *testing.T) { 32 oldhome := helmHome 33 hh, err := tempHelmHome(t) 34 if err != nil { 35 t.Fatal(err) 36 } 37 helmHome = hh 38 defer func() { 39 os.RemoveAll(hh) 40 helmHome = oldhome 41 }() 42 43 srv := repotest.NewServer(hh) 44 defer srv.Stop() 45 _, err = srv.CopyCharts("testdata/testcharts/*.tgz") 46 if err != nil { 47 t.Fatal(err) 48 } 49 50 chartname := "depbuild" 51 if err := createTestingChart(hh, chartname, srv.URL()); err != nil { 52 t.Fatal(err) 53 } 54 55 out := bytes.NewBuffer(nil) 56 dbc := &dependencyBuildCmd{out: out} 57 dbc.helmhome = helmpath.Home(hh) 58 dbc.chartpath = filepath.Join(hh, chartname) 59 60 // In the first pass, we basically want the same results as an update. 61 if err := dbc.run(); err != nil { 62 output := out.String() 63 t.Logf("Output: %s", output) 64 t.Fatal(err) 65 } 66 67 output := out.String() 68 if !strings.Contains(output, `update from the "test" chart repository`) { 69 t.Errorf("Repo did not get updated\n%s", output) 70 } 71 72 // Make sure the actual file got downloaded. 73 expect := filepath.Join(hh, chartname, "charts/reqtest-0.1.0.tgz") 74 if _, err := os.Stat(expect); err != nil { 75 t.Fatal(err) 76 } 77 78 // In the second pass, we want to remove the chart's request dependency, 79 // then see if it restores from the lock. 80 lockfile := filepath.Join(hh, chartname, "requirements.lock") 81 if _, err := os.Stat(lockfile); err != nil { 82 t.Fatal(err) 83 } 84 if err := os.RemoveAll(expect); err != nil { 85 t.Fatal(err) 86 } 87 88 if err := dbc.run(); err != nil { 89 output := out.String() 90 t.Logf("Output: %s", output) 91 t.Fatal(err) 92 } 93 94 // Now repeat the test that the dependency exists. 95 expect = filepath.Join(hh, chartname, "charts/reqtest-0.1.0.tgz") 96 if _, err := os.Stat(expect); err != nil { 97 t.Fatal(err) 98 } 99 100 // Make sure that build is also fetching the correct version. 101 hash, err := provenance.DigestFile(expect) 102 if err != nil { 103 t.Fatal(err) 104 } 105 106 i, err := repo.LoadIndexFile(dbc.helmhome.CacheIndex("test")) 107 if err != nil { 108 t.Fatal(err) 109 } 110 111 reqver := i.Entries["reqtest"][0] 112 if h := reqver.Digest; h != hash { 113 t.Errorf("Failed hash match: expected %s, got %s", hash, h) 114 } 115 if v := reqver.Version; v != "0.1.0" { 116 t.Errorf("mismatched versions. Expected %q, got %q", "0.1.0", v) 117 } 118 119 }