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
|
|
|
"encoding/json"
|
|
|
|
"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"
|
2017-05-16 19:09:53 -04:00
|
|
|
registrytypes "github.com/docker/docker/api/types/registry"
|
2016-09-06 14:46:37 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2018-12-31 12:22:43 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2022-03-04 08:49:42 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2019-08-05 10:37:47 -04:00
|
|
|
v1 "github.com/opencontainers/image-spec/specs-go/v1"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-09-06 14:46:37 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServiceCreateError(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.ServiceCreate(context.Background(), swarm.ServiceSpec{}, types.ServiceCreateOptions{})
|
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 TestServiceCreate(t *testing.T) {
|
|
|
|
expectedURL := "/services/create"
|
|
|
|
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)
|
|
|
|
}
|
2019-10-12 14:41:14 -04:00
|
|
|
if req.Method != http.MethodPost {
|
2016-09-06 14:46:37 -04:00
|
|
|
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(types.ServiceCreateResponse{
|
|
|
|
ID: "service_id",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 06:10:50 -04:00
|
|
|
Body: io.NopCloser(bytes.NewReader(b)),
|
2016-09-06 14:46:37 -04:00
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := client.ServiceCreate(context.Background(), swarm.ServiceSpec{}, types.ServiceCreateOptions{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if r.ID != "service_id" {
|
|
|
|
t.Fatalf("expected `service_id`, got %s", r.ID)
|
|
|
|
}
|
|
|
|
}
|
2017-05-16 19:09:53 -04:00
|
|
|
|
|
|
|
func TestServiceCreateCompatiblePlatforms(t *testing.T) {
|
|
|
|
client := &Client{
|
2017-06-07 12:09:07 -04:00
|
|
|
version: "1.30",
|
2017-05-16 19:09:53 -04:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2017-06-07 12:09:07 -04:00
|
|
|
if strings.HasPrefix(req.URL.Path, "/v1.30/services/create") {
|
2017-06-12 10:10:15 -04:00
|
|
|
var serviceSpec swarm.ServiceSpec
|
|
|
|
|
2017-05-17 19:58:54 -04:00
|
|
|
// check if the /distribution endpoint returned correct output
|
2017-06-12 10:10:15 -04:00
|
|
|
err := json.NewDecoder(req.Body).Decode(&serviceSpec)
|
2017-05-17 19:58:54 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-05-16 19:09:53 -04:00
|
|
|
}
|
2017-05-17 19:58:54 -04:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal("foobar:1.0@sha256:c0537ff6a5218ef531ece93d4984efc99bbf3f7497c0a7726c88e2bb7584dc96", serviceSpec.TaskTemplate.ContainerSpec.Image))
|
|
|
|
assert.Check(t, is.Len(serviceSpec.TaskTemplate.Placement.Platforms, 1))
|
2017-06-12 10:10:15 -04:00
|
|
|
|
|
|
|
p := serviceSpec.TaskTemplate.Placement.Platforms[0]
|
2017-05-16 19:09:53 -04:00
|
|
|
b, err := json.Marshal(types.ServiceCreateResponse{
|
2017-06-12 10:10:15 -04:00
|
|
|
ID: "service_" + p.OS + "_" + p.Architecture,
|
2017-05-16 19:09:53 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 06:10:50 -04:00
|
|
|
Body: io.NopCloser(bytes.NewReader(b)),
|
2017-05-16 19:09:53 -04:00
|
|
|
}, nil
|
2017-06-07 12:09:07 -04:00
|
|
|
} else if strings.HasPrefix(req.URL.Path, "/v1.30/distribution/") {
|
2017-05-16 19:09:53 -04:00
|
|
|
b, err := json.Marshal(registrytypes.DistributionInspect{
|
2017-06-12 10:10:15 -04:00
|
|
|
Descriptor: v1.Descriptor{
|
|
|
|
Digest: "sha256:c0537ff6a5218ef531ece93d4984efc99bbf3f7497c0a7726c88e2bb7584dc96",
|
|
|
|
},
|
|
|
|
Platforms: []v1.Platform{
|
|
|
|
{
|
|
|
|
Architecture: "amd64",
|
|
|
|
OS: "linux",
|
|
|
|
},
|
|
|
|
},
|
2017-05-16 19:09:53 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 06:10:50 -04:00
|
|
|
Body: io.NopCloser(bytes.NewReader(b)),
|
2017-05-16 19:09:53 -04:00
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("unexpected URL '%s'", req.URL.Path)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2017-06-07 13:07:01 -04:00
|
|
|
spec := swarm.ServiceSpec{TaskTemplate: swarm.TaskSpec{ContainerSpec: &swarm.ContainerSpec{Image: "foobar:1.0"}}}
|
2017-06-12 10:10:15 -04:00
|
|
|
|
|
|
|
r, err := client.ServiceCreate(context.Background(), spec, types.ServiceCreateOptions{QueryRegistry: true})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Check(t, is.Equal("service_linux_amd64", r.ID))
|
2017-05-16 19:09:53 -04:00
|
|
|
}
|
2017-05-18 20:10:49 -04:00
|
|
|
|
|
|
|
func TestServiceCreateDigestPinning(t *testing.T) {
|
|
|
|
dgst := "sha256:c0537ff6a5218ef531ece93d4984efc99bbf3f7497c0a7726c88e2bb7584dc96"
|
|
|
|
dgstAlt := "sha256:37ffbf3f7497c07584dc9637ffbf3f7497c0758c0537ffbf3f7497c0c88e2bb7"
|
|
|
|
serviceCreateImage := ""
|
|
|
|
pinByDigestTests := []struct {
|
|
|
|
img string // input image provided by the user
|
|
|
|
expected string // expected image after digest pinning
|
|
|
|
}{
|
|
|
|
// default registry returns familiar string
|
|
|
|
{"docker.io/library/alpine", "alpine:latest@" + dgst},
|
|
|
|
// provided tag is preserved and digest added
|
|
|
|
{"alpine:edge", "alpine:edge@" + dgst},
|
|
|
|
// image with provided alternative digest remains unchanged
|
|
|
|
{"alpine@" + dgstAlt, "alpine@" + dgstAlt},
|
|
|
|
// image with provided tag and alternative digest remains unchanged
|
|
|
|
{"alpine:edge@" + dgstAlt, "alpine:edge@" + dgstAlt},
|
|
|
|
// image on alternative registry does not result in familiar string
|
|
|
|
{"alternate.registry/library/alpine", "alternate.registry/library/alpine:latest@" + dgst},
|
|
|
|
// unresolvable image does not get a digest
|
|
|
|
{"cannotresolve", "cannotresolve:latest"},
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &Client{
|
2017-06-07 12:09:07 -04:00
|
|
|
version: "1.30",
|
2017-05-18 20:10:49 -04:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2017-06-07 12:09:07 -04:00
|
|
|
if strings.HasPrefix(req.URL.Path, "/v1.30/services/create") {
|
2017-05-18 20:10:49 -04:00
|
|
|
// reset and set image received by the service create endpoint
|
|
|
|
serviceCreateImage = ""
|
|
|
|
var service swarm.ServiceSpec
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&service); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse service create request")
|
|
|
|
}
|
|
|
|
serviceCreateImage = service.TaskTemplate.ContainerSpec.Image
|
|
|
|
|
|
|
|
b, err := json.Marshal(types.ServiceCreateResponse{
|
|
|
|
ID: "service_id",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 06:10:50 -04:00
|
|
|
Body: io.NopCloser(bytes.NewReader(b)),
|
2017-05-18 20:10:49 -04:00
|
|
|
}, nil
|
2017-06-07 12:09:07 -04:00
|
|
|
} else if strings.HasPrefix(req.URL.Path, "/v1.30/distribution/cannotresolve") {
|
2017-05-18 20:10:49 -04:00
|
|
|
// unresolvable image
|
|
|
|
return nil, fmt.Errorf("cannot resolve image")
|
2017-06-07 12:09:07 -04:00
|
|
|
} else if strings.HasPrefix(req.URL.Path, "/v1.30/distribution/") {
|
2017-05-18 20:10:49 -04:00
|
|
|
// resolvable images
|
|
|
|
b, err := json.Marshal(registrytypes.DistributionInspect{
|
|
|
|
Descriptor: v1.Descriptor{
|
|
|
|
Digest: digest.Digest(dgst),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 06:10:50 -04:00
|
|
|
Body: io.NopCloser(bytes.NewReader(b)),
|
2017-05-18 20:10:49 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unexpected URL '%s'", req.URL.Path)
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
// run pin by digest tests
|
|
|
|
for _, p := range pinByDigestTests {
|
|
|
|
r, err := client.ServiceCreate(context.Background(), swarm.ServiceSpec{
|
|
|
|
TaskTemplate: swarm.TaskSpec{
|
2017-06-07 13:07:01 -04:00
|
|
|
ContainerSpec: &swarm.ContainerSpec{
|
2017-05-18 20:10:49 -04:00
|
|
|
Image: p.img,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, types.ServiceCreateOptions{QueryRegistry: true})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.ID != "service_id" {
|
|
|
|
t.Fatalf("expected `service_id`, got %s", r.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.expected != serviceCreateImage {
|
|
|
|
t.Fatalf("expected image %s, got %s", p.expected, serviceCreateImage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|