copy_test.go 6.3 KB

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