Browse code

Make sockRequestRaw return reader, not []byte

Signed-off-by: Brian Goff <cpuguy83@gmail.com>

Brian Goff authored on 2015/04/14 11:53:54
Showing 2 changed files
... ...
@@ -369,10 +369,15 @@ func TestBuildApiDockerfilePath(t *testing.T) {
369 369
 		t.Fatalf("failed to close tar archive: %v", err)
370 370
 	}
371 371
 
372
-	_, out, err := sockRequestRaw("POST", "/build?dockerfile=../Dockerfile", buffer, "application/x-tar")
372
+	_, body, err := sockRequestRaw("POST", "/build?dockerfile=../Dockerfile", buffer, "application/x-tar")
373 373
 	if err == nil {
374
+		out, _ := readBody(body)
374 375
 		t.Fatalf("Build was supposed to fail: %s", out)
375 376
 	}
377
+	out, err := readBody(body)
378
+	if err != nil {
379
+		t.Fatal(err)
380
+	}
376 381
 
377 382
 	if !strings.Contains(string(out), "must be within the build context") {
378 383
 		t.Fatalf("Didn't complain about leaving build context: %s", out)
... ...
@@ -393,10 +398,14 @@ RUN find /tmp/`,
393 393
 	}
394 394
 	defer server.Close()
395 395
 
396
-	_, buf, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+server.URL()+"/testD", nil, "application/json")
396
+	_, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+server.URL()+"/testD", nil, "application/json")
397 397
 	if err != nil {
398 398
 		t.Fatalf("Build failed: %s", err)
399 399
 	}
400
+	buf, err := readBody(body)
401
+	if err != nil {
402
+		t.Fatal(err)
403
+	}
400 404
 
401 405
 	// Make sure Dockerfile exists.
402 406
 	// Make sure 'baz' doesn't exist ANYWHERE despite being mentioned in the URL
... ...
@@ -419,10 +428,15 @@ RUN echo from dockerfile`,
419 419
 	}
420 420
 	defer git.Close()
421 421
 
422
-	_, buf, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
422
+	_, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
423 423
 	if err != nil {
424
+		buf, _ := readBody(body)
424 425
 		t.Fatalf("Build failed: %s\n%q", err, buf)
425 426
 	}
427
+	buf, err := readBody(body)
428
+	if err != nil {
429
+		t.Fatal(err)
430
+	}
426 431
 
427 432
 	out := string(buf)
428 433
 	if !strings.Contains(out, "from dockerfile") {
... ...
@@ -445,10 +459,15 @@ RUN echo from Dockerfile`,
445 445
 	defer git.Close()
446 446
 
447 447
 	// Make sure it tries to 'dockerfile' query param value
448
-	_, buf, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+git.RepoURL, nil, "application/json")
448
+	_, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+git.RepoURL, nil, "application/json")
449 449
 	if err != nil {
450
+		buf, _ := readBody(body)
450 451
 		t.Fatalf("Build failed: %s\n%q", err, buf)
451 452
 	}
453
+	buf, err := readBody(body)
454
+	if err != nil {
455
+		t.Fatal(err)
456
+	}
452 457
 
453 458
 	out := string(buf)
454 459
 	if !strings.Contains(out, "from baz") {
... ...
@@ -472,10 +491,14 @@ RUN echo from dockerfile`,
472 472
 	defer git.Close()
473 473
 
474 474
 	// Make sure it tries to 'dockerfile' query param value
475
-	_, buf, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
475
+	_, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
476 476
 	if err != nil {
477 477
 		t.Fatalf("Build failed: %s", err)
478 478
 	}
479
+	buf, err := readBody(body)
480
+	if err != nil {
481
+		t.Fatal(err)
482
+	}
479 483
 
480 484
 	out := string(buf)
481 485
 	if !strings.Contains(out, "from Dockerfile") {
... ...
@@ -503,10 +526,15 @@ func TestBuildApiDockerfileSymlink(t *testing.T) {
503 503
 		t.Fatalf("failed to close tar archive: %v", err)
504 504
 	}
505 505
 
506
-	_, out, err := sockRequestRaw("POST", "/build", buffer, "application/x-tar")
506
+	_, body, err := sockRequestRaw("POST", "/build", buffer, "application/x-tar")
507 507
 	if err == nil {
508
+		out, _ := readBody(body)
508 509
 		t.Fatalf("Build was supposed to fail: %s", out)
509 510
 	}
511
+	out, err := readBody(body)
512
+	if err != nil {
513
+		t.Fatal(err)
514
+	}
510 515
 
511 516
 	// The reason the error is "Cannot locate specified Dockerfile" is because
512 517
 	// in the builder, the symlink is resolved within the context, therefore
... ...
@@ -22,6 +22,7 @@ import (
22 22
 	"time"
23 23
 
24 24
 	"github.com/docker/docker/api"
25
+	"github.com/docker/docker/pkg/ioutils"
25 26
 	"github.com/docker/docker/pkg/stringutils"
26 27
 )
27 28
 
... ...
@@ -304,20 +305,27 @@ func sockRequest(method, endpoint string, data interface{}) (int, []byte, error)
304 304
 		return -1, nil, err
305 305
 	}
306 306
 
307
-	return sockRequestRaw(method, endpoint, jsonData, "application/json")
307
+	status, body, err := sockRequestRaw(method, endpoint, jsonData, "application/json")
308
+	if err != nil {
309
+		b, _ := ioutil.ReadAll(body)
310
+		return status, b, err
311
+	}
312
+	var b []byte
313
+	b, err = readBody(body)
314
+	return status, b, err
308 315
 }
309 316
 
310
-func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (int, []byte, error) {
317
+func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (int, io.ReadCloser, error) {
311 318
 	c, err := sockConn(time.Duration(10 * time.Second))
312 319
 	if err != nil {
313 320
 		return -1, nil, fmt.Errorf("could not dial docker daemon: %v", err)
314 321
 	}
315 322
 
316 323
 	client := httputil.NewClientConn(c, nil)
317
-	defer client.Close()
318 324
 
319 325
 	req, err := http.NewRequest(method, endpoint, data)
320 326
 	if err != nil {
327
+		client.Close()
321 328
 		return -1, nil, fmt.Errorf("could not create new request: %v", err)
322 329
 	}
323 330
 
... ...
@@ -328,17 +336,23 @@ func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (int, []
328 328
 
329 329
 	resp, err := client.Do(req)
330 330
 	if err != nil {
331
+		client.Close()
331 332
 		return -1, nil, fmt.Errorf("could not perform request: %v", err)
332 333
 	}
333
-	defer resp.Body.Close()
334
+	body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
335
+		defer client.Close()
336
+		return resp.Body.Close()
337
+	})
334 338
 	if resp.StatusCode != http.StatusOK {
335
-		body, _ := ioutil.ReadAll(resp.Body)
336 339
 		return resp.StatusCode, body, fmt.Errorf("received status != 200 OK: %s", resp.Status)
337 340
 	}
338 341
 
339
-	b, err := ioutil.ReadAll(resp.Body)
342
+	return resp.StatusCode, body, err
343
+}
340 344
 
341
-	return resp.StatusCode, b, err
345
+func readBody(b io.ReadCloser) ([]byte, error) {
346
+	defer b.Close()
347
+	return ioutil.ReadAll(b)
342 348
 }
343 349
 
344 350
 func deleteContainer(container string) error {