image_load_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/errdefs"
  11. "gotest.tools/v3/assert"
  12. is "gotest.tools/v3/assert/cmp"
  13. )
  14. func TestImageLoadError(t *testing.T) {
  15. client := &Client{
  16. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  17. }
  18. _, err := client.ImageLoad(context.Background(), nil, true)
  19. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  20. }
  21. func TestImageLoad(t *testing.T) {
  22. expectedURL := "/images/load"
  23. expectedInput := "inputBody"
  24. expectedOutput := "outputBody"
  25. loadCases := []struct {
  26. quiet bool
  27. responseContentType string
  28. expectedResponseJSON bool
  29. expectedQueryParams map[string]string
  30. }{
  31. {
  32. quiet: false,
  33. responseContentType: "text/plain",
  34. expectedResponseJSON: false,
  35. expectedQueryParams: map[string]string{
  36. "quiet": "0",
  37. },
  38. },
  39. {
  40. quiet: true,
  41. responseContentType: "application/json",
  42. expectedResponseJSON: true,
  43. expectedQueryParams: map[string]string{
  44. "quiet": "1",
  45. },
  46. },
  47. }
  48. for _, loadCase := range loadCases {
  49. client := &Client{
  50. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  51. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  52. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  53. }
  54. contentType := req.Header.Get("Content-Type")
  55. if contentType != "application/x-tar" {
  56. return nil, fmt.Errorf("content-type not set in URL headers properly. Expected 'application/x-tar', got %s", contentType)
  57. }
  58. query := req.URL.Query()
  59. for key, expected := range loadCase.expectedQueryParams {
  60. actual := query.Get(key)
  61. if actual != expected {
  62. return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
  63. }
  64. }
  65. headers := http.Header{}
  66. headers.Add("Content-Type", loadCase.responseContentType)
  67. return &http.Response{
  68. StatusCode: http.StatusOK,
  69. Body: io.NopCloser(bytes.NewReader([]byte(expectedOutput))),
  70. Header: headers,
  71. }, nil
  72. }),
  73. }
  74. input := bytes.NewReader([]byte(expectedInput))
  75. imageLoadResponse, err := client.ImageLoad(context.Background(), input, loadCase.quiet)
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. if imageLoadResponse.JSON != loadCase.expectedResponseJSON {
  80. t.Fatalf("expected a JSON response, was not.")
  81. }
  82. body, err := io.ReadAll(imageLoadResponse.Body)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. if string(body) != expectedOutput {
  87. t.Fatalf("expected %s, got %s", expectedOutput, string(body))
  88. }
  89. }
  90. }