The `TestServiceCreateCompatiblePlatforms()` test was confusing, because it
did not actually use the mocked API response, but used a local
`distributionInspectBody` variable to verify that the `/distribution/` endpoint
was called.
This flow was especially confusing, because a comment in the test describes;
"check if the /distribution endpoint returned correct output"
If (for whatever reason), the endpoint was not called, the test would panic,
because the `distributionInspectBody` would not be set.
This patch rewrites the test to use the actual API response that is returned
by the mock, and verifies that the information returned by the `/distribution/`
endpoint is properly used in the service's definition.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
| ... | ... |
@@ -4,7 +4,6 @@ import ( |
| 4 | 4 |
"bytes" |
| 5 | 5 |
"encoding/json" |
| 6 | 6 |
"fmt" |
| 7 |
- "io" |
|
| 8 | 7 |
"io/ioutil" |
| 9 | 8 |
"net/http" |
| 10 | 9 |
"strings" |
| ... | ... |
@@ -15,6 +14,7 @@ import ( |
| 15 | 15 |
"github.com/docker/docker/api/types/swarm" |
| 16 | 16 |
"github.com/opencontainers/go-digest" |
| 17 | 17 |
"github.com/opencontainers/image-spec/specs-go/v1" |
| 18 |
+ "github.com/stretchr/testify/assert" |
|
| 18 | 19 |
"golang.org/x/net/context" |
| 19 | 20 |
) |
| 20 | 21 |
|
| ... | ... |
@@ -61,27 +61,24 @@ func TestServiceCreate(t *testing.T) {
|
| 61 | 61 |
} |
| 62 | 62 |
|
| 63 | 63 |
func TestServiceCreateCompatiblePlatforms(t *testing.T) {
|
| 64 |
- var ( |
|
| 65 |
- platforms []v1.Platform |
|
| 66 |
- distributionInspectBody io.ReadCloser |
|
| 67 |
- distributionInspect registrytypes.DistributionInspect |
|
| 68 |
- ) |
|
| 69 |
- |
|
| 70 | 64 |
client := &Client{
|
| 71 | 65 |
version: "1.30", |
| 72 | 66 |
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
| 73 | 67 |
if strings.HasPrefix(req.URL.Path, "/v1.30/services/create") {
|
| 68 |
+ var serviceSpec swarm.ServiceSpec |
|
| 69 |
+ |
|
| 74 | 70 |
// check if the /distribution endpoint returned correct output |
| 75 |
- err := json.NewDecoder(distributionInspectBody).Decode(&distributionInspect) |
|
| 71 |
+ err := json.NewDecoder(req.Body).Decode(&serviceSpec) |
|
| 76 | 72 |
if err != nil {
|
| 77 | 73 |
return nil, err |
| 78 | 74 |
} |
| 79 |
- if len(distributionInspect.Platforms) == 0 || distributionInspect.Platforms[0].Architecture != platforms[0].Architecture || distributionInspect.Platforms[0].OS != platforms[0].OS {
|
|
| 80 |
- return nil, fmt.Errorf("received incorrect platform information from registry")
|
|
| 81 |
- } |
|
| 82 | 75 |
|
| 76 |
+ assert.Equal(t, "foobar:1.0@sha256:c0537ff6a5218ef531ece93d4984efc99bbf3f7497c0a7726c88e2bb7584dc96", serviceSpec.TaskTemplate.ContainerSpec.Image) |
|
| 77 |
+ assert.Len(t, serviceSpec.TaskTemplate.Placement.Platforms, 1) |
|
| 78 |
+ |
|
| 79 |
+ p := serviceSpec.TaskTemplate.Placement.Platforms[0] |
|
| 83 | 80 |
b, err := json.Marshal(types.ServiceCreateResponse{
|
| 84 |
- ID: "service_" + platforms[0].Architecture, |
|
| 81 |
+ ID: "service_" + p.OS + "_" + p.Architecture, |
|
| 85 | 82 |
}) |
| 86 | 83 |
if err != nil {
|
| 87 | 84 |
return nil, err |
| ... | ... |
@@ -91,20 +88,20 @@ func TestServiceCreateCompatiblePlatforms(t *testing.T) {
|
| 91 | 91 |
Body: ioutil.NopCloser(bytes.NewReader(b)), |
| 92 | 92 |
}, nil |
| 93 | 93 |
} else if strings.HasPrefix(req.URL.Path, "/v1.30/distribution/") {
|
| 94 |
- platforms = []v1.Platform{
|
|
| 95 |
- {
|
|
| 96 |
- Architecture: "amd64", |
|
| 97 |
- OS: "linux", |
|
| 98 |
- }, |
|
| 99 |
- } |
|
| 100 | 94 |
b, err := json.Marshal(registrytypes.DistributionInspect{
|
| 101 |
- Descriptor: v1.Descriptor{},
|
|
| 102 |
- Platforms: platforms, |
|
| 95 |
+ Descriptor: v1.Descriptor{
|
|
| 96 |
+ Digest: "sha256:c0537ff6a5218ef531ece93d4984efc99bbf3f7497c0a7726c88e2bb7584dc96", |
|
| 97 |
+ }, |
|
| 98 |
+ Platforms: []v1.Platform{
|
|
| 99 |
+ {
|
|
| 100 |
+ Architecture: "amd64", |
|
| 101 |
+ OS: "linux", |
|
| 102 |
+ }, |
|
| 103 |
+ }, |
|
| 103 | 104 |
}) |
| 104 | 105 |
if err != nil {
|
| 105 | 106 |
return nil, err |
| 106 | 107 |
} |
| 107 |
- distributionInspectBody = ioutil.NopCloser(bytes.NewReader(b)) |
|
| 108 | 108 |
return &http.Response{
|
| 109 | 109 |
StatusCode: http.StatusOK, |
| 110 | 110 |
Body: ioutil.NopCloser(bytes.NewReader(b)), |
| ... | ... |
@@ -115,13 +112,11 @@ func TestServiceCreateCompatiblePlatforms(t *testing.T) {
|
| 115 | 115 |
}), |
| 116 | 116 |
} |
| 117 | 117 |
|
| 118 |
- r, err := client.ServiceCreate(context.Background(), swarm.ServiceSpec{}, types.ServiceCreateOptions{QueryRegistry: true})
|
|
| 119 |
- if err != nil {
|
|
| 120 |
- t.Fatal(err) |
|
| 121 |
- } |
|
| 122 |
- if r.ID != "service_amd64" {
|
|
| 123 |
- t.Fatalf("expected `service_amd64`, got %s", r.ID)
|
|
| 124 |
- } |
|
| 118 |
+ spec := swarm.ServiceSpec{TaskTemplate: swarm.TaskSpec{ContainerSpec: swarm.ContainerSpec{Image: "foobar:1.0"}}}
|
|
| 119 |
+ |
|
| 120 |
+ r, err := client.ServiceCreate(context.Background(), spec, types.ServiceCreateOptions{QueryRegistry: true})
|
|
| 121 |
+ assert.NoError(t, err) |
|
| 122 |
+ assert.Equal(t, "service_linux_amd64", r.ID) |
|
| 125 | 123 |
} |
| 126 | 124 |
|
| 127 | 125 |
func TestServiceCreateDigestPinning(t *testing.T) {
|