image_push_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/api/types"
  11. "github.com/docker/docker/errdefs"
  12. )
  13. func TestImagePushReferenceError(t *testing.T) {
  14. client := &Client{
  15. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  16. return nil, nil
  17. }),
  18. }
  19. // An empty reference is an invalid reference
  20. _, err := client.ImagePush(context.Background(), "", types.ImagePushOptions{})
  21. if err == nil || !strings.Contains(err.Error(), "invalid reference format") {
  22. t.Fatalf("expected an error, got %v", err)
  23. }
  24. // An canonical reference cannot be pushed
  25. _, err = client.ImagePush(context.Background(), "repo@sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", types.ImagePushOptions{})
  26. if err == nil || err.Error() != "cannot push a digest reference" {
  27. t.Fatalf("expected an error, got %v", err)
  28. }
  29. }
  30. func TestImagePushAnyError(t *testing.T) {
  31. client := &Client{
  32. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  33. }
  34. _, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{})
  35. if !errdefs.IsSystem(err) {
  36. t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
  37. }
  38. }
  39. func TestImagePushStatusUnauthorizedError(t *testing.T) {
  40. client := &Client{
  41. client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
  42. }
  43. _, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{})
  44. if !errdefs.IsUnauthorized(err) {
  45. t.Fatalf("expected a Unauthorized Error, got %[1]T: %[1]v", err)
  46. }
  47. }
  48. func TestImagePushWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
  49. client := &Client{
  50. client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
  51. }
  52. privilegeFunc := func() (string, error) {
  53. return "", fmt.Errorf("Error requesting privilege")
  54. }
  55. _, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{
  56. PrivilegeFunc: privilegeFunc,
  57. })
  58. if err == nil || err.Error() != "Error requesting privilege" {
  59. t.Fatalf("expected an error requesting privilege, got %v", err)
  60. }
  61. }
  62. func TestImagePushWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) {
  63. client := &Client{
  64. client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
  65. }
  66. privilegeFunc := func() (string, error) {
  67. return "a-auth-header", nil
  68. }
  69. _, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{
  70. PrivilegeFunc: privilegeFunc,
  71. })
  72. if !errdefs.IsUnauthorized(err) {
  73. t.Fatalf("expected a Unauthorized Error, got %[1]T: %[1]v", err)
  74. }
  75. }
  76. func TestImagePushWithPrivilegedFuncNoError(t *testing.T) {
  77. expectedURL := "/images/myimage/push"
  78. client := &Client{
  79. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  80. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  81. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  82. }
  83. auth := req.Header.Get("X-Registry-Auth")
  84. if auth == "NotValid" {
  85. return &http.Response{
  86. StatusCode: http.StatusUnauthorized,
  87. Body: io.NopCloser(bytes.NewReader([]byte("Invalid credentials"))),
  88. }, nil
  89. }
  90. if auth != "IAmValid" {
  91. return nil, fmt.Errorf("Invalid auth header : expected %s, got %s", "IAmValid", auth)
  92. }
  93. query := req.URL.Query()
  94. tag := query.Get("tag")
  95. if tag != "tag" {
  96. return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", "tag", tag)
  97. }
  98. return &http.Response{
  99. StatusCode: http.StatusOK,
  100. Body: io.NopCloser(bytes.NewReader([]byte("hello world"))),
  101. }, nil
  102. }),
  103. }
  104. privilegeFunc := func() (string, error) {
  105. return "IAmValid", nil
  106. }
  107. resp, err := client.ImagePush(context.Background(), "myimage:tag", types.ImagePushOptions{
  108. RegistryAuth: "NotValid",
  109. PrivilegeFunc: privilegeFunc,
  110. })
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. body, err := io.ReadAll(resp)
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. if string(body) != "hello world" {
  119. t.Fatalf("expected 'hello world', got %s", string(body))
  120. }
  121. }
  122. func TestImagePushWithoutErrors(t *testing.T) {
  123. expectedOutput := "hello world"
  124. expectedURLFormat := "/images/%s/push"
  125. testCases := []struct {
  126. all bool
  127. reference string
  128. expectedImage string
  129. expectedTag string
  130. }{
  131. {
  132. all: false,
  133. reference: "myimage",
  134. expectedImage: "myimage",
  135. expectedTag: "latest",
  136. },
  137. {
  138. all: false,
  139. reference: "myimage:tag",
  140. expectedImage: "myimage",
  141. expectedTag: "tag",
  142. },
  143. {
  144. all: true,
  145. reference: "myimage",
  146. expectedImage: "myimage",
  147. expectedTag: "",
  148. },
  149. {
  150. all: true,
  151. reference: "myimage:anything",
  152. expectedImage: "myimage",
  153. expectedTag: "",
  154. },
  155. }
  156. for _, tc := range testCases {
  157. tc := tc
  158. t.Run(fmt.Sprintf("%s,all-tags=%t", tc.reference, tc.all), func(t *testing.T) {
  159. client := &Client{
  160. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  161. expectedURL := fmt.Sprintf(expectedURLFormat, tc.expectedImage)
  162. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  163. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  164. }
  165. query := req.URL.Query()
  166. tag := query.Get("tag")
  167. if tag != tc.expectedTag {
  168. return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", tc.expectedTag, tag)
  169. }
  170. return &http.Response{
  171. StatusCode: http.StatusOK,
  172. Body: io.NopCloser(bytes.NewReader([]byte(expectedOutput))),
  173. }, nil
  174. }),
  175. }
  176. resp, err := client.ImagePush(context.Background(), tc.reference, types.ImagePushOptions{
  177. All: tc.all,
  178. })
  179. if err != nil {
  180. t.Fatal(err)
  181. }
  182. body, err := io.ReadAll(resp)
  183. if err != nil {
  184. t.Fatal(err)
  185. }
  186. if string(body) != expectedOutput {
  187. t.Fatalf("expected '%s', got %s", expectedOutput, string(body))
  188. }
  189. })
  190. }
  191. }