Browse code

Expose whole Response struct in sockRequestRaw

Signed-off-by: Antonio Murdaca <me@runcom.ninja>

Antonio Murdaca authored on 2015/04/28 01:33:08
Showing 4 changed files
... ...
@@ -339,8 +339,8 @@ func (s *DockerSuite) TestBuildApiDockerfilePath(c *check.C) {
339 339
 		c.Fatalf("failed to close tar archive: %v", err)
340 340
 	}
341 341
 
342
-	status, body, err := sockRequestRaw("POST", "/build?dockerfile=../Dockerfile", buffer, "application/x-tar")
343
-	c.Assert(status, check.Equals, http.StatusInternalServerError)
342
+	res, body, err := sockRequestRaw("POST", "/build?dockerfile=../Dockerfile", buffer, "application/x-tar")
343
+	c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
344 344
 	c.Assert(err, check.IsNil)
345 345
 
346 346
 	out, err := readBody(body)
... ...
@@ -365,8 +365,8 @@ RUN find /tmp/`,
365 365
 	}
366 366
 	defer server.Close()
367 367
 
368
-	status, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+server.URL()+"/testD", nil, "application/json")
369
-	c.Assert(status, check.Equals, http.StatusOK)
368
+	res, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+server.URL()+"/testD", nil, "application/json")
369
+	c.Assert(res.StatusCode, check.Equals, http.StatusOK)
370 370
 	c.Assert(err, check.IsNil)
371 371
 
372 372
 	buf, err := readBody(body)
... ...
@@ -393,8 +393,8 @@ RUN echo from dockerfile`,
393 393
 	}
394 394
 	defer git.Close()
395 395
 
396
-	status, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
397
-	c.Assert(status, check.Equals, http.StatusOK)
396
+	res, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
397
+	c.Assert(res.StatusCode, check.Equals, http.StatusOK)
398 398
 	c.Assert(err, check.IsNil)
399 399
 
400 400
 	buf, err := readBody(body)
... ...
@@ -421,8 +421,8 @@ RUN echo from Dockerfile`,
421 421
 	defer git.Close()
422 422
 
423 423
 	// Make sure it tries to 'dockerfile' query param value
424
-	status, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+git.RepoURL, nil, "application/json")
425
-	c.Assert(status, check.Equals, http.StatusOK)
424
+	res, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+git.RepoURL, nil, "application/json")
425
+	c.Assert(res.StatusCode, check.Equals, http.StatusOK)
426 426
 	c.Assert(err, check.IsNil)
427 427
 
428 428
 	buf, err := readBody(body)
... ...
@@ -450,8 +450,8 @@ RUN echo from dockerfile`,
450 450
 	defer git.Close()
451 451
 
452 452
 	// Make sure it tries to 'dockerfile' query param value
453
-	status, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
454
-	c.Assert(status, check.Equals, http.StatusOK)
453
+	res, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
454
+	c.Assert(res.StatusCode, check.Equals, http.StatusOK)
455 455
 	c.Assert(err, check.IsNil)
456 456
 
457 457
 	buf, err := readBody(body)
... ...
@@ -483,8 +483,8 @@ func (s *DockerSuite) TestBuildApiDockerfileSymlink(c *check.C) {
483 483
 		c.Fatalf("failed to close tar archive: %v", err)
484 484
 	}
485 485
 
486
-	status, body, err := sockRequestRaw("POST", "/build", buffer, "application/x-tar")
487
-	c.Assert(status, check.Equals, http.StatusInternalServerError)
486
+	res, body, err := sockRequestRaw("POST", "/build", buffer, "application/x-tar")
487
+	c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
488 488
 	c.Assert(err, check.IsNil)
489 489
 
490 490
 	out, err := readBody(body)
... ...
@@ -720,7 +720,7 @@ func (s *DockerSuite) TestContainerApiVerifyHeader(c *check.C) {
720 720
 		"Image": "busybox",
721 721
 	}
722 722
 
723
-	create := func(ct string) (int, io.ReadCloser, error) {
723
+	create := func(ct string) (*http.Response, io.ReadCloser, error) {
724 724
 		jsonData := bytes.NewBuffer(nil)
725 725
 		if err := json.NewEncoder(jsonData).Encode(config); err != nil {
726 726
 			c.Fatal(err)
... ...
@@ -729,21 +729,21 @@ func (s *DockerSuite) TestContainerApiVerifyHeader(c *check.C) {
729 729
 	}
730 730
 
731 731
 	// Try with no content-type
732
-	status, body, err := create("")
733
-	c.Assert(status, check.Equals, http.StatusInternalServerError)
732
+	res, body, err := create("")
734 733
 	c.Assert(err, check.IsNil)
734
+	c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
735 735
 	body.Close()
736 736
 
737 737
 	// Try with wrong content-type
738
-	status, body, err = create("application/xml")
739
-	c.Assert(status, check.Equals, http.StatusInternalServerError)
738
+	res, body, err = create("application/xml")
740 739
 	c.Assert(err, check.IsNil)
740
+	c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
741 741
 	body.Close()
742 742
 
743 743
 	// now application/json
744
-	status, body, err = create("application/json")
745
-	c.Assert(status, check.Equals, http.StatusCreated)
744
+	res, body, err = create("application/json")
746 745
 	c.Assert(err, check.IsNil)
746
+	c.Assert(res.StatusCode, check.Equals, http.StatusCreated)
747 747
 	body.Close()
748 748
 }
749 749
 
... ...
@@ -774,8 +774,8 @@ func (s *DockerSuite) TestContainerApiPostCreateNull(c *check.C) {
774 774
 		"NetworkDisabled":false,
775 775
 		"OnBuild":null}`
776 776
 
777
-	status, body, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json")
778
-	c.Assert(status, check.Equals, http.StatusCreated)
777
+	res, body, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json")
778
+	c.Assert(res.StatusCode, check.Equals, http.StatusCreated)
779 779
 	c.Assert(err, check.IsNil)
780 780
 
781 781
 	b, err := readBody(body)
... ...
@@ -808,13 +808,13 @@ func (s *DockerSuite) TestCreateWithTooLowMemoryLimit(c *check.C) {
808 808
 		"Memory":    524287
809 809
 	}`
810 810
 
811
-	status, body, _ := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json")
811
+	res, body, _ := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json")
812 812
 	b, err2 := readBody(body)
813 813
 	if err2 != nil {
814 814
 		c.Fatal(err2)
815 815
 	}
816 816
 
817
-	c.Assert(status, check.Equals, http.StatusInternalServerError)
817
+	c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
818 818
 	c.Assert(strings.Contains(string(b), "Minimum memory limit allowed is 4MB"), check.Equals, true)
819 819
 }
820 820
 
... ...
@@ -831,13 +831,13 @@ func (s *DockerSuite) TestStartWithTooLowMemoryLimit(c *check.C) {
831 831
                 "Memory":    524287
832 832
         }`
833 833
 
834
-	status, body, _ := sockRequestRaw("POST", "/containers/"+containerID+"/start", strings.NewReader(config), "application/json")
834
+	res, body, _ := sockRequestRaw("POST", "/containers/"+containerID+"/start", strings.NewReader(config), "application/json")
835 835
 	b, err2 := readBody(body)
836 836
 	if err2 != nil {
837 837
 		c.Fatal(err2)
838 838
 	}
839 839
 
840
-	c.Assert(status, check.Equals, http.StatusInternalServerError)
840
+	c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
841 841
 	c.Assert(strings.Contains(string(b), "Minimum memory limit allowed is 4MB"), check.Equals, true)
842 842
 }
843 843
 
... ...
@@ -74,9 +74,9 @@ func (s *DockerSuite) TestApiImagesSaveAndLoad(c *check.C) {
74 74
 	}
75 75
 	id := strings.TrimSpace(out)
76 76
 
77
-	status, body, err := sockRequestRaw("GET", "/images/"+id+"/get", nil, "")
78
-	c.Assert(status, check.Equals, http.StatusOK)
77
+	res, body, err := sockRequestRaw("GET", "/images/"+id+"/get", nil, "")
79 78
 	c.Assert(err, check.IsNil)
79
+	c.Assert(res.StatusCode, check.Equals, http.StatusOK)
80 80
 
81 81
 	defer body.Close()
82 82
 
... ...
@@ -84,9 +84,9 @@ func (s *DockerSuite) TestApiImagesSaveAndLoad(c *check.C) {
84 84
 		c.Fatal(err, out)
85 85
 	}
86 86
 
87
-	status, loadBody, err := sockRequestRaw("POST", "/images/load", body, "application/x-tar")
88
-	c.Assert(status, check.Equals, http.StatusOK)
87
+	res, loadBody, err := sockRequestRaw("POST", "/images/load", body, "application/x-tar")
89 88
 	c.Assert(err, check.IsNil)
89
+	c.Assert(res.StatusCode, check.Equals, http.StatusOK)
90 90
 
91 91
 	defer loadBody.Close()
92 92
 
... ...
@@ -20,22 +20,22 @@ func (s *DockerSuite) TestLogsApiWithStdout(c *check.C) {
20 20
 	}
21 21
 
22 22
 	type logOut struct {
23
-		out    string
24
-		status int
25
-		err    error
23
+		out string
24
+		res *http.Response
25
+		err error
26 26
 	}
27 27
 	chLog := make(chan logOut)
28 28
 
29 29
 	go func() {
30
-		statusCode, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&timestamps=1", id), nil, "")
30
+		res, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&timestamps=1", id), nil, "")
31 31
 		out, _ := bufio.NewReader(body).ReadString('\n')
32
-		chLog <- logOut{strings.TrimSpace(out), statusCode, err}
32
+		chLog <- logOut{strings.TrimSpace(out), res, err}
33 33
 	}()
34 34
 
35 35
 	select {
36 36
 	case l := <-chLog:
37
-		c.Assert(l.status, check.Equals, http.StatusOK)
38 37
 		c.Assert(l.err, check.IsNil)
38
+		c.Assert(l.res.StatusCode, check.Equals, http.StatusOK)
39 39
 		if !strings.HasSuffix(l.out, "hello") {
40 40
 			c.Fatalf("expected log output to container 'hello', but it does not")
41 41
 		}
... ...
@@ -313,20 +313,20 @@ func sockRequest(method, endpoint string, data interface{}) (int, []byte, error)
313 313
 		return -1, nil, err
314 314
 	}
315 315
 
316
-	status, body, err := sockRequestRaw(method, endpoint, jsonData, "application/json")
316
+	res, body, err := sockRequestRaw(method, endpoint, jsonData, "application/json")
317 317
 	if err != nil {
318 318
 		b, _ := ioutil.ReadAll(body)
319
-		return status, b, err
319
+		return -1, b, err
320 320
 	}
321 321
 	var b []byte
322 322
 	b, err = readBody(body)
323
-	return status, b, err
323
+	return res.StatusCode, b, err
324 324
 }
325 325
 
326
-func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (int, io.ReadCloser, error) {
326
+func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (*http.Response, io.ReadCloser, error) {
327 327
 	c, err := sockConn(time.Duration(10 * time.Second))
328 328
 	if err != nil {
329
-		return -1, nil, fmt.Errorf("could not dial docker daemon: %v", err)
329
+		return nil, nil, fmt.Errorf("could not dial docker daemon: %v", err)
330 330
 	}
331 331
 
332 332
 	client := httputil.NewClientConn(c, nil)
... ...
@@ -334,7 +334,7 @@ func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (int, io
334 334
 	req, err := http.NewRequest(method, endpoint, data)
335 335
 	if err != nil {
336 336
 		client.Close()
337
-		return -1, nil, fmt.Errorf("could not create new request: %v", err)
337
+		return nil, nil, fmt.Errorf("could not create new request: %v", err)
338 338
 	}
339 339
 
340 340
 	if ct != "" {
... ...
@@ -344,14 +344,14 @@ func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (int, io
344 344
 	resp, err := client.Do(req)
345 345
 	if err != nil {
346 346
 		client.Close()
347
-		return -1, nil, fmt.Errorf("could not perform request: %v", err)
347
+		return nil, nil, fmt.Errorf("could not perform request: %v", err)
348 348
 	}
349 349
 	body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
350 350
 		defer client.Close()
351 351
 		return resp.Body.Close()
352 352
 	})
353 353
 
354
-	return resp.StatusCode, body, err
354
+	return resp, body, nil
355 355
 }
356 356
 
357 357
 func readBody(b io.ReadCloser) ([]byte, error) {