浏览代码

Merge pull request #9973 from duglin/Issue9880

Make sure that ADD/COPY still populate the cache even if they don't use it
Alexander Morozov 10 年之前
父节点
当前提交
99a15ec8bd
共有 2 个文件被更改,包括 53 次插入21 次删除
  1. 13 21
      builder/internals.go
  2. 40 0
      integration-cli/docker_cli_build_test.go

+ 13 - 21
builder/internals.go

@@ -308,22 +308,20 @@ func calcCopyInfo(b *Builder, cmdName string, cInfos *[]*copyInfo, origPath stri
 			ci.destPath = ci.destPath + filename
 			ci.destPath = ci.destPath + filename
 		}
 		}
 
 
-		// Calc the checksum, only if we're using the cache
-		if b.UtilizeCache {
-			r, err := archive.Tar(tmpFileName, archive.Uncompressed)
-			if err != nil {
-				return err
-			}
-			tarSum, err := tarsum.NewTarSum(r, true, tarsum.Version0)
-			if err != nil {
-				return err
-			}
-			if _, err := io.Copy(ioutil.Discard, tarSum); err != nil {
-				return err
-			}
-			ci.hash = tarSum.Sum(nil)
-			r.Close()
+		// Calc the checksum, even if we're using the cache
+		r, err := archive.Tar(tmpFileName, archive.Uncompressed)
+		if err != nil {
+			return err
 		}
 		}
+		tarSum, err := tarsum.NewTarSum(r, true, tarsum.Version0)
+		if err != nil {
+			return err
+		}
+		if _, err := io.Copy(ioutil.Discard, tarSum); err != nil {
+			return err
+		}
+		ci.hash = tarSum.Sum(nil)
+		r.Close()
 
 
 		return nil
 		return nil
 	}
 	}
@@ -358,12 +356,6 @@ func calcCopyInfo(b *Builder, cmdName string, cInfos *[]*copyInfo, origPath stri
 	ci.decompress = allowDecompression
 	ci.decompress = allowDecompression
 	*cInfos = append(*cInfos, &ci)
 	*cInfos = append(*cInfos, &ci)
 
 
-	// If not using cache don't need to do anything else.
-	// If we are using a cache then calc the hash for the src file/dir
-	if !b.UtilizeCache {
-		return nil
-	}
-
 	// Deal with the single file case
 	// Deal with the single file case
 	if !fi.IsDir() {
 	if !fi.IsDir() {
 		// This will match first file in sums of the archive
 		// This will match first file in sums of the archive

+ 40 - 0
integration-cli/docker_cli_build_test.go

@@ -2302,6 +2302,46 @@ func TestBuildWithoutCache(t *testing.T) {
 	logDone("build - without cache")
 	logDone("build - without cache")
 }
 }
 
 
+func TestBuildConditionalCache(t *testing.T) {
+	name := "testbuildconditionalcache"
+	name2 := "testbuildconditionalcache2"
+	defer deleteImages(name, name2)
+
+	dockerfile := `
+		FROM busybox
+        ADD foo /tmp/`
+	ctx, err := fakeContext(dockerfile, map[string]string{
+		"foo": "hello",
+	})
+
+	id1, err := buildImageFromContext(name, ctx, true)
+	if err != nil {
+		t.Fatalf("Error building #1: %s", err)
+	}
+
+	if err := ctx.Add("foo", "bye"); err != nil {
+		t.Fatalf("Error modifying foo: %s", err)
+	}
+
+	id2, err := buildImageFromContext(name, ctx, false)
+	if err != nil {
+		t.Fatalf("Error building #2: %s", err)
+	}
+	if id2 == id1 {
+		t.Fatal("Should not have used the cache")
+	}
+
+	id3, err := buildImageFromContext(name, ctx, true)
+	if err != nil {
+		t.Fatalf("Error building #3: %s", err)
+	}
+	if id3 != id2 {
+		t.Fatal("Should have used the cache")
+	}
+
+	logDone("build - conditional cache")
+}
+
 func TestBuildADDLocalFileWithCache(t *testing.T) {
 func TestBuildADDLocalFileWithCache(t *testing.T) {
 	name := "testbuildaddlocalfilewithcache"
 	name := "testbuildaddlocalfilewithcache"
 	name2 := "testbuildaddlocalfilewithcache2"
 	name2 := "testbuildaddlocalfilewithcache2"