Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
tejal29 committed Mar 6, 2020
1 parent 861c039 commit b4b70d0
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 18 deletions.
13 changes: 6 additions & 7 deletions pkg/commands/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,13 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
return err
}

// If the destination dir is a symlink we need to resolve the path and use
// that instead of the symlink path
destPath, err = resolveIfSymlink(destPath)
if err != nil {
return err
}

if fi.IsDir() {
// If the destination dir is a symlink we need to resolve the path and use
// that instead of the symlink path
destPath, err = resolveIfSymlink(destPath)
if err != nil {
return err
}
copiedFiles, err := util.CopyDir(fullPath, destPath, c.buildcontext, uid, gid)
if err != nil {
return err
Expand Down
136 changes: 136 additions & 0 deletions pkg/commands/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,139 @@ func TestGetUserGroup(t *testing.T) {
})
}
}

func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) {
setupDirs := func(t *testing.T) (string, string) {
testDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}

targetDir := filepath.Join(testDir, "bar")

if err := os.MkdirAll(targetDir, 0777); err != nil {
t.Fatal(err)
}

targetPath := filepath.Join(targetDir, "bam.txt")

if err := ioutil.WriteFile(targetPath, []byte("woof"), 0777); err != nil {
t.Fatal(err)
}

return testDir, filepath.Base(targetDir)
}

t.Run("copy dir to another dir", func(t *testing.T) {
testDir, targetDir := setupDirs(t)
defer os.RemoveAll(testDir)

cmd := CopyCommand{
cmd: &instructions.CopyCommand{
SourcesAndDest: []string{targetDir, "dest"},
},
buildcontext: testDir,
}

cfg := &v1.Config{
Cmd: nil,
Env: []string{},
WorkingDir: testDir,
}

err := cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{}))
testutil.CheckNoError(t, err)
})

t.Run("copy file to a dir", func(t *testing.T) {
testDir, targetDir := setupDirs(t)
defer os.RemoveAll(testDir)
cmd := CopyCommand{
cmd: &instructions.CopyCommand{
SourcesAndDest: []string{filepath.Join(targetDir, "bam.txt"), "dest/"},
},
buildcontext: testDir,
}

cfg := &v1.Config{
Cmd: nil,
Env: []string{},
WorkingDir: testDir,
}

err := cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{}))
testutil.CheckNoError(t, err)
})

t.Run("copy file to a filepath", func(t *testing.T) {
testDir, targetDir := setupDirs(t)
defer os.RemoveAll(testDir)
cmd := CopyCommand{
cmd: &instructions.CopyCommand{
SourcesAndDest: []string{filepath.Join(targetDir, "bam.txt"), "dest"},
},
buildcontext: testDir,
}

cfg := &v1.Config{
Cmd: nil,
Env: []string{},
WorkingDir: testDir,
}

err := cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{}))
testutil.CheckNoError(t, err)
})
t.Run("copy file to a dir without trailing /", func(t *testing.T) {
testDir, targetDir := setupDirs(t)
defer os.RemoveAll(testDir)

destDir := filepath.Join(testDir, "dest")
if err := os.MkdirAll(destDir, 0777); err != nil {
t.Fatal(err)
}

cmd := CopyCommand{
cmd: &instructions.CopyCommand{
SourcesAndDest: []string{filepath.Join(targetDir, "bam.txt"), "dest"},
},
buildcontext: testDir,
}

cfg := &v1.Config{
Cmd: nil,
Env: []string{},
WorkingDir: testDir,
}

err := cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{}))
testutil.CheckNoError(t, err)
})
t.Run("copy symlink file to a dir", func(t *testing.T) {
testDir, targetDir := setupDirs(t)
defer os.RemoveAll(testDir)

another := filepath.Join(testDir, "another")
if err := os.MkdirAll(another, 0777); err != nil {
t.Fatal(err)
}
symlink := filepath.Join(another, "sym.link")
os.Symlink(filepath.Join(targetDir, "bam.txt"), symlink)

cmd := CopyCommand{
cmd: &instructions.CopyCommand{
SourcesAndDest: []string{symlink, "dest"},
},
buildcontext: testDir,
}

cfg := &v1.Config{
Cmd: nil,
Env: []string{},
WorkingDir: testDir,
}

err := cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{}))
testutil.CheckNoError(t, err)
})
}
4 changes: 3 additions & 1 deletion pkg/filesystem/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package filesystem

import (
"fmt"
"os"
"path/filepath"

Expand Down Expand Up @@ -126,14 +127,15 @@ func resolveSymlinkAncestor(path string) (string, error) {
return "", errors.New("dest path must be abs")
}

fmt.Println(path)
last := ""
newPath := filepath.Clean(path)

loop:
for newPath != "/" {
fi, err := os.Lstat(newPath)
if err != nil {
return "", errors.Wrap(err, "failed to lstat")
return "", errors.Wrap(err, "resolvePaths: failed to lstat")
}

if util.IsSymlink(fi) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/util/command_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ func DestinationFilepath(src, dest, cwd string) (string, error) {
_, srcFileName := filepath.Split(src)
newDest := dest

if IsDestDir(newDest) {
newDest = filepath.Join(newDest, srcFileName)
}

if !filepath.IsAbs(newDest) {
newDest = filepath.Join(cwd, newDest)
}

if IsDestDir(newDest) {
newDest = filepath.Join(newDest, srcFileName)
}

if len(srcFileName) <= 0 && !strings.HasSuffix(newDest, "/") {
newDest += "/"
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/util/fs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,15 +505,16 @@ func FilepathExists(path string) bool {
func CreateFile(path string, reader io.Reader, perm os.FileMode, uid uint32, gid uint32) error {
// Create directory path if it doesn't exist
if err := createParentDirectory(path); err != nil {
return err
return errors.Wrap(err, "creating parent dir")
}

dest, err := os.Create(path)
if err != nil {
return err
return errors.Wrap(err, "creating file")
}
defer dest.Close()
if _, err := io.Copy(dest, reader); err != nil {
return err
return errors.Wrap(err, "copying file")
}
return setFilePermissions(path, perm, int(uid), int(gid))
}
Expand Down Expand Up @@ -614,9 +615,9 @@ func CopySymlink(src, dest, buildcontext string, uid int64, gid int64) (bool, er
logrus.Debugf("%s found in .dockerignore, ignoring", src)
return true, nil
}
link, err := filepath.EvalSymlinks(src)
link, err := os.Readlink(src)
if err != nil {
return false, err
logrus.Debugf("could not evaluate %s, probably a dead link", src)
}
if FilepathExists(dest) {
if err := os.RemoveAll(dest); err != nil {
Expand All @@ -626,7 +627,7 @@ func CopySymlink(src, dest, buildcontext string, uid int64, gid int64) (bool, er
if err := createParentDirectory(dest); err != nil {
return false, err
}
return CopyFile(link, dest, buildcontext, uid, gid)
return true, os.Symlink(link, dest)
}

// CopyFile copies the file at src to dest
Expand Down
6 changes: 6 additions & 0 deletions testutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func CheckError(t *testing.T, shouldErr bool, err error) {
}
}

func CheckNoError(t *testing.T, err error) {
if err != nil {
t.Error(err)
}
}

func checkErr(shouldErr bool, err error) error {
if err == nil && shouldErr {
return fmt.Errorf("Expected error, but returned none")
Expand Down

0 comments on commit b4b70d0

Please sign in to comment.