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