//go:build linux // +build linux package copy // import "github.com/docker/docker/daemon/graphdriver/copy" import ( "fmt" "math/rand" "os" "path/filepath" "syscall" "testing" "time" "github.com/docker/docker/pkg/system" "golang.org/x/sys/unix" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" ) func TestCopy(t *testing.T) { copyWithFileRange := true copyWithFileClone := true doCopyTest(t, ©WithFileRange, ©WithFileClone) } func TestCopyWithoutRange(t *testing.T) { copyWithFileRange := false copyWithFileClone := false doCopyTest(t, ©WithFileRange, ©WithFileClone) } func TestCopyDir(t *testing.T) { srcDir, err := os.MkdirTemp("", "srcDir") assert.NilError(t, err) defer os.RemoveAll(srcDir) populateSrcDir(t, srcDir, 3) dstDir, err := os.MkdirTemp("", "testdst") assert.NilError(t, err) defer os.RemoveAll(dstDir) assert.Check(t, DirCopy(srcDir, dstDir, Content, false)) assert.NilError(t, filepath.Walk(srcDir, func(srcPath string, f os.FileInfo, err error) error { if err != nil { return err } // Rebase path relPath, err := filepath.Rel(srcDir, srcPath) assert.NilError(t, err) if relPath == "." { return nil } dstPath := filepath.Join(dstDir, relPath) // If we add non-regular dirs and files to the test // then we need to add more checks here. dstFileInfo, err := os.Lstat(dstPath) assert.NilError(t, err) srcFileSys := f.Sys().(*syscall.Stat_t) dstFileSys := dstFileInfo.Sys().(*syscall.Stat_t) t.Log(relPath) if srcFileSys.Dev == dstFileSys.Dev { assert.Check(t, srcFileSys.Ino != dstFileSys.Ino) } // Todo: check size, and ctim is not equal on filesystems that have granular ctimes assert.Check(t, is.DeepEqual(srcFileSys.Mode, dstFileSys.Mode)) assert.Check(t, is.DeepEqual(srcFileSys.Uid, dstFileSys.Uid)) assert.Check(t, is.DeepEqual(srcFileSys.Gid, dstFileSys.Gid)) assert.Check(t, is.DeepEqual(srcFileSys.Mtim, dstFileSys.Mtim)) return nil })) } func randomMode(baseMode int) os.FileMode { for i := 0; i < 7; i++ { baseMode = baseMode | (1&rand.Intn(2))<