Skip to content

Commit 9d1b4f5

Browse files
committed
Add regression tests for invalid platform status codes
Before we handled containerd errors, using an invalid platform produced a 500 status: ```bash curl -v \ -X POST \ --unix-socket /var/run/docker.sock \ "http://localhost:2375/v1.40/images/create?fromImage=hello-world&platform=foobar&tag=latest" \ -H "Content-Type: application/json" ``` ``` * Connected to localhost (docker.sock) port 80 (#0) > POST /v1.40/images/create?fromImage=hello-world&platform=foobar&tag=latest HTTP/1.1 > Host: localhost:2375 > User-Agent: curl/7.54.0 > Accept: */* > Content-Type: application/json > < HTTP/1.1 500 Internal Server Error < Api-Version: 1.40 < Content-Length: 85 < Content-Type: application/json < Date: Mon, 15 Jul 2019 15:25:44 GMT < Docker-Experimental: true < Ostype: linux < Server: Docker/19.03.0-rc2 (linux) < {"message":"\"foobar\": unknown operating system or architecture: invalid argument"} ``` That problem is now fixed, and the API correctly returns a 4xx status: ```bash curl -v \ -X POST \ --unix-socket /var/run/docker.sock \ "http://localhost:2375/v1.40/images/create?fromImage=hello-world&platform=foobar&tag=latest" \ -H "Content-Type: application/json" ``` ``` * Connected to localhost (/var/run/docker.sock) port 80 (#0) > POST /v1.40/images/create?fromImage=hello-world&platform=foobar&tag=latest HTTP/1.1 > Host: localhost:2375 > User-Agent: curl/7.52.1 > Accept: */* > Content-Type: application/json > < HTTP/1.1 400 Bad Request < Api-Version: 1.41 < Content-Type: application/json < Docker-Experimental: true < Ostype: linux < Server: Docker/dev (linux) < Date: Mon, 15 Jul 2019 15:13:42 GMT < Content-Length: 85 < {"message":"\"foobar\": unknown operating system or architecture: invalid argument"} * Curl_http_done: called premature == 0 ``` This patch adds tests to validate the behaviour Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 4a51621 commit 9d1b4f5

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

errdefs/http_helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func statusCodeFromDistributionError(err error) int {
177177
}
178178

179179
// statusCodeFromContainerdError returns status code for containerd errors when
180-
// consumed directory (not through gRPC)
180+
// consumed directly (not through gRPC)
181181
func statusCodeFromContainerdError(err error) int {
182182
switch {
183183
case containerderrors.IsInvalidArgument(err):

integration/build/build_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/docker/docker/api/types"
1414
"github.com/docker/docker/api/types/filters"
1515
"github.com/docker/docker/api/types/versions"
16+
"github.com/docker/docker/errdefs"
1617
"github.com/docker/docker/internal/test/fakecontext"
1718
"github.com/docker/docker/pkg/jsonmessage"
1819
"gotest.tools/assert"
@@ -562,6 +563,35 @@ func TestBuildPreserveOwnership(t *testing.T) {
562563
}
563564
}
564565

566+
func TestBuildPlatformInvalid(t *testing.T) {
567+
skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.40"), "experimental in older versions")
568+
569+
ctx := context.Background()
570+
defer setupTest(t)()
571+
572+
dockerfile := `FROM busybox
573+
`
574+
575+
buf := bytes.NewBuffer(nil)
576+
w := tar.NewWriter(buf)
577+
writeTarRecord(t, w, "Dockerfile", dockerfile)
578+
err := w.Close()
579+
assert.NilError(t, err)
580+
581+
apiclient := testEnv.APIClient()
582+
_, err = apiclient.ImageBuild(ctx,
583+
buf,
584+
types.ImageBuildOptions{
585+
Remove: true,
586+
ForceRemove: true,
587+
Platform: "foobar",
588+
})
589+
590+
assert.Assert(t, err != nil)
591+
assert.ErrorContains(t, err, "unknown operating system or architecture")
592+
assert.Assert(t, errdefs.IsInvalidParameter(err))
593+
}
594+
565595
func writeTarRecord(t *testing.T, w *tar.Writer, fn, contents string) {
566596
err := w.WriteHeader(&tar.Header{
567597
Name: fn,

integration/image/pull_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package image
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/docker/docker/api/types"
8+
"github.com/docker/docker/api/types/versions"
9+
"github.com/docker/docker/errdefs"
10+
"gotest.tools/assert"
11+
"gotest.tools/skip"
12+
)
13+
14+
func TestImagePullPlatformInvalid(t *testing.T) {
15+
skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.40"), "experimental in older versions")
16+
defer setupTest(t)()
17+
client := testEnv.APIClient()
18+
ctx := context.Background()
19+
20+
_, err := client.ImagePull(ctx, "docker.io/library/hello-world:latest", types.ImagePullOptions{Platform: "foobar"})
21+
assert.Assert(t, err != nil)
22+
assert.ErrorContains(t, err, "unknown operating system or architecture")
23+
assert.Assert(t, errdefs.IsInvalidParameter(err))
24+
}

0 commit comments

Comments
 (0)