Ver código fonte

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

Guillaume J. Charmes 12 anos atrás
pai
commit
b6f288a1ce
3 arquivos alterados com 49 adições e 21 exclusões
  1. 38 14
      api_test.go
  2. 0 1
      runtime_test.go
  3. 11 6
      server_test.go

+ 38 - 14
api_test.go

@@ -137,6 +137,11 @@ func TestGetImagesJSON(t *testing.T) {
 
 	srv := &Server{runtime: runtime}
 
+	initialImages, err := srv.Images(true, "")
+	if err != nil {
+		t.Fatal(err)
+	}
+
 	// all=0
 	req, err := http.NewRequest("GET", "/images/json?all=0", nil)
 	if err != nil {
@@ -154,12 +159,19 @@ func TestGetImagesJSON(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	if len(images) != 1 {
-		t.Errorf("Excepted 1 image, %d found", len(images))
+	if len(images) != len(initialImages) {
+		t.Errorf("Excepted %d image, %d found", len(initialImages), len(images))
 	}
 
-	if images[0].Repository != unitTestImageName {
-		t.Errorf("Excepted image %s, %s found", unitTestImageName, images[0].Repository)
+	found := false
+	for _, img := range images {
+		if img.Repository == unitTestImageName {
+			found = true
+			break
+		}
+	}
+	if !found {
+		t.Errorf("Excepted image %s, %+v found", unitTestImageName, images)
 	}
 
 	r2 := httptest.NewRecorder()
@@ -179,18 +191,25 @@ func TestGetImagesJSON(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	if len(images2) != 1 {
-		t.Errorf("Excepted 1 image, %d found", len(images2))
+	if len(images2) != len(initialImages) {
+		t.Errorf("Excepted %d image, %d found", len(initialImages), len(images2))
 	}
 
-	if images2[0].ID != GetTestImage(runtime).ID {
-		t.Errorf("Retrieved image Id differs, expected %s, received %s", GetTestImage(runtime).ID, images2[0].ID)
+	found = false
+	for _, img := range images2 {
+		if img.ID == GetTestImage(runtime).ID {
+			found = true
+			break
+		}
+	}
+	if !found {
+		t.Errorf("Retrieved image Id differs, expected %s, received %+v", GetTestImage(runtime).ID, images2)
 	}
 
 	r3 := httptest.NewRecorder()
 
 	// filter=a
-	req3, err := http.NewRequest("GET", "/images/json?filter=a", nil)
+	req3, err := http.NewRequest("GET", "/images/json?filter=aaaaaaaaaa", nil)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -205,7 +224,7 @@ func TestGetImagesJSON(t *testing.T) {
 	}
 
 	if len(images3) != 0 {
-		t.Errorf("Excepted 1 image, %d found", len(images3))
+		t.Errorf("Excepted 0 image, %d found", len(images3))
 	}
 
 	r4 := httptest.NewRecorder()
@@ -1310,6 +1329,11 @@ func TestDeleteImages(t *testing.T) {
 
 	srv := &Server{runtime: runtime}
 
+	initialImages, err := srv.Images(false, "")
+	if err != nil {
+		t.Fatal(err)
+	}
+
 	if err := srv.runtime.repositories.Set("test", "test", unitTestImageName, true); err != nil {
 		t.Fatal(err)
 	}
@@ -1319,8 +1343,8 @@ func TestDeleteImages(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	if len(images) != 2 {
-		t.Errorf("Excepted 2 images, %d found", len(images))
+	if len(images) != len(initialImages)+1 {
+		t.Errorf("Excepted %d images, %d found", len(initialImages)+1, len(images))
 	}
 
 	req, err := http.NewRequest("DELETE", "/images/test:test", nil)
@@ -1348,8 +1372,8 @@ func TestDeleteImages(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	if len(images) != 1 {
-		t.Errorf("Excepted 1 image, %d found", len(images))
+	if len(images) != len(initialImages) {
+		t.Errorf("Excepted %d image, %d found", len(initialImages), len(images))
 	}
 
 	/*	if c := runtime.Get(container.Id); c != nil {

+ 0 - 1
runtime_test.go

@@ -97,7 +97,6 @@ func init() {
 	if err := srv.ImagePull(unitTestImageName, "", "", os.Stdout, utils.NewStreamFormatter(false), nil); err != nil {
 		panic(err)
 	}
-
 	// Spawn a Daemon
 	go func() {
 		if err := ListenAndServe(testDaemonProto, testDaemonAddr, srv, os.Getenv("DEBUG") != ""); err != nil {

+ 11 - 6
server_test.go

@@ -13,6 +13,11 @@ func TestContainerTagImageDelete(t *testing.T) {
 
 	srv := &Server{runtime: runtime}
 
+	initialImages, err := srv.Images(false, "")
+	if err != nil {
+		t.Fatal(err)
+	}
+
 	if err := srv.runtime.repositories.Set("utest", "tag1", unitTestImageName, false); err != nil {
 		t.Fatal(err)
 	}
@@ -25,8 +30,8 @@ func TestContainerTagImageDelete(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	if len(images) != 3 {
-		t.Errorf("Excepted 3 images, %d found", len(images))
+	if len(images) != len(initialImages)+2 {
+		t.Errorf("Excepted %d images, %d found", len(initialImages)+2, len(images))
 	}
 
 	if _, err := srv.ImageDelete("utest/docker:tag2", true); err != nil {
@@ -38,8 +43,8 @@ func TestContainerTagImageDelete(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	if len(images) != 2 {
-		t.Errorf("Excepted 2 images, %d found", len(images))
+	if len(images) != len(initialImages)+1 {
+		t.Errorf("Excepted %d images, %d found", len(initialImages)+1, len(images))
 	}
 
 	if _, err := srv.ImageDelete("utest:tag1", true); err != nil {
@@ -51,8 +56,8 @@ func TestContainerTagImageDelete(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	if len(images) != 1 {
-		t.Errorf("Excepted 1 image, %d found", len(images))
+	if len(images) != len(initialImages) {
+		t.Errorf("Excepted %d image, %d found", len(initialImages), len(images))
 	}
 }