github.com/koderover/helm@v2.17.0+incompatible/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  }
   108  
   109  func TestUpdateCmdStrictFlag(t *testing.T) {
   110  	thome, err := tempHelmHome(t)
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  
   115  	cleanup := resetEnv()
   116  	defer func() {
   117  		os.RemoveAll(thome.String())
   118  		cleanup()
   119  	}()
   120  
   121  	settings.Home = thome
   122  
   123  	out := bytes.NewBuffer(nil)
   124  	cmd := newRepoUpdateCmd(out)
   125  	cmd.ParseFlags([]string{"--strict"})
   126  
   127  	if err := cmd.RunE(cmd, []string{}); err == nil {
   128  		t.Fatal("expected error due to strict flag")
   129  	}
   130  
   131  	if got := out.String(); !strings.Contains(got, "Unable to get an update") {
   132  		t.Errorf("Expected 'Unable to get an update', got %q", got)
   133  	}
   134  }
   135  
   136  func TestUpdateCmdWithSingleRepoNameWhichDoesntExist(t *testing.T) {
   137  	thome, err := tempHelmHome(t)
   138  	if err != nil {
   139  		t.Fatal(err)
   140  	}
   141  
   142  	cleanup := resetEnv()
   143  	defer func() {
   144  		os.RemoveAll(thome.String())
   145  		cleanup()
   146  	}()
   147  
   148  	settings.Home = thome
   149  
   150  	out := bytes.NewBuffer(nil)
   151  	cmd := newRepoUpdateCmd(out)
   152  
   153  	if err = cmd.RunE(cmd, []string{"randomRepo"}); err == nil {
   154  		t.Fatal("expected error due to wrong repo name")
   155  	}
   156  
   157  	if got := fmt.Sprintf("%v", err); !strings.Contains(got, "no repositories found matching the provided name. Verify if the repo exists") {
   158  		t.Errorf("Expected 'no repositories found matching the provided name. Verify if the repo exists', got %q", got)
   159  	}
   160  }
   161  
   162  func TestUpdateRepo(t *testing.T) {
   163  	ts, thome, err := repotest.NewTempServer("testdata/testserver/*.*")
   164  	if err != nil {
   165  		t.Fatal(err)
   166  	}
   167  
   168  	hh := helmpath.Home(thome)
   169  	cleanup := resetEnv()
   170  	defer func() {
   171  		ts.Stop()
   172  		os.RemoveAll(thome.String())
   173  		cleanup()
   174  	}()
   175  	if err := ensureTestHome(hh, t); err != nil {
   176  		t.Fatal(err)
   177  	}
   178  
   179  	settings.Home = thome
   180  
   181  	if err := addRepository("repo1", ts.URL(), "", "", hh, "", "", "", true); err != nil {
   182  		t.Error(err)
   183  	}
   184  
   185  	if err := addRepository("repo2", ts.URL(), "", "", hh, "", "", "", true); err != nil {
   186  		t.Error(err)
   187  	}
   188  
   189  	out := bytes.NewBuffer(nil)
   190  	cmd := newRepoUpdateCmd(out)
   191  
   192  	if err = cmd.RunE(cmd, []string{"repo1"}); err != nil {
   193  		t.Fatal("expected to update repo1 correctly")
   194  	}
   195  
   196  	got := out.String()
   197  
   198  	if !strings.Contains(got, "Successfully got an update from the \"repo1\"") {
   199  		t.Errorf("Expected to successfully update \"repo1\" repository, got %q", got)
   200  	}
   201  
   202  	if strings.Contains(got, "Successfully got an update from the \"repo2\"") {
   203  		t.Errorf("Shouldn't have updated \"repo2\" repository, got %q", got)
   204  	}
   205  
   206  	if !strings.Contains(got, "Update Complete.") {
   207  		t.Error("Update was not successful")
   208  	}
   209  }