2018-02-27 21:46:14 +00:00
|
|
|
package container // import "github.com/docker/docker/integration/container"
|
|
|
|
|
|
|
|
import (
|
2019-06-11 02:03:58 +00:00
|
|
|
"archive/tar"
|
2018-02-27 21:46:14 +00:00
|
|
|
"context"
|
2019-06-11 02:03:58 +00:00
|
|
|
"encoding/json"
|
2018-02-27 21:46:14 +00:00
|
|
|
"fmt"
|
2019-06-11 02:03:58 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-02-27 21:46:14 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2019-06-11 02:03:58 +00:00
|
|
|
"github.com/docker/docker/internal/test/fakecontext"
|
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2018-06-11 13:32:11 +00:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
|
|
|
"gotest.tools/skip"
|
2018-02-27 21:46:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCopyFromContainerPathDoesNotExist(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
2018-04-19 09:14:15 +00:00
|
|
|
skip.If(t, testEnv.OSType == "windows")
|
2018-02-27 21:46:14 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
apiclient := testEnv.APIClient()
|
2019-06-06 11:00:37 +00:00
|
|
|
cid := container.Create(ctx, t, apiclient)
|
2018-02-27 21:46:14 +00:00
|
|
|
|
|
|
|
_, _, err := apiclient.CopyFromContainer(ctx, cid, "/dne")
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, client.IsErrNotFound(err))
|
2018-02-27 21:46:14 +00:00
|
|
|
expected := fmt.Sprintf("No such container:path: %s:%s", cid, "/dne")
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, expected))
|
2018-02-27 21:46:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCopyFromContainerPathIsNotDir(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
skip.If(t, testEnv.OSType == "windows")
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
apiclient := testEnv.APIClient()
|
2019-06-06 11:00:37 +00:00
|
|
|
cid := container.Create(ctx, t, apiclient)
|
2018-02-27 21:46:14 +00:00
|
|
|
|
|
|
|
_, _, err := apiclient.CopyFromContainer(ctx, cid, "/etc/passwd/")
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Assert(t, is.ErrorContains(err, "not a directory"))
|
2018-02-27 21:46:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCopyToContainerPathDoesNotExist(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
skip.If(t, testEnv.OSType == "windows")
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
apiclient := testEnv.APIClient()
|
2019-06-06 11:00:37 +00:00
|
|
|
cid := container.Create(ctx, t, apiclient)
|
2018-02-27 21:46:14 +00:00
|
|
|
|
|
|
|
err := apiclient.CopyToContainer(ctx, cid, "/dne", nil, types.CopyToContainerOptions{})
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, client.IsErrNotFound(err))
|
2018-02-27 21:46:14 +00:00
|
|
|
expected := fmt.Sprintf("No such container:path: %s:%s", cid, "/dne")
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, expected))
|
2018-02-27 21:46:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCopyToContainerPathIsNotDir(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
skip.If(t, testEnv.OSType == "windows")
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
apiclient := testEnv.APIClient()
|
2019-06-06 11:00:37 +00:00
|
|
|
cid := container.Create(ctx, t, apiclient)
|
2018-02-27 21:46:14 +00:00
|
|
|
|
|
|
|
err := apiclient.CopyToContainer(ctx, cid, "/etc/passwd/", nil, types.CopyToContainerOptions{})
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Assert(t, is.ErrorContains(err, "not a directory"))
|
2018-02-27 21:46:14 +00:00
|
|
|
}
|
2019-06-11 02:03:58 +00:00
|
|
|
|
|
|
|
func TestCopyFromContainerRoot(t *testing.T) {
|
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
|
|
|
defer setupTest(t)()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
apiClient := testEnv.APIClient()
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
buildCtx := fakecontext.New(t, dir, fakecontext.WithFile("foo", "hello"), fakecontext.WithFile("baz", "world"), fakecontext.WithDockerfile(`
|
|
|
|
FROM scratch
|
|
|
|
COPY foo /foo
|
|
|
|
COPY baz /bar/baz
|
|
|
|
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
|
|
|
|
err = jsonmessage.DisplayJSONMessagesStream(resp.Body, ioutil.Discard, 0, false, func(msg jsonmessage.JSONMessage) {
|
|
|
|
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))
|
|
|
|
|
|
|
|
rdr, _, err := apiClient.CopyFromContainer(ctx, cid, "/")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer rdr.Close()
|
|
|
|
|
|
|
|
tr := tar.NewReader(rdr)
|
|
|
|
expect := map[string]string{
|
|
|
|
"/foo": "hello",
|
|
|
|
"/bar/baz": "world",
|
|
|
|
}
|
|
|
|
found := make(map[string]bool, 2)
|
|
|
|
var numFound int
|
|
|
|
for {
|
|
|
|
h, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
expected, exists := 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
|
|
|
|
|
|
|
|
buf, err := ioutil.ReadAll(tr)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(string(buf), expected))
|
|
|
|
|
|
|
|
if numFound == len(expect) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Check(t, found["/foo"], "/foo file not found in archive")
|
|
|
|
assert.Check(t, found["/bar/baz"], "/bar/baz file not found in archive")
|
|
|
|
}
|