github.com/zsuzhengdu/helm@v3.0.0-beta.3+incompatible/cmd/helm/dependency_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  	"fmt"
    20  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  	"testing"
    25  
    26  	"helm.sh/helm/internal/test/ensure"
    27  	"helm.sh/helm/pkg/chart"
    28  	"helm.sh/helm/pkg/chartutil"
    29  	"helm.sh/helm/pkg/helmpath"
    30  	"helm.sh/helm/pkg/provenance"
    31  	"helm.sh/helm/pkg/repo"
    32  	"helm.sh/helm/pkg/repo/repotest"
    33  )
    34  
    35  func TestDependencyUpdateCmd(t *testing.T) {
    36  	srv, err := repotest.NewTempServer("testdata/testcharts/*.tgz")
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	defer srv.Stop()
    41  	t.Logf("Listening on directory %s", srv.Root())
    42  
    43  	if err := srv.LinkIndices(); err != nil {
    44  		t.Fatal(err)
    45  	}
    46  
    47  	dir := func(p ...string) string {
    48  		return filepath.Join(append([]string{srv.Root()}, p...)...)
    49  	}
    50  
    51  	chartname := "depup"
    52  	ch := createTestingMetadata(chartname, srv.URL())
    53  	md := ch.Metadata
    54  	if err := chartutil.SaveDir(ch, dir()); err != nil {
    55  		t.Fatal(err)
    56  	}
    57  
    58  	_, out, err := executeActionCommand(
    59  		fmt.Sprintf("dependency update '%s' --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()),
    60  	)
    61  	if err != nil {
    62  		t.Logf("Output: %s", out)
    63  		t.Fatal(err)
    64  	}
    65  
    66  	// This is written directly to stdout, so we have to capture as is.
    67  	if !strings.Contains(out, `update from the "test" chart repository`) {
    68  		t.Errorf("Repo did not get updated\n%s", out)
    69  	}
    70  
    71  	// Make sure the actual file got downloaded.
    72  	expect := dir(chartname, "charts/reqtest-0.1.0.tgz")
    73  	if _, err := os.Stat(expect); err != nil {
    74  		t.Fatal(err)
    75  	}
    76  
    77  	hash, err := provenance.DigestFile(expect)
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  
    82  	i, err := repo.LoadIndexFile(dir(helmpath.CacheIndexFile("test")))
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  
    87  	reqver := i.Entries["reqtest"][0]
    88  	if h := reqver.Digest; h != hash {
    89  		t.Errorf("Failed hash match: expected %s, got %s", hash, h)
    90  	}
    91  
    92  	// Now change the dependencies and update. This verifies that on update,
    93  	// old dependencies are cleansed and new dependencies are added.
    94  	md.Dependencies = []*chart.Dependency{
    95  		{Name: "reqtest", Version: "0.1.0", Repository: srv.URL()},
    96  		{Name: "compressedchart", Version: "0.3.0", Repository: srv.URL()},
    97  	}
    98  	if err := chartutil.SaveChartfile(dir(chartname, "Chart.yaml"), md); err != nil {
    99  		t.Fatal(err)
   100  	}
   101  
   102  	_, out, err = executeActionCommand(fmt.Sprintf("dependency update '%s' --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()))
   103  	if err != nil {
   104  		t.Logf("Output: %s", out)
   105  		t.Fatal(err)
   106  	}
   107  
   108  	// In this second run, we should see compressedchart-0.3.0.tgz, and not
   109  	// the 0.1.0 version.
   110  	expect = dir(chartname, "charts/compressedchart-0.3.0.tgz")
   111  	if _, err := os.Stat(expect); err != nil {
   112  		t.Fatalf("Expected %q: %s", expect, err)
   113  	}
   114  	dontExpect := dir(chartname, "charts/compressedchart-0.1.0.tgz")
   115  	if _, err := os.Stat(dontExpect); err == nil {
   116  		t.Fatalf("Unexpected %q", dontExpect)
   117  	}
   118  }
   119  
   120  func TestDependencyUpdateCmd_SkipRefresh(t *testing.T) {
   121  	defer resetEnv()()
   122  	defer ensure.HelmHome(t)()
   123  
   124  	srv := repotest.NewServer(helmpath.ConfigPath())
   125  	defer srv.Stop()
   126  	copied, err := srv.CopyCharts("testdata/testcharts/*.tgz")
   127  	if err != nil {
   128  		t.Fatal(err)
   129  	}
   130  	t.Logf("Copied charts:\n%s", strings.Join(copied, "\n"))
   131  	t.Logf("Listening on directory %s", srv.Root())
   132  
   133  	chartname := "depup"
   134  	createTestingChart(t, helmpath.DataPath(), chartname, srv.URL())
   135  
   136  	_, out, err := executeActionCommand(fmt.Sprintf("dependency update --skip-refresh %s", helmpath.DataPath(chartname)))
   137  	if err == nil {
   138  		t.Fatal("Expected failure to find the repo with skipRefresh")
   139  	}
   140  
   141  	// This is written directly to stdout, so we have to capture as is.
   142  	if strings.Contains(out, `update from the "test" chart repository`) {
   143  		t.Errorf("Repo was unexpectedly updated\n%s", out)
   144  	}
   145  }
   146  
   147  func TestDependencyUpdateCmd_DontDeleteOldChartsOnError(t *testing.T) {
   148  	defer resetEnv()()
   149  	defer ensure.HelmHome(t)()
   150  
   151  	srv, err := repotest.NewTempServer("testdata/testcharts/*.tgz")
   152  	if err != nil {
   153  		t.Fatal(err)
   154  	}
   155  	defer srv.Stop()
   156  	t.Logf("Listening on directory %s", srv.Root())
   157  
   158  	if err := srv.LinkIndices(); err != nil {
   159  		t.Fatal(err)
   160  	}
   161  
   162  	chartname := "depupdelete"
   163  
   164  	dir := func(p ...string) string {
   165  		return filepath.Join(append([]string{srv.Root()}, p...)...)
   166  	}
   167  	createTestingChart(t, dir(), chartname, srv.URL())
   168  
   169  	_, output, err := executeActionCommand(fmt.Sprintf("dependency update %s --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()))
   170  	if err != nil {
   171  		t.Logf("Output: %s", output)
   172  		t.Fatal(err)
   173  	}
   174  
   175  	// Chart repo is down
   176  	srv.Stop()
   177  
   178  	_, output, err = executeActionCommand(fmt.Sprintf("dependency update %s --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()))
   179  	if err == nil {
   180  		t.Logf("Output: %s", output)
   181  		t.Fatal("Expected error, got nil")
   182  	}
   183  
   184  	// Make sure charts dir still has dependencies
   185  	files, err := ioutil.ReadDir(filepath.Join(dir(chartname), "charts"))
   186  	if err != nil {
   187  		t.Fatal(err)
   188  	}
   189  	dependencies := []string{"compressedchart-0.1.0.tgz", "reqtest-0.1.0.tgz"}
   190  
   191  	if len(dependencies) != len(files) {
   192  		t.Fatalf("Expected %d chart dependencies, got %d", len(dependencies), len(files))
   193  	}
   194  	for index, file := range files {
   195  		if dependencies[index] != file.Name() {
   196  			t.Fatalf("Chart dependency %s not matching %s", dependencies[index], file.Name())
   197  		}
   198  	}
   199  
   200  	// Make sure tmpcharts is deleted
   201  	if _, err := os.Stat(filepath.Join(dir(chartname), "tmpcharts")); !os.IsNotExist(err) {
   202  		t.Fatalf("tmpcharts dir still exists")
   203  	}
   204  }
   205  
   206  // createTestingMetadata creates a basic chart that depends on reqtest-0.1.0
   207  //
   208  // The baseURL can be used to point to a particular repository server.
   209  func createTestingMetadata(name, baseURL string) *chart.Chart {
   210  	return &chart.Chart{
   211  		Metadata: &chart.Metadata{
   212  			APIVersion: chart.APIVersionV1,
   213  			Name:       name,
   214  			Version:    "1.2.3",
   215  			Dependencies: []*chart.Dependency{
   216  				{Name: "reqtest", Version: "0.1.0", Repository: baseURL},
   217  				{Name: "compressedchart", Version: "0.1.0", Repository: baseURL},
   218  			},
   219  		},
   220  	}
   221  }
   222  
   223  // createTestingChart creates a basic chart that depends on reqtest-0.1.0
   224  //
   225  // The baseURL can be used to point to a particular repository server.
   226  func createTestingChart(t *testing.T, dest, name, baseURL string) {
   227  	t.Helper()
   228  	cfile := createTestingMetadata(name, baseURL)
   229  	if err := chartutil.SaveDir(cfile, dest); err != nil {
   230  		t.Fatal(err)
   231  	}
   232  }