|
|
|
|
@ -17,11 +17,73 @@ limitations under the License.
|
|
|
|
|
package chartutil
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"archive/tar"
|
|
|
|
|
"bytes"
|
|
|
|
|
"compress/gzip"
|
|
|
|
|
"io/fs"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// makeTestChartArchive builds a gzipped tar archive from the given sourceDir directory, file entries are prefixed with the given chartName
|
|
|
|
|
func makeTestChartArchive(t *testing.T, chartName, sourceDir string) *bytes.Buffer {
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
|
|
var result bytes.Buffer
|
|
|
|
|
gw := gzip.NewWriter(&result)
|
|
|
|
|
tw := tar.NewWriter(gw)
|
|
|
|
|
|
|
|
|
|
dir := os.DirFS(sourceDir)
|
|
|
|
|
|
|
|
|
|
writeFile := func(relPath string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
f, err := dir.Open(relPath)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
fStat, err := f.Stat()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
err = tw.WriteHeader(&tar.Header{
|
|
|
|
|
Name: filepath.Join(chartName, relPath),
|
|
|
|
|
Mode: int64(fStat.Mode()),
|
|
|
|
|
Size: fStat.Size(),
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
data, err := fs.ReadFile(dir, relPath)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
tw.Write(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := fs.WalkDir(dir, ".", func(path string, d os.DirEntry, walkErr error) error {
|
|
|
|
|
if walkErr != nil {
|
|
|
|
|
return walkErr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if d.IsDir() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeFile(path)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = tw.Close()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
err = gw.Close()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
return &result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestExpand(t *testing.T) {
|
|
|
|
|
dest := t.TempDir()
|
|
|
|
|
|
|
|
|
|
@ -75,6 +137,28 @@ func TestExpand(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestExpandError(t *testing.T) {
|
|
|
|
|
tests := map[string]struct {
|
|
|
|
|
chartName string
|
|
|
|
|
chartDir string
|
|
|
|
|
wantErr string
|
|
|
|
|
}{
|
|
|
|
|
"dot name": {"dotname", "testdata/dotname", "not allowed"},
|
|
|
|
|
"dotdot name": {"dotdotname", "testdata/dotdotname", "not allowed"},
|
|
|
|
|
"slash in name": {"slashinname", "testdata/slashinname", "must not contain path separators"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for name, tt := range tests {
|
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
|
archive := makeTestChartArchive(t, tt.chartName, tt.chartDir)
|
|
|
|
|
|
|
|
|
|
dest := t.TempDir()
|
|
|
|
|
err := Expand(dest, archive)
|
|
|
|
|
assert.ErrorContains(t, err, tt.wantErr)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestExpandFile(t *testing.T) {
|
|
|
|
|
dest := t.TempDir()
|
|
|
|
|
|
|
|
|
|
|