From fe4ed9d78f496df5cac1eb914877f213f3f1c12c Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Fri, 1 Dec 2017 10:31:03 -0800 Subject: [PATCH] Add testcase for onbuild command in multi stage build Signed-off-by: Tonis Tiigi --- integration/build/build_test.go | 70 +++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/integration/build/build_test.go b/integration/build/build_test.go index b447b623cc..1271daea9b 100644 --- a/integration/build/build_test.go +++ b/integration/build/build_test.go @@ -197,3 +197,73 @@ func TestBuildWithEmptyLayers(t *testing.T) { resp.Body.Close() require.NoError(t, err) } + +// TestBuildMultiStageOnBuild checks that ONBUILD commands are applied to +// multiple subsequent stages +// #35652 +func TestBuildMultiStageOnBuild(t *testing.T) { + defer setupTest(t)() + // test both metadata and layer based commands as they may be implemented differently + dockerfile := `FROM busybox AS stage1 +ONBUILD RUN echo 'foo' >somefile +ONBUILD ENV bar=baz + +FROM stage1 +RUN cat somefile # fails if ONBUILD RUN fails + +FROM stage1 +RUN cat somefile` + + ctx := context.Background() + source := fakecontext.New(t, "", + fakecontext.WithDockerfile(dockerfile)) + defer source.Close() + + apiclient := testEnv.APIClient() + resp, err := apiclient.ImageBuild(ctx, + source.AsTarReader(t), + types.ImageBuildOptions{ + Remove: true, + ForceRemove: true, + }) + + out := bytes.NewBuffer(nil) + require.NoError(t, err) + _, err = io.Copy(out, resp.Body) + resp.Body.Close() + require.NoError(t, err) + + assert.Contains(t, out.String(), "Successfully built") + + imageIDs, err := getImageIDsFromBuild(out.Bytes()) + require.NoError(t, err) + assert.Equal(t, 3, len(imageIDs)) + + image, _, err := apiclient.ImageInspectWithRaw(context.Background(), imageIDs[2]) + require.NoError(t, err) + assert.Contains(t, image.Config.Env, "bar=baz") +} + +type buildLine struct { + Stream string + Aux struct { + ID string + } +} + +func getImageIDsFromBuild(output []byte) ([]string, error) { + ids := []string{} + for _, line := range bytes.Split(output, []byte("\n")) { + if len(line) == 0 { + continue + } + entry := buildLine{} + if err := json.Unmarshal(line, &entry); err != nil { + return nil, err + } + if entry.Aux.ID != "" { + ids = append(ids, entry.Aux.ID) + } + } + return ids, nil +}