github.com/appscode/helm@v3.0.0-alpha.1+incompatible/cmd/helm/pull_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  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  	"regexp"
    24  	"strings"
    25  	"testing"
    26  
    27  	"helm.sh/helm/pkg/repo/repotest"
    28  )
    29  
    30  func TestPullCmd(t *testing.T) {
    31  	defer resetEnv()()
    32  
    33  	hh := testHelmHome(t)
    34  	settings.Home = hh
    35  
    36  	srv := repotest.NewServer(hh.String())
    37  	defer srv.Stop()
    38  
    39  	// all flags will get "--home=TMDIR -d outdir" appended.
    40  	tests := []struct {
    41  		name         string
    42  		args         []string
    43  		wantError    bool
    44  		failExpect   string
    45  		expectFile   string
    46  		expectDir    bool
    47  		expectVerify bool
    48  	}{
    49  		{
    50  			name:       "Basic chart fetch",
    51  			args:       []string{"test/signtest"},
    52  			expectFile: "./signtest-0.1.0.tgz",
    53  		},
    54  		{
    55  			name:       "Chart fetch with version",
    56  			args:       []string{"test/signtest --version=0.1.0"},
    57  			expectFile: "./signtest-0.1.0.tgz",
    58  		},
    59  		{
    60  			name:       "Fail chart fetch with non-existent version",
    61  			args:       []string{"test/signtest --version=99.1.0"},
    62  			wantError:  true,
    63  			failExpect: "no such chart",
    64  		},
    65  		{
    66  			name:       "Fail fetching non-existent chart",
    67  			args:       []string{"test/nosuchthing"},
    68  			failExpect: "Failed to fetch",
    69  			wantError:  true,
    70  		},
    71  		{
    72  			name:         "Fetch and verify",
    73  			args:         []string{"test/signtest --verify --keyring testdata/helm-test-key.pub"},
    74  			expectFile:   "./signtest-0.1.0.tgz",
    75  			expectVerify: true,
    76  		},
    77  		{
    78  			name:       "Fetch and fail verify",
    79  			args:       []string{"test/reqtest --verify --keyring testdata/helm-test-key.pub"},
    80  			failExpect: "Failed to fetch provenance",
    81  			wantError:  true,
    82  		},
    83  		{
    84  			name:       "Fetch and untar",
    85  			args:       []string{"test/signtest --untar --untardir signtest"},
    86  			expectFile: "./signtest",
    87  			expectDir:  true,
    88  		},
    89  		{
    90  			name:         "Fetch, verify, untar",
    91  			args:         []string{"test/signtest --verify --keyring=testdata/helm-test-key.pub --untar --untardir signtest"},
    92  			expectFile:   "./signtest",
    93  			expectDir:    true,
    94  			expectVerify: true,
    95  		},
    96  		{
    97  			name:       "Chart fetch using repo URL",
    98  			expectFile: "./signtest-0.1.0.tgz",
    99  			args:       []string{"signtest --repo", srv.URL()},
   100  		},
   101  		{
   102  			name:       "Fail fetching non-existent chart on repo URL",
   103  			args:       []string{"someChart --repo", srv.URL()},
   104  			failExpect: "Failed to fetch chart",
   105  			wantError:  true,
   106  		},
   107  		{
   108  			name:       "Specific version chart fetch using repo URL",
   109  			expectFile: "./signtest-0.1.0.tgz",
   110  			args:       []string{"signtest --version=0.1.0 --repo", srv.URL()},
   111  		},
   112  		{
   113  			name:       "Specific version chart fetch using repo URL",
   114  			args:       []string{"signtest --version=0.2.0 --repo", srv.URL()},
   115  			failExpect: "Failed to fetch chart version",
   116  			wantError:  true,
   117  		},
   118  	}
   119  
   120  	if _, err := srv.CopyCharts("testdata/testcharts/*.tgz*"); err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	if err := srv.LinkIndices(); err != nil {
   124  		t.Fatal(err)
   125  	}
   126  
   127  	for _, tt := range tests {
   128  		outdir := hh.Path("testout")
   129  		os.RemoveAll(outdir)
   130  		os.Mkdir(outdir, 0755)
   131  
   132  		cmd := strings.Join(append(tt.args, "-d", "'"+outdir+"'", "--home", "'"+hh.String()+"'"), " ")
   133  		_, out, err := executeActionCommand("fetch " + cmd)
   134  		if err != nil {
   135  			if tt.wantError {
   136  				continue
   137  			}
   138  			t.Errorf("%q reported error: %s", tt.name, err)
   139  			continue
   140  		}
   141  
   142  		if tt.expectVerify {
   143  			pointerAddressPattern := "0[xX][A-Fa-f0-9]+"
   144  			sha256Pattern := "[A-Fa-f0-9]{64}"
   145  			verificationRegex := regexp.MustCompile(
   146  				fmt.Sprintf("Verification: &{%s sha256:%s signtest-0.1.0.tgz}\n", pointerAddressPattern, sha256Pattern))
   147  			if !verificationRegex.MatchString(out) {
   148  				t.Errorf("%q: expected match for regex %s, got %s", tt.name, verificationRegex, out)
   149  			}
   150  		}
   151  
   152  		ef := filepath.Join(outdir, tt.expectFile)
   153  		fi, err := os.Stat(ef)
   154  		if err != nil {
   155  			t.Errorf("%q: expected a file at %s. %s", tt.name, ef, err)
   156  		}
   157  		if fi.IsDir() != tt.expectDir {
   158  			t.Errorf("%q: expected directory=%t, but it's not.", tt.name, tt.expectDir)
   159  		}
   160  	}
   161  }