migrate TestAPIImageImportBadSrc to integration test

Signed-off-by: Aditya Mishra <mishraaditya675@gmail.com>
This commit is contained in:
Aditya Mishra
2025-11-17 23:59:16 +05:30
parent 3bd2edb375
commit ae4c17fdb7
2 changed files with 55 additions and 27 deletions

View File

@@ -2,8 +2,6 @@ package main
import ( import (
"net/http" "net/http"
"net/http/httptest"
"strings"
"testing" "testing"
"github.com/moby/moby/v2/internal/testutil" "github.com/moby/moby/v2/internal/testutil"
@@ -11,31 +9,6 @@ import (
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
) )
func (s *DockerAPISuite) TestAPIImagesImportBadSrc(c *testing.T) {
testRequires(c, Network, testEnv.IsLocalDaemon)
server := httptest.NewServer(http.NewServeMux())
defer server.Close()
tt := []struct {
statusExp int
fromSrc string
}{
{http.StatusNotFound, server.URL + "/nofile.tar"},
{http.StatusNotFound, strings.TrimPrefix(server.URL, "http://") + "/nofile.tar"},
{http.StatusNotFound, strings.TrimPrefix(server.URL, "http://") + "%2Fdata%2Ffile.tar"},
{http.StatusInternalServerError, "%2Fdata%2Ffile.tar"},
}
ctx := testutil.GetContext(c)
for _, te := range tt {
res, _, err := request.Post(ctx, "/images/create?fromSrc="+te.fromSrc, request.JSON)
assert.NilError(c, err)
assert.Equal(c, res.StatusCode, te.statusExp)
assert.Equal(c, res.Header.Get("Content-Type"), "application/json")
}
}
// #14846 // #14846
func (s *DockerAPISuite) TestAPIImagesSearchJSONContentType(c *testing.T) { func (s *DockerAPISuite) TestAPIImagesSearchJSONContentType(c *testing.T) {
testRequires(c, Network) testRequires(c, Network)

View File

@@ -4,6 +4,7 @@ import (
"archive/tar" "archive/tar"
"bytes" "bytes"
"io" "io"
"net/http/httptest"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@@ -189,3 +190,57 @@ func TestImportWithCustomPlatformReject(t *testing.T) {
}) })
} }
} }
func TestImageImportBadSrc(t *testing.T) {
ctx := setupTest(t)
apiClient := testEnv.APIClient()
skip.If(t, testEnv.IsRootless, "rootless daemon cannot access the test's HTTP server in the host's netns")
server := httptest.NewServer(nil)
defer server.Close()
trimmedHTTP := strings.TrimPrefix(server.URL, "http://")
tests := []struct {
name string
fromSrc string
expectErr func(error) bool
}{
{
name: "missing file via full URL",
fromSrc: server.URL + "/nofile.tar",
expectErr: cerrdefs.IsNotFound,
},
{
name: "missing file via trimmed URL",
fromSrc: trimmedHTTP + "/nofile.tar",
expectErr: cerrdefs.IsNotFound,
},
{
name: "encoded path via trimmed URL",
fromSrc: trimmedHTTP + "/%2Fdata%2Ffile.tar",
expectErr: cerrdefs.IsNotFound,
},
{
name: "encoded absolute path",
fromSrc: "%2Fdata%2Ffile.tar",
expectErr: cerrdefs.IsInvalidArgument,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
_, err := apiClient.ImageImport(ctx,
client.ImageImportSource{
SourceName: tc.fromSrc,
},
"import-bad-src:test",
client.ImageImportOptions{},
)
assert.Check(t, tc.expectErr(err))
})
}
}