container_copy_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/base64"
  6. "encoding/json"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "strings"
  11. "testing"
  12. "github.com/docker/docker/api/types"
  13. "github.com/docker/docker/errdefs"
  14. "gotest.tools/v3/assert"
  15. is "gotest.tools/v3/assert/cmp"
  16. )
  17. func TestContainerStatPathError(t *testing.T) {
  18. client := &Client{
  19. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  20. }
  21. _, err := client.ContainerStatPath(context.Background(), "container_id", "path")
  22. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  23. }
  24. func TestContainerStatPathNotFoundError(t *testing.T) {
  25. client := &Client{
  26. client: newMockClient(errorMock(http.StatusNotFound, "Not found")),
  27. }
  28. _, err := client.ContainerStatPath(context.Background(), "container_id", "path")
  29. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  30. }
  31. func TestContainerStatPathNoHeaderError(t *testing.T) {
  32. client := &Client{
  33. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  34. return &http.Response{
  35. StatusCode: http.StatusOK,
  36. Body: io.NopCloser(bytes.NewReader([]byte(""))),
  37. }, nil
  38. }),
  39. }
  40. _, err := client.ContainerStatPath(context.Background(), "container_id", "path/to/file")
  41. if err == nil {
  42. t.Fatalf("expected an error, got nothing")
  43. }
  44. }
  45. func TestContainerStatPath(t *testing.T) {
  46. expectedURL := "/containers/container_id/archive"
  47. expectedPath := "path/to/file"
  48. client := &Client{
  49. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  50. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  51. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  52. }
  53. if req.Method != http.MethodHead {
  54. return nil, fmt.Errorf("expected HEAD method, got %s", req.Method)
  55. }
  56. query := req.URL.Query()
  57. path := query.Get("path")
  58. if path != expectedPath {
  59. return nil, fmt.Errorf("path not set in URL query properly")
  60. }
  61. content, err := json.Marshal(types.ContainerPathStat{
  62. Name: "name",
  63. Mode: 0o700,
  64. })
  65. if err != nil {
  66. return nil, err
  67. }
  68. base64PathStat := base64.StdEncoding.EncodeToString(content)
  69. return &http.Response{
  70. StatusCode: http.StatusOK,
  71. Body: io.NopCloser(bytes.NewReader([]byte(""))),
  72. Header: http.Header{
  73. "X-Docker-Container-Path-Stat": []string{base64PathStat},
  74. },
  75. }, nil
  76. }),
  77. }
  78. stat, err := client.ContainerStatPath(context.Background(), "container_id", expectedPath)
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. if stat.Name != "name" {
  83. t.Fatalf("expected container path stat name to be 'name', got '%s'", stat.Name)
  84. }
  85. if stat.Mode != 0o700 {
  86. t.Fatalf("expected container path stat mode to be 0700, got '%v'", stat.Mode)
  87. }
  88. }
  89. func TestCopyToContainerError(t *testing.T) {
  90. client := &Client{
  91. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  92. }
  93. err := client.CopyToContainer(context.Background(), "container_id", "path/to/file", bytes.NewReader([]byte("")), types.CopyToContainerOptions{})
  94. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  95. }
  96. func TestCopyToContainerNotFoundError(t *testing.T) {
  97. client := &Client{
  98. client: newMockClient(errorMock(http.StatusNotFound, "Not found")),
  99. }
  100. err := client.CopyToContainer(context.Background(), "container_id", "path/to/file", bytes.NewReader([]byte("")), types.CopyToContainerOptions{})
  101. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  102. }
  103. // TestCopyToContainerEmptyResponse verifies that no error is returned when a
  104. // "204 No Content" is returned by the API.
  105. func TestCopyToContainerEmptyResponse(t *testing.T) {
  106. client := &Client{
  107. client: newMockClient(errorMock(http.StatusNoContent, "No content")),
  108. }
  109. err := client.CopyToContainer(context.Background(), "container_id", "path/to/file", bytes.NewReader([]byte("")), types.CopyToContainerOptions{})
  110. if err != nil {
  111. t.Fatalf("unexpected error: %v", err)
  112. }
  113. }
  114. func TestCopyToContainer(t *testing.T) {
  115. expectedURL := "/containers/container_id/archive"
  116. expectedPath := "path/to/file"
  117. client := &Client{
  118. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  119. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  120. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  121. }
  122. if req.Method != http.MethodPut {
  123. return nil, fmt.Errorf("expected PUT method, got %s", req.Method)
  124. }
  125. query := req.URL.Query()
  126. path := query.Get("path")
  127. if path != expectedPath {
  128. return nil, fmt.Errorf("path not set in URL query properly, expected '%s', got %s", expectedPath, path)
  129. }
  130. noOverwriteDirNonDir := query.Get("noOverwriteDirNonDir")
  131. if noOverwriteDirNonDir != "true" {
  132. return nil, fmt.Errorf("noOverwriteDirNonDir not set in URL query properly, expected true, got %s", noOverwriteDirNonDir)
  133. }
  134. content, err := io.ReadAll(req.Body)
  135. if err != nil {
  136. return nil, err
  137. }
  138. if err := req.Body.Close(); err != nil {
  139. return nil, err
  140. }
  141. if string(content) != "content" {
  142. return nil, fmt.Errorf("expected content to be 'content', got %s", string(content))
  143. }
  144. return &http.Response{
  145. StatusCode: http.StatusOK,
  146. Body: io.NopCloser(bytes.NewReader([]byte(""))),
  147. }, nil
  148. }),
  149. }
  150. err := client.CopyToContainer(context.Background(), "container_id", expectedPath, bytes.NewReader([]byte("content")), types.CopyToContainerOptions{
  151. AllowOverwriteDirWithFile: false,
  152. })
  153. if err != nil {
  154. t.Fatal(err)
  155. }
  156. }
  157. func TestCopyFromContainerError(t *testing.T) {
  158. client := &Client{
  159. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  160. }
  161. _, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
  162. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  163. }
  164. func TestCopyFromContainerNotFoundError(t *testing.T) {
  165. client := &Client{
  166. client: newMockClient(errorMock(http.StatusNotFound, "Not found")),
  167. }
  168. _, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
  169. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  170. }
  171. // TestCopyFromContainerEmptyResponse verifies that no error is returned when a
  172. // "204 No Content" is returned by the API.
  173. func TestCopyFromContainerEmptyResponse(t *testing.T) {
  174. client := &Client{
  175. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  176. content, err := json.Marshal(types.ContainerPathStat{
  177. Name: "path/to/file",
  178. Mode: 0o700,
  179. })
  180. if err != nil {
  181. return nil, err
  182. }
  183. base64PathStat := base64.StdEncoding.EncodeToString(content)
  184. return &http.Response{
  185. StatusCode: http.StatusNoContent,
  186. Header: http.Header{
  187. "X-Docker-Container-Path-Stat": []string{base64PathStat},
  188. },
  189. }, nil
  190. }),
  191. }
  192. _, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
  193. if err != nil {
  194. t.Fatalf("unexpected error: %v", err)
  195. }
  196. }
  197. func TestCopyFromContainerNoHeaderError(t *testing.T) {
  198. client := &Client{
  199. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  200. return &http.Response{
  201. StatusCode: http.StatusOK,
  202. Body: io.NopCloser(bytes.NewReader([]byte(""))),
  203. }, nil
  204. }),
  205. }
  206. _, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
  207. if err == nil {
  208. t.Fatalf("expected an error, got nothing")
  209. }
  210. }
  211. func TestCopyFromContainer(t *testing.T) {
  212. expectedURL := "/containers/container_id/archive"
  213. expectedPath := "path/to/file"
  214. client := &Client{
  215. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  216. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  217. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  218. }
  219. if req.Method != http.MethodGet {
  220. return nil, fmt.Errorf("expected GET method, got %s", req.Method)
  221. }
  222. query := req.URL.Query()
  223. path := query.Get("path")
  224. if path != expectedPath {
  225. return nil, fmt.Errorf("path not set in URL query properly, expected '%s', got %s", expectedPath, path)
  226. }
  227. headercontent, err := json.Marshal(types.ContainerPathStat{
  228. Name: "name",
  229. Mode: 0o700,
  230. })
  231. if err != nil {
  232. return nil, err
  233. }
  234. base64PathStat := base64.StdEncoding.EncodeToString(headercontent)
  235. return &http.Response{
  236. StatusCode: http.StatusOK,
  237. Body: io.NopCloser(bytes.NewReader([]byte("content"))),
  238. Header: http.Header{
  239. "X-Docker-Container-Path-Stat": []string{base64PathStat},
  240. },
  241. }, nil
  242. }),
  243. }
  244. r, stat, err := client.CopyFromContainer(context.Background(), "container_id", expectedPath)
  245. if err != nil {
  246. t.Fatal(err)
  247. }
  248. if stat.Name != "name" {
  249. t.Fatalf("expected container path stat name to be 'name', got '%s'", stat.Name)
  250. }
  251. if stat.Mode != 0o700 {
  252. t.Fatalf("expected container path stat mode to be 0700, got '%v'", stat.Mode)
  253. }
  254. content, err := io.ReadAll(r)
  255. if err != nil {
  256. t.Fatal(err)
  257. }
  258. if err := r.Close(); err != nil {
  259. t.Fatal(err)
  260. }
  261. if string(content) != "content" {
  262. t.Fatalf("expected content to be 'content', got %s", string(content))
  263. }
  264. }