github.com/sgoings/helm@v2.0.0-alpha.2.0.20170406211108-734e92851ac3+incompatible/cmd/helm/repo_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 "fmt" 21 "io" 22 "os" 23 "strings" 24 "testing" 25 26 "k8s.io/helm/pkg/helm/helmpath" 27 "k8s.io/helm/pkg/repo" 28 "k8s.io/helm/pkg/repo/repotest" 29 ) 30 31 func TestUpdateCmd(t *testing.T) { 32 thome, err := tempHelmHome(t) 33 if err != nil { 34 t.Fatal(err) 35 } 36 oldhome := homePath() 37 helmHome = thome 38 defer func() { 39 helmHome = oldhome 40 os.Remove(thome) 41 }() 42 43 out := bytes.NewBuffer(nil) 44 // Instead of using the HTTP updater, we provide our own for this test. 45 // The TestUpdateCharts test verifies the HTTP behavior independently. 46 updater := func(repos []*repo.ChartRepository, out io.Writer, hh helmpath.Home) { 47 for _, re := range repos { 48 fmt.Fprintln(out, re.Config.Name) 49 } 50 } 51 uc := &repoUpdateCmd{ 52 update: updater, 53 home: helmpath.Home(thome), 54 out: out, 55 } 56 if err := uc.run(); err != nil { 57 t.Fatal(err) 58 } 59 60 if got := out.String(); !strings.Contains(got, "charts") || !strings.Contains(got, "local") { 61 t.Errorf("Expected 'charts' and 'local' (in any order) got %q", got) 62 } 63 } 64 65 func TestUpdateCharts(t *testing.T) { 66 ts, thome, err := repotest.NewTempServer("testdata/testserver/*.*") 67 if err != nil { 68 t.Fatal(err) 69 } 70 71 oldhome := homePath() 72 helmHome = thome 73 hh := helmpath.Home(thome) 74 defer func() { 75 ts.Stop() 76 helmHome = oldhome 77 os.Remove(thome) 78 }() 79 if err := ensureTestHome(hh, t); err != nil { 80 t.Fatal(err) 81 } 82 83 r, err := repo.NewChartRepository(&repo.Entry{ 84 Name: "charts", 85 URL: ts.URL(), 86 Cache: hh.CacheIndex("charts"), 87 }) 88 if err != nil { 89 t.Error(err) 90 } 91 92 b := bytes.NewBuffer(nil) 93 updateCharts([]*repo.ChartRepository{r}, b, hh) 94 95 got := b.String() 96 if strings.Contains(got, "Unable to get an update") { 97 t.Errorf("Failed to get a repo: %q", got) 98 } 99 if !strings.Contains(got, "Update Complete.") { 100 t.Error("Update was not successful") 101 } 102 }