2018-02-05 16:05:59 -05:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 14:46:37 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2016-09-06 14:46:37 -04:00
|
|
|
"fmt"
|
2021-08-24 06:10:50 -04:00
|
|
|
"io"
|
2016-09-06 14:46:37 -04:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2018-12-31 12:22:43 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2016-09-06 14:46:37 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestImagePushReferenceError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-08 23:44:25 -04:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 14:46:37 -04:00
|
|
|
return nil, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
// An empty reference is an invalid reference
|
|
|
|
_, err := client.ImagePush(context.Background(), "", types.ImagePushOptions{})
|
2017-01-25 19:54:18 -05:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "invalid reference format") {
|
2016-09-06 14:46:37 -04:00
|
|
|
t.Fatalf("expected an error, got %v", err)
|
|
|
|
}
|
|
|
|
// An canonical reference cannot be pushed
|
|
|
|
_, err = client.ImagePush(context.Background(), "repo@sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", types.ImagePushOptions{})
|
|
|
|
if err == nil || err.Error() != "cannot push a digest reference" {
|
|
|
|
t.Fatalf("expected an error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImagePushAnyError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-08 23:44:25 -04:00
|
|
|
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
_, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{})
|
2018-12-31 12:22:43 -05:00
|
|
|
if !errdefs.IsSystem(err) {
|
2019-10-12 18:31:53 -04:00
|
|
|
t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
|
2018-12-31 12:22:43 -05:00
|
|
|
}
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestImagePushStatusUnauthorizedError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-08 23:44:25 -04:00
|
|
|
client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
_, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{})
|
2019-10-12 18:31:53 -04:00
|
|
|
if !errdefs.IsUnauthorized(err) {
|
|
|
|
t.Fatalf("expected a Unauthorized Error, got %[1]T: %[1]v", err)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImagePushWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-08 23:44:25 -04:00
|
|
|
client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
privilegeFunc := func() (string, error) {
|
|
|
|
return "", fmt.Errorf("Error requesting privilege")
|
|
|
|
}
|
|
|
|
_, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{
|
|
|
|
PrivilegeFunc: privilegeFunc,
|
|
|
|
})
|
|
|
|
if err == nil || err.Error() != "Error requesting privilege" {
|
|
|
|
t.Fatalf("expected an error requesting privilege, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImagePushWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-08 23:44:25 -04:00
|
|
|
client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
privilegeFunc := func() (string, error) {
|
|
|
|
return "a-auth-header", nil
|
|
|
|
}
|
|
|
|
_, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{
|
|
|
|
PrivilegeFunc: privilegeFunc,
|
|
|
|
})
|
2019-10-12 18:31:53 -04:00
|
|
|
if !errdefs.IsUnauthorized(err) {
|
|
|
|
t.Fatalf("expected a Unauthorized Error, got %[1]T: %[1]v", err)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImagePushWithPrivilegedFuncNoError(t *testing.T) {
|
|
|
|
expectedURL := "/images/myimage/push"
|
|
|
|
client := &Client{
|
2016-09-08 23:44:25 -04:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 14:46:37 -04:00
|
|
|
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
|
|
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
|
|
}
|
|
|
|
auth := req.Header.Get("X-Registry-Auth")
|
|
|
|
if auth == "NotValid" {
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusUnauthorized,
|
2021-08-24 06:10:50 -04:00
|
|
|
Body: io.NopCloser(bytes.NewReader([]byte("Invalid credentials"))),
|
2016-09-06 14:46:37 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
if auth != "IAmValid" {
|
|
|
|
return nil, fmt.Errorf("Invalid auth header : expected %s, got %s", "IAmValid", auth)
|
|
|
|
}
|
|
|
|
query := req.URL.Query()
|
|
|
|
tag := query.Get("tag")
|
|
|
|
if tag != "tag" {
|
|
|
|
return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", "tag", tag)
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 06:10:50 -04:00
|
|
|
Body: io.NopCloser(bytes.NewReader([]byte("hello world"))),
|
2016-09-06 14:46:37 -04:00
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
privilegeFunc := func() (string, error) {
|
|
|
|
return "IAmValid", nil
|
|
|
|
}
|
|
|
|
resp, err := client.ImagePush(context.Background(), "myimage:tag", types.ImagePushOptions{
|
|
|
|
RegistryAuth: "NotValid",
|
|
|
|
PrivilegeFunc: privilegeFunc,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-24 06:10:50 -04:00
|
|
|
body, err := io.ReadAll(resp)
|
2016-09-06 14:46:37 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(body) != "hello world" {
|
|
|
|
t.Fatalf("expected 'hello world', got %s", string(body))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImagePushWithoutErrors(t *testing.T) {
|
|
|
|
expectedOutput := "hello world"
|
|
|
|
expectedURLFormat := "/images/%s/push"
|
client.ImagePush(): default to ":latest" instead of "all tags"
The `docker push` command up until docker v0.9.1 always pushed all tags of a given
image, so `docker push foo/bar` would push (e.g.) all of `foo/bar:latest`, `foo:/bar:v1`,
and `foo/bar:v1.0.0`.
Pushing all tags of an image was not desirable in many case, so docker v0.10.0
enhanced `docker push` to optionally specify a tag to push (`docker push foo/bar:v1`)
(see issue 3411 and PR 4948 (commit e648a186d68dcb3ee0d6123b041c5aa66438cc89).
This behavior exists up until today, and is confusing, because unlike other commands,
`docker push` does not default to use the `:latest` tag when omitted, but instead
makes it push "all tags of the image".
`docker pull` had a similar behavior, but PR 7759 (9c08364a412a51c80e8d17ae14f92549dc543e68)
changed the behavior to default to the `:latest` tag, and added a `--all-tags` flag
to the CLI to optionally pull all images.
This patch implements the API client changes to make `docker push` match the behavior
of `docker pull`, and default to pull a single image, unless the `all` option is passed.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-12-10 06:24:14 -05:00
|
|
|
testCases := []struct {
|
|
|
|
all bool
|
2016-09-06 14:46:37 -04:00
|
|
|
reference string
|
|
|
|
expectedImage string
|
|
|
|
expectedTag string
|
|
|
|
}{
|
|
|
|
{
|
client.ImagePush(): default to ":latest" instead of "all tags"
The `docker push` command up until docker v0.9.1 always pushed all tags of a given
image, so `docker push foo/bar` would push (e.g.) all of `foo/bar:latest`, `foo:/bar:v1`,
and `foo/bar:v1.0.0`.
Pushing all tags of an image was not desirable in many case, so docker v0.10.0
enhanced `docker push` to optionally specify a tag to push (`docker push foo/bar:v1`)
(see issue 3411 and PR 4948 (commit e648a186d68dcb3ee0d6123b041c5aa66438cc89).
This behavior exists up until today, and is confusing, because unlike other commands,
`docker push` does not default to use the `:latest` tag when omitted, but instead
makes it push "all tags of the image".
`docker pull` had a similar behavior, but PR 7759 (9c08364a412a51c80e8d17ae14f92549dc543e68)
changed the behavior to default to the `:latest` tag, and added a `--all-tags` flag
to the CLI to optionally pull all images.
This patch implements the API client changes to make `docker push` match the behavior
of `docker pull`, and default to pull a single image, unless the `all` option is passed.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-12-10 06:24:14 -05:00
|
|
|
all: false,
|
2016-09-06 14:46:37 -04:00
|
|
|
reference: "myimage",
|
|
|
|
expectedImage: "myimage",
|
client.ImagePush(): default to ":latest" instead of "all tags"
The `docker push` command up until docker v0.9.1 always pushed all tags of a given
image, so `docker push foo/bar` would push (e.g.) all of `foo/bar:latest`, `foo:/bar:v1`,
and `foo/bar:v1.0.0`.
Pushing all tags of an image was not desirable in many case, so docker v0.10.0
enhanced `docker push` to optionally specify a tag to push (`docker push foo/bar:v1`)
(see issue 3411 and PR 4948 (commit e648a186d68dcb3ee0d6123b041c5aa66438cc89).
This behavior exists up until today, and is confusing, because unlike other commands,
`docker push` does not default to use the `:latest` tag when omitted, but instead
makes it push "all tags of the image".
`docker pull` had a similar behavior, but PR 7759 (9c08364a412a51c80e8d17ae14f92549dc543e68)
changed the behavior to default to the `:latest` tag, and added a `--all-tags` flag
to the CLI to optionally pull all images.
This patch implements the API client changes to make `docker push` match the behavior
of `docker pull`, and default to pull a single image, unless the `all` option is passed.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-12-10 06:24:14 -05:00
|
|
|
expectedTag: "latest",
|
2016-09-06 14:46:37 -04:00
|
|
|
},
|
|
|
|
{
|
client.ImagePush(): default to ":latest" instead of "all tags"
The `docker push` command up until docker v0.9.1 always pushed all tags of a given
image, so `docker push foo/bar` would push (e.g.) all of `foo/bar:latest`, `foo:/bar:v1`,
and `foo/bar:v1.0.0`.
Pushing all tags of an image was not desirable in many case, so docker v0.10.0
enhanced `docker push` to optionally specify a tag to push (`docker push foo/bar:v1`)
(see issue 3411 and PR 4948 (commit e648a186d68dcb3ee0d6123b041c5aa66438cc89).
This behavior exists up until today, and is confusing, because unlike other commands,
`docker push` does not default to use the `:latest` tag when omitted, but instead
makes it push "all tags of the image".
`docker pull` had a similar behavior, but PR 7759 (9c08364a412a51c80e8d17ae14f92549dc543e68)
changed the behavior to default to the `:latest` tag, and added a `--all-tags` flag
to the CLI to optionally pull all images.
This patch implements the API client changes to make `docker push` match the behavior
of `docker pull`, and default to pull a single image, unless the `all` option is passed.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-12-10 06:24:14 -05:00
|
|
|
all: false,
|
2016-09-06 14:46:37 -04:00
|
|
|
reference: "myimage:tag",
|
|
|
|
expectedImage: "myimage",
|
|
|
|
expectedTag: "tag",
|
|
|
|
},
|
client.ImagePush(): default to ":latest" instead of "all tags"
The `docker push` command up until docker v0.9.1 always pushed all tags of a given
image, so `docker push foo/bar` would push (e.g.) all of `foo/bar:latest`, `foo:/bar:v1`,
and `foo/bar:v1.0.0`.
Pushing all tags of an image was not desirable in many case, so docker v0.10.0
enhanced `docker push` to optionally specify a tag to push (`docker push foo/bar:v1`)
(see issue 3411 and PR 4948 (commit e648a186d68dcb3ee0d6123b041c5aa66438cc89).
This behavior exists up until today, and is confusing, because unlike other commands,
`docker push` does not default to use the `:latest` tag when omitted, but instead
makes it push "all tags of the image".
`docker pull` had a similar behavior, but PR 7759 (9c08364a412a51c80e8d17ae14f92549dc543e68)
changed the behavior to default to the `:latest` tag, and added a `--all-tags` flag
to the CLI to optionally pull all images.
This patch implements the API client changes to make `docker push` match the behavior
of `docker pull`, and default to pull a single image, unless the `all` option is passed.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-12-10 06:24:14 -05:00
|
|
|
{
|
|
|
|
all: true,
|
|
|
|
reference: "myimage",
|
|
|
|
expectedImage: "myimage",
|
|
|
|
expectedTag: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
all: true,
|
|
|
|
reference: "myimage:anything",
|
|
|
|
expectedImage: "myimage",
|
|
|
|
expectedTag: "",
|
|
|
|
},
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
client.ImagePush(): default to ":latest" instead of "all tags"
The `docker push` command up until docker v0.9.1 always pushed all tags of a given
image, so `docker push foo/bar` would push (e.g.) all of `foo/bar:latest`, `foo:/bar:v1`,
and `foo/bar:v1.0.0`.
Pushing all tags of an image was not desirable in many case, so docker v0.10.0
enhanced `docker push` to optionally specify a tag to push (`docker push foo/bar:v1`)
(see issue 3411 and PR 4948 (commit e648a186d68dcb3ee0d6123b041c5aa66438cc89).
This behavior exists up until today, and is confusing, because unlike other commands,
`docker push` does not default to use the `:latest` tag when omitted, but instead
makes it push "all tags of the image".
`docker pull` had a similar behavior, but PR 7759 (9c08364a412a51c80e8d17ae14f92549dc543e68)
changed the behavior to default to the `:latest` tag, and added a `--all-tags` flag
to the CLI to optionally pull all images.
This patch implements the API client changes to make `docker push` match the behavior
of `docker pull`, and default to pull a single image, unless the `all` option is passed.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-12-10 06:24:14 -05:00
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(fmt.Sprintf("%s,all-tags=%t", tc.reference, tc.all), func(t *testing.T) {
|
|
|
|
client := &Client{
|
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
expectedURL := fmt.Sprintf(expectedURLFormat, tc.expectedImage)
|
|
|
|
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
|
|
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
|
|
}
|
|
|
|
query := req.URL.Query()
|
|
|
|
tag := query.Get("tag")
|
|
|
|
if tag != tc.expectedTag {
|
|
|
|
return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", tc.expectedTag, tag)
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 06:10:50 -04:00
|
|
|
Body: io.NopCloser(bytes.NewReader([]byte(expectedOutput))),
|
client.ImagePush(): default to ":latest" instead of "all tags"
The `docker push` command up until docker v0.9.1 always pushed all tags of a given
image, so `docker push foo/bar` would push (e.g.) all of `foo/bar:latest`, `foo:/bar:v1`,
and `foo/bar:v1.0.0`.
Pushing all tags of an image was not desirable in many case, so docker v0.10.0
enhanced `docker push` to optionally specify a tag to push (`docker push foo/bar:v1`)
(see issue 3411 and PR 4948 (commit e648a186d68dcb3ee0d6123b041c5aa66438cc89).
This behavior exists up until today, and is confusing, because unlike other commands,
`docker push` does not default to use the `:latest` tag when omitted, but instead
makes it push "all tags of the image".
`docker pull` had a similar behavior, but PR 7759 (9c08364a412a51c80e8d17ae14f92549dc543e68)
changed the behavior to default to the `:latest` tag, and added a `--all-tags` flag
to the CLI to optionally pull all images.
This patch implements the API client changes to make `docker push` match the behavior
of `docker pull`, and default to pull a single image, unless the `all` option is passed.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-12-10 06:24:14 -05:00
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
resp, err := client.ImagePush(context.Background(), tc.reference, types.ImagePushOptions{
|
|
|
|
All: tc.all,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-24 06:10:50 -04:00
|
|
|
body, err := io.ReadAll(resp)
|
client.ImagePush(): default to ":latest" instead of "all tags"
The `docker push` command up until docker v0.9.1 always pushed all tags of a given
image, so `docker push foo/bar` would push (e.g.) all of `foo/bar:latest`, `foo:/bar:v1`,
and `foo/bar:v1.0.0`.
Pushing all tags of an image was not desirable in many case, so docker v0.10.0
enhanced `docker push` to optionally specify a tag to push (`docker push foo/bar:v1`)
(see issue 3411 and PR 4948 (commit e648a186d68dcb3ee0d6123b041c5aa66438cc89).
This behavior exists up until today, and is confusing, because unlike other commands,
`docker push` does not default to use the `:latest` tag when omitted, but instead
makes it push "all tags of the image".
`docker pull` had a similar behavior, but PR 7759 (9c08364a412a51c80e8d17ae14f92549dc543e68)
changed the behavior to default to the `:latest` tag, and added a `--all-tags` flag
to the CLI to optionally pull all images.
This patch implements the API client changes to make `docker push` match the behavior
of `docker pull`, and default to pull a single image, unless the `all` option is passed.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-12-10 06:24:14 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(body) != expectedOutput {
|
|
|
|
t.Fatalf("expected '%s', got %s", expectedOutput, string(body))
|
|
|
|
}
|
|
|
|
})
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
}
|