github.com/wangchanggan/helm@v0.0.0-20211020154240-11b1b7d5406d/cmd/helm/fetch_test.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"os"
    23  	"path/filepath"
    24  	"regexp"
    25  	"strings"
    26  	"testing"
    27  
    28  	"k8s.io/helm/pkg/repo/repotest"
    29  )
    30  
    31  func TestFetchCmd(t *testing.T) {
    32  	hh, err := tempHelmHome(t)
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	cleanup := resetEnv()
    37  	defer func() {
    38  		os.RemoveAll(hh.String())
    39  		cleanup()
    40  	}()
    41  	srv := repotest.NewServer(hh.String())
    42  	defer srv.Stop()
    43  
    44  	settings.Home = hh
    45  
    46  	// all flags will get "--home=TMDIR -d outdir" appended.
    47  	tests := []struct {
    48  		name         string
    49  		chart        string
    50  		flags        []string
    51  		fail         bool
    52  		wantErrorMsg string
    53  		existFile    string
    54  		existDir     string
    55  		failExpect   string
    56  		expectFile   string
    57  		expectDir    bool
    58  		expectVerify bool
    59  	}{
    60  		{
    61  			name:       "Basic chart fetch",
    62  			chart:      "test/signtest",
    63  			expectFile: "./signtest-0.1.0.tgz",
    64  		},
    65  		{
    66  			name:       "Chart fetch with version",
    67  			chart:      "test/signtest",
    68  			flags:      []string{"--version", "0.1.0"},
    69  			expectFile: "./signtest-0.1.0.tgz",
    70  		},
    71  		{
    72  			name:       "Fail chart fetch with non-existent version",
    73  			chart:      "test/signtest",
    74  			flags:      []string{"--version", "99.1.0"},
    75  			fail:       true,
    76  			failExpect: "no such chart",
    77  		},
    78  		{
    79  			name:       "Fail fetching non-existent chart",
    80  			chart:      "test/nosuchthing",
    81  			failExpect: "Failed to fetch",
    82  			fail:       true,
    83  		},
    84  		{
    85  			name:         "Fetch and verify",
    86  			chart:        "test/signtest",
    87  			flags:        []string{"--verify", "--keyring", "testdata/helm-test-key.pub"},
    88  			expectFile:   "./signtest-0.1.0.tgz",
    89  			expectVerify: true,
    90  		},
    91  		{
    92  			name:       "Fetch and fail verify",
    93  			chart:      "test/reqtest",
    94  			flags:      []string{"--verify", "--keyring", "testdata/helm-test-key.pub"},
    95  			failExpect: "Failed to fetch provenance",
    96  			fail:       true,
    97  		},
    98  		{
    99  			name:       "Fetch and untar",
   100  			chart:      "test/signtest",
   101  			flags:      []string{"--untar", "--untardir", "signtest"},
   102  			expectFile: "./signtest",
   103  			expectDir:  true,
   104  		},
   105  		{
   106  			name:         "Fetch untar when file with same name existed",
   107  			flags:        []string{"test/test1 --untar --untardir test1"},
   108  			existFile:    "test1",
   109  			fail:         true,
   110  			wantErrorMsg: "failed to untar",
   111  		},
   112  		{
   113  			name:         "Fetch untar when dir with same name existed",
   114  			flags:        []string{"test/test2 --untar --untardir test2"},
   115  			existDir:     "test2",
   116  			fail:         true,
   117  			wantErrorMsg: "failed to untar",
   118  		},
   119  		{
   120  			name:         "Fetch, verify, untar",
   121  			chart:        "test/signtest",
   122  			flags:        []string{"--verify", "--keyring", "testdata/helm-test-key.pub", "--untar", "--untardir", "signtest2"},
   123  			expectFile:   "./signtest2",
   124  			expectDir:    true,
   125  			expectVerify: true,
   126  			failExpect:   "Failed to untar signtest",
   127  			fail:         true,
   128  		},
   129  		{
   130  			name:       "Chart fetch using repo URL",
   131  			chart:      "signtest",
   132  			expectFile: "./signtest-0.1.0.tgz",
   133  			flags:      []string{"--repo", srv.URL()},
   134  		},
   135  		{
   136  			name:       "Fail fetching non-existent chart on repo URL",
   137  			chart:      "someChart",
   138  			flags:      []string{"--repo", srv.URL()},
   139  			failExpect: "Failed to fetch chart",
   140  			fail:       true,
   141  		},
   142  		{
   143  			name:       "Specific version chart fetch using repo URL",
   144  			chart:      "signtest",
   145  			expectFile: "./signtest-0.1.0.tgz",
   146  			flags:      []string{"--repo", srv.URL(), "--version", "0.1.0"},
   147  		},
   148  		{
   149  			name:       "Specific version chart fetch using repo URL",
   150  			chart:      "signtest",
   151  			flags:      []string{"--repo", srv.URL(), "--version", "0.2.0"},
   152  			failExpect: "Failed to fetch chart version",
   153  			fail:       true,
   154  		},
   155  	}
   156  
   157  	if _, err := srv.CopyCharts("testdata/testcharts/*.tgz*"); err != nil {
   158  		t.Fatal(err)
   159  	}
   160  	if err := srv.LinkIndices(); err != nil {
   161  		t.Fatal(err)
   162  	}
   163  
   164  	for _, tt := range tests {
   165  		outdir := filepath.Join(hh.String(), "testout")
   166  		os.RemoveAll(outdir)
   167  		os.Mkdir(outdir, 0755)
   168  
   169  		// Create file or Dir before helm pull --untar, see: https://github.com/helm/helm/issues/7182
   170  		if tt.existFile != "" {
   171  			file := filepath.Join(outdir, tt.existFile)
   172  			_, err := os.Create(file)
   173  			if err != nil {
   174  				t.Fatal("err")
   175  			}
   176  		}
   177  		if tt.existDir != "" {
   178  			file := filepath.Join(outdir, tt.existDir)
   179  			err := os.Mkdir(file, 0755)
   180  			if err != nil {
   181  				t.Fatal(err)
   182  			}
   183  		}
   184  		buf := bytes.NewBuffer(nil)
   185  		cmd := newFetchCmd(buf)
   186  		tt.flags = append(tt.flags, "-d", outdir)
   187  		cmd.ParseFlags(tt.flags)
   188  		if err := cmd.RunE(cmd, []string{tt.chart}); err != nil {
   189  			if tt.fail {
   190  				if tt.wantErrorMsg != "" && strings.Contains(tt.wantErrorMsg, err.Error()) {
   191  					t.Fatalf("Actual error %s, not equal to expected error %s", err, tt.wantErrorMsg)
   192  				}
   193  				return
   194  			}
   195  			t.Errorf("%q reported error: %s", tt.name, err)
   196  			continue
   197  		}
   198  		if tt.expectVerify {
   199  			pointerAddressPattern := "0[xX][A-Fa-f0-9]+"
   200  			sha256Pattern := "[A-Fa-f0-9]{64}"
   201  			verificationRegex := regexp.MustCompile(
   202  				fmt.Sprintf("Verification: &{%s sha256:%s signtest-0.1.0.tgz}\n", pointerAddressPattern, sha256Pattern))
   203  			if !verificationRegex.MatchString(buf.String()) {
   204  				t.Errorf("%q: expected match for regex %s, got %s", tt.name, verificationRegex, buf.String())
   205  			}
   206  		}
   207  
   208  		ef := filepath.Join(outdir, tt.expectFile)
   209  		fi, err := os.Stat(ef)
   210  		if err != nil {
   211  			t.Errorf("%q: expected a file at %s. %s", tt.name, ef, err)
   212  		}
   213  		if fi.IsDir() != tt.expectDir {
   214  			t.Errorf("%q: expected directory=%t, but it's not.", tt.name, tt.expectDir)
   215  		}
   216  	}
   217  }