Browse code

Fix unit tests when there is more than one tag within the test image

Guillaume J. Charmes authored on 2013/07/02 03:45:45
Showing 3 changed files
... ...
@@ -137,6 +137,11 @@ func TestGetImagesJSON(t *testing.T) {
137 137
 
138 138
 	srv := &Server{runtime: runtime}
139 139
 
140
+	initialImages, err := srv.Images(true, "")
141
+	if err != nil {
142
+		t.Fatal(err)
143
+	}
144
+
140 145
 	// all=0
141 146
 	req, err := http.NewRequest("GET", "/images/json?all=0", nil)
142 147
 	if err != nil {
... ...
@@ -154,12 +159,19 @@ func TestGetImagesJSON(t *testing.T) {
154 154
 		t.Fatal(err)
155 155
 	}
156 156
 
157
-	if len(images) != 1 {
158
-		t.Errorf("Excepted 1 image, %d found", len(images))
157
+	if len(images) != len(initialImages) {
158
+		t.Errorf("Excepted %d image, %d found", len(initialImages), len(images))
159 159
 	}
160 160
 
161
-	if images[0].Repository != unitTestImageName {
162
-		t.Errorf("Excepted image %s, %s found", unitTestImageName, images[0].Repository)
161
+	found := false
162
+	for _, img := range images {
163
+		if img.Repository == unitTestImageName {
164
+			found = true
165
+			break
166
+		}
167
+	}
168
+	if !found {
169
+		t.Errorf("Excepted image %s, %+v found", unitTestImageName, images)
163 170
 	}
164 171
 
165 172
 	r2 := httptest.NewRecorder()
... ...
@@ -179,18 +191,25 @@ func TestGetImagesJSON(t *testing.T) {
179 179
 		t.Fatal(err)
180 180
 	}
181 181
 
182
-	if len(images2) != 1 {
183
-		t.Errorf("Excepted 1 image, %d found", len(images2))
182
+	if len(images2) != len(initialImages) {
183
+		t.Errorf("Excepted %d image, %d found", len(initialImages), len(images2))
184 184
 	}
185 185
 
186
-	if images2[0].ID != GetTestImage(runtime).ID {
187
-		t.Errorf("Retrieved image Id differs, expected %s, received %s", GetTestImage(runtime).ID, images2[0].ID)
186
+	found = false
187
+	for _, img := range images2 {
188
+		if img.ID == GetTestImage(runtime).ID {
189
+			found = true
190
+			break
191
+		}
192
+	}
193
+	if !found {
194
+		t.Errorf("Retrieved image Id differs, expected %s, received %+v", GetTestImage(runtime).ID, images2)
188 195
 	}
189 196
 
190 197
 	r3 := httptest.NewRecorder()
191 198
 
192 199
 	// filter=a
193
-	req3, err := http.NewRequest("GET", "/images/json?filter=a", nil)
200
+	req3, err := http.NewRequest("GET", "/images/json?filter=aaaaaaaaaa", nil)
194 201
 	if err != nil {
195 202
 		t.Fatal(err)
196 203
 	}
... ...
@@ -205,7 +224,7 @@ func TestGetImagesJSON(t *testing.T) {
205 205
 	}
206 206
 
207 207
 	if len(images3) != 0 {
208
-		t.Errorf("Excepted 1 image, %d found", len(images3))
208
+		t.Errorf("Excepted 0 image, %d found", len(images3))
209 209
 	}
210 210
 
211 211
 	r4 := httptest.NewRecorder()
... ...
@@ -1310,6 +1329,11 @@ func TestDeleteImages(t *testing.T) {
1310 1310
 
1311 1311
 	srv := &Server{runtime: runtime}
1312 1312
 
1313
+	initialImages, err := srv.Images(false, "")
1314
+	if err != nil {
1315
+		t.Fatal(err)
1316
+	}
1317
+
1313 1318
 	if err := srv.runtime.repositories.Set("test", "test", unitTestImageName, true); err != nil {
1314 1319
 		t.Fatal(err)
1315 1320
 	}
... ...
@@ -1319,8 +1343,8 @@ func TestDeleteImages(t *testing.T) {
1319 1319
 		t.Fatal(err)
1320 1320
 	}
1321 1321
 
1322
-	if len(images) != 2 {
1323
-		t.Errorf("Excepted 2 images, %d found", len(images))
1322
+	if len(images) != len(initialImages)+1 {
1323
+		t.Errorf("Excepted %d images, %d found", len(initialImages)+1, len(images))
1324 1324
 	}
1325 1325
 
1326 1326
 	req, err := http.NewRequest("DELETE", "/images/test:test", nil)
... ...
@@ -1348,8 +1372,8 @@ func TestDeleteImages(t *testing.T) {
1348 1348
 		t.Fatal(err)
1349 1349
 	}
1350 1350
 
1351
-	if len(images) != 1 {
1352
-		t.Errorf("Excepted 1 image, %d found", len(images))
1351
+	if len(images) != len(initialImages) {
1352
+		t.Errorf("Excepted %d image, %d found", len(initialImages), len(images))
1353 1353
 	}
1354 1354
 
1355 1355
 	/*	if c := runtime.Get(container.Id); c != nil {
... ...
@@ -97,7 +97,6 @@ func init() {
97 97
 	if err := srv.ImagePull(unitTestImageName, "", "", os.Stdout, utils.NewStreamFormatter(false), nil); err != nil {
98 98
 		panic(err)
99 99
 	}
100
-
101 100
 	// Spawn a Daemon
102 101
 	go func() {
103 102
 		if err := ListenAndServe(testDaemonProto, testDaemonAddr, srv, os.Getenv("DEBUG") != ""); err != nil {
... ...
@@ -13,6 +13,11 @@ func TestContainerTagImageDelete(t *testing.T) {
13 13
 
14 14
 	srv := &Server{runtime: runtime}
15 15
 
16
+	initialImages, err := srv.Images(false, "")
17
+	if err != nil {
18
+		t.Fatal(err)
19
+	}
20
+
16 21
 	if err := srv.runtime.repositories.Set("utest", "tag1", unitTestImageName, false); err != nil {
17 22
 		t.Fatal(err)
18 23
 	}
... ...
@@ -25,8 +30,8 @@ func TestContainerTagImageDelete(t *testing.T) {
25 25
 		t.Fatal(err)
26 26
 	}
27 27
 
28
-	if len(images) != 3 {
29
-		t.Errorf("Excepted 3 images, %d found", len(images))
28
+	if len(images) != len(initialImages)+2 {
29
+		t.Errorf("Excepted %d images, %d found", len(initialImages)+2, len(images))
30 30
 	}
31 31
 
32 32
 	if _, err := srv.ImageDelete("utest/docker:tag2", true); err != nil {
... ...
@@ -38,8 +43,8 @@ func TestContainerTagImageDelete(t *testing.T) {
38 38
 		t.Fatal(err)
39 39
 	}
40 40
 
41
-	if len(images) != 2 {
42
-		t.Errorf("Excepted 2 images, %d found", len(images))
41
+	if len(images) != len(initialImages)+1 {
42
+		t.Errorf("Excepted %d images, %d found", len(initialImages)+1, len(images))
43 43
 	}
44 44
 
45 45
 	if _, err := srv.ImageDelete("utest:tag1", true); err != nil {
... ...
@@ -51,8 +56,8 @@ func TestContainerTagImageDelete(t *testing.T) {
51 51
 		t.Fatal(err)
52 52
 	}
53 53
 
54
-	if len(images) != 1 {
55
-		t.Errorf("Excepted 1 image, %d found", len(images))
54
+	if len(images) != len(initialImages) {
55
+		t.Errorf("Excepted %d image, %d found", len(initialImages), len(images))
56 56
 	}
57 57
 }
58 58