...

Source file src/helm.sh/helm/v3/pkg/chart/loader/archive_test.go

Documentation: helm.sh/helm/v3/pkg/chart/loader

     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 loader
    18  
    19  import (
    20  	"archive/tar"
    21  	"bytes"
    22  	"compress/gzip"
    23  	"testing"
    24  )
    25  
    26  func TestLoadArchiveFiles(t *testing.T) {
    27  	tcs := []struct {
    28  		name     string
    29  		generate func(w *tar.Writer)
    30  		check    func(t *testing.T, files []*BufferedFile, err error)
    31  	}{
    32  		{
    33  			name:     "empty input should return no files",
    34  			generate: func(_ *tar.Writer) {},
    35  			check: func(t *testing.T, _ []*BufferedFile, err error) {
    36  				if err.Error() != "no files in chart archive" {
    37  					t.Fatalf(`expected "no files in chart archive", got [%#v]`, err)
    38  				}
    39  			},
    40  		},
    41  		{
    42  			name: "should ignore files with XGlobalHeader type",
    43  			generate: func(w *tar.Writer) {
    44  				// simulate the presence of a `pax_global_header` file like you would get when
    45  				// processing a GitHub release archive.
    46  				err := w.WriteHeader(&tar.Header{
    47  					Typeflag: tar.TypeXGlobalHeader,
    48  					Name:     "pax_global_header",
    49  				})
    50  				if err != nil {
    51  					t.Fatal(err)
    52  				}
    53  
    54  				// we need to have at least one file, otherwise we'll get the "no files in chart archive" error
    55  				err = w.WriteHeader(&tar.Header{
    56  					Typeflag: tar.TypeReg,
    57  					Name:     "dir/empty",
    58  				})
    59  				if err != nil {
    60  					t.Fatal(err)
    61  				}
    62  			},
    63  			check: func(t *testing.T, files []*BufferedFile, err error) {
    64  				if err != nil {
    65  					t.Fatalf(`got unwanted error [%#v] for tar file with pax_global_header content`, err)
    66  				}
    67  
    68  				if len(files) != 1 {
    69  					t.Fatalf(`expected to get one file but got [%v]`, files)
    70  				}
    71  			},
    72  		},
    73  	}
    74  
    75  	for _, tc := range tcs {
    76  		t.Run(tc.name, func(t *testing.T) {
    77  			buf := &bytes.Buffer{}
    78  			gzw := gzip.NewWriter(buf)
    79  			tw := tar.NewWriter(gzw)
    80  
    81  			tc.generate(tw)
    82  
    83  			_ = tw.Close()
    84  			_ = gzw.Close()
    85  
    86  			files, err := LoadArchiveFiles(buf)
    87  			tc.check(t, files, err)
    88  		})
    89  	}
    90  }
    91  

View as plain text