Browse code

Fix already loaded detection for frozen images

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>

Tonis Tiigi authored on 2016/09/13 03:59:10
Showing 2 changed files
... ...
@@ -21,16 +21,26 @@ var frozenImgDir = "/docker-frozen-images"
21 21
 // the passed in images
22 22
 func FrozenImagesLinux(dockerBinary string, images ...string) error {
23 23
 	imgNS := os.Getenv("TEST_IMAGE_NAMESPACE")
24
-	var loadImages []string
24
+	var loadImages []struct{ srcName, destName string }
25 25
 	for _, img := range images {
26
-		if imgNS != "" {
27
-			img = imgNS + "/" + img
28
-		}
29 26
 		if err := exec.Command(dockerBinary, "inspect", "--type=image", img).Run(); err != nil {
30
-			loadImages = append(loadImages, img)
27
+			srcName := img
28
+			// hello-world:latest gets re-tagged as hello-world:frozen
29
+			// there are some tests that use hello-world:latest specifically so it pulls
30
+			// the image and hello-world:frozen is used for when we just want a super
31
+			// small image
32
+			if img == "hello-world:frozen" {
33
+				srcName = "hello-world:latest"
34
+			}
35
+			if imgNS != "" {
36
+				srcName = imgNS + "/" + srcName
37
+			}
38
+			loadImages = append(loadImages, struct{ srcName, destName string }{
39
+				srcName:  srcName,
40
+				destName: img,
41
+			})
31 42
 		}
32 43
 	}
33
-
34 44
 	if len(loadImages) == 0 {
35 45
 		// everything is loaded, we're done
36 46
 		return nil
... ...
@@ -38,32 +48,33 @@ func FrozenImagesLinux(dockerBinary string, images ...string) error {
38 38
 
39 39
 	fi, err := os.Stat(frozenImgDir)
40 40
 	if err != nil || !fi.IsDir() {
41
-		if err := pullImages(dockerBinary, loadImages); err != nil {
41
+		srcImages := make([]string, 0, len(loadImages))
42
+		for _, img := range loadImages {
43
+			srcImages = append(srcImages, img.srcName)
44
+		}
45
+		if err := pullImages(dockerBinary, srcImages); err != nil {
42 46
 			return errors.Wrap(err, "error pulling image list")
43 47
 		}
44 48
 	} else {
45
-		if err := loadFrozenImags(dockerBinary); err != nil {
49
+		if err := loadFrozenImages(dockerBinary); err != nil {
46 50
 			return err
47 51
 		}
48 52
 	}
49 53
 
50
-	if imgNS != "" {
51
-		for _, img := range loadImages {
52
-			target := strings.TrimPrefix(img, imgNS+"/")
53
-			if target != img {
54
-				if out, err := exec.Command(dockerBinary, "tag", img, target).CombinedOutput(); err != nil {
55
-					return errors.Errorf("%v: %s", err, string(out))
56
-				}
57
-				if out, err := exec.Command(dockerBinary, "rmi", img).CombinedOutput(); err != nil {
58
-					return errors.Errorf("%v: %s", err, string(out))
59
-				}
54
+	for _, img := range loadImages {
55
+		if img.srcName != img.destName {
56
+			if out, err := exec.Command(dockerBinary, "tag", img.srcName, img.destName).CombinedOutput(); err != nil {
57
+				return errors.Errorf("%v: %s", err, string(out))
58
+			}
59
+			if out, err := exec.Command(dockerBinary, "rmi", img.srcName).CombinedOutput(); err != nil {
60
+				return errors.Errorf("%v: %s", err, string(out))
60 61
 			}
61 62
 		}
62 63
 	}
63 64
 	return nil
64 65
 }
65 66
 
66
-func loadFrozenImags(dockerBinary string) error {
67
+func loadFrozenImages(dockerBinary string) error {
67 68
 	tar, err := exec.LookPath("tar")
68 69
 	if err != nil {
69 70
 		return errors.Wrap(err, "could not find tar binary")
... ...
@@ -16,30 +16,13 @@ import (
16 16
 )
17 17
 
18 18
 func ensureFrozenImagesLinux(t *testing.T) {
19
-	images := []string{"busybox:latest", "hello-world:latest", "debian:jessie"}
19
+	images := []string{"busybox:latest", "hello-world:frozen", "debian:jessie"}
20 20
 	err := load.FrozenImagesLinux(dockerBinary, images...)
21 21
 	if err != nil {
22 22
 		t.Log(dockerCmdWithError("images"))
23 23
 		t.Fatalf("%+v", err)
24 24
 	}
25
-
26
-	// hello-world:latest gets re-tagged as hello-world:frozen
27
-	// there are some tests that use hello-world:latest specifically so it pulls
28
-	// the image and hello-world:frozen is used for when we just want a super
29
-	// small image
30
-	if out, err := exec.Command(dockerBinary, "tag", "hello-world:latest", "hello-world:frozen").CombinedOutput(); err != nil {
31
-		t.Log(dockerCmdWithError("images"))
32
-		t.Fatal(string(out))
33
-	}
34
-	if out, err := exec.Command(dockerBinary, "rmi", "hello-world:latest").CombinedOutput(); err != nil {
35
-		t.Log(dockerCmdWithError("images"))
36
-		t.Fatal(string(out))
37
-	}
38
-
39 25
 	for _, img := range images {
40
-		if img == "hello-world:latest" {
41
-			img = "hello-world:frozen"
42
-		}
43 26
 		protectedImages[img] = struct{}{}
44 27
 	}
45 28
 }