copy_test.go 7.7 KB

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