more tests similar to docker cp
This commit is contained in:
parent
2181c5e6f5
commit
9592f2640f
|
|
@ -98,7 +98,7 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
|
|||
c.snapshotFiles = append(c.snapshotFiles, copiedFiles...)
|
||||
} else if util.IsSymlink(fi) {
|
||||
// If file is a symlink, we want to copy the target file to destPath
|
||||
exclude, target, err := util.CopySymlink(fullPath, destPath, c.buildcontext, uid, gid)
|
||||
exclude, err := util.CopySymlink(fullPath, destPath, c.buildcontext)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "copying symlink")
|
||||
}
|
||||
|
|
@ -106,9 +106,6 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
|
|||
continue
|
||||
}
|
||||
c.snapshotFiles = append(c.snapshotFiles, destPath)
|
||||
if target != "" {
|
||||
c.snapshotFiles = append(c.snapshotFiles, target)
|
||||
}
|
||||
} else {
|
||||
// ... Else, we want to copy over a file
|
||||
exclude, err := util.CopyFile(fullPath, destPath, c.buildcontext, uid, gid)
|
||||
|
|
|
|||
|
|
@ -616,10 +616,9 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
// bam.txt and sym.link should be present
|
||||
testutil.CheckDeepEqual(t, 2, len(files))
|
||||
testutil.CheckDeepEqual(t, files[0].Name(), "dam.txt")
|
||||
testutil.CheckDeepEqual(t, files[1].Name(), "sym.link")
|
||||
testutil.CheckDeepEqual(t, true, files[1].Mode()&os.ModeSymlink != 0)
|
||||
testutil.CheckDeepEqual(t, 1, len(files))
|
||||
testutil.CheckDeepEqual(t, files[0].Name(), "sym.link")
|
||||
testutil.CheckDeepEqual(t, true, files[0].Mode()&os.ModeSymlink != 0)
|
||||
linkName, err := os.Readlink(filepath.Join(testDir, "dest", "sym.link"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
@ -741,7 +740,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) {
|
|||
|
||||
err = cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{}))
|
||||
testutil.CheckNoError(t, err)
|
||||
// Check if "dest" dir exists contests of srcDir and an extra zSym.link created
|
||||
// Check if "dest" dir exists contents of srcDir and an extra zSym.link created
|
||||
// in this test
|
||||
actual, err := ioutil.ReadDir(filepath.Join(testDir, "dest"))
|
||||
if err != nil {
|
||||
|
|
@ -794,9 +793,9 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) {
|
|||
testutil.CheckDeepEqual(t, expected[i].Mode(), f.Mode())
|
||||
}
|
||||
})
|
||||
t.Run("copy file to a dest which is a symlink", func(t *testing.T) {
|
||||
t.Run("copy src dir to a dest dir which is a symlink", func(t *testing.T) {
|
||||
testDir, srcDir := setupDirs(t)
|
||||
//defer os.RemoveAll(testDir)
|
||||
defer os.RemoveAll(testDir)
|
||||
expected, err := ioutil.ReadDir(filepath.Join(testDir, srcDir))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
@ -842,4 +841,50 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) {
|
|||
}
|
||||
testutil.CheckDeepEqual(t, linkName, dest)
|
||||
})
|
||||
t.Run("copy src file to a dest dir which is a symlink", func(t *testing.T) {
|
||||
testDir, srcDir := setupDirs(t)
|
||||
defer os.RemoveAll(testDir)
|
||||
|
||||
dest := filepath.Join(testDir, "dest")
|
||||
if err := os.MkdirAll(dest, 0777); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
linkedDest := filepath.Join(testDir, "linkDest")
|
||||
if err := os.Symlink(dest, linkedDest); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cmd := CopyCommand{
|
||||
cmd: &instructions.CopyCommand{
|
||||
SourcesAndDest: []string{fmt.Sprintf("%s/bam.txt", srcDir), linkedDest},
|
||||
},
|
||||
buildcontext: testDir,
|
||||
}
|
||||
|
||||
cfg := &v1.Config{
|
||||
Cmd: nil,
|
||||
Env: []string{},
|
||||
WorkingDir: testDir,
|
||||
}
|
||||
|
||||
err := cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{}))
|
||||
testutil.CheckNoError(t, err)
|
||||
// Check if "linkDest" link is same.
|
||||
actual, err := ioutil.ReadDir(filepath.Join(testDir, "dest"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
testutil.CheckDeepEqual(t, "bam.txt", actual[0].Name())
|
||||
c, err := ioutil.ReadFile(filepath.Join(testDir, "dest", "bam.txt"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
testutil.CheckDeepEqual(t, "meow", string(c))
|
||||
// Check if linkDest -> dest
|
||||
linkName, err := os.Readlink(filepath.Join(testDir, "linkDest"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
testutil.CheckDeepEqual(t, linkName, dest)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -595,7 +595,7 @@ func CopyDir(src, dest, buildcontext string, uid, gid int64) ([]string, error) {
|
|||
}
|
||||
} else if IsSymlink(fi) {
|
||||
// If file is a symlink, we want to create the same relative symlink
|
||||
if _, _, err := CopySymlink(fullPath, destPath, buildcontext, uid, gid); err != nil {
|
||||
if _, err := CopySymlink(fullPath, destPath, buildcontext); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
|
|
@ -609,32 +609,25 @@ func CopyDir(src, dest, buildcontext string, uid, gid int64) ([]string, error) {
|
|||
return copiedFiles, nil
|
||||
}
|
||||
|
||||
// CopySymlink copies the symlink at src to dest along with the regular file.
|
||||
func CopySymlink(src, dest, buildcontext string, uid int64, gid int64) (bool, string, error) {
|
||||
// CopySymlink copies the symlink at src to dest.
|
||||
func CopySymlink(src, dest, buildcontext string) (bool, error) {
|
||||
if ExcludeFile(src, buildcontext) {
|
||||
logrus.Debugf("%s found in .dockerignore, ignoring", src)
|
||||
return true, "", nil
|
||||
return true, nil
|
||||
}
|
||||
if FilepathExists(dest) {
|
||||
if err := os.RemoveAll(dest); err != nil {
|
||||
return false, "", err
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
if err := createParentDirectory(dest); err != nil {
|
||||
return false, "", err
|
||||
return false, err
|
||||
}
|
||||
target := ""
|
||||
link, err := os.Readlink(src)
|
||||
if err != nil {
|
||||
logrus.Debugf("could not read link for %s", src)
|
||||
} else {
|
||||
cpDest := filepath.Clean(filepath.Join(filepath.Dir(dest), link))
|
||||
srcLink := filepath.Clean(filepath.Join(filepath.Dir(src), link))
|
||||
if t, err := CopyFile(srcLink, cpDest, buildcontext, uid, gid); !t && err != nil {
|
||||
target = link
|
||||
}
|
||||
}
|
||||
return false, target, os.Symlink(link, dest)
|
||||
return false, os.Symlink(link, dest)
|
||||
}
|
||||
|
||||
// CopyFile copies the file at src to dest
|
||||
|
|
|
|||
|
|
@ -835,7 +835,7 @@ func TestCopySymlink(t *testing.T) {
|
|||
if err := os.Symlink(tc.linkTarget, link); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err := CopySymlink(link, dest, "", DoNotChangeUID, DoNotChangeGID); err != nil {
|
||||
if _, err := CopySymlink(link, dest, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Lstat(dest); err != nil {
|
||||
|
|
|
|||
Loading…
Reference in New Issue