Browse code

integ-cli: Use status code from sockRequest (fix #12335)

sockRequest now makes the status code available in the returned
values. This helps avoid string checking for non-HttpStatusOK(=200)
yet successful error messages.

Signed-off-by: Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>

Ahmet Alp Balkan authored on 2015/04/14 17:00:46
Showing 2 changed files
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"bytes"
5 5
 	"encoding/json"
6 6
 	"io"
7
+	"net/http"
7 8
 	"os/exec"
8 9
 	"strings"
9 10
 	"testing"
... ...
@@ -134,7 +135,7 @@ func TestContainerApiStartVolumeBinds(t *testing.T) {
134 134
 		"Volumes": map[string]struct{}{"/tmp": {}},
135 135
 	}
136 136
 
137
-	if _, _, err := sockRequest("POST", "/containers/create?name="+name, config); err != nil && !strings.Contains(err.Error(), "201 Created") {
137
+	if status, _, err := sockRequest("POST", "/containers/create?name="+name, config); err != nil && status != http.StatusCreated {
138 138
 		t.Fatal(err)
139 139
 	}
140 140
 
... ...
@@ -142,7 +143,7 @@ func TestContainerApiStartVolumeBinds(t *testing.T) {
142 142
 	config = map[string]interface{}{
143 143
 		"Binds": []string{bindPath + ":/tmp"},
144 144
 	}
145
-	if _, _, err := sockRequest("POST", "/containers/"+name+"/start", config); err != nil && !strings.Contains(err.Error(), "204 No Content") {
145
+	if status, _, err := sockRequest("POST", "/containers/"+name+"/start", config); err != nil && status != http.StatusNoContent {
146 146
 		t.Fatal(err)
147 147
 	}
148 148
 
... ...
@@ -167,7 +168,7 @@ func TestContainerApiStartDupVolumeBinds(t *testing.T) {
167 167
 		"Volumes": map[string]struct{}{"/tmp": {}},
168 168
 	}
169 169
 
170
-	if _, _, err := sockRequest("POST", "/containers/create?name="+name, config); err != nil && !strings.Contains(err.Error(), "201 Created") {
170
+	if status, _, err := sockRequest("POST", "/containers/create?name="+name, config); err != nil && status != http.StatusCreated {
171 171
 		t.Fatal(err)
172 172
 	}
173 173
 
... ...
@@ -202,14 +203,14 @@ func TestContainerApiStartVolumesFrom(t *testing.T) {
202 202
 		"Volumes": map[string]struct{}{volPath: {}},
203 203
 	}
204 204
 
205
-	if _, _, err := sockRequest("POST", "/containers/create?name="+name, config); err != nil && !strings.Contains(err.Error(), "201 Created") {
205
+	if status, _, err := sockRequest("POST", "/containers/create?name="+name, config); err != nil && status != http.StatusCreated {
206 206
 		t.Fatal(err)
207 207
 	}
208 208
 
209 209
 	config = map[string]interface{}{
210 210
 		"VolumesFrom": []string{volName},
211 211
 	}
212
-	if _, _, err := sockRequest("POST", "/containers/"+name+"/start", config); err != nil && !strings.Contains(err.Error(), "204 No Content") {
212
+	if status, _, err := sockRequest("POST", "/containers/"+name+"/start", config); err != nil && status != http.StatusNoContent {
213 213
 		t.Fatal(err)
214 214
 	}
215 215
 
... ...
@@ -246,7 +247,7 @@ func TestVolumesFromHasPriority(t *testing.T) {
246 246
 		"Volumes": map[string]struct{}{volPath: {}},
247 247
 	}
248 248
 
249
-	if _, _, err := sockRequest("POST", "/containers/create?name="+name, config); err != nil && !strings.Contains(err.Error(), "201 Created") {
249
+	if status, _, err := sockRequest("POST", "/containers/create?name="+name, config); err != nil && status != http.StatusCreated {
250 250
 		t.Fatal(err)
251 251
 	}
252 252
 
... ...
@@ -255,7 +256,7 @@ func TestVolumesFromHasPriority(t *testing.T) {
255 255
 		"VolumesFrom": []string{volName},
256 256
 		"Binds":       []string{bindPath + ":/tmp"},
257 257
 	}
258
-	if _, _, err := sockRequest("POST", "/containers/"+name+"/start", config); err != nil && !strings.Contains(err.Error(), "204 No Content") {
258
+	if status, _, err := sockRequest("POST", "/containers/"+name+"/start", config); err != nil && status != http.StatusNoContent {
259 259
 		t.Fatal(err)
260 260
 	}
261 261
 
... ...
@@ -538,8 +539,7 @@ func TestPostContainerBindNormalVolume(t *testing.T) {
538 538
 	}
539 539
 
540 540
 	bindSpec := map[string][]string{"Binds": {fooDir + ":/foo"}}
541
-	_, _, err = sockRequest("POST", "/containers/two/start", bindSpec)
542
-	if err != nil && !strings.Contains(err.Error(), "204 No Content") {
541
+	if status, _, err := sockRequest("POST", "/containers/two/start", bindSpec); err != nil && status != http.StatusNoContent {
543 542
 		t.Fatal(err)
544 543
 	}
545 544
 
... ...
@@ -566,7 +566,7 @@ func TestContainerApiPause(t *testing.T) {
566 566
 	}
567 567
 	ContainerID := strings.TrimSpace(out)
568 568
 
569
-	if _, _, err = sockRequest("POST", "/containers/"+ContainerID+"/pause", nil); err != nil && !strings.Contains(err.Error(), "204 No Content") {
569
+	if status, _, err := sockRequest("POST", "/containers/"+ContainerID+"/pause", nil); err != nil && status != http.StatusNoContent {
570 570
 		t.Fatalf("POST a container pause: sockRequest failed: %v", err)
571 571
 	}
572 572
 
... ...
@@ -580,7 +580,7 @@ func TestContainerApiPause(t *testing.T) {
580 580
 		t.Fatalf("there should be one paused container and not %d", len(pausedContainers))
581 581
 	}
582 582
 
583
-	if _, _, err = sockRequest("POST", "/containers/"+ContainerID+"/unpause", nil); err != nil && !strings.Contains(err.Error(), "204 No Content") {
583
+	if status, _, err := sockRequest("POST", "/containers/"+ContainerID+"/unpause", nil); err != nil && status != http.StatusNoContent {
584 584
 		t.Fatalf("POST a container pause: sockRequest failed: %v", err)
585 585
 	}
586 586
 
... ...
@@ -1,6 +1,7 @@
1 1
 package main
2 2
 
3 3
 import (
4
+	"net/http"
4 5
 	"os"
5 6
 	"os/exec"
6 7
 	"strings"
... ...
@@ -64,12 +65,11 @@ func TestRmRunningContainerCheckError409(t *testing.T) {
64 64
 	createRunningContainer(t, "foo")
65 65
 
66 66
 	endpoint := "/containers/foo"
67
-	_, _, err := sockRequest("DELETE", endpoint, nil)
67
+	status, _, err := sockRequest("DELETE", endpoint, nil)
68 68
 
69 69
 	if err == nil {
70 70
 		t.Fatalf("Expected error, can't rm a running container")
71
-	}
72
-	if !strings.Contains(err.Error(), "409 Conflict") {
71
+	} else if status != http.StatusConflict {
73 72
 		t.Fatalf("Expected error to contain '409 Conflict' but found %s", err)
74 73
 	}
75 74