2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"fmt"
|
2021-08-24 10:10:50 +00:00
|
|
|
"io"
|
2016-09-06 18:46:37 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2021-08-26 19:08:38 +00:00
|
|
|
"github.com/docker/docker/api/types/registry"
|
2018-12-31 17:22:43 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestImagePullReferenceParseError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
return nil, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
// An empty reference is an invalid reference
|
|
|
|
_, err := client.ImagePull(context.Background(), "", types.ImagePullOptions{})
|
2017-01-26 00:54:18 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "invalid reference format") {
|
2016-09-06 18:46:37 +00:00
|
|
|
t.Fatalf("expected an error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImagePullAnyError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
_, err := client.ImagePull(context.Background(), "myimage", types.ImagePullOptions{})
|
2018-12-31 17:22:43 +00:00
|
|
|
if !errdefs.IsSystem(err) {
|
2019-10-12 22:31:53 +00:00
|
|
|
t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
|
2018-12-31 17:22:43 +00:00
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestImagePullStatusUnauthorizedError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
_, err := client.ImagePull(context.Background(), "myimage", types.ImagePullOptions{})
|
2019-10-12 22:31:53 +00:00
|
|
|
if !errdefs.IsUnauthorized(err) {
|
|
|
|
t.Fatalf("expected a Unauthorized Error, got %[1]T: %[1]v", err)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImagePullWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
privilegeFunc := func() (string, error) {
|
|
|
|
return "", fmt.Errorf("Error requesting privilege")
|
|
|
|
}
|
|
|
|
_, err := client.ImagePull(context.Background(), "myimage", types.ImagePullOptions{
|
|
|
|
PrivilegeFunc: privilegeFunc,
|
|
|
|
})
|
|
|
|
if err == nil || err.Error() != "Error requesting privilege" {
|
|
|
|
t.Fatalf("expected an error requesting privilege, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImagePullWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
privilegeFunc := func() (string, error) {
|
|
|
|
return "a-auth-header", nil
|
|
|
|
}
|
|
|
|
_, err := client.ImagePull(context.Background(), "myimage", types.ImagePullOptions{
|
|
|
|
PrivilegeFunc: privilegeFunc,
|
|
|
|
})
|
2019-10-12 22:31:53 +00:00
|
|
|
if !errdefs.IsUnauthorized(err) {
|
|
|
|
t.Fatalf("expected a Unauthorized Error, got %[1]T: %[1]v", err)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImagePullWithPrivilegedFuncNoError(t *testing.T) {
|
|
|
|
expectedURL := "/images/create"
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
2022-06-03 11:48:58 +00:00
|
|
|
return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
2021-08-26 19:08:38 +00:00
|
|
|
auth := req.Header.Get(registry.AuthHeader)
|
2016-09-06 18:46:37 +00:00
|
|
|
if auth == "NotValid" {
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusUnauthorized,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader([]byte("Invalid credentials"))),
|
2016-09-06 18:46:37 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
if auth != "IAmValid" {
|
2022-06-03 11:48:58 +00:00
|
|
|
return nil, fmt.Errorf("invalid auth header: expected %s, got %s", "IAmValid", auth)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
query := req.URL.Query()
|
|
|
|
fromImage := query.Get("fromImage")
|
|
|
|
if fromImage != "myimage" {
|
|
|
|
return nil, fmt.Errorf("fromimage not set in URL query properly. Expected '%s', got %s", "myimage", fromImage)
|
|
|
|
}
|
|
|
|
tag := query.Get("tag")
|
|
|
|
if tag != "latest" {
|
|
|
|
return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", "latest", tag)
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader([]byte("hello world"))),
|
2016-09-06 18:46:37 +00:00
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
privilegeFunc := func() (string, error) {
|
|
|
|
return "IAmValid", nil
|
|
|
|
}
|
|
|
|
resp, err := client.ImagePull(context.Background(), "myimage", types.ImagePullOptions{
|
|
|
|
RegistryAuth: "NotValid",
|
|
|
|
PrivilegeFunc: privilegeFunc,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-24 10:10:50 +00:00
|
|
|
body, err := io.ReadAll(resp)
|
2016-09-06 18:46:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(body) != "hello world" {
|
|
|
|
t.Fatalf("expected 'hello world', got %s", string(body))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImagePullWithoutErrors(t *testing.T) {
|
|
|
|
expectedURL := "/images/create"
|
|
|
|
expectedOutput := "hello world"
|
|
|
|
pullCases := []struct {
|
|
|
|
all bool
|
|
|
|
reference string
|
|
|
|
expectedImage string
|
|
|
|
expectedTag string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
all: false,
|
|
|
|
reference: "myimage",
|
|
|
|
expectedImage: "myimage",
|
|
|
|
expectedTag: "latest",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
all: false,
|
|
|
|
reference: "myimage:tag",
|
|
|
|
expectedImage: "myimage",
|
|
|
|
expectedTag: "tag",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
all: true,
|
|
|
|
reference: "myimage",
|
|
|
|
expectedImage: "myimage",
|
|
|
|
expectedTag: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
all: true,
|
|
|
|
reference: "myimage:anything",
|
|
|
|
expectedImage: "myimage",
|
|
|
|
expectedTag: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, pullCase := range pullCases {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
|
|
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
|
|
}
|
|
|
|
query := req.URL.Query()
|
|
|
|
fromImage := query.Get("fromImage")
|
|
|
|
if fromImage != pullCase.expectedImage {
|
|
|
|
return nil, fmt.Errorf("fromimage not set in URL query properly. Expected '%s', got %s", pullCase.expectedImage, fromImage)
|
|
|
|
}
|
|
|
|
tag := query.Get("tag")
|
|
|
|
if tag != pullCase.expectedTag {
|
|
|
|
return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", pullCase.expectedTag, tag)
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader([]byte(expectedOutput))),
|
2016-09-06 18:46:37 +00:00
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
resp, err := client.ImagePull(context.Background(), pullCase.reference, types.ImagePullOptions{
|
|
|
|
All: pullCase.all,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-24 10:10:50 +00:00
|
|
|
body, err := io.ReadAll(resp)
|
2016-09-06 18:46:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(body) != expectedOutput {
|
|
|
|
t.Fatalf("expected '%s', got %s", expectedOutput, string(body))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|