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"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"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"
|
2017-05-18 20:10:49 -04:00
|
|
|
"github.com/opencontainers/go-digest"
|
2017-05-16 19:09:53 -04:00
|
|
|
"github.com/opencontainers/image-spec/specs-go/v1"
|
2017-06-12 10:10:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-09-06 14:46:37 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
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{})
|
|
|
|
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
|
|
|
t.Fatalf("expected a Server Error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if req.Method != "POST" {
|
|
|
|
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,
|
|
|
|
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
|
|
|
}, 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
|
|
|
|
2017-06-12 10:10:15 -04:00
|
|
|
assert.Equal(t, "foobar:1.0@sha256:c0537ff6a5218ef531ece93d4984efc99bbf3f7497c0a7726c88e2bb7584dc96", serviceSpec.TaskTemplate.ContainerSpec.Image)
|
|
|
|
assert.Len(t, serviceSpec.TaskTemplate.Placement.Platforms, 1)
|
|
|
|
|
|
|
|
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,
|
|
|
|
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
|
|
|
}, 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,
|
|
|
|
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
|
|
|
}, 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})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "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,
|
|
|
|
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
|
|
|
}, 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,
|
|
|
|
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
|
|
|
}, 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|