Skip to content
This repository has been archived by the owner on May 4, 2021. It is now read-only.

Make ADD untar tar.gz files #309

Merged
merged 3 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/snapshot/copy_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,18 @@ func (c *CopyOperation) Execute() error {
}
defer reader.Close()

dstFi, err := os.Stat(c.dst)
if os.IsNotExist(err) {
if err := os.MkdirAll(c.dst, 0755); err != nil {
return fmt.Errorf("create untar directory: %s", err)
}
if err := os.MkdirAll(c.dst, 0755); err != nil {
return fmt.Errorf("create/validate untar dst: %s", err)
}
if !dstFi.IsDir() {
return fmt.Errorf("target untar path exists and is not directory: %s", c.dst)

gzipReader, err := tario.NewGzipReader(reader)
if err != nil {
return fmt.Errorf("unzip gz %s: %s", src, err)
}
if err := tario.Untar(reader, c.dst); err != nil {
return fmt.Errorf("untar tar: %s", err)
if err := tario.Untar(gzipReader, c.dst); err != nil {
return fmt.Errorf("untar %s to dst %s: %s", src, c.dst, err)
}
return nil
}

var copier fileio.Copier
Expand Down
42 changes: 42 additions & 0 deletions lib/snapshot/copy_op_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
package snapshot

import (
"archive/tar"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"

"github.com/uber/makisu/lib/pathutils"
"github.com/uber/makisu/lib/tario"
"github.com/uber/makisu/lib/utils/testutil"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we make it just two blocks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's two blocks.

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -242,4 +245,43 @@ func TestExecuteCopyOperation(t *testing.T) {
require.NoError(err)
require.Equal(_hello2, b)
})

t.Run("untar gz if created from add step", func(t *testing.T) {
require := require.New(t)

tmpDir, err := ioutil.TempDir("/tmp", "makisu-test")
require.NoError(err)
defer os.RemoveAll(tmpDir)
srcRoot, err := ioutil.TempDir("/tmp", "makisu-test")
require.NoError(err)
defer os.RemoveAll(srcRoot)
workDir, err := ioutil.TempDir("/tmp", "makisu-test")
require.NoError(err)
defer os.RemoveAll(workDir)
dst := "test_dst/" // dst is not absolute and will be placed under workdir.

require.NoError(os.MkdirAll(filepath.Join(tmpDir, "test"), os.ModePerm))
require.NoError(ioutil.WriteFile(filepath.Join(tmpDir, "test", "test.txt"), _hello, os.ModePerm))
require.NoError(os.Chown(filepath.Join(tmpDir, "test", "test.txt"), testutil.CurrUID(), testutil.CurrGID()))

tempGzipTar, err := os.Create(path.Join(srcRoot, "test_add.tar.gz"))
require.NoError(err)
gzipper, err := tario.NewGzipWriter(tempGzipTar)
require.NoError(err)
tarWriter := tar.NewWriter(gzipper)
require.NoError(writeTarHelper(tmpDir, tarWriter))
tarWriter.Close()
gzipper.Close()
tempGzipTar.Close()

srcs := []string{"test_add.tar.gz"}
c, err := NewCopyOperation(
srcs, srcRoot, workDir, dst, validChown, false, false, pathutils.DefaultBlacklist, true)
require.NoError(err)
require.NoError(c.Execute())

b, err := ioutil.ReadFile(filepath.Join(workDir, dst, "test", "test.txt"))
require.NoError(err)
require.Equal(_hello, b)
})
}
6 changes: 5 additions & 1 deletion lib/snapshot/testutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func findNode(fs *MemFS, p string, followSymlink bool, depth int) (*memFSNode, e
return nil, os.ErrNotExist
}

func writeTarHelper(m *memFile, srcRoot string, w *tar.Writer) error {
func writeTarHelper(srcRoot string, w *tar.Writer) error {
return filepath.Walk(srcRoot, func(p string, fi os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -250,6 +250,10 @@ func writeTarHelper(m *memFile, srcRoot string, w *tar.Writer) error {
return err
}

if h.Name, err = filepath.Rel(srcRoot, p); err != nil {
return err
}

return tario.WriteEntry(w, p, h)
})
}
Expand Down