2018-02-27 16:46:14 -05:00
|
|
|
package container // import "github.com/docker/docker/integration/container"
|
|
|
|
|
|
|
|
import (
|
2019-06-10 22:03:58 -04:00
|
|
|
"archive/tar"
|
2022-03-20 12:13:04 -04:00
|
|
|
"bytes"
|
2018-02-27 16:46:14 -05:00
|
|
|
"context"
|
2019-06-10 22:03:58 -04:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"os"
|
2022-03-20 12:13:04 -04:00
|
|
|
"path/filepath"
|
2018-02-27 16:46:14 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2022-03-20 12:13:04 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2019-06-10 22:03:58 -04:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2019-08-29 16:52:40 -04:00
|
|
|
"github.com/docker/docker/testutil/fakecontext"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/skip"
|
2018-02-27 16:46:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCopyFromContainerPathDoesNotExist(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
apiclient := testEnv.APIClient()
|
2019-06-06 07:00:37 -04:00
|
|
|
cid := container.Create(ctx, t, apiclient)
|
2018-02-27 16:46:14 -05:00
|
|
|
|
|
|
|
_, _, err := apiclient.CopyFromContainer(ctx, cid, "/dne")
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, client.IsErrNotFound(err))
|
2022-03-20 11:55:42 -04:00
|
|
|
assert.ErrorContains(t, err, "Could not find the file /dne in container "+cid)
|
2018-02-27 16:46:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCopyFromContainerPathIsNotDir(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
apiclient := testEnv.APIClient()
|
2019-06-06 07:00:37 -04:00
|
|
|
cid := container.Create(ctx, t, apiclient)
|
2018-02-27 16:46:14 -05:00
|
|
|
|
2021-03-18 11:55:11 -04:00
|
|
|
path := "/etc/passwd/"
|
|
|
|
expected := "not a directory"
|
|
|
|
if testEnv.OSType == "windows" {
|
|
|
|
path = "c:/windows/system32/drivers/etc/hosts/"
|
|
|
|
expected = "The filename, directory name, or volume label syntax is incorrect."
|
|
|
|
}
|
|
|
|
_, _, err := apiclient.CopyFromContainer(ctx, cid, path)
|
|
|
|
assert.Assert(t, is.ErrorContains(err, expected))
|
2018-02-27 16:46:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCopyToContainerPathDoesNotExist(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
apiclient := testEnv.APIClient()
|
2019-06-06 07:00:37 -04:00
|
|
|
cid := container.Create(ctx, t, apiclient)
|
2018-02-27 16:46:14 -05:00
|
|
|
|
|
|
|
err := apiclient.CopyToContainer(ctx, cid, "/dne", nil, types.CopyToContainerOptions{})
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, client.IsErrNotFound(err))
|
2022-03-20 11:55:42 -04:00
|
|
|
assert.ErrorContains(t, err, "Could not find the file /dne in container "+cid)
|
2018-02-27 16:46:14 -05:00
|
|
|
}
|
|
|
|
|
2022-03-20 12:13:04 -04:00
|
|
|
func TestCopyEmptyFile(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
srcPath := filepath.Join(tmpDir, "empty-file.txt")
|
|
|
|
err := os.WriteFile(srcPath, []byte(""), 0400)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
// TODO(thaJeztah) Add utilities to the client to make steps below less complicated.
|
|
|
|
// Code below is taken from copyToContainer() in docker/cli.
|
|
|
|
srcInfo, err := archive.CopyInfoSourcePath(srcPath, false)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
srcArchive, err := archive.TarResource(srcInfo)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer srcArchive.Close()
|
|
|
|
|
|
|
|
ctrPath := "/empty-file.txt"
|
|
|
|
dstInfo := archive.CopyInfo{Path: ctrPath}
|
|
|
|
dstDir, preparedArchive, err := archive.PrepareArchiveCopy(srcArchive, srcInfo, dstInfo)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer preparedArchive.Close()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
apiclient := testEnv.APIClient()
|
|
|
|
cid := container.Create(ctx, t, apiclient)
|
|
|
|
|
|
|
|
// empty content
|
|
|
|
err = apiclient.CopyToContainer(ctx, cid, dstDir, bytes.NewReader([]byte("")), types.CopyToContainerOptions{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
// tar with empty file
|
|
|
|
err = apiclient.CopyToContainer(ctx, cid, dstDir, preparedArchive, types.CopyToContainerOptions{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
// copy from empty file
|
|
|
|
rdr, _, err := apiclient.CopyFromContainer(ctx, cid, dstDir)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer rdr.Close()
|
|
|
|
}
|
|
|
|
|
2018-02-27 16:46:14 -05:00
|
|
|
func TestCopyToContainerPathIsNotDir(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
apiclient := testEnv.APIClient()
|
2019-06-06 07:00:37 -04:00
|
|
|
cid := container.Create(ctx, t, apiclient)
|
2018-02-27 16:46:14 -05:00
|
|
|
|
2021-03-18 11:55:11 -04:00
|
|
|
path := "/etc/passwd/"
|
|
|
|
if testEnv.OSType == "windows" {
|
|
|
|
path = "c:/windows/system32/drivers/etc/hosts/"
|
|
|
|
}
|
|
|
|
err := apiclient.CopyToContainer(ctx, cid, path, nil, types.CopyToContainerOptions{})
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Assert(t, is.ErrorContains(err, "not a directory"))
|
2018-02-27 16:46:14 -05:00
|
|
|
}
|
2019-06-10 22:03:58 -04:00
|
|
|
|
2019-06-13 01:36:21 -04:00
|
|
|
func TestCopyFromContainer(t *testing.T) {
|
2019-06-10 22:03:58 -04:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
|
|
|
defer setupTest(t)()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
apiClient := testEnv.APIClient()
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
dir, err := os.MkdirTemp("", t.Name())
|
2019-06-10 22:03:58 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
buildCtx := fakecontext.New(t, dir, fakecontext.WithFile("foo", "hello"), fakecontext.WithFile("baz", "world"), fakecontext.WithDockerfile(`
|
2019-06-13 01:36:21 -04:00
|
|
|
FROM busybox
|
2019-06-10 22:03:58 -04:00
|
|
|
COPY foo /foo
|
2019-06-13 01:36:21 -04:00
|
|
|
COPY baz /bar/quux/baz
|
|
|
|
RUN ln -s notexist /bar/notarget && ln -s quux/baz /bar/filesymlink && ln -s quux /bar/dirsymlink && ln -s / /bar/root
|
2019-06-10 22:03:58 -04:00
|
|
|
CMD /fake
|
|
|
|
`))
|
|
|
|
defer buildCtx.Close()
|
|
|
|
|
|
|
|
resp, err := apiClient.ImageBuild(ctx, buildCtx.AsTarReader(t), types.ImageBuildOptions{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var imageID string
|
2021-08-24 06:10:50 -04:00
|
|
|
err = jsonmessage.DisplayJSONMessagesStream(resp.Body, io.Discard, 0, false, func(msg jsonmessage.JSONMessage) {
|
2019-06-10 22:03:58 -04:00
|
|
|
var r types.BuildResult
|
|
|
|
assert.NilError(t, json.Unmarshal(*msg.Aux, &r))
|
|
|
|
imageID = r.ID
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, imageID != "")
|
|
|
|
|
|
|
|
cid := container.Create(ctx, t, apiClient, container.WithImage(imageID))
|
|
|
|
|
2019-06-13 01:36:21 -04:00
|
|
|
for _, x := range []struct {
|
|
|
|
src string
|
|
|
|
expect map[string]string
|
|
|
|
}{
|
|
|
|
{"/", map[string]string{"/": "", "/foo": "hello", "/bar/quux/baz": "world", "/bar/filesymlink": "", "/bar/dirsymlink": "", "/bar/notarget": ""}},
|
|
|
|
{"/bar/root", map[string]string{"root": ""}},
|
|
|
|
{"/bar/root/", map[string]string{"root/": "", "root/foo": "hello", "root/bar/quux/baz": "world", "root/bar/filesymlink": "", "root/bar/dirsymlink": "", "root/bar/notarget": ""}},
|
|
|
|
|
|
|
|
{"bar/quux", map[string]string{"quux/": "", "quux/baz": "world"}},
|
|
|
|
{"bar/quux/", map[string]string{"quux/": "", "quux/baz": "world"}},
|
|
|
|
{"bar/quux/baz", map[string]string{"baz": "world"}},
|
|
|
|
|
|
|
|
{"bar/filesymlink", map[string]string{"filesymlink": ""}},
|
|
|
|
{"bar/dirsymlink", map[string]string{"dirsymlink": ""}},
|
|
|
|
{"bar/dirsymlink/", map[string]string{"dirsymlink/": "", "dirsymlink/baz": "world"}},
|
|
|
|
{"bar/notarget", map[string]string{"notarget": ""}},
|
|
|
|
} {
|
|
|
|
t.Run(x.src, func(t *testing.T) {
|
|
|
|
rdr, _, err := apiClient.CopyFromContainer(ctx, cid, x.src)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer rdr.Close()
|
|
|
|
|
|
|
|
found := make(map[string]bool, len(x.expect))
|
|
|
|
var numFound int
|
|
|
|
tr := tar.NewReader(rdr)
|
|
|
|
for numFound < len(x.expect) {
|
|
|
|
h, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
expected, exists := x.expect[h.Name]
|
|
|
|
if !exists {
|
|
|
|
// this archive will have extra stuff in it since we are copying from root
|
|
|
|
// and docker adds a bunch of stuff
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
numFound++
|
|
|
|
found[h.Name] = true
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
buf, err := io.ReadAll(tr)
|
2019-06-13 01:36:21 -04:00
|
|
|
if err == nil {
|
|
|
|
assert.Check(t, is.Equal(string(buf), expected))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for f := range x.expect {
|
|
|
|
assert.Check(t, found[f], f+" not found in archive")
|
|
|
|
}
|
|
|
|
})
|
2019-06-10 22:03:58 -04:00
|
|
|
}
|
|
|
|
}
|