github.com/codefresh-io/kcfi@v0.0.0-20230301195427-c1578715cc46/cmd/kcfi/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 main
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  	"testing"
    24  
    25  	"helm.sh/helm/v3/pkg/provenance"
    26  	"helm.sh/helm/v3/pkg/repo"
    27  	"helm.sh/helm/v3/pkg/repo/repotest"
    28  )
    29  
    30  func TestDependencyBuildCmd(t *testing.T) {
    31  	srv, err := repotest.NewTempServer("testdata/testcharts/*.tgz")
    32  	defer srv.Stop()
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	rootDir := srv.Root()
    38  	srv.LinkIndices()
    39  
    40  	chartname := "depbuild"
    41  	createTestingChart(t, rootDir, chartname, srv.URL())
    42  	repoFile := filepath.Join(rootDir, "repositories.yaml")
    43  
    44  	cmd := fmt.Sprintf("dependency build '%s' --repository-config %s --repository-cache %s", filepath.Join(rootDir, chartname), repoFile, rootDir)
    45  	_, out, err := executeActionCommand(cmd)
    46  
    47  	// In the first pass, we basically want the same results as an update.
    48  	if err != nil {
    49  		t.Logf("Output: %s", out)
    50  		t.Fatal(err)
    51  	}
    52  
    53  	if !strings.Contains(out, `update from the "test" chart repository`) {
    54  		t.Errorf("Repo did not get updated\n%s", out)
    55  	}
    56  
    57  	// Make sure the actual file got downloaded.
    58  	expect := filepath.Join(rootDir, chartname, "charts/reqtest-0.1.0.tgz")
    59  	if _, err := os.Stat(expect); err != nil {
    60  		t.Fatal(err)
    61  	}
    62  
    63  	// In the second pass, we want to remove the chart's request dependency,
    64  	// then see if it restores from the lock.
    65  	lockfile := filepath.Join(rootDir, chartname, "Chart.lock")
    66  	if _, err := os.Stat(lockfile); err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	if err := os.RemoveAll(expect); err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  	_, out, err = executeActionCommand(cmd)
    74  	if err != nil {
    75  		t.Logf("Output: %s", out)
    76  		t.Fatal(err)
    77  	}
    78  
    79  	// Now repeat the test that the dependency exists.
    80  	if _, err := os.Stat(expect); err != nil {
    81  		t.Fatal(err)
    82  	}
    83  
    84  	// Make sure that build is also fetching the correct version.
    85  	hash, err := provenance.DigestFile(expect)
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  
    90  	i, err := repo.LoadIndexFile(filepath.Join(rootDir, "index.yaml"))
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  
    95  	reqver := i.Entries["reqtest"][0]
    96  	if h := reqver.Digest; h != hash {
    97  		t.Errorf("Failed hash match: expected %s, got %s", hash, h)
    98  	}
    99  	if v := reqver.Version; v != "0.1.0" {
   100  		t.Errorf("mismatched versions. Expected %q, got %q", "0.1.0", v)
   101  	}
   102  }
   103  
   104  func TestDependencyBuildCmdWithHelmV2Hash(t *testing.T) {
   105  	chartName := "testdata/testcharts/issue-7233"
   106  
   107  	cmd := fmt.Sprintf("dependency build '%s'", chartName)
   108  	_, out, err := executeActionCommand(cmd)
   109  
   110  	// Want to make sure the build can verify Helm v2 hash
   111  	if err != nil {
   112  		t.Logf("Output: %s", out)
   113  		t.Fatal(err)
   114  	}
   115  }