github.com/defensepoint-snyk-test/helm-new@v0.0.0-20211130153739-c57ea64d6603/cmd/helm/repo_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  	"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.RemoveAll(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, strict bool) error {
    50  		for _, re := range repos {
    51  			fmt.Fprintln(out, re.Config.Name)
    52  		}
    53  		return nil
    54  	}
    55  	uc := &repoUpdateCmd{
    56  		update: updater,
    57  		home:   helmpath.Home(thome),
    58  		out:    out,
    59  	}
    60  	if err := uc.run(); err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	if got := out.String(); !strings.Contains(got, "charts") || !strings.Contains(got, "local") {
    65  		t.Errorf("Expected 'charts' and 'local' (in any order) got %q", got)
    66  	}
    67  }
    68  
    69  func TestUpdateCharts(t *testing.T) {
    70  	ts, thome, err := repotest.NewTempServer("testdata/testserver/*.*")
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  
    75  	hh := helmpath.Home(thome)
    76  	cleanup := resetEnv()
    77  	defer func() {
    78  		ts.Stop()
    79  		os.RemoveAll(thome.String())
    80  		cleanup()
    81  	}()
    82  	if err := ensureTestHome(hh, t); err != nil {
    83  		t.Fatal(err)
    84  	}
    85  
    86  	settings.Home = thome
    87  
    88  	r, err := repo.NewChartRepository(&repo.Entry{
    89  		Name:  "charts",
    90  		URL:   ts.URL(),
    91  		Cache: hh.CacheIndex("charts"),
    92  	}, getter.All(settings))
    93  	if err != nil {
    94  		t.Error(err)
    95  	}
    96  
    97  	b := bytes.NewBuffer(nil)
    98  	updateCharts([]*repo.ChartRepository{r}, b, hh, false)
    99  
   100  	got := b.String()
   101  	if strings.Contains(got, "Unable to get an update") {
   102  		t.Errorf("Failed to get a repo: %q", got)
   103  	}
   104  	if !strings.Contains(got, "Update Complete.") {
   105  		t.Error("Update was not successful")
   106  	}
   107  }