2018-02-05 16:05:59 -05:00
|
|
|
package archive // import "github.com/docker/docker/pkg/archive"
|
2013-11-14 16:59:04 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
2016-02-12 16:58:57 -05:00
|
|
|
"runtime"
|
2013-11-14 16:59:04 -05:00
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2016-02-08 18:40:12 -05:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/system"
|
2018-03-13 15:28:34 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
2018-04-20 05:59:08 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/skip"
|
2013-11-14 16:59:04 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func max(x, y int) int {
|
|
|
|
if x >= y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyDir(src, dst string) error {
|
2017-10-24 14:32:52 -04:00
|
|
|
return exec.Command("cp", "-a", src, dst).Run()
|
2013-11-14 16:59:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type FileType uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
Regular FileType = iota
|
|
|
|
Dir
|
|
|
|
Symlink
|
|
|
|
)
|
|
|
|
|
|
|
|
type FileData struct {
|
|
|
|
filetype FileType
|
|
|
|
path string
|
|
|
|
contents string
|
|
|
|
permissions os.FileMode
|
|
|
|
}
|
|
|
|
|
|
|
|
func createSampleDir(t *testing.T, root string) {
|
|
|
|
files := []FileData{
|
2017-03-19 22:39:40 -04:00
|
|
|
{filetype: Regular, path: "file1", contents: "file1\n", permissions: 0600},
|
|
|
|
{filetype: Regular, path: "file2", contents: "file2\n", permissions: 0666},
|
|
|
|
{filetype: Regular, path: "file3", contents: "file3\n", permissions: 0404},
|
|
|
|
{filetype: Regular, path: "file4", contents: "file4\n", permissions: 0600},
|
|
|
|
{filetype: Regular, path: "file5", contents: "file5\n", permissions: 0600},
|
|
|
|
{filetype: Regular, path: "file6", contents: "file6\n", permissions: 0600},
|
|
|
|
{filetype: Regular, path: "file7", contents: "file7\n", permissions: 0600},
|
|
|
|
{filetype: Dir, path: "dir1", contents: "", permissions: 0740},
|
|
|
|
{filetype: Regular, path: "dir1/file1-1", contents: "file1-1\n", permissions: 01444},
|
|
|
|
{filetype: Regular, path: "dir1/file1-2", contents: "file1-2\n", permissions: 0666},
|
|
|
|
{filetype: Dir, path: "dir2", contents: "", permissions: 0700},
|
|
|
|
{filetype: Regular, path: "dir2/file2-1", contents: "file2-1\n", permissions: 0666},
|
|
|
|
{filetype: Regular, path: "dir2/file2-2", contents: "file2-2\n", permissions: 0666},
|
|
|
|
{filetype: Dir, path: "dir3", contents: "", permissions: 0700},
|
|
|
|
{filetype: Regular, path: "dir3/file3-1", contents: "file3-1\n", permissions: 0666},
|
|
|
|
{filetype: Regular, path: "dir3/file3-2", contents: "file3-2\n", permissions: 0666},
|
|
|
|
{filetype: Dir, path: "dir4", contents: "", permissions: 0700},
|
|
|
|
{filetype: Regular, path: "dir4/file3-1", contents: "file4-1\n", permissions: 0666},
|
|
|
|
{filetype: Regular, path: "dir4/file3-2", contents: "file4-2\n", permissions: 0666},
|
|
|
|
{filetype: Symlink, path: "symlink1", contents: "target1", permissions: 0666},
|
|
|
|
{filetype: Symlink, path: "symlink2", contents: "target2", permissions: 0666},
|
|
|
|
{filetype: Symlink, path: "symlink3", contents: root + "/file1", permissions: 0666},
|
|
|
|
{filetype: Symlink, path: "symlink4", contents: root + "/symlink3", permissions: 0666},
|
|
|
|
{filetype: Symlink, path: "dirSymlink", contents: root + "/dir1", permissions: 0740},
|
2013-11-14 16:59:04 -05:00
|
|
|
}
|
2017-03-19 22:39:40 -04:00
|
|
|
provisionSampleDir(t, root, files)
|
|
|
|
}
|
2013-11-25 22:58:14 -05:00
|
|
|
|
2017-03-19 22:39:40 -04:00
|
|
|
func provisionSampleDir(t *testing.T, root string, files []FileData) {
|
2013-11-25 22:58:14 -05:00
|
|
|
now := time.Now()
|
2013-11-14 16:59:04 -05:00
|
|
|
for _, info := range files {
|
2013-11-25 22:58:14 -05:00
|
|
|
p := path.Join(root, info.path)
|
2013-11-14 16:59:04 -05:00
|
|
|
if info.filetype == Dir {
|
2017-08-08 22:27:01 -04:00
|
|
|
err := os.MkdirAll(p, info.permissions)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
} else if info.filetype == Regular {
|
2017-08-08 22:27:01 -04:00
|
|
|
err := ioutil.WriteFile(p, []byte(info.contents), info.permissions)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
} else if info.filetype == Symlink {
|
2017-08-08 22:27:01 -04:00
|
|
|
err := os.Symlink(info.contents, p)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-25 22:58:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if info.filetype != Symlink {
|
|
|
|
// Set a consistent ctime, atime for all files and dirs
|
2017-08-08 22:27:01 -04:00
|
|
|
err := system.Chtimes(p, now, now)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 11:03:33 -04:00
|
|
|
func TestChangeString(t *testing.T) {
|
2017-05-21 19:24:07 -04:00
|
|
|
modifyChange := Change{"change", ChangeModify}
|
|
|
|
toString := modifyChange.String()
|
2015-04-24 11:03:33 -04:00
|
|
|
if toString != "C change" {
|
2017-05-21 19:24:07 -04:00
|
|
|
t.Fatalf("String() of a change with ChangeModify Kind should have been %s but was %s", "C change", toString)
|
2015-04-24 11:03:33 -04:00
|
|
|
}
|
|
|
|
addChange := Change{"change", ChangeAdd}
|
|
|
|
toString = addChange.String()
|
|
|
|
if toString != "A change" {
|
|
|
|
t.Fatalf("String() of a change with ChangeAdd Kind should have been %s but was %s", "A change", toString)
|
|
|
|
}
|
|
|
|
deleteChange := Change{"change", ChangeDelete}
|
|
|
|
toString = deleteChange.String()
|
|
|
|
if toString != "D change" {
|
|
|
|
t.Fatalf("String() of a change with ChangeDelete Kind should have been %s but was %s", "D change", toString)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangesWithNoChanges(t *testing.T) {
|
2016-02-12 16:58:57 -05:00
|
|
|
// TODO Windows. There may be a way of running this, but turning off for now
|
|
|
|
// as createSampleDir uses symlinks.
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("symlinks on Windows")
|
|
|
|
}
|
2015-04-24 11:03:33 -04:00
|
|
|
rwLayer, err := ioutil.TempDir("", "docker-changes-test")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 11:03:33 -04:00
|
|
|
defer os.RemoveAll(rwLayer)
|
|
|
|
layer, err := ioutil.TempDir("", "docker-changes-test-layer")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 11:03:33 -04:00
|
|
|
defer os.RemoveAll(layer)
|
|
|
|
createSampleDir(t, layer)
|
|
|
|
changes, err := Changes([]string{layer}, rwLayer)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 11:03:33 -04:00
|
|
|
if len(changes) != 0 {
|
|
|
|
t.Fatalf("Changes with no difference should have detect no changes, but detected %d", len(changes))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangesWithChanges(t *testing.T) {
|
2016-02-12 16:58:57 -05:00
|
|
|
// TODO Windows. There may be a way of running this, but turning off for now
|
|
|
|
// as createSampleDir uses symlinks.
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("symlinks on Windows")
|
|
|
|
}
|
2015-05-29 04:39:14 -04:00
|
|
|
// Mock the readonly layer
|
|
|
|
layer, err := ioutil.TempDir("", "docker-changes-test-layer")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-05-29 04:39:14 -04:00
|
|
|
defer os.RemoveAll(layer)
|
|
|
|
createSampleDir(t, layer)
|
|
|
|
os.MkdirAll(path.Join(layer, "dir1/subfolder"), 0740)
|
|
|
|
|
|
|
|
// Mock the RW layer
|
2015-04-24 11:03:33 -04:00
|
|
|
rwLayer, err := ioutil.TempDir("", "docker-changes-test")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 11:03:33 -04:00
|
|
|
defer os.RemoveAll(rwLayer)
|
2015-05-29 04:39:14 -04:00
|
|
|
|
|
|
|
// Create a folder in RW layer
|
2015-04-24 11:03:33 -04:00
|
|
|
dir1 := path.Join(rwLayer, "dir1")
|
|
|
|
os.MkdirAll(dir1, 0740)
|
|
|
|
deletedFile := path.Join(dir1, ".wh.file1-2")
|
|
|
|
ioutil.WriteFile(deletedFile, []byte{}, 0600)
|
|
|
|
modifiedFile := path.Join(dir1, "file1-1")
|
|
|
|
ioutil.WriteFile(modifiedFile, []byte{0x00}, 01444)
|
|
|
|
// Let's add a subfolder for a newFile
|
|
|
|
subfolder := path.Join(dir1, "subfolder")
|
|
|
|
os.MkdirAll(subfolder, 0740)
|
|
|
|
newFile := path.Join(subfolder, "newFile")
|
|
|
|
ioutil.WriteFile(newFile, []byte{}, 0740)
|
2015-05-29 04:39:14 -04:00
|
|
|
|
|
|
|
changes, err := Changes([]string{layer}, rwLayer)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-05-29 04:39:14 -04:00
|
|
|
|
|
|
|
expectedChanges := []Change{
|
|
|
|
{"/dir1", ChangeModify},
|
|
|
|
{"/dir1/file1-1", ChangeModify},
|
|
|
|
{"/dir1/file1-2", ChangeDelete},
|
|
|
|
{"/dir1/subfolder", ChangeModify},
|
|
|
|
{"/dir1/subfolder/newFile", ChangeAdd},
|
|
|
|
}
|
|
|
|
checkChanges(expectedChanges, changes, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// See https://github.com/docker/docker/pull/13590
|
|
|
|
func TestChangesWithChangesGH13590(t *testing.T) {
|
2016-02-12 16:58:57 -05:00
|
|
|
// TODO Windows. There may be a way of running this, but turning off for now
|
|
|
|
// as createSampleDir uses symlinks.
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("symlinks on Windows")
|
|
|
|
}
|
2015-05-29 04:39:14 -04:00
|
|
|
baseLayer, err := ioutil.TempDir("", "docker-changes-test.")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-05-29 04:39:14 -04:00
|
|
|
defer os.RemoveAll(baseLayer)
|
|
|
|
|
|
|
|
dir3 := path.Join(baseLayer, "dir1/dir2/dir3")
|
|
|
|
os.MkdirAll(dir3, 07400)
|
|
|
|
|
|
|
|
file := path.Join(dir3, "file.txt")
|
|
|
|
ioutil.WriteFile(file, []byte("hello"), 0666)
|
|
|
|
|
|
|
|
layer, err := ioutil.TempDir("", "docker-changes-test2.")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 11:03:33 -04:00
|
|
|
defer os.RemoveAll(layer)
|
|
|
|
|
2015-05-29 04:39:14 -04:00
|
|
|
// Test creating a new file
|
|
|
|
if err := copyDir(baseLayer+"/dir1", layer+"/"); err != nil {
|
|
|
|
t.Fatalf("Cmd failed: %q", err)
|
2015-04-24 11:03:33 -04:00
|
|
|
}
|
|
|
|
|
2015-05-29 04:39:14 -04:00
|
|
|
os.Remove(path.Join(layer, "dir1/dir2/dir3/file.txt"))
|
|
|
|
file = path.Join(layer, "dir1/dir2/dir3/file1.txt")
|
|
|
|
ioutil.WriteFile(file, []byte("bye"), 0666)
|
2015-04-24 11:03:33 -04:00
|
|
|
|
2015-05-29 04:39:14 -04:00
|
|
|
changes, err := Changes([]string{baseLayer}, layer)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 11:03:33 -04:00
|
|
|
|
|
|
|
expectedChanges := []Change{
|
2015-05-29 04:39:14 -04:00
|
|
|
{"/dir1/dir2/dir3", ChangeModify},
|
|
|
|
{"/dir1/dir2/dir3/file1.txt", ChangeAdd},
|
2015-04-24 11:03:33 -04:00
|
|
|
}
|
2015-05-29 04:39:14 -04:00
|
|
|
checkChanges(expectedChanges, changes, t)
|
2015-04-24 11:03:33 -04:00
|
|
|
|
2015-05-29 04:39:14 -04:00
|
|
|
// Now test changing a file
|
|
|
|
layer, err = ioutil.TempDir("", "docker-changes-test3.")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-05-29 04:39:14 -04:00
|
|
|
defer os.RemoveAll(layer)
|
|
|
|
|
|
|
|
if err := copyDir(baseLayer+"/dir1", layer+"/"); err != nil {
|
|
|
|
t.Fatalf("Cmd failed: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
file = path.Join(layer, "dir1/dir2/dir3/file.txt")
|
|
|
|
ioutil.WriteFile(file, []byte("bye"), 0666)
|
|
|
|
|
|
|
|
changes, err = Changes([]string{baseLayer}, layer)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-05-29 04:39:14 -04:00
|
|
|
|
|
|
|
expectedChanges = []Change{
|
|
|
|
{"/dir1/dir2/dir3/file.txt", ChangeModify},
|
|
|
|
}
|
|
|
|
checkChanges(expectedChanges, changes, t)
|
2015-04-24 11:03:33 -04:00
|
|
|
}
|
|
|
|
|
2016-05-07 21:36:10 -04:00
|
|
|
// Create a directory, copy it, make sure we report no changes between the two
|
2013-11-14 16:59:04 -05:00
|
|
|
func TestChangesDirsEmpty(t *testing.T) {
|
2016-02-12 16:58:57 -05:00
|
|
|
// TODO Windows. There may be a way of running this, but turning off for now
|
|
|
|
// as createSampleDir uses symlinks.
|
2017-10-24 14:32:52 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("symlinks on Windows")
|
2016-02-12 16:58:57 -05:00
|
|
|
}
|
2013-11-14 16:59:04 -05:00
|
|
|
src, err := ioutil.TempDir("", "docker-changes-test")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 11:03:33 -04:00
|
|
|
defer os.RemoveAll(src)
|
2013-11-14 16:59:04 -05:00
|
|
|
createSampleDir(t, src)
|
|
|
|
dst := src + "-copy"
|
2017-08-08 22:27:01 -04:00
|
|
|
err = copyDir(src, dst)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 11:03:33 -04:00
|
|
|
defer os.RemoveAll(dst)
|
2013-11-14 16:59:04 -05:00
|
|
|
changes, err := ChangesDirs(dst, src)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
|
|
|
|
if len(changes) != 0 {
|
|
|
|
t.Fatalf("Reported changes for identical dirs: %v", changes)
|
|
|
|
}
|
|
|
|
os.RemoveAll(src)
|
|
|
|
os.RemoveAll(dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mutateSampleDir(t *testing.T, root string) {
|
|
|
|
// Remove a regular file
|
2017-08-08 22:27:01 -04:00
|
|
|
err := os.RemoveAll(path.Join(root, "file1"))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
|
|
|
|
// Remove a directory
|
2017-08-08 22:27:01 -04:00
|
|
|
err = os.RemoveAll(path.Join(root, "dir1"))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
|
|
|
|
// Remove a symlink
|
2017-08-08 22:27:01 -04:00
|
|
|
err = os.RemoveAll(path.Join(root, "symlink1"))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
|
|
|
|
// Rewrite a file
|
2017-08-08 22:27:01 -04:00
|
|
|
err = ioutil.WriteFile(path.Join(root, "file2"), []byte("fileNN\n"), 0777)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
|
|
|
|
// Replace a file
|
2017-08-08 22:27:01 -04:00
|
|
|
err = os.RemoveAll(path.Join(root, "file3"))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-08-08 22:27:01 -04:00
|
|
|
err = ioutil.WriteFile(path.Join(root, "file3"), []byte("fileMM\n"), 0404)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
|
|
|
|
// Touch file
|
2017-08-08 22:27:01 -04:00
|
|
|
err = system.Chtimes(path.Join(root, "file4"), time.Now().Add(time.Second), time.Now().Add(time.Second))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
|
|
|
|
// Replace file with dir
|
2017-08-08 22:27:01 -04:00
|
|
|
err = os.RemoveAll(path.Join(root, "file5"))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-08-08 22:27:01 -04:00
|
|
|
err = os.MkdirAll(path.Join(root, "file5"), 0666)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
|
|
|
|
// Create new file
|
2017-08-08 22:27:01 -04:00
|
|
|
err = ioutil.WriteFile(path.Join(root, "filenew"), []byte("filenew\n"), 0777)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
|
|
|
|
// Create new dir
|
2017-08-08 22:27:01 -04:00
|
|
|
err = os.MkdirAll(path.Join(root, "dirnew"), 0766)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
|
|
|
|
// Create a new symlink
|
2017-08-08 22:27:01 -04:00
|
|
|
err = os.Symlink("targetnew", path.Join(root, "symlinknew"))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
|
|
|
|
// Change a symlink
|
2017-08-08 22:27:01 -04:00
|
|
|
err = os.RemoveAll(path.Join(root, "symlink2"))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-08-08 22:27:01 -04:00
|
|
|
|
|
|
|
err = os.Symlink("target2change", path.Join(root, "symlink2"))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
|
|
|
|
// Replace dir with file
|
2017-08-08 22:27:01 -04:00
|
|
|
err = os.RemoveAll(path.Join(root, "dir2"))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-08-08 22:27:01 -04:00
|
|
|
err = ioutil.WriteFile(path.Join(root, "dir2"), []byte("dir2\n"), 0777)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
|
|
|
|
// Touch dir
|
2017-08-08 22:27:01 -04:00
|
|
|
err = system.Chtimes(path.Join(root, "dir3"), time.Now().Add(time.Second), time.Now().Add(time.Second))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangesDirsMutated(t *testing.T) {
|
2016-02-12 16:58:57 -05:00
|
|
|
// TODO Windows. There may be a way of running this, but turning off for now
|
|
|
|
// as createSampleDir uses symlinks.
|
2017-10-24 14:32:52 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("symlinks on Windows")
|
2016-02-12 16:58:57 -05:00
|
|
|
}
|
2013-11-14 16:59:04 -05:00
|
|
|
src, err := ioutil.TempDir("", "docker-changes-test")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
createSampleDir(t, src)
|
|
|
|
dst := src + "-copy"
|
2017-08-08 22:27:01 -04:00
|
|
|
err = copyDir(src, dst)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-25 22:58:14 -05:00
|
|
|
defer os.RemoveAll(src)
|
|
|
|
defer os.RemoveAll(dst)
|
|
|
|
|
2013-11-14 16:59:04 -05:00
|
|
|
mutateSampleDir(t, dst)
|
|
|
|
|
|
|
|
changes, err := ChangesDirs(dst, src)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-11-14 16:59:04 -05:00
|
|
|
|
2015-02-05 05:48:58 -05:00
|
|
|
sort.Sort(changesByPath(changes))
|
2013-11-14 16:59:04 -05:00
|
|
|
|
|
|
|
expectedChanges := []Change{
|
|
|
|
{"/dir1", ChangeDelete},
|
|
|
|
{"/dir2", ChangeModify},
|
|
|
|
{"/dirnew", ChangeAdd},
|
|
|
|
{"/file1", ChangeDelete},
|
|
|
|
{"/file2", ChangeModify},
|
|
|
|
{"/file3", ChangeModify},
|
|
|
|
{"/file4", ChangeModify},
|
|
|
|
{"/file5", ChangeModify},
|
|
|
|
{"/filenew", ChangeAdd},
|
|
|
|
{"/symlink1", ChangeDelete},
|
|
|
|
{"/symlink2", ChangeModify},
|
|
|
|
{"/symlinknew", ChangeAdd},
|
|
|
|
}
|
|
|
|
|
2013-11-25 22:58:14 -05:00
|
|
|
for i := 0; i < max(len(changes), len(expectedChanges)); i++ {
|
2013-11-14 16:59:04 -05:00
|
|
|
if i >= len(expectedChanges) {
|
|
|
|
t.Fatalf("unexpected change %s\n", changes[i].String())
|
|
|
|
}
|
|
|
|
if i >= len(changes) {
|
|
|
|
t.Fatalf("no change for expected change %s\n", expectedChanges[i].String())
|
|
|
|
}
|
|
|
|
if changes[i].Path == expectedChanges[i].Path {
|
|
|
|
if changes[i] != expectedChanges[i] {
|
2013-11-29 19:53:20 -05:00
|
|
|
t.Fatalf("Wrong change for %s, expected %s, got %s\n", changes[i].Path, changes[i].String(), expectedChanges[i].String())
|
2013-11-14 16:59:04 -05:00
|
|
|
}
|
|
|
|
} else if changes[i].Path < expectedChanges[i].Path {
|
|
|
|
t.Fatalf("unexpected change %s\n", changes[i].String())
|
|
|
|
} else {
|
2013-11-25 22:58:14 -05:00
|
|
|
t.Fatalf("no change for expected change %s != %s\n", expectedChanges[i].String(), changes[i].String())
|
2013-11-14 16:59:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApplyLayer(t *testing.T) {
|
2016-02-12 16:58:57 -05:00
|
|
|
// TODO Windows. There may be a way of running this, but turning off for now
|
|
|
|
// as createSampleDir uses symlinks.
|
2017-10-24 14:32:52 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("symlinks on Windows")
|
2016-02-12 16:58:57 -05:00
|
|
|
}
|
2013-12-13 09:50:25 -05:00
|
|
|
src, err := ioutil.TempDir("", "docker-changes-test")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-12-13 09:50:25 -05:00
|
|
|
createSampleDir(t, src)
|
|
|
|
defer os.RemoveAll(src)
|
|
|
|
dst := src + "-copy"
|
2017-08-08 22:27:01 -04:00
|
|
|
err = copyDir(src, dst)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-12-13 09:50:25 -05:00
|
|
|
mutateSampleDir(t, dst)
|
|
|
|
defer os.RemoveAll(dst)
|
|
|
|
|
|
|
|
changes, err := ChangesDirs(dst, src)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-12-13 09:50:25 -05:00
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
layer, err := ExportChanges(dst, changes, nil, nil)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-12-13 09:50:25 -05:00
|
|
|
|
|
|
|
layerCopy, err := NewTempArchive(layer, "")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-12-13 09:50:25 -05:00
|
|
|
|
2017-08-08 22:27:01 -04:00
|
|
|
_, err = ApplyLayer(src, layerCopy)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-12-13 09:50:25 -05:00
|
|
|
|
|
|
|
changes2, err := ChangesDirs(src, dst)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2013-12-13 09:50:25 -05:00
|
|
|
|
|
|
|
if len(changes2) != 0 {
|
2013-12-24 19:37:00 -05:00
|
|
|
t.Fatalf("Unexpected differences after reapplying mutation: %v", changes2)
|
2013-12-13 09:50:25 -05:00
|
|
|
}
|
2013-11-14 16:59:04 -05:00
|
|
|
}
|
2015-04-24 11:03:33 -04:00
|
|
|
|
2015-10-12 15:11:22 -04:00
|
|
|
func TestChangesSizeWithHardlinks(t *testing.T) {
|
2016-02-12 16:58:57 -05:00
|
|
|
// TODO Windows. There may be a way of running this, but turning off for now
|
|
|
|
// as createSampleDir uses symlinks.
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("hardlinks on Windows")
|
|
|
|
}
|
2015-10-12 15:11:22 -04:00
|
|
|
srcDir, err := ioutil.TempDir("", "docker-test-srcDir")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-10-12 15:11:22 -04:00
|
|
|
defer os.RemoveAll(srcDir)
|
|
|
|
|
|
|
|
destDir, err := ioutil.TempDir("", "docker-test-destDir")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-10-12 15:11:22 -04:00
|
|
|
defer os.RemoveAll(destDir)
|
|
|
|
|
|
|
|
creationSize, err := prepareUntarSourceDirectory(100, destDir, true)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-10-12 15:11:22 -04:00
|
|
|
|
|
|
|
changes, err := ChangesDirs(destDir, srcDir)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-10-12 15:11:22 -04:00
|
|
|
|
|
|
|
got := ChangesSize(destDir, changes)
|
|
|
|
if got != int64(creationSize) {
|
|
|
|
t.Errorf("Expected %d bytes of changes, got %d", creationSize, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 11:03:33 -04:00
|
|
|
func TestChangesSizeWithNoChanges(t *testing.T) {
|
|
|
|
size := ChangesSize("/tmp", nil)
|
|
|
|
if size != 0 {
|
|
|
|
t.Fatalf("ChangesSizes with no changes should be 0, was %d", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangesSizeWithOnlyDeleteChanges(t *testing.T) {
|
|
|
|
changes := []Change{
|
|
|
|
{Path: "deletedPath", Kind: ChangeDelete},
|
|
|
|
}
|
|
|
|
size := ChangesSize("/tmp", changes)
|
|
|
|
if size != 0 {
|
|
|
|
t.Fatalf("ChangesSizes with only delete changes should be 0, was %d", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangesSize(t *testing.T) {
|
|
|
|
parentPath, err := ioutil.TempDir("", "docker-changes-test")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 11:03:33 -04:00
|
|
|
defer os.RemoveAll(parentPath)
|
|
|
|
addition := path.Join(parentPath, "addition")
|
2017-08-08 22:27:01 -04:00
|
|
|
err = ioutil.WriteFile(addition, []byte{0x01, 0x01, 0x01}, 0744)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 11:03:33 -04:00
|
|
|
modification := path.Join(parentPath, "modification")
|
2017-08-08 22:27:01 -04:00
|
|
|
err = ioutil.WriteFile(modification, []byte{0x01, 0x01, 0x01}, 0744)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-08-08 22:27:01 -04:00
|
|
|
|
2015-04-24 11:03:33 -04:00
|
|
|
changes := []Change{
|
|
|
|
{Path: "addition", Kind: ChangeAdd},
|
|
|
|
{Path: "modification", Kind: ChangeModify},
|
|
|
|
}
|
|
|
|
size := ChangesSize(parentPath, changes)
|
|
|
|
if size != 6 {
|
2015-10-12 15:11:22 -04:00
|
|
|
t.Fatalf("Expected 6 bytes of changes, got %d", size)
|
2015-04-24 11:03:33 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-29 04:39:14 -04:00
|
|
|
|
|
|
|
func checkChanges(expectedChanges, changes []Change, t *testing.T) {
|
2018-04-20 05:59:08 -04:00
|
|
|
skip.If(t, os.Getuid() != 0, "skipping test that requires root")
|
2015-05-29 04:39:14 -04:00
|
|
|
sort.Sort(changesByPath(expectedChanges))
|
|
|
|
sort.Sort(changesByPath(changes))
|
|
|
|
for i := 0; i < max(len(changes), len(expectedChanges)); i++ {
|
|
|
|
if i >= len(expectedChanges) {
|
|
|
|
t.Fatalf("unexpected change %s\n", changes[i].String())
|
|
|
|
}
|
|
|
|
if i >= len(changes) {
|
|
|
|
t.Fatalf("no change for expected change %s\n", expectedChanges[i].String())
|
|
|
|
}
|
|
|
|
if changes[i].Path == expectedChanges[i].Path {
|
|
|
|
if changes[i] != expectedChanges[i] {
|
|
|
|
t.Fatalf("Wrong change for %s, expected %s, got %s\n", changes[i].Path, changes[i].String(), expectedChanges[i].String())
|
|
|
|
}
|
|
|
|
} else if changes[i].Path < expectedChanges[i].Path {
|
|
|
|
t.Fatalf("unexpected change %s\n", changes[i].String())
|
|
|
|
} else {
|
|
|
|
t.Fatalf("no change for expected change %s != %s\n", expectedChanges[i].String(), changes[i].String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|