image_load_test.go 2.6 KB

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