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
|
|
|
"encoding/json"
|
|
|
|
"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"
|
2017-05-16 23:09:53 +00:00
|
|
|
registrytypes "github.com/docker/docker/api/types/registry"
|
2016-09-06 18:46:37 +00:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2018-12-31 17:22:43 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2022-03-04 13:49:42 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2023-05-08 09:57:52 +00:00
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServiceCreateError(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.ServiceCreate(context.Background(), swarm.ServiceSpec{}, types.ServiceCreateOptions{})
|
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 TestServiceCreate(t *testing.T) {
|
|
|
|
expectedURL := "/services/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) {
|
|
|
|
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
|
|
}
|
2019-10-12 18:41:14 +00:00
|
|
|
if req.Method != http.MethodPost {
|
2016-09-06 18:46:37 +00: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 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader(b)),
|
2016-09-06 18:46:37 +00: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 23:09:53 +00:00
|
|
|
|
|
|
|
func TestServiceCreateCompatiblePlatforms(t *testing.T) {
|
|
|
|
client := &Client{
|
2017-06-07 16:09:07 +00:00
|
|
|
version: "1.30",
|
2017-05-16 23:09:53 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2017-06-07 16:09:07 +00:00
|
|
|
if strings.HasPrefix(req.URL.Path, "/v1.30/services/create") {
|
2017-06-12 14:10:15 +00:00
|
|
|
var serviceSpec swarm.ServiceSpec
|
|
|
|
|
2017-05-17 23:58:54 +00:00
|
|
|
// check if the /distribution endpoint returned correct output
|
2017-06-12 14:10:15 +00:00
|
|
|
err := json.NewDecoder(req.Body).Decode(&serviceSpec)
|
2017-05-17 23:58:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-05-16 23:09:53 +00:00
|
|
|
}
|
2017-05-17 23:58:54 +00:00
|
|
|
|
2018-03-13 19:28:34 +00: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 14:10:15 +00:00
|
|
|
|
|
|
|
p := serviceSpec.TaskTemplate.Placement.Platforms[0]
|
2017-05-16 23:09:53 +00:00
|
|
|
b, err := json.Marshal(types.ServiceCreateResponse{
|
2017-06-12 14:10:15 +00:00
|
|
|
ID: "service_" + p.OS + "_" + p.Architecture,
|
2017-05-16 23:09:53 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader(b)),
|
2017-05-16 23:09:53 +00:00
|
|
|
}, nil
|
2017-06-07 16:09:07 +00:00
|
|
|
} else if strings.HasPrefix(req.URL.Path, "/v1.30/distribution/") {
|
2017-05-16 23:09:53 +00:00
|
|
|
b, err := json.Marshal(registrytypes.DistributionInspect{
|
2023-05-08 09:57:52 +00:00
|
|
|
Descriptor: ocispec.Descriptor{
|
2017-06-12 14:10:15 +00:00
|
|
|
Digest: "sha256:c0537ff6a5218ef531ece93d4984efc99bbf3f7497c0a7726c88e2bb7584dc96",
|
|
|
|
},
|
2023-05-08 09:57:52 +00:00
|
|
|
Platforms: []ocispec.Platform{
|
2017-06-12 14:10:15 +00:00
|
|
|
{
|
|
|
|
Architecture: "amd64",
|
|
|
|
OS: "linux",
|
|
|
|
},
|
|
|
|
},
|
2017-05-16 23:09:53 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader(b)),
|
2017-05-16 23:09:53 +00:00
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("unexpected URL '%s'", req.URL.Path)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2017-06-07 17:07:01 +00:00
|
|
|
spec := swarm.ServiceSpec{TaskTemplate: swarm.TaskSpec{ContainerSpec: &swarm.ContainerSpec{Image: "foobar:1.0"}}}
|
2017-06-12 14:10:15 +00:00
|
|
|
|
|
|
|
r, err := client.ServiceCreate(context.Background(), spec, types.ServiceCreateOptions{QueryRegistry: true})
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Check(t, is.Equal("service_linux_amd64", r.ID))
|
2017-05-16 23:09:53 +00:00
|
|
|
}
|
2017-05-19 00:10:49 +00: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 16:09:07 +00:00
|
|
|
version: "1.30",
|
2017-05-19 00:10:49 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2017-06-07 16:09:07 +00:00
|
|
|
if strings.HasPrefix(req.URL.Path, "/v1.30/services/create") {
|
2017-05-19 00:10:49 +00: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 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader(b)),
|
2017-05-19 00:10:49 +00:00
|
|
|
}, nil
|
2017-06-07 16:09:07 +00:00
|
|
|
} else if strings.HasPrefix(req.URL.Path, "/v1.30/distribution/cannotresolve") {
|
2017-05-19 00:10:49 +00:00
|
|
|
// unresolvable image
|
|
|
|
return nil, fmt.Errorf("cannot resolve image")
|
2017-06-07 16:09:07 +00:00
|
|
|
} else if strings.HasPrefix(req.URL.Path, "/v1.30/distribution/") {
|
2017-05-19 00:10:49 +00:00
|
|
|
// resolvable images
|
|
|
|
b, err := json.Marshal(registrytypes.DistributionInspect{
|
2023-05-08 09:57:52 +00:00
|
|
|
Descriptor: ocispec.Descriptor{
|
2017-05-19 00:10:49 +00:00
|
|
|
Digest: digest.Digest(dgst),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader(b)),
|
2017-05-19 00:10:49 +00: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 17:07:01 +00:00
|
|
|
ContainerSpec: &swarm.ContainerSpec{
|
2017-05-19 00:10:49 +00: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|