github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/integration/container/copy_test.go (about) 1 package container // import "github.com/docker/docker/integration/container" 2 3 import ( 4 "archive/tar" 5 "context" 6 "encoding/json" 7 "fmt" 8 "io" 9 "os" 10 "testing" 11 12 "github.com/docker/docker/api/types" 13 "github.com/docker/docker/client" 14 "github.com/docker/docker/integration/internal/container" 15 "github.com/docker/docker/pkg/jsonmessage" 16 "github.com/docker/docker/testutil/fakecontext" 17 "gotest.tools/v3/assert" 18 is "gotest.tools/v3/assert/cmp" 19 "gotest.tools/v3/skip" 20 ) 21 22 func TestCopyFromContainerPathDoesNotExist(t *testing.T) { 23 defer setupTest(t)() 24 25 ctx := context.Background() 26 apiclient := testEnv.APIClient() 27 cid := container.Create(ctx, t, apiclient) 28 29 _, _, err := apiclient.CopyFromContainer(ctx, cid, "/dne") 30 assert.Check(t, client.IsErrNotFound(err)) 31 expected := fmt.Sprintf("No such container:path: %s:%s", cid, "/dne") 32 assert.Check(t, is.ErrorContains(err, expected)) 33 } 34 35 func TestCopyFromContainerPathIsNotDir(t *testing.T) { 36 defer setupTest(t)() 37 skip.If(t, testEnv.OSType == "windows") 38 39 ctx := context.Background() 40 apiclient := testEnv.APIClient() 41 cid := container.Create(ctx, t, apiclient) 42 43 _, _, err := apiclient.CopyFromContainer(ctx, cid, "/etc/passwd/") 44 assert.Assert(t, is.ErrorContains(err, "not a directory")) 45 } 46 47 func TestCopyToContainerPathDoesNotExist(t *testing.T) { 48 defer setupTest(t)() 49 50 ctx := context.Background() 51 apiclient := testEnv.APIClient() 52 cid := container.Create(ctx, t, apiclient) 53 54 err := apiclient.CopyToContainer(ctx, cid, "/dne", nil, types.CopyToContainerOptions{}) 55 assert.Check(t, client.IsErrNotFound(err)) 56 expected := fmt.Sprintf("No such container:path: %s:%s", cid, "/dne") 57 assert.Check(t, is.ErrorContains(err, expected)) 58 } 59 60 func TestCopyToContainerPathIsNotDir(t *testing.T) { 61 defer setupTest(t)() 62 skip.If(t, testEnv.OSType == "windows") 63 64 ctx := context.Background() 65 apiclient := testEnv.APIClient() 66 cid := container.Create(ctx, t, apiclient) 67 68 err := apiclient.CopyToContainer(ctx, cid, "/etc/passwd/", nil, types.CopyToContainerOptions{}) 69 assert.Assert(t, is.ErrorContains(err, "not a directory")) 70 } 71 72 func TestCopyFromContainer(t *testing.T) { 73 skip.If(t, testEnv.DaemonInfo.OSType == "windows") 74 defer setupTest(t)() 75 76 ctx := context.Background() 77 apiClient := testEnv.APIClient() 78 79 dir, err := os.MkdirTemp("", t.Name()) 80 assert.NilError(t, err) 81 defer os.RemoveAll(dir) 82 83 buildCtx := fakecontext.New(t, dir, fakecontext.WithFile("foo", "hello"), fakecontext.WithFile("baz", "world"), fakecontext.WithDockerfile(` 84 FROM busybox 85 COPY foo /foo 86 COPY baz /bar/quux/baz 87 RUN ln -s notexist /bar/notarget && ln -s quux/baz /bar/filesymlink && ln -s quux /bar/dirsymlink && ln -s / /bar/root 88 CMD /fake 89 `)) 90 defer buildCtx.Close() 91 92 resp, err := apiClient.ImageBuild(ctx, buildCtx.AsTarReader(t), types.ImageBuildOptions{}) 93 assert.NilError(t, err) 94 defer resp.Body.Close() 95 96 var imageID string 97 err = jsonmessage.DisplayJSONMessagesStream(resp.Body, io.Discard, 0, false, func(msg jsonmessage.JSONMessage) { 98 var r types.BuildResult 99 assert.NilError(t, json.Unmarshal(*msg.Aux, &r)) 100 imageID = r.ID 101 }) 102 assert.NilError(t, err) 103 assert.Assert(t, imageID != "") 104 105 cid := container.Create(ctx, t, apiClient, container.WithImage(imageID)) 106 107 for _, x := range []struct { 108 src string 109 expect map[string]string 110 }{ 111 {"/", map[string]string{"/": "", "/foo": "hello", "/bar/quux/baz": "world", "/bar/filesymlink": "", "/bar/dirsymlink": "", "/bar/notarget": ""}}, 112 {"/bar/root", map[string]string{"root": ""}}, 113 {"/bar/root/", map[string]string{"root/": "", "root/foo": "hello", "root/bar/quux/baz": "world", "root/bar/filesymlink": "", "root/bar/dirsymlink": "", "root/bar/notarget": ""}}, 114 115 {"bar/quux", map[string]string{"quux/": "", "quux/baz": "world"}}, 116 {"bar/quux/", map[string]string{"quux/": "", "quux/baz": "world"}}, 117 {"bar/quux/baz", map[string]string{"baz": "world"}}, 118 119 {"bar/filesymlink", map[string]string{"filesymlink": ""}}, 120 {"bar/dirsymlink", map[string]string{"dirsymlink": ""}}, 121 {"bar/dirsymlink/", map[string]string{"dirsymlink/": "", "dirsymlink/baz": "world"}}, 122 {"bar/notarget", map[string]string{"notarget": ""}}, 123 } { 124 t.Run(x.src, func(t *testing.T) { 125 rdr, _, err := apiClient.CopyFromContainer(ctx, cid, x.src) 126 assert.NilError(t, err) 127 defer rdr.Close() 128 129 found := make(map[string]bool, len(x.expect)) 130 var numFound int 131 tr := tar.NewReader(rdr) 132 for numFound < len(x.expect) { 133 h, err := tr.Next() 134 if err == io.EOF { 135 break 136 } 137 assert.NilError(t, err) 138 139 expected, exists := x.expect[h.Name] 140 if !exists { 141 // this archive will have extra stuff in it since we are copying from root 142 // and docker adds a bunch of stuff 143 continue 144 } 145 146 numFound++ 147 found[h.Name] = true 148 149 buf, err := io.ReadAll(tr) 150 if err == nil { 151 assert.Check(t, is.Equal(string(buf), expected)) 152 } 153 } 154 155 for f := range x.expect { 156 assert.Check(t, found[f], f+" not found in archive") 157 } 158 }) 159 } 160 }