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