Browse code

migrate TestAPIImageImportBadSrc to integration test

Signed-off-by: Aditya Mishra <mishraaditya675@gmail.com>

Aditya Mishra authored on 2025/11/18 03:29:16
Showing 2 changed files
... ...
@@ -2,8 +2,6 @@ package main
2 2
 
3 3
 import (
4 4
 	"net/http"
5
-	"net/http/httptest"
6
-	"strings"
7 5
 	"testing"
8 6
 
9 7
 	"github.com/moby/moby/v2/internal/testutil"
... ...
@@ -11,31 +9,6 @@ import (
11 11
 	"gotest.tools/v3/assert"
12 12
 )
13 13
 
14
-func (s *DockerAPISuite) TestAPIImagesImportBadSrc(c *testing.T) {
15
-	testRequires(c, Network, testEnv.IsLocalDaemon)
16
-
17
-	server := httptest.NewServer(http.NewServeMux())
18
-	defer server.Close()
19
-
20
-	tt := []struct {
21
-		statusExp int
22
-		fromSrc   string
23
-	}{
24
-		{http.StatusNotFound, server.URL + "/nofile.tar"},
25
-		{http.StatusNotFound, strings.TrimPrefix(server.URL, "http://") + "/nofile.tar"},
26
-		{http.StatusNotFound, strings.TrimPrefix(server.URL, "http://") + "%2Fdata%2Ffile.tar"},
27
-		{http.StatusInternalServerError, "%2Fdata%2Ffile.tar"},
28
-	}
29
-
30
-	ctx := testutil.GetContext(c)
31
-	for _, te := range tt {
32
-		res, _, err := request.Post(ctx, "/images/create?fromSrc="+te.fromSrc, request.JSON)
33
-		assert.NilError(c, err)
34
-		assert.Equal(c, res.StatusCode, te.statusExp)
35
-		assert.Equal(c, res.Header.Get("Content-Type"), "application/json")
36
-	}
37
-}
38
-
39 14
 // #14846
40 15
 func (s *DockerAPISuite) TestAPIImagesSearchJSONContentType(c *testing.T) {
41 16
 	testRequires(c, Network)
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"archive/tar"
5 5
 	"bytes"
6 6
 	"io"
7
+	"net/http/httptest"
7 8
 	"runtime"
8 9
 	"strconv"
9 10
 	"strings"
... ...
@@ -189,3 +190,57 @@ func TestImportWithCustomPlatformReject(t *testing.T) {
189 189
 		})
190 190
 	}
191 191
 }
192
+
193
+func TestImageImportBadSrc(t *testing.T) {
194
+	ctx := setupTest(t)
195
+	apiClient := testEnv.APIClient()
196
+
197
+	skip.If(t, testEnv.IsRootless, "rootless daemon cannot access the test's HTTP server in the host's netns")
198
+
199
+	server := httptest.NewServer(nil)
200
+	defer server.Close()
201
+
202
+	trimmedHTTP := strings.TrimPrefix(server.URL, "http://")
203
+
204
+	tests := []struct {
205
+		name      string
206
+		fromSrc   string
207
+		expectErr func(error) bool
208
+	}{
209
+		{
210
+			name:      "missing file via full URL",
211
+			fromSrc:   server.URL + "/nofile.tar",
212
+			expectErr: cerrdefs.IsNotFound,
213
+		},
214
+		{
215
+			name:      "missing file via trimmed URL",
216
+			fromSrc:   trimmedHTTP + "/nofile.tar",
217
+			expectErr: cerrdefs.IsNotFound,
218
+		},
219
+		{
220
+			name:      "encoded path via trimmed URL",
221
+			fromSrc:   trimmedHTTP + "/%2Fdata%2Ffile.tar",
222
+			expectErr: cerrdefs.IsNotFound,
223
+		},
224
+		{
225
+			name:      "encoded absolute path",
226
+			fromSrc:   "%2Fdata%2Ffile.tar",
227
+			expectErr: cerrdefs.IsInvalidArgument,
228
+		},
229
+	}
230
+
231
+	for _, tc := range tests {
232
+		t.Run(tc.name, func(t *testing.T) {
233
+			_, err := apiClient.ImageImport(ctx,
234
+				client.ImageImportSource{
235
+					SourceName: tc.fromSrc,
236
+				},
237
+				"import-bad-src:test",
238
+				client.ImageImportOptions{},
239
+			)
240
+
241
+			assert.Check(t, tc.expectErr(err))
242
+		})
243
+	}
244
+
245
+}