copy_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "archive/tar"
  4. "bytes"
  5. "encoding/json"
  6. "io"
  7. "os"
  8. "path/filepath"
  9. "testing"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/errdefs"
  12. "github.com/docker/docker/integration/internal/container"
  13. "github.com/docker/docker/pkg/archive"
  14. "github.com/docker/docker/pkg/jsonmessage"
  15. "github.com/docker/docker/testutil/fakecontext"
  16. "gotest.tools/v3/assert"
  17. is "gotest.tools/v3/assert/cmp"
  18. "gotest.tools/v3/skip"
  19. )
  20. func TestCopyFromContainerPathDoesNotExist(t *testing.T) {
  21. ctx := setupTest(t)
  22. apiClient := testEnv.APIClient()
  23. cid := container.Create(ctx, t, apiClient)
  24. _, _, err := apiClient.CopyFromContainer(ctx, cid, "/dne")
  25. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  26. assert.Check(t, is.ErrorContains(err, "Could not find the file /dne in container "+cid))
  27. }
  28. func TestCopyFromContainerPathIsNotDir(t *testing.T) {
  29. skip.If(t, testEnv.UsingSnapshotter(), "FIXME: https://github.com/moby/moby/issues/47107")
  30. ctx := setupTest(t)
  31. apiClient := testEnv.APIClient()
  32. cid := container.Create(ctx, t, apiClient)
  33. path := "/etc/passwd/"
  34. expected := "not a directory"
  35. if testEnv.DaemonInfo.OSType == "windows" {
  36. path = "c:/windows/system32/drivers/etc/hosts/"
  37. expected = "The filename, directory name, or volume label syntax is incorrect."
  38. }
  39. _, _, err := apiClient.CopyFromContainer(ctx, cid, path)
  40. assert.Assert(t, is.ErrorContains(err, expected))
  41. }
  42. func TestCopyToContainerPathDoesNotExist(t *testing.T) {
  43. ctx := setupTest(t)
  44. apiClient := testEnv.APIClient()
  45. cid := container.Create(ctx, t, apiClient)
  46. err := apiClient.CopyToContainer(ctx, cid, "/dne", nil, types.CopyToContainerOptions{})
  47. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  48. assert.Check(t, is.ErrorContains(err, "Could not find the file /dne in container "+cid))
  49. }
  50. func TestCopyEmptyFile(t *testing.T) {
  51. ctx := setupTest(t)
  52. apiClient := testEnv.APIClient()
  53. cid := container.Create(ctx, t, apiClient)
  54. // empty content
  55. dstDir, _ := makeEmptyArchive(t)
  56. err := apiClient.CopyToContainer(ctx, cid, dstDir, bytes.NewReader([]byte("")), types.CopyToContainerOptions{})
  57. assert.NilError(t, err)
  58. // tar with empty file
  59. dstDir, preparedArchive := makeEmptyArchive(t)
  60. err = apiClient.CopyToContainer(ctx, cid, dstDir, preparedArchive, types.CopyToContainerOptions{})
  61. assert.NilError(t, err)
  62. // tar with empty file archive mode
  63. dstDir, preparedArchive = makeEmptyArchive(t)
  64. err = apiClient.CopyToContainer(ctx, cid, dstDir, preparedArchive, types.CopyToContainerOptions{
  65. CopyUIDGID: true,
  66. })
  67. assert.NilError(t, err)
  68. // copy from empty file
  69. rdr, _, err := apiClient.CopyFromContainer(ctx, cid, dstDir)
  70. assert.NilError(t, err)
  71. defer rdr.Close()
  72. }
  73. func makeEmptyArchive(t *testing.T) (string, io.ReadCloser) {
  74. tmpDir := t.TempDir()
  75. srcPath := filepath.Join(tmpDir, "empty-file.txt")
  76. err := os.WriteFile(srcPath, []byte(""), 0o400)
  77. assert.NilError(t, err)
  78. // TODO(thaJeztah) Add utilities to the client to make steps below less complicated.
  79. // Code below is taken from copyToContainer() in docker/cli.
  80. srcInfo, err := archive.CopyInfoSourcePath(srcPath, false)
  81. assert.NilError(t, err)
  82. srcArchive, err := archive.TarResource(srcInfo)
  83. assert.NilError(t, err)
  84. t.Cleanup(func() {
  85. srcArchive.Close()
  86. })
  87. ctrPath := "/empty-file.txt"
  88. dstInfo := archive.CopyInfo{Path: ctrPath}
  89. dstDir, preparedArchive, err := archive.PrepareArchiveCopy(srcArchive, srcInfo, dstInfo)
  90. assert.NilError(t, err)
  91. t.Cleanup(func() {
  92. preparedArchive.Close()
  93. })
  94. return dstDir, preparedArchive
  95. }
  96. func TestCopyToContainerPathIsNotDir(t *testing.T) {
  97. ctx := setupTest(t)
  98. apiClient := testEnv.APIClient()
  99. cid := container.Create(ctx, t, apiClient)
  100. path := "/etc/passwd/"
  101. if testEnv.DaemonInfo.OSType == "windows" {
  102. path = "c:/windows/system32/drivers/etc/hosts/"
  103. }
  104. err := apiClient.CopyToContainer(ctx, cid, path, nil, types.CopyToContainerOptions{})
  105. assert.Check(t, is.ErrorContains(err, "not a directory"))
  106. }
  107. func TestCopyFromContainer(t *testing.T) {
  108. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  109. ctx := setupTest(t)
  110. apiClient := testEnv.APIClient()
  111. dir, err := os.MkdirTemp("", t.Name())
  112. assert.NilError(t, err)
  113. defer os.RemoveAll(dir)
  114. buildCtx := fakecontext.New(t, dir, fakecontext.WithFile("foo", "hello"), fakecontext.WithFile("baz", "world"), fakecontext.WithDockerfile(`
  115. FROM busybox
  116. COPY foo /foo
  117. COPY baz /bar/quux/baz
  118. RUN ln -s notexist /bar/notarget && ln -s quux/baz /bar/filesymlink && ln -s quux /bar/dirsymlink && ln -s / /bar/root
  119. CMD /fake
  120. `))
  121. defer buildCtx.Close()
  122. resp, err := apiClient.ImageBuild(ctx, buildCtx.AsTarReader(t), types.ImageBuildOptions{})
  123. assert.NilError(t, err)
  124. defer resp.Body.Close()
  125. var imageID string
  126. err = jsonmessage.DisplayJSONMessagesStream(resp.Body, io.Discard, 0, false, func(msg jsonmessage.JSONMessage) {
  127. var r types.BuildResult
  128. assert.NilError(t, json.Unmarshal(*msg.Aux, &r))
  129. imageID = r.ID
  130. })
  131. assert.NilError(t, err)
  132. assert.Assert(t, imageID != "")
  133. cid := container.Create(ctx, t, apiClient, container.WithImage(imageID))
  134. for _, x := range []struct {
  135. src string
  136. expect map[string]string
  137. }{
  138. {"/", map[string]string{"/": "", "/foo": "hello", "/bar/quux/baz": "world", "/bar/filesymlink": "", "/bar/dirsymlink": "", "/bar/notarget": ""}},
  139. {".", map[string]string{"./": "", "./foo": "hello", "./bar/quux/baz": "world", "./bar/filesymlink": "", "./bar/dirsymlink": "", "./bar/notarget": ""}},
  140. {"/.", map[string]string{"./": "", "./foo": "hello", "./bar/quux/baz": "world", "./bar/filesymlink": "", "./bar/dirsymlink": "", "./bar/notarget": ""}},
  141. {"./", map[string]string{"./": "", "./foo": "hello", "./bar/quux/baz": "world", "./bar/filesymlink": "", "./bar/dirsymlink": "", "./bar/notarget": ""}},
  142. {"/./", map[string]string{"./": "", "./foo": "hello", "./bar/quux/baz": "world", "./bar/filesymlink": "", "./bar/dirsymlink": "", "./bar/notarget": ""}},
  143. {"/bar/root", map[string]string{"root": ""}},
  144. {"/bar/root/", map[string]string{"root/": "", "root/foo": "hello", "root/bar/quux/baz": "world", "root/bar/filesymlink": "", "root/bar/dirsymlink": "", "root/bar/notarget": ""}},
  145. {"/bar/root/.", map[string]string{"./": "", "./foo": "hello", "./bar/quux/baz": "world", "./bar/filesymlink": "", "./bar/dirsymlink": "", "./bar/notarget": ""}},
  146. {"bar/quux", map[string]string{"quux/": "", "quux/baz": "world"}},
  147. {"bar/quux/", map[string]string{"quux/": "", "quux/baz": "world"}},
  148. {"bar/quux/.", map[string]string{"./": "", "./baz": "world"}},
  149. {"bar/quux/baz", map[string]string{"baz": "world"}},
  150. {"bar/filesymlink", map[string]string{"filesymlink": ""}},
  151. {"bar/dirsymlink", map[string]string{"dirsymlink": ""}},
  152. {"bar/dirsymlink/", map[string]string{"dirsymlink/": "", "dirsymlink/baz": "world"}},
  153. {"bar/dirsymlink/.", map[string]string{"./": "", "./baz": "world"}},
  154. {"bar/notarget", map[string]string{"notarget": ""}},
  155. } {
  156. t.Run(x.src, func(t *testing.T) {
  157. rdr, _, err := apiClient.CopyFromContainer(ctx, cid, x.src)
  158. assert.NilError(t, err)
  159. defer rdr.Close()
  160. found := make(map[string]bool, len(x.expect))
  161. var numFound int
  162. tr := tar.NewReader(rdr)
  163. for numFound < len(x.expect) {
  164. h, err := tr.Next()
  165. if err == io.EOF {
  166. break
  167. }
  168. assert.NilError(t, err)
  169. expected, exists := x.expect[h.Name]
  170. if !exists {
  171. // this archive will have extra stuff in it since we are copying from root
  172. // and docker adds a bunch of stuff
  173. continue
  174. }
  175. numFound++
  176. found[h.Name] = true
  177. buf, err := io.ReadAll(tr)
  178. if err == nil {
  179. assert.Check(t, is.Equal(string(buf), expected))
  180. }
  181. }
  182. for f := range x.expect {
  183. assert.Check(t, found[f], f+" not found in archive")
  184. }
  185. })
  186. }
  187. }