Browse code

Merge pull request #6451 from cyphar/6445-fix-onbuild-passing-to-grandchildren

Ensure that ONBUILD triggers aren't committed to grandchildren

unclejack authored on 2014/06/20 00:11:33
Showing 2 changed files
... ...
@@ -588,6 +588,7 @@ func TestBuildRm(t *testing.T) {
588 588
 	logDone("build - ensure --rm doesn't leave containers behind and that --rm=true is the default")
589 589
 	logDone("build - ensure --rm=false overrides the default")
590 590
 }
591
+
591 592
 func TestBuildWithVolumes(t *testing.T) {
592 593
 	name := "testbuildvolumes"
593 594
 	expected := "map[/test1:map[] /test2:map[]]"
... ...
@@ -766,6 +767,68 @@ func TestBuildEntrypoint(t *testing.T) {
766 766
 	logDone("build - entrypoint")
767 767
 }
768 768
 
769
+// #6445 ensure ONBUILD triggers aren't committed to grandchildren
770
+func TestBuildOnBuildLimitedInheritence(t *testing.T) {
771
+	name1 := "testonbuildtrigger1"
772
+	dockerfile1 := `
773
+		FROM busybox
774
+		RUN echo "GRANDPARENT"
775
+		ONBUILD RUN echo "ONBUILD PARENT"
776
+	`
777
+	ctx1, err := fakeContext(dockerfile1, nil)
778
+	if err != nil {
779
+		t.Fatal(err)
780
+	}
781
+
782
+	buildCmd := exec.Command(dockerBinary, "build", "-t", name1, ".")
783
+	buildCmd.Dir = ctx1.Dir
784
+	out1, _, err := runCommandWithOutput(buildCmd)
785
+	errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out1, err))
786
+	defer deleteImages(name1)
787
+
788
+	name2 := "testonbuildtrigger2"
789
+	dockerfile2 := `
790
+		FROM testonbuildtrigger1
791
+	`
792
+	ctx2, err := fakeContext(dockerfile2, nil)
793
+	if err != nil {
794
+		t.Fatal(err)
795
+	}
796
+
797
+	buildCmd = exec.Command(dockerBinary, "build", "-t", name2, ".")
798
+	buildCmd.Dir = ctx2.Dir
799
+	out2, _, err := runCommandWithOutput(buildCmd)
800
+	errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out2, err))
801
+	defer deleteImages(name2)
802
+
803
+	name3 := "testonbuildtrigger3"
804
+	dockerfile3 := `
805
+		FROM testonbuildtrigger2
806
+	`
807
+	ctx3, err := fakeContext(dockerfile3, nil)
808
+	if err != nil {
809
+		t.Fatal(err)
810
+	}
811
+
812
+	buildCmd = exec.Command(dockerBinary, "build", "-t", name3, ".")
813
+	buildCmd.Dir = ctx3.Dir
814
+	out3, _, err := runCommandWithOutput(buildCmd)
815
+	errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out3, err))
816
+	defer deleteImages(name3)
817
+
818
+	// ONBUILD should be run in second build.
819
+	if !strings.Contains(out2, "ONBUILD PARENT") {
820
+		t.Fatalf("ONBUILD instruction did not run in child of ONBUILD parent")
821
+	}
822
+
823
+	// ONBUILD should *not* be run in third build.
824
+	if strings.Contains(out3, "ONBUILD PARENT") {
825
+		t.Fatalf("ONBUILD instruction ran in grandchild of ONBUILD parent")
826
+	}
827
+
828
+	logDone("build - onbuild")
829
+}
830
+
769 831
 func TestBuildWithCache(t *testing.T) {
770 832
 	name := "testbuildwithcache"
771 833
 	defer deleteImages(name)
... ...
@@ -123,7 +123,12 @@ func (b *buildFile) CmdFrom(name string) error {
123 123
 	if nTriggers := len(b.config.OnBuild); nTriggers != 0 {
124 124
 		fmt.Fprintf(b.errStream, "# Executing %d build triggers\n", nTriggers)
125 125
 	}
126
-	for n, step := range b.config.OnBuild {
126
+
127
+	// Copy the ONBUILD triggers, and remove them from the config, since the config will be commited.
128
+	onBuildTriggers := b.config.OnBuild
129
+	b.config.OnBuild = []string{}
130
+
131
+	for n, step := range onBuildTriggers {
127 132
 		splitStep := strings.Split(step, " ")
128 133
 		stepInstruction := strings.ToUpper(strings.Trim(splitStep[0], " "))
129 134
 		switch stepInstruction {
... ...
@@ -136,7 +141,6 @@ func (b *buildFile) CmdFrom(name string) error {
136 136
 			return err
137 137
 		}
138 138
 	}
139
-	b.config.OnBuild = []string{}
140 139
 	return nil
141 140
 }
142 141