github.com/codefresh-io/kcfi@v0.0.0-20230301195427-c1578715cc46/cmd/kcfi/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  	"testing"
    24  
    25  	"helm.sh/helm/v3/pkg/repo/repotest"
    26  )
    27  
    28  func TestPullCmd(t *testing.T) {
    29  	srv, err := repotest.NewTempServer("testdata/testcharts/*.tgz*")
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	defer srv.Stop()
    34  
    35  	if err := srv.LinkIndices(); err != nil {
    36  		t.Fatal(err)
    37  	}
    38  
    39  	helmTestKeyOut := "Signed by: Helm Testing (This key should only be used for testing. DO NOT TRUST.) <helm-testing@helm.sh>\n" +
    40  		"Using Key With Fingerprint: 5E615389B53CA37F0EE60BD3843BBF981FC18762\n" +
    41  		"Chart Hash Verified: "
    42  
    43  	// all flags will get "-d outdir" appended.
    44  	tests := []struct {
    45  		name         string
    46  		args         string
    47  		existFile    string
    48  		existDir     string
    49  		wantError    bool
    50  		wantErrorMsg string
    51  		failExpect   string
    52  		expectFile   string
    53  		expectDir    bool
    54  		expectVerify bool
    55  		expectSha    string
    56  	}{
    57  		{
    58  			name:       "Basic chart fetch",
    59  			args:       "test/signtest",
    60  			expectFile: "./signtest-0.1.0.tgz",
    61  		},
    62  		{
    63  			name:       "Chart fetch with version",
    64  			args:       "test/signtest --version=0.1.0",
    65  			expectFile: "./signtest-0.1.0.tgz",
    66  		},
    67  		{
    68  			name:       "Fail chart fetch with non-existent version",
    69  			args:       "test/signtest --version=99.1.0",
    70  			wantError:  true,
    71  			failExpect: "no such chart",
    72  		},
    73  		{
    74  			name:       "Fail fetching non-existent chart",
    75  			args:       "test/nosuchthing",
    76  			failExpect: "Failed to fetch",
    77  			wantError:  true,
    78  		},
    79  		{
    80  			name:         "Fetch and verify",
    81  			args:         "test/signtest --verify --keyring testdata/helm-test-key.pub",
    82  			expectFile:   "./signtest-0.1.0.tgz",
    83  			expectVerify: true,
    84  			expectSha:    "sha256:e5ef611620fb97704d8751c16bab17fedb68883bfb0edc76f78a70e9173f9b55",
    85  		},
    86  		{
    87  			name:       "Fetch and fail verify",
    88  			args:       "test/reqtest --verify --keyring testdata/helm-test-key.pub",
    89  			failExpect: "Failed to fetch provenance",
    90  			wantError:  true,
    91  		},
    92  		{
    93  			name:       "Fetch and untar",
    94  			args:       "test/signtest --untar --untardir signtest",
    95  			expectFile: "./signtest",
    96  			expectDir:  true,
    97  		},
    98  		{
    99  			name:         "Fetch untar when file with same name existed",
   100  			args:         "test/test1 --untar --untardir test1",
   101  			existFile:    "test1",
   102  			wantError:    true,
   103  			wantErrorMsg: fmt.Sprintf("failed to untar: a file or directory with the name %s already exists", filepath.Join(srv.Root(), "test1")),
   104  		},
   105  		{
   106  			name:         "Fetch untar when dir with same name existed",
   107  			args:         "test/test2 --untar --untardir test2",
   108  			existDir:     "test2",
   109  			wantError:    true,
   110  			wantErrorMsg: fmt.Sprintf("failed to untar: a file or directory with the name %s already exists", filepath.Join(srv.Root(), "test2")),
   111  		},
   112  		{
   113  			name:         "Fetch, verify, untar",
   114  			args:         "test/signtest --verify --keyring=testdata/helm-test-key.pub --untar --untardir signtest2",
   115  			expectFile:   "./signtest2",
   116  			expectDir:    true,
   117  			expectVerify: true,
   118  			expectSha:    "sha256:e5ef611620fb97704d8751c16bab17fedb68883bfb0edc76f78a70e9173f9b55",
   119  		},
   120  		{
   121  			name:       "Chart fetch using repo URL",
   122  			expectFile: "./signtest-0.1.0.tgz",
   123  			args:       "signtest --repo " + srv.URL(),
   124  		},
   125  		{
   126  			name:       "Fail fetching non-existent chart on repo URL",
   127  			args:       "someChart --repo " + srv.URL(),
   128  			failExpect: "Failed to fetch chart",
   129  			wantError:  true,
   130  		},
   131  		{
   132  			name:       "Specific version chart fetch using repo URL",
   133  			expectFile: "./signtest-0.1.0.tgz",
   134  			args:       "signtest --version=0.1.0 --repo " + srv.URL(),
   135  		},
   136  		{
   137  			name:       "Specific version chart fetch using repo URL",
   138  			args:       "signtest --version=0.2.0 --repo " + srv.URL(),
   139  			failExpect: "Failed to fetch chart version",
   140  			wantError:  true,
   141  		},
   142  	}
   143  
   144  	for _, tt := range tests {
   145  		t.Run(tt.name, func(t *testing.T) {
   146  			outdir := srv.Root()
   147  			cmd := fmt.Sprintf("fetch %s -d '%s' --repository-config %s --repository-cache %s ",
   148  				tt.args,
   149  				outdir,
   150  				filepath.Join(outdir, "repositories.yaml"),
   151  				outdir,
   152  			)
   153  			// Create file or Dir before helm pull --untar, see: https://github.com/helm/helm/issues/7182
   154  			if tt.existFile != "" {
   155  				file := filepath.Join(outdir, tt.existFile)
   156  				_, err := os.Create(file)
   157  				if err != nil {
   158  					t.Fatal("err")
   159  				}
   160  			}
   161  			if tt.existDir != "" {
   162  				file := filepath.Join(outdir, tt.existDir)
   163  				err := os.Mkdir(file, 0755)
   164  				if err != nil {
   165  					t.Fatal(err)
   166  				}
   167  			}
   168  			_, out, err := executeActionCommand(cmd)
   169  			if err != nil {
   170  				if tt.wantError {
   171  					if tt.wantErrorMsg != "" && tt.wantErrorMsg == err.Error() {
   172  						t.Fatalf("Actual error %s, not equal to expected error %s", err, tt.wantErrorMsg)
   173  					}
   174  					return
   175  				}
   176  				t.Fatalf("%q reported error: %s", tt.name, err)
   177  			}
   178  
   179  			if tt.expectVerify {
   180  				outString := helmTestKeyOut + tt.expectSha + "\n"
   181  				if out != outString {
   182  					t.Errorf("%q: expected verification output %q, got %q", tt.name, outString, out)
   183  				}
   184  
   185  			}
   186  
   187  			ef := filepath.Join(outdir, tt.expectFile)
   188  			fi, err := os.Stat(ef)
   189  			if err != nil {
   190  				t.Errorf("%q: expected a file at %s. %s", tt.name, ef, err)
   191  			}
   192  			if fi.IsDir() != tt.expectDir {
   193  				t.Errorf("%q: expected directory=%t, but it's not.", tt.name, tt.expectDir)
   194  			}
   195  		})
   196  	}
   197  }