github.com/Racer159/helm-experiment@v0.0.0-20230822001441-1eb31183f614/src/dependency_build_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 cmd
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  	"testing"
    24  
    25  	"helm.sh/helm/v3/pkg/chartutil"
    26  	"helm.sh/helm/v3/pkg/provenance"
    27  	"helm.sh/helm/v3/pkg/repo"
    28  	"helm.sh/helm/v3/pkg/repo/repotest"
    29  )
    30  
    31  func TestDependencyBuildCmd(t *testing.T) {
    32  	srv, err := repotest.NewTempServerWithCleanup(t, "testdata/testcharts/*.tgz")
    33  	defer srv.Stop()
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  
    38  	rootDir := srv.Root()
    39  	srv.LinkIndices()
    40  
    41  	ociSrv, err := repotest.NewOCIServer(t, srv.Root())
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  
    46  	ociChartName := "oci-depending-chart"
    47  	c := createTestingMetadataForOCI(ociChartName, ociSrv.RegistryURL)
    48  	if _, err := chartutil.Save(c, ociSrv.Dir); err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	ociSrv.Run(t, repotest.WithDependingChart(c))
    52  
    53  	dir := func(p ...string) string {
    54  		return filepath.Join(append([]string{srv.Root()}, p...)...)
    55  	}
    56  
    57  	chartname := "depbuild"
    58  	createTestingChart(t, rootDir, chartname, srv.URL())
    59  	repoFile := filepath.Join(rootDir, "repositories.yaml")
    60  
    61  	cmd := fmt.Sprintf("dependency build '%s' --repository-config %s --repository-cache %s", filepath.Join(rootDir, chartname), repoFile, rootDir)
    62  	_, out, err := executeActionCommand(cmd)
    63  
    64  	// In the first pass, we basically want the same results as an update.
    65  	if err != nil {
    66  		t.Logf("Output: %s", out)
    67  		t.Fatal(err)
    68  	}
    69  
    70  	if !strings.Contains(out, `update from the "test" chart repository`) {
    71  		t.Errorf("Repo did not get updated\n%s", out)
    72  	}
    73  
    74  	// Make sure the actual file got downloaded.
    75  	expect := filepath.Join(rootDir, chartname, "charts/reqtest-0.1.0.tgz")
    76  	if _, err := os.Stat(expect); err != nil {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	// In the second pass, we want to remove the chart's request dependency,
    81  	// then see if it restores from the lock.
    82  	lockfile := filepath.Join(rootDir, chartname, "Chart.lock")
    83  	if _, err := os.Stat(lockfile); err != nil {
    84  		t.Fatal(err)
    85  	}
    86  	if err := os.RemoveAll(expect); err != nil {
    87  		t.Fatal(err)
    88  	}
    89  
    90  	_, out, err = executeActionCommand(cmd)
    91  	if err != nil {
    92  		t.Logf("Output: %s", out)
    93  		t.Fatal(err)
    94  	}
    95  
    96  	// Now repeat the test that the dependency exists.
    97  	if _, err := os.Stat(expect); err != nil {
    98  		t.Fatal(err)
    99  	}
   100  
   101  	// Make sure that build is also fetching the correct version.
   102  	hash, err := provenance.DigestFile(expect)
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  
   107  	i, err := repo.LoadIndexFile(filepath.Join(rootDir, "index.yaml"))
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  
   112  	reqver := i.Entries["reqtest"][0]
   113  	if h := reqver.Digest; h != hash {
   114  		t.Errorf("Failed hash match: expected %s, got %s", hash, h)
   115  	}
   116  	if v := reqver.Version; v != "0.1.0" {
   117  		t.Errorf("mismatched versions. Expected %q, got %q", "0.1.0", v)
   118  	}
   119  
   120  	skipRefreshCmd := fmt.Sprintf("dependency build '%s' --skip-refresh --repository-config %s --repository-cache %s", filepath.Join(rootDir, chartname), repoFile, rootDir)
   121  	_, out, err = executeActionCommand(skipRefreshCmd)
   122  
   123  	// In this pass, we check --skip-refresh option becomes effective.
   124  	if err != nil {
   125  		t.Logf("Output: %s", out)
   126  		t.Fatal(err)
   127  	}
   128  
   129  	if strings.Contains(out, `update from the "test" chart repository`) {
   130  		t.Errorf("Repo did get updated\n%s", out)
   131  	}
   132  
   133  	// OCI dependencies
   134  	if err := chartutil.SaveDir(c, dir()); err != nil {
   135  		t.Fatal(err)
   136  	}
   137  	cmd = fmt.Sprintf("dependency build '%s' --repository-config %s --repository-cache %s --registry-config %s/config.json",
   138  		dir(ociChartName),
   139  		dir("repositories.yaml"),
   140  		dir(),
   141  		dir())
   142  	_, out, err = executeActionCommand(cmd)
   143  	if err != nil {
   144  		t.Logf("Output: %s", out)
   145  		t.Fatal(err)
   146  	}
   147  	expect = dir(ociChartName, "charts/oci-dependent-chart-0.1.0.tgz")
   148  	if _, err := os.Stat(expect); err != nil {
   149  		t.Fatal(err)
   150  	}
   151  }
   152  
   153  func TestDependencyBuildCmdWithHelmV2Hash(t *testing.T) {
   154  	chartName := "testdata/testcharts/issue-7233"
   155  
   156  	cmd := fmt.Sprintf("dependency build '%s'", chartName)
   157  	_, out, err := executeActionCommand(cmd)
   158  
   159  	// Want to make sure the build can verify Helm v2 hash
   160  	if err != nil {
   161  		t.Logf("Output: %s", out)
   162  		t.Fatal(err)
   163  	}
   164  }