use testify to check error for test unit test of archive package

Signed-off-by: yangshukui <yangshukui@huawei.com>
This commit is contained in:
yangshukui 2017-08-09 10:27:01 +08:00
parent 82eb9002e9
commit 1f1e8e9c0e
6 changed files with 239 additions and 407 deletions

View file

@ -8,6 +8,7 @@ import (
"testing"
"github.com/docker/docker/pkg/system"
"github.com/stretchr/testify/require"
"golang.org/x/sys/unix"
)
@ -22,46 +23,37 @@ import (
// └── f1 # whiteout, 0644
func setupOverlayTestDir(t *testing.T, src string) {
// Create opaque directory containing single file and permission 0700
if err := os.Mkdir(filepath.Join(src, "d1"), 0700); err != nil {
t.Fatal(err)
}
err := os.Mkdir(filepath.Join(src, "d1"), 0700)
require.NoError(t, err)
if err := system.Lsetxattr(filepath.Join(src, "d1"), "trusted.overlay.opaque", []byte("y"), 0); err != nil {
t.Fatal(err)
}
err = system.Lsetxattr(filepath.Join(src, "d1"), "trusted.overlay.opaque", []byte("y"), 0)
require.NoError(t, err)
if err := ioutil.WriteFile(filepath.Join(src, "d1", "f1"), []byte{}, 0600); err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile(filepath.Join(src, "d1", "f1"), []byte{}, 0600)
require.NoError(t, err)
// Create another opaque directory containing single file but with permission 0750
if err := os.Mkdir(filepath.Join(src, "d2"), 0750); err != nil {
t.Fatal(err)
}
err = os.Mkdir(filepath.Join(src, "d2"), 0750)
require.NoError(t, err)
if err := system.Lsetxattr(filepath.Join(src, "d2"), "trusted.overlay.opaque", []byte("y"), 0); err != nil {
t.Fatal(err)
}
err = system.Lsetxattr(filepath.Join(src, "d2"), "trusted.overlay.opaque", []byte("y"), 0)
require.NoError(t, err)
if err := ioutil.WriteFile(filepath.Join(src, "d2", "f1"), []byte{}, 0660); err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile(filepath.Join(src, "d2", "f1"), []byte{}, 0660)
require.NoError(t, err)
// Create regular directory with deleted file
if err := os.Mkdir(filepath.Join(src, "d3"), 0700); err != nil {
t.Fatal(err)
}
err = os.Mkdir(filepath.Join(src, "d3"), 0700)
require.NoError(t, err)
if err := system.Mknod(filepath.Join(src, "d3", "f1"), unix.S_IFCHR, 0); err != nil {
t.Fatal(err)
}
err = system.Mknod(filepath.Join(src, "d3", "f1"), unix.S_IFCHR, 0)
require.NoError(t, err)
}
func checkOpaqueness(t *testing.T, path string, opaque string) {
xattrOpaque, err := system.Lgetxattr(path, "trusted.overlay.opaque")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if string(xattrOpaque) != opaque {
t.Fatalf("Unexpected opaque value: %q, expected %q", string(xattrOpaque), opaque)
}
@ -70,9 +62,8 @@ func checkOpaqueness(t *testing.T, path string, opaque string) {
func checkOverlayWhiteout(t *testing.T, path string) {
stat, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
statT, ok := stat.Sys().(*syscall.Stat_t)
if !ok {
t.Fatalf("Unexpected type: %t, expected *syscall.Stat_t", stat.Sys())
@ -84,9 +75,8 @@ func checkOverlayWhiteout(t *testing.T, path string) {
func checkFileMode(t *testing.T, path string, perm os.FileMode) {
stat, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if stat.Mode() != perm {
t.Fatalf("Unexpected file mode for %s: %o, expected %o", path, stat.Mode(), perm)
}
@ -94,23 +84,17 @@ func checkFileMode(t *testing.T, path string, perm os.FileMode) {
func TestOverlayTarUntar(t *testing.T) {
oldmask, err := system.Umask(0)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer system.Umask(oldmask)
src, err := ioutil.TempDir("", "docker-test-overlay-tar-src")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(src)
setupOverlayTestDir(t, src)
dst, err := ioutil.TempDir("", "docker-test-overlay-tar-dst")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(dst)
options := &TarOptions{
@ -118,14 +102,11 @@ func TestOverlayTarUntar(t *testing.T) {
WhiteoutFormat: OverlayWhiteoutFormat,
}
archive, err := TarWithOptions(src, options)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer archive.Close()
if err := Untar(archive, dst, options); err != nil {
t.Fatal(err)
}
err = Untar(archive, dst, options)
require.NoError(t, err)
checkFileMode(t, filepath.Join(dst, "d1"), 0700|os.ModeDir)
checkFileMode(t, filepath.Join(dst, "d2"), 0750|os.ModeDir)
@ -142,40 +123,31 @@ func TestOverlayTarUntar(t *testing.T) {
func TestOverlayTarAUFSUntar(t *testing.T) {
oldmask, err := system.Umask(0)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer system.Umask(oldmask)
src, err := ioutil.TempDir("", "docker-test-overlay-tar-src")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(src)
setupOverlayTestDir(t, src)
dst, err := ioutil.TempDir("", "docker-test-overlay-tar-dst")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(dst)
archive, err := TarWithOptions(src, &TarOptions{
Compression: Uncompressed,
WhiteoutFormat: OverlayWhiteoutFormat,
})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer archive.Close()
if err := Untar(archive, dst, &TarOptions{
err = Untar(archive, dst, &TarOptions{
Compression: Uncompressed,
WhiteoutFormat: AUFSWhiteoutFormat,
}); err != nil {
t.Fatal(err)
}
})
require.NoError(t, err)
checkFileMode(t, filepath.Join(dst, "d1"), 0700|os.ModeDir)
checkFileMode(t, filepath.Join(dst, "d1", WhiteoutOpaqueDir), 0700)

View file

@ -263,9 +263,7 @@ func TestCmdStreamGood(t *testing.T) {
func TestUntarPathWithInvalidDest(t *testing.T) {
tempFolder, err := ioutil.TempDir("", "docker-archive-test")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(tempFolder)
invalidDestFolder := filepath.Join(tempFolder, "invalidDest")
// Create a src file
@ -284,9 +282,7 @@ func TestUntarPathWithInvalidDest(t *testing.T) {
cmd := exec.Command("sh", "-c", "tar cf "+tarFileU+" "+srcFileU)
_, err = cmd.CombinedOutput()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
err = defaultUntarPath(tarFile, invalidDestFolder)
if err == nil {
@ -308,9 +304,7 @@ func TestUntarPathWithInvalidSrc(t *testing.T) {
func TestUntarPath(t *testing.T) {
tmpFolder, err := ioutil.TempDir("", "docker-archive-test")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(tmpFolder)
srcFile := filepath.Join(tmpFolder, "src")
tarFile := filepath.Join(tmpFolder, "src.tar")
@ -331,9 +325,7 @@ func TestUntarPath(t *testing.T) {
}
cmd := exec.Command("sh", "-c", "tar cf "+tarFileU+" "+srcFileU)
_, err = cmd.CombinedOutput()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
err = defaultUntarPath(tarFile, destFolder)
if err != nil {

View file

@ -13,6 +13,8 @@ import (
"testing"
"github.com/docker/docker/pkg/system"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sys/unix"
)
@ -70,113 +72,89 @@ func TestChmodTarEntry(t *testing.T) {
func TestTarWithHardLink(t *testing.T) {
origin, err := ioutil.TempDir("", "docker-test-tar-hardlink")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(origin)
if err := ioutil.WriteFile(filepath.Join(origin, "1"), []byte("hello world"), 0700); err != nil {
t.Fatal(err)
}
if err := os.Link(filepath.Join(origin, "1"), filepath.Join(origin, "2")); err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile(filepath.Join(origin, "1"), []byte("hello world"), 0700)
require.NoError(t, err)
err = os.Link(filepath.Join(origin, "1"), filepath.Join(origin, "2"))
require.NoError(t, err)
var i1, i2 uint64
if i1, err = getNlink(filepath.Join(origin, "1")); err != nil {
t.Fatal(err)
}
i1, err = getNlink(filepath.Join(origin, "1"))
require.NoError(t, err)
// sanity check that we can hardlink
if i1 != 2 {
t.Skipf("skipping since hardlinks don't work here; expected 2 links, got %d", i1)
}
dest, err := ioutil.TempDir("", "docker-test-tar-hardlink-dest")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(dest)
// we'll do this in two steps to separate failure
fh, err := Tar(origin, Uncompressed)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
// ensure we can read the whole thing with no error, before writing back out
buf, err := ioutil.ReadAll(fh)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
bRdr := bytes.NewReader(buf)
err = Untar(bRdr, dest, &TarOptions{Compression: Uncompressed})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if i1, err = getInode(filepath.Join(dest, "1")); err != nil {
t.Fatal(err)
}
if i2, err = getInode(filepath.Join(dest, "2")); err != nil {
t.Fatal(err)
}
i1, err = getInode(filepath.Join(dest, "1"))
require.NoError(t, err)
if i1 != i2 {
t.Errorf("expected matching inodes, but got %d and %d", i1, i2)
}
i2, err = getInode(filepath.Join(dest, "2"))
require.NoError(t, err)
assert.Equal(t, i1, i2)
}
func TestTarWithHardLinkAndRebase(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "docker-test-tar-hardlink-rebase")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
origin := filepath.Join(tmpDir, "origin")
if err := os.Mkdir(origin, 0700); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(origin, "1"), []byte("hello world"), 0700); err != nil {
t.Fatal(err)
}
if err := os.Link(filepath.Join(origin, "1"), filepath.Join(origin, "2")); err != nil {
t.Fatal(err)
}
err = os.Mkdir(origin, 0700)
require.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(origin, "1"), []byte("hello world"), 0700)
require.NoError(t, err)
err = os.Link(filepath.Join(origin, "1"), filepath.Join(origin, "2"))
require.NoError(t, err)
var i1, i2 uint64
if i1, err = getNlink(filepath.Join(origin, "1")); err != nil {
t.Fatal(err)
}
i1, err = getNlink(filepath.Join(origin, "1"))
require.NoError(t, err)
// sanity check that we can hardlink
if i1 != 2 {
t.Skipf("skipping since hardlinks don't work here; expected 2 links, got %d", i1)
}
dest := filepath.Join(tmpDir, "dest")
bRdr, err := TarResourceRebase(origin, "origin")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
dstDir, srcBase := SplitPathDirEntry(origin)
_, dstBase := SplitPathDirEntry(dest)
content := RebaseArchiveEntries(bRdr, srcBase, dstBase)
err = Untar(content, dstDir, &TarOptions{Compression: Uncompressed, NoLchown: true, NoOverwriteDirNonDir: true})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if i1, err = getInode(filepath.Join(dest, "1")); err != nil {
t.Fatal(err)
}
if i2, err = getInode(filepath.Join(dest, "2")); err != nil {
t.Fatal(err)
}
i1, err = getInode(filepath.Join(dest, "1"))
require.NoError(t, err)
i2, err = getInode(filepath.Join(dest, "2"))
require.NoError(t, err)
if i1 != i2 {
t.Errorf("expected matching inodes, but got %d and %d", i1, i2)
}
assert.Equal(t, i1, i2)
}
func getNlink(path string) (uint64, error) {
@ -206,51 +184,38 @@ func getInode(path string) (uint64, error) {
func TestTarWithBlockCharFifo(t *testing.T) {
origin, err := ioutil.TempDir("", "docker-test-tar-hardlink")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(origin)
if err := ioutil.WriteFile(filepath.Join(origin, "1"), []byte("hello world"), 0700); err != nil {
t.Fatal(err)
}
if err := system.Mknod(filepath.Join(origin, "2"), unix.S_IFBLK, int(system.Mkdev(int64(12), int64(5)))); err != nil {
t.Fatal(err)
}
if err := system.Mknod(filepath.Join(origin, "3"), unix.S_IFCHR, int(system.Mkdev(int64(12), int64(5)))); err != nil {
t.Fatal(err)
}
if err := system.Mknod(filepath.Join(origin, "4"), unix.S_IFIFO, int(system.Mkdev(int64(12), int64(5)))); err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile(filepath.Join(origin, "1"), []byte("hello world"), 0700)
require.NoError(t, err)
err = system.Mknod(filepath.Join(origin, "2"), unix.S_IFBLK, int(system.Mkdev(int64(12), int64(5))))
require.NoError(t, err)
err = system.Mknod(filepath.Join(origin, "3"), unix.S_IFCHR, int(system.Mkdev(int64(12), int64(5))))
require.NoError(t, err)
err = system.Mknod(filepath.Join(origin, "4"), unix.S_IFIFO, int(system.Mkdev(int64(12), int64(5))))
require.NoError(t, err)
dest, err := ioutil.TempDir("", "docker-test-tar-hardlink-dest")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(dest)
// we'll do this in two steps to separate failure
fh, err := Tar(origin, Uncompressed)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
// ensure we can read the whole thing with no error, before writing back out
buf, err := ioutil.ReadAll(fh)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
bRdr := bytes.NewReader(buf)
err = Untar(bRdr, dest, &TarOptions{Compression: Uncompressed})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
changes, err := ChangesDirs(origin, dest)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if len(changes) > 0 {
t.Fatalf("Tar with special device (block, char, fifo) should keep them (recreate them when untar) : %v", changes)
}
@ -262,22 +227,17 @@ func TestTarUntarWithXattr(t *testing.T) {
t.Skip()
}
origin, err := ioutil.TempDir("", "docker-test-untar-origin")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(origin)
if err := ioutil.WriteFile(filepath.Join(origin, "1"), []byte("hello world"), 0700); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(origin, "2"), []byte("welcome!"), 0700); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(origin, "3"), []byte("will be ignored"), 0700); err != nil {
t.Fatal(err)
}
if err := system.Lsetxattr(filepath.Join(origin, "2"), "security.capability", []byte{0x00}, 0); err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile(filepath.Join(origin, "1"), []byte("hello world"), 0700)
require.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(origin, "2"), []byte("welcome!"), 0700)
require.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(origin, "3"), []byte("will be ignored"), 0700)
require.NoError(t, err)
err = system.Lsetxattr(filepath.Join(origin, "2"), "security.capability", []byte{0x00}, 0)
require.NoError(t, err)
for _, c := range []Compression{
Uncompressed,

View file

@ -11,6 +11,7 @@ import (
"time"
"github.com/docker/docker/pkg/system"
"github.com/stretchr/testify/require"
)
func max(x, y int) int {
@ -79,24 +80,20 @@ func createSampleDir(t *testing.T, root string) {
for _, info := range files {
p := path.Join(root, info.path)
if info.filetype == Dir {
if err := os.MkdirAll(p, info.permissions); err != nil {
t.Fatal(err)
}
err := os.MkdirAll(p, info.permissions)
require.NoError(t, err)
} else if info.filetype == Regular {
if err := ioutil.WriteFile(p, []byte(info.contents), info.permissions); err != nil {
t.Fatal(err)
}
err := ioutil.WriteFile(p, []byte(info.contents), info.permissions)
require.NoError(t, err)
} else if info.filetype == Symlink {
if err := os.Symlink(info.contents, p); err != nil {
t.Fatal(err)
}
err := os.Symlink(info.contents, p)
require.NoError(t, err)
}
if info.filetype != Symlink {
// Set a consistent ctime, atime for all files and dirs
if err := system.Chtimes(p, now, now); err != nil {
t.Fatal(err)
}
err := system.Chtimes(p, now, now)
require.NoError(t, err)
}
}
}
@ -126,20 +123,14 @@ func TestChangesWithNoChanges(t *testing.T) {
t.Skip("symlinks on Windows")
}
rwLayer, err := ioutil.TempDir("", "docker-changes-test")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(rwLayer)
layer, err := ioutil.TempDir("", "docker-changes-test-layer")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(layer)
createSampleDir(t, layer)
changes, err := Changes([]string{layer}, rwLayer)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if len(changes) != 0 {
t.Fatalf("Changes with no difference should have detect no changes, but detected %d", len(changes))
}
@ -153,18 +144,14 @@ func TestChangesWithChanges(t *testing.T) {
}
// Mock the readonly layer
layer, err := ioutil.TempDir("", "docker-changes-test-layer")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(layer)
createSampleDir(t, layer)
os.MkdirAll(path.Join(layer, "dir1/subfolder"), 0740)
// Mock the RW layer
rwLayer, err := ioutil.TempDir("", "docker-changes-test")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(rwLayer)
// Create a folder in RW layer
@ -181,9 +168,7 @@ func TestChangesWithChanges(t *testing.T) {
ioutil.WriteFile(newFile, []byte{}, 0740)
changes, err := Changes([]string{layer}, rwLayer)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
expectedChanges := []Change{
{"/dir1", ChangeModify},
@ -224,9 +209,7 @@ func TestChangesWithChangesGH13590(t *testing.T) {
ioutil.WriteFile(file, []byte("bye"), 0666)
changes, err := Changes([]string{baseLayer}, layer)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
expectedChanges := []Change{
{"/dir1/dir2/dir3", ChangeModify},
@ -246,9 +229,7 @@ func TestChangesWithChangesGH13590(t *testing.T) {
ioutil.WriteFile(file, []byte("bye"), 0666)
changes, err = Changes([]string{baseLayer}, layer)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
expectedChanges = []Change{
{"/dir1/dir2/dir3/file.txt", ChangeModify},
@ -265,20 +246,15 @@ func TestChangesDirsEmpty(t *testing.T) {
t.Skip("symlinks on Windows; gcp failure on Solaris")
}
src, err := ioutil.TempDir("", "docker-changes-test")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(src)
createSampleDir(t, src)
dst := src + "-copy"
if err := copyDir(src, dst); err != nil {
t.Fatal(err)
}
err = copyDir(src, dst)
require.NoError(t, err)
defer os.RemoveAll(dst)
changes, err := ChangesDirs(dst, src)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if len(changes) != 0 {
t.Fatalf("Reported changes for identical dirs: %v", changes)
@ -289,81 +265,65 @@ func TestChangesDirsEmpty(t *testing.T) {
func mutateSampleDir(t *testing.T, root string) {
// Remove a regular file
if err := os.RemoveAll(path.Join(root, "file1")); err != nil {
t.Fatal(err)
}
err := os.RemoveAll(path.Join(root, "file1"))
require.NoError(t, err)
// Remove a directory
if err := os.RemoveAll(path.Join(root, "dir1")); err != nil {
t.Fatal(err)
}
err = os.RemoveAll(path.Join(root, "dir1"))
require.NoError(t, err)
// Remove a symlink
if err := os.RemoveAll(path.Join(root, "symlink1")); err != nil {
t.Fatal(err)
}
err = os.RemoveAll(path.Join(root, "symlink1"))
require.NoError(t, err)
// Rewrite a file
if err := ioutil.WriteFile(path.Join(root, "file2"), []byte("fileNN\n"), 0777); err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile(path.Join(root, "file2"), []byte("fileNN\n"), 0777)
require.NoError(t, err)
// Replace a file
if err := os.RemoveAll(path.Join(root, "file3")); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(path.Join(root, "file3"), []byte("fileMM\n"), 0404); err != nil {
t.Fatal(err)
}
err = os.RemoveAll(path.Join(root, "file3"))
require.NoError(t, err)
err = ioutil.WriteFile(path.Join(root, "file3"), []byte("fileMM\n"), 0404)
require.NoError(t, err)
// Touch file
if err := system.Chtimes(path.Join(root, "file4"), time.Now().Add(time.Second), time.Now().Add(time.Second)); err != nil {
t.Fatal(err)
}
err = system.Chtimes(path.Join(root, "file4"), time.Now().Add(time.Second), time.Now().Add(time.Second))
require.NoError(t, err)
// Replace file with dir
if err := os.RemoveAll(path.Join(root, "file5")); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(path.Join(root, "file5"), 0666); err != nil {
t.Fatal(err)
}
err = os.RemoveAll(path.Join(root, "file5"))
require.NoError(t, err)
err = os.MkdirAll(path.Join(root, "file5"), 0666)
require.NoError(t, err)
// Create new file
if err := ioutil.WriteFile(path.Join(root, "filenew"), []byte("filenew\n"), 0777); err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile(path.Join(root, "filenew"), []byte("filenew\n"), 0777)
require.NoError(t, err)
// Create new dir
if err := os.MkdirAll(path.Join(root, "dirnew"), 0766); err != nil {
t.Fatal(err)
}
err = os.MkdirAll(path.Join(root, "dirnew"), 0766)
require.NoError(t, err)
// Create a new symlink
if err := os.Symlink("targetnew", path.Join(root, "symlinknew")); err != nil {
t.Fatal(err)
}
err = os.Symlink("targetnew", path.Join(root, "symlinknew"))
require.NoError(t, err)
// Change a symlink
if err := os.RemoveAll(path.Join(root, "symlink2")); err != nil {
t.Fatal(err)
}
if err := os.Symlink("target2change", path.Join(root, "symlink2")); err != nil {
t.Fatal(err)
}
err = os.RemoveAll(path.Join(root, "symlink2"))
require.NoError(t, err)
err = os.Symlink("target2change", path.Join(root, "symlink2"))
require.NoError(t, err)
// Replace dir with file
if err := os.RemoveAll(path.Join(root, "dir2")); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(path.Join(root, "dir2"), []byte("dir2\n"), 0777); err != nil {
t.Fatal(err)
}
err = os.RemoveAll(path.Join(root, "dir2"))
require.NoError(t, err)
err = ioutil.WriteFile(path.Join(root, "dir2"), []byte("dir2\n"), 0777)
require.NoError(t, err)
// Touch dir
if err := system.Chtimes(path.Join(root, "dir3"), time.Now().Add(time.Second), time.Now().Add(time.Second)); err != nil {
t.Fatal(err)
}
err = system.Chtimes(path.Join(root, "dir3"), time.Now().Add(time.Second), time.Now().Add(time.Second))
require.NoError(t, err)
}
func TestChangesDirsMutated(t *testing.T) {
@ -374,23 +334,18 @@ func TestChangesDirsMutated(t *testing.T) {
t.Skip("symlinks on Windows; gcp failures on Solaris")
}
src, err := ioutil.TempDir("", "docker-changes-test")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
createSampleDir(t, src)
dst := src + "-copy"
if err := copyDir(src, dst); err != nil {
t.Fatal(err)
}
err = copyDir(src, dst)
require.NoError(t, err)
defer os.RemoveAll(src)
defer os.RemoveAll(dst)
mutateSampleDir(t, dst)
changes, err := ChangesDirs(dst, src)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
sort.Sort(changesByPath(changes))
@ -436,41 +391,29 @@ func TestApplyLayer(t *testing.T) {
t.Skip("symlinks on Windows; gcp failures on Solaris")
}
src, err := ioutil.TempDir("", "docker-changes-test")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
createSampleDir(t, src)
defer os.RemoveAll(src)
dst := src + "-copy"
if err := copyDir(src, dst); err != nil {
t.Fatal(err)
}
err = copyDir(src, dst)
require.NoError(t, err)
mutateSampleDir(t, dst)
defer os.RemoveAll(dst)
changes, err := ChangesDirs(dst, src)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
layer, err := ExportChanges(dst, changes, nil, nil)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
layerCopy, err := NewTempArchive(layer, "")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if _, err := ApplyLayer(src, layerCopy); err != nil {
t.Fatal(err)
}
_, err = ApplyLayer(src, layerCopy)
require.NoError(t, err)
changes2, err := ChangesDirs(src, dst)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if len(changes2) != 0 {
t.Fatalf("Unexpected differences after reapplying mutation: %v", changes2)
@ -484,26 +427,18 @@ func TestChangesSizeWithHardlinks(t *testing.T) {
t.Skip("hardlinks on Windows")
}
srcDir, err := ioutil.TempDir("", "docker-test-srcDir")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(srcDir)
destDir, err := ioutil.TempDir("", "docker-test-destDir")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(destDir)
creationSize, err := prepareUntarSourceDirectory(100, destDir, true)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
changes, err := ChangesDirs(destDir, srcDir)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
got := ChangesSize(destDir, changes)
if got != int64(creationSize) {
@ -532,13 +467,12 @@ func TestChangesSize(t *testing.T) {
parentPath, err := ioutil.TempDir("", "docker-changes-test")
defer os.RemoveAll(parentPath)
addition := path.Join(parentPath, "addition")
if err := ioutil.WriteFile(addition, []byte{0x01, 0x01, 0x01}, 0744); err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile(addition, []byte{0x01, 0x01, 0x01}, 0744)
require.NoError(t, err)
modification := path.Join(parentPath, "modification")
if err = ioutil.WriteFile(modification, []byte{0x01, 0x01, 0x01}, 0744); err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile(modification, []byte{0x01, 0x01, 0x01}, 0744)
require.NoError(t, err)
changes := []Change{
{Path: "addition", Kind: ChangeAdd},
{Path: "modification", Kind: ChangeModify},

View file

@ -15,6 +15,8 @@ import (
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func removeAllPaths(paths ...string) {
@ -26,13 +28,11 @@ func removeAllPaths(paths ...string) {
func getTestTempDirs(t *testing.T) (tmpDirA, tmpDirB string) {
var err error
if tmpDirA, err = ioutil.TempDir("", "archive-copy-test"); err != nil {
t.Fatal(err)
}
tmpDirA, err = ioutil.TempDir("", "archive-copy-test")
require.NoError(t, err)
if tmpDirB, err = ioutil.TempDir("", "archive-copy-test"); err != nil {
t.Fatal(err)
}
tmpDirB, err = ioutil.TempDir("", "archive-copy-test")
require.NoError(t, err)
return
}
@ -118,9 +118,8 @@ func logDirContents(t *testing.T, dirPath string) {
t.Logf("logging directory contents: %q", dirPath)
if err := filepath.Walk(dirPath, logWalkedPaths); err != nil {
t.Fatal(err)
}
err := filepath.Walk(dirPath, logWalkedPaths)
require.NoError(t, err)
}
func testCopyHelper(t *testing.T, srcPath, dstPath string) (err error) {
@ -293,9 +292,8 @@ func TestCopyCaseA(t *testing.T) {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = fileContentsEqual(t, srcPath, dstPath); err != nil {
t.Fatal(err)
}
err = fileContentsEqual(t, srcPath, dstPath)
require.NoError(t, err)
os.Remove(dstPath)
symlinkPath := filepath.Join(tmpDirA, "symlink3")
@ -306,17 +304,15 @@ func TestCopyCaseA(t *testing.T) {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = fileContentsEqual(t, linkTarget, dstPath); err != nil {
t.Fatal(err)
}
err = fileContentsEqual(t, linkTarget, dstPath)
require.NoError(t, err)
os.Remove(dstPath)
if err = testCopyHelperFSym(t, symlinkPath1, dstPath); err != nil {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = fileContentsEqual(t, linkTarget, dstPath); err != nil {
t.Fatal(err)
}
err = fileContentsEqual(t, linkTarget, dstPath)
require.NoError(t, err)
}
// B. SRC specifies a file and DST (with trailing path separator) doesn't
@ -377,9 +373,8 @@ func TestCopyCaseC(t *testing.T) {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = fileContentsEqual(t, srcPath, dstPath); err != nil {
t.Fatal(err)
}
err = fileContentsEqual(t, srcPath, dstPath)
require.NoError(t, err)
}
// C. Symbol link following version:
@ -415,9 +410,8 @@ func TestCopyCaseCFSym(t *testing.T) {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = fileContentsEqual(t, linkTarget, dstPath); err != nil {
t.Fatal(err)
}
err = fileContentsEqual(t, linkTarget, dstPath)
require.NoError(t, err)
}
// D. SRC specifies a file and DST exists as a directory. This should place
@ -446,9 +440,8 @@ func TestCopyCaseD(t *testing.T) {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = fileContentsEqual(t, srcPath, dstPath); err != nil {
t.Fatal(err)
}
err = fileContentsEqual(t, srcPath, dstPath)
require.NoError(t, err)
// Now try again but using a trailing path separator for dstDir.
@ -466,9 +459,8 @@ func TestCopyCaseD(t *testing.T) {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = fileContentsEqual(t, srcPath, dstPath); err != nil {
t.Fatal(err)
}
err = fileContentsEqual(t, srcPath, dstPath)
require.NoError(t, err)
}
// D. Symbol link following version:
@ -499,9 +491,8 @@ func TestCopyCaseDFSym(t *testing.T) {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = fileContentsEqual(t, linkTarget, dstPath); err != nil {
t.Fatal(err)
}
err = fileContentsEqual(t, linkTarget, dstPath)
require.NoError(t, err)
// Now try again but using a trailing path separator for dstDir.
@ -519,9 +510,8 @@ func TestCopyCaseDFSym(t *testing.T) {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = fileContentsEqual(t, linkTarget, dstPath); err != nil {
t.Fatal(err)
}
err = fileContentsEqual(t, linkTarget, dstPath)
require.NoError(t, err)
}
// E. SRC specifies a directory and DST does not exist. This should create a
@ -563,9 +553,8 @@ func TestCopyCaseE(t *testing.T) {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = dirContentsEqual(t, dstDir, srcDir); err != nil {
t.Fatal(err)
}
err = dirContentsEqual(t, dstDir, srcDir)
require.NoError(t, err)
}
// E. Symbol link following version:
@ -609,9 +598,8 @@ func TestCopyCaseEFSym(t *testing.T) {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = dirContentsEqual(t, dstDir, linkTarget); err != nil {
t.Fatal(err)
}
err = dirContentsEqual(t, dstDir, linkTarget)
require.NoError(t, err)
}
// F. SRC specifies a directory and DST exists as a file. This should cause an
@ -669,9 +657,8 @@ func TestCopyCaseG(t *testing.T) {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = dirContentsEqual(t, resultDir, srcDir); err != nil {
t.Fatal(err)
}
err = dirContentsEqual(t, resultDir, srcDir)
require.NoError(t, err)
// Now try again but using a trailing path separator for dstDir.
@ -689,9 +676,8 @@ func TestCopyCaseG(t *testing.T) {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = dirContentsEqual(t, resultDir, srcDir); err != nil {
t.Fatal(err)
}
err = dirContentsEqual(t, resultDir, srcDir)
require.NoError(t, err)
}
// G. Symbol link version:
@ -717,9 +703,8 @@ func TestCopyCaseGFSym(t *testing.T) {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = dirContentsEqual(t, resultDir, linkTarget); err != nil {
t.Fatal(err)
}
err = dirContentsEqual(t, resultDir, linkTarget)
require.NoError(t, err)
// Now try again but using a trailing path separator for dstDir.
@ -737,9 +722,8 @@ func TestCopyCaseGFSym(t *testing.T) {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = dirContentsEqual(t, resultDir, linkTarget); err != nil {
t.Fatal(err)
}
err = dirContentsEqual(t, resultDir, linkTarget)
require.NoError(t, err)
}
// H. SRC specifies a directory's contents only and DST does not exist. This
@ -899,9 +883,8 @@ func TestCopyCaseJ(t *testing.T) {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = dirContentsEqual(t, dstDir, srcDir); err != nil {
t.Fatal(err)
}
err = dirContentsEqual(t, dstDir, srcDir)
require.NoError(t, err)
// Now try again but using a trailing path separator for dstDir.
@ -919,9 +902,8 @@ func TestCopyCaseJ(t *testing.T) {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = dirContentsEqual(t, dstDir, srcDir); err != nil {
t.Fatal(err)
}
err = dirContentsEqual(t, dstDir, srcDir)
require.NoError(t, err)
}
// J. Symbol link following version:
@ -952,9 +934,8 @@ func TestCopyCaseJFSym(t *testing.T) {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = dirContentsEqual(t, dstDir, linkTarget); err != nil {
t.Fatal(err)
}
err = dirContentsEqual(t, dstDir, linkTarget)
require.NoError(t, err)
// Now try again but using a trailing path separator for dstDir.
@ -972,7 +953,6 @@ func TestCopyCaseJFSym(t *testing.T) {
t.Fatalf("unexpected error %T: %s", err, err)
}
if err = dirContentsEqual(t, dstDir, linkTarget); err != nil {
t.Fatal(err)
}
err = dirContentsEqual(t, dstDir, linkTarget)
require.NoError(t, err)
}

View file

@ -5,13 +5,13 @@ import (
"bytes"
"io"
"testing"
"github.com/stretchr/testify/require"
)
func TestGenerateEmptyFile(t *testing.T) {
archive, err := Generate("emptyFile")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if archive == nil {
t.Fatal("The generated archive should not be nil.")
}
@ -28,9 +28,7 @@ func TestGenerateEmptyFile(t *testing.T) {
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
buf := new(bytes.Buffer)
buf.ReadFrom(tr)
content := buf.String()
@ -54,9 +52,7 @@ func TestGenerateEmptyFile(t *testing.T) {
func TestGenerateWithContent(t *testing.T) {
archive, err := Generate("file", "content")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if archive == nil {
t.Fatal("The generated archive should not be nil.")
}
@ -73,9 +69,7 @@ func TestGenerateWithContent(t *testing.T) {
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
buf := new(bytes.Buffer)
buf.ReadFrom(tr)
content := buf.String()