copy_test.go 7.5 KB

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