2016-09-06 18:46:37 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-05-17 23:58:54 +00:00
|
|
|
"io"
|
2016-09-06 18:46:37 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"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"
|
2017-05-16 23:09:53 +00:00
|
|
|
"github.com/opencontainers/image-spec/specs-go/v1"
|
2016-09-06 18:46:37 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
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{})
|
|
|
|
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-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)
|
|
|
|
}
|
|
|
|
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 23:09:53 +00:00
|
|
|
|
|
|
|
func TestServiceCreateCompatiblePlatforms(t *testing.T) {
|
2017-05-17 23:58:54 +00:00
|
|
|
var (
|
|
|
|
platforms []v1.Platform
|
|
|
|
distributionInspectBody io.ReadCloser
|
|
|
|
distributionInspect registrytypes.DistributionInspect
|
|
|
|
)
|
|
|
|
|
2017-05-16 23:09:53 +00:00
|
|
|
client := &Client{
|
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
if strings.HasPrefix(req.URL.Path, "/services/create") {
|
2017-05-17 23:58:54 +00:00
|
|
|
// check if the /distribution endpoint returned correct output
|
|
|
|
err := json.NewDecoder(distributionInspectBody).Decode(&distributionInspect)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-05-16 23:09:53 +00:00
|
|
|
}
|
2017-05-17 23:58:54 +00:00
|
|
|
if len(distributionInspect.Platforms) == 0 || distributionInspect.Platforms[0].Architecture != platforms[0].Architecture || distributionInspect.Platforms[0].OS != platforms[0].OS {
|
|
|
|
return nil, fmt.Errorf("received incorrect platform information from registry")
|
|
|
|
}
|
|
|
|
|
2017-05-16 23:09:53 +00:00
|
|
|
b, err := json.Marshal(types.ServiceCreateResponse{
|
|
|
|
ID: "service_" + platforms[0].Architecture,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
|
|
|
}, nil
|
|
|
|
} else if strings.HasPrefix(req.URL.Path, "/distribution/") {
|
|
|
|
platforms = []v1.Platform{
|
|
|
|
{
|
|
|
|
Architecture: "amd64",
|
|
|
|
OS: "linux",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(registrytypes.DistributionInspect{
|
|
|
|
Descriptor: v1.Descriptor{},
|
|
|
|
Platforms: platforms,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-17 23:58:54 +00:00
|
|
|
distributionInspectBody = ioutil.NopCloser(bytes.NewReader(b))
|
2017-05-16 23:09:53 +00:00
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("unexpected URL '%s'", req.URL.Path)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := client.ServiceCreate(context.Background(), swarm.ServiceSpec{}, types.ServiceCreateOptions{QueryRegistry: true})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if r.ID != "service_amd64" {
|
|
|
|
t.Fatalf("expected `service_amd64`, got %s", r.ID)
|
|
|
|
}
|
|
|
|
}
|