1/*
2Copyright The Helm Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package loader
18
19import (
20	"archive/tar"
21	"bytes"
22	"compress/gzip"
23	"testing"
24)
25
26func 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(w *tar.Writer) {},
35			check: func(t *testing.T, files []*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				_ = w.WriteHeader(&tar.Header{
47					Typeflag: tar.TypeXGlobalHeader,
48					Name:     "pax_global_header",
49				})
50
51				// we need to have at least one file, otherwise we'll get the "no files in chart archive" error
52				_ = w.WriteHeader(&tar.Header{
53					Typeflag: tar.TypeReg,
54					Name:     "dir/empty",
55				})
56			},
57			check: func(t *testing.T, files []*BufferedFile, err error) {
58				if err != nil {
59					t.Fatalf(`got unwanted error [%#v] for tar file with pax_global_header content`, err)
60				}
61
62				if len(files) != 1 {
63					t.Fatalf(`expected to get one file but got [%v]`, files)
64				}
65			},
66		},
67		{
68			name: "should ignore files with TypeXHeader type",
69			generate: func(w *tar.Writer) {
70				// simulate the presence of a `pax_header` file like you might get when
71				// processing a GitHub release archive.
72				_ = w.WriteHeader(&tar.Header{
73					Typeflag: tar.TypeXHeader,
74					Name:     "pax_header",
75				})
76
77				// we need to have at least one file, otherwise we'll get the "no files in chart archive" error
78				_ = w.WriteHeader(&tar.Header{
79					Typeflag: tar.TypeReg,
80					Name:     "dir/empty",
81				})
82			},
83			check: func(t *testing.T, files []*BufferedFile, err error) {
84				if err != nil {
85					t.Fatalf(`got unwanted error [%#v] for tar file with pax_header content`, err)
86				}
87
88				if len(files) != 1 {
89					t.Fatalf(`expected to get one file but got [%v]`, files)
90				}
91			},
92		},
93	}
94
95	for _, tc := range tcs {
96		t.Run(tc.name, func(t *testing.T) {
97			buf := &bytes.Buffer{}
98			gzw := gzip.NewWriter(buf)
99			tw := tar.NewWriter(gzw)
100
101			tc.generate(tw)
102
103			_ = tw.Close()
104			_ = gzw.Close()
105
106			files, err := LoadArchiveFiles(buf)
107			tc.check(t, files, err)
108		})
109	}
110}
111