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