github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/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  	"io/ioutil"
    23  	"os"
    24  	"path/filepath"
    25  	"strings"
    26  	"testing"
    27  
    28  	"github.com/stefanmcshane/helm/internal/test/ensure"
    29  	"github.com/stefanmcshane/helm/pkg/getter"
    30  	"github.com/stefanmcshane/helm/pkg/repo"
    31  	"github.com/stefanmcshane/helm/pkg/repo/repotest"
    32  )
    33  
    34  func TestUpdateCmd(t *testing.T) {
    35  	var out bytes.Buffer
    36  	// Instead of using the HTTP updater, we provide our own for this test.
    37  	// The TestUpdateCharts test verifies the HTTP behavior independently.
    38  	updater := func(repos []*repo.ChartRepository, out io.Writer, failOnRepoUpdateFail bool) error {
    39  		for _, re := range repos {
    40  			fmt.Fprintln(out, re.Config.Name)
    41  		}
    42  		return nil
    43  	}
    44  	o := &repoUpdateOptions{
    45  		update:   updater,
    46  		repoFile: "testdata/repositories.yaml",
    47  	}
    48  	if err := o.run(&out); err != nil {
    49  		t.Fatal(err)
    50  	}
    51  
    52  	if got := out.String(); !strings.Contains(got, "charts") ||
    53  		!strings.Contains(got, "firstexample") ||
    54  		!strings.Contains(got, "secondexample") {
    55  		t.Errorf("Expected 'charts', 'firstexample' and 'secondexample' but got %q", got)
    56  	}
    57  }
    58  
    59  func TestUpdateCmdMultiple(t *testing.T) {
    60  	var out bytes.Buffer
    61  	// Instead of using the HTTP updater, we provide our own for this test.
    62  	// The TestUpdateCharts test verifies the HTTP behavior independently.
    63  	updater := func(repos []*repo.ChartRepository, out io.Writer, failOnRepoUpdateFail bool) error {
    64  		for _, re := range repos {
    65  			fmt.Fprintln(out, re.Config.Name)
    66  		}
    67  		return nil
    68  	}
    69  	o := &repoUpdateOptions{
    70  		update:   updater,
    71  		repoFile: "testdata/repositories.yaml",
    72  		names:    []string{"firstexample", "charts"},
    73  	}
    74  	if err := o.run(&out); err != nil {
    75  		t.Fatal(err)
    76  	}
    77  
    78  	if got := out.String(); !strings.Contains(got, "charts") ||
    79  		!strings.Contains(got, "firstexample") ||
    80  		strings.Contains(got, "secondexample") {
    81  		t.Errorf("Expected 'charts' and 'firstexample' but not 'secondexample' but got %q", got)
    82  	}
    83  }
    84  
    85  func TestUpdateCmdInvalid(t *testing.T) {
    86  	var out bytes.Buffer
    87  	// Instead of using the HTTP updater, we provide our own for this test.
    88  	// The TestUpdateCharts test verifies the HTTP behavior independently.
    89  	updater := func(repos []*repo.ChartRepository, out io.Writer, failOnRepoUpdateFail bool) error {
    90  		for _, re := range repos {
    91  			fmt.Fprintln(out, re.Config.Name)
    92  		}
    93  		return nil
    94  	}
    95  	o := &repoUpdateOptions{
    96  		update:   updater,
    97  		repoFile: "testdata/repositories.yaml",
    98  		names:    []string{"firstexample", "invalid"},
    99  	}
   100  	if err := o.run(&out); err == nil {
   101  		t.Fatal("expected error but did not get one")
   102  	}
   103  }
   104  
   105  func TestUpdateCustomCacheCmd(t *testing.T) {
   106  	rootDir := ensure.TempDir(t)
   107  	cachePath := filepath.Join(rootDir, "updcustomcache")
   108  	os.Mkdir(cachePath, os.ModePerm)
   109  	defer os.RemoveAll(cachePath)
   110  
   111  	ts, err := repotest.NewTempServerWithCleanup(t, "testdata/testserver/*.*")
   112  	if err != nil {
   113  		t.Fatal(err)
   114  	}
   115  	defer ts.Stop()
   116  
   117  	o := &repoUpdateOptions{
   118  		update:    updateCharts,
   119  		repoFile:  filepath.Join(ts.Root(), "repositories.yaml"),
   120  		repoCache: cachePath,
   121  	}
   122  	b := ioutil.Discard
   123  	if err := o.run(b); err != nil {
   124  		t.Fatal(err)
   125  	}
   126  	if _, err := os.Stat(filepath.Join(cachePath, "test-index.yaml")); err != nil {
   127  		t.Fatalf("error finding created index file in custom cache: %v", err)
   128  	}
   129  }
   130  
   131  func TestUpdateCharts(t *testing.T) {
   132  	defer resetEnv()()
   133  	defer ensure.HelmHome(t)()
   134  
   135  	ts, err := repotest.NewTempServerWithCleanup(t, "testdata/testserver/*.*")
   136  	if err != nil {
   137  		t.Fatal(err)
   138  	}
   139  	defer ts.Stop()
   140  
   141  	r, err := repo.NewChartRepository(&repo.Entry{
   142  		Name: "charts",
   143  		URL:  ts.URL(),
   144  	}, getter.All(settings))
   145  	if err != nil {
   146  		t.Error(err)
   147  	}
   148  
   149  	b := bytes.NewBuffer(nil)
   150  	updateCharts([]*repo.ChartRepository{r}, b, false)
   151  
   152  	got := b.String()
   153  	if strings.Contains(got, "Unable to get an update") {
   154  		t.Errorf("Failed to get a repo: %q", got)
   155  	}
   156  	if !strings.Contains(got, "Update Complete.") {
   157  		t.Error("Update was not successful")
   158  	}
   159  }
   160  
   161  func TestRepoUpdateFileCompletion(t *testing.T) {
   162  	checkFileCompletion(t, "repo update", false)
   163  	checkFileCompletion(t, "repo update repo1", false)
   164  }
   165  
   166  func TestUpdateChartsFail(t *testing.T) {
   167  	defer resetEnv()()
   168  	defer ensure.HelmHome(t)()
   169  
   170  	ts, err := repotest.NewTempServerWithCleanup(t, "testdata/testserver/*.*")
   171  	if err != nil {
   172  		t.Fatal(err)
   173  	}
   174  	defer ts.Stop()
   175  
   176  	var invalidURL = ts.URL() + "55"
   177  	r, err := repo.NewChartRepository(&repo.Entry{
   178  		Name: "charts",
   179  		URL:  invalidURL,
   180  	}, getter.All(settings))
   181  	if err != nil {
   182  		t.Error(err)
   183  	}
   184  
   185  	b := bytes.NewBuffer(nil)
   186  	if err := updateCharts([]*repo.ChartRepository{r}, b, false); err != nil {
   187  		t.Error("Repo update should not return error if update of repository fails")
   188  	}
   189  
   190  	got := b.String()
   191  	if !strings.Contains(got, "Unable to get an update") {
   192  		t.Errorf("Repo should have failed update but instead got: %q", got)
   193  	}
   194  	if !strings.Contains(got, "Update Complete.") {
   195  		t.Error("Update was not successful")
   196  	}
   197  }
   198  
   199  func TestUpdateChartsFailWithError(t *testing.T) {
   200  	defer resetEnv()()
   201  	defer ensure.HelmHome(t)()
   202  
   203  	ts, err := repotest.NewTempServerWithCleanup(t, "testdata/testserver/*.*")
   204  	if err != nil {
   205  		t.Fatal(err)
   206  	}
   207  	defer ts.Stop()
   208  
   209  	var invalidURL = ts.URL() + "55"
   210  	r, err := repo.NewChartRepository(&repo.Entry{
   211  		Name: "charts",
   212  		URL:  invalidURL,
   213  	}, getter.All(settings))
   214  	if err != nil {
   215  		t.Error(err)
   216  	}
   217  
   218  	b := bytes.NewBuffer(nil)
   219  	err = updateCharts([]*repo.ChartRepository{r}, b, true)
   220  	if err == nil {
   221  		t.Error("Repo update should return error because update of repository fails and 'fail-on-repo-update-fail' flag set")
   222  		return
   223  	}
   224  	var expectedErr = "Failed to update the following repositories"
   225  	var receivedErr = err.Error()
   226  	if !strings.Contains(receivedErr, expectedErr) {
   227  		t.Errorf("Expected error (%s) but got (%s) instead", expectedErr, receivedErr)
   228  	}
   229  	if !strings.Contains(receivedErr, invalidURL) {
   230  		t.Errorf("Expected invalid URL (%s) in error message but got (%s) instead", invalidURL, receivedErr)
   231  	}
   232  
   233  	got := b.String()
   234  	if !strings.Contains(got, "Unable to get an update") {
   235  		t.Errorf("Repo should have failed update but instead got: %q", got)
   236  	}
   237  	if strings.Contains(got, "Update Complete.") {
   238  		t.Error("Update was not successful and should return error message because 'fail-on-repo-update-fail' flag set")
   239  	}
   240  }