From 3f2604157790408acf5ad05c74cebe105f2b6979 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Fri, 2 Jun 2017 16:06:14 -0400 Subject: [PATCH] Fix ONBUILD COPY the source was missing from the second dispatch Signed-off-by: Daniel Nephin --- builder/dockerfile/builder.go | 5 ++-- builder/dockerfile/dispatchers.go | 2 +- .../cli/build/fakecontext/context.go | 12 +++++++++ integration-cli/docker_api_build_test.go | 25 +++++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/builder/dockerfile/builder.go b/builder/dockerfile/builder.go index f507766220..52b540ab5d 100644 --- a/builder/dockerfile/builder.go +++ b/builder/dockerfile/builder.go @@ -274,7 +274,7 @@ func BuildFromConfig(config *container.Config, changes []string) (*container.Con } dispatchState := newDispatchState() dispatchState.runConfig = config - return dispatchFromDockerfile(b, dockerfile, dispatchState) + return dispatchFromDockerfile(b, dockerfile, dispatchState, nil) } func checkDispatchDockerfile(dockerfile *parser.Node) error { @@ -286,7 +286,7 @@ func checkDispatchDockerfile(dockerfile *parser.Node) error { return nil } -func dispatchFromDockerfile(b *Builder, result *parser.Result, dispatchState *dispatchState) (*container.Config, error) { +func dispatchFromDockerfile(b *Builder, result *parser.Result, dispatchState *dispatchState, source builder.Source) (*container.Config, error) { shlex := NewShellLex(result.EscapeToken) ast := result.AST total := len(ast.Children) @@ -297,6 +297,7 @@ func dispatchFromDockerfile(b *Builder, result *parser.Result, dispatchState *di stepMsg: formatStep(i, total), node: n, shlex: shlex, + source: source, } if _, err := b.dispatch(opts); err != nil { return nil, err diff --git a/builder/dockerfile/dispatchers.go b/builder/dockerfile/dispatchers.go index dd5714ecb0..ca67eb7f30 100644 --- a/builder/dockerfile/dispatchers.go +++ b/builder/dockerfile/dispatchers.go @@ -325,7 +325,7 @@ func processOnBuild(req dispatchRequest) error { } } - if _, err := dispatchFromDockerfile(req.builder, dockerfile, dispatchState); err != nil { + if _, err := dispatchFromDockerfile(req.builder, dockerfile, dispatchState, req.source); err != nil { return err } } diff --git a/integration-cli/cli/build/fakecontext/context.go b/integration-cli/cli/build/fakecontext/context.go index 94f05c3623..8ecf4e3c63 100644 --- a/integration-cli/cli/build/fakecontext/context.go +++ b/integration-cli/cli/build/fakecontext/context.go @@ -2,9 +2,12 @@ package fakecontext import ( "bytes" + "io" "io/ioutil" "os" "path/filepath" + + "github.com/docker/docker/pkg/archive" ) type testingT interface { @@ -110,3 +113,12 @@ func (f *Fake) Delete(file string) error { func (f *Fake) Close() error { return os.RemoveAll(f.Dir) } + +// AsTarReader returns a ReadCloser with the contents of Dir as a tar archive. +func (f *Fake) AsTarReader(t testingT) io.ReadCloser { + reader, err := archive.TarWithOptions(f.Dir, &archive.TarOptions{}) + if err != nil { + t.Fatalf("Failed to create tar from %s: %s", f.Dir, err) + } + return reader +} diff --git a/integration-cli/docker_api_build_test.go b/integration-cli/docker_api_build_test.go index feb20ee724..12d3374dd5 100644 --- a/integration-cli/docker_api_build_test.go +++ b/integration-cli/docker_api_build_test.go @@ -249,3 +249,28 @@ func (s *DockerSuite) TestBuildAPIUnnormalizedTarPaths(c *check.C) { c.Assert(imageA, checker.Not(checker.Equals), imageB) } + +func (s *DockerSuite) TestBuildOnBuildWithCopy(c *check.C) { + dockerfile := ` + FROM ` + minimalBaseImage() + ` as onbuildbase + ONBUILD COPY file /file + + FROM onbuildbase + ` + ctx := fakecontext.New(c, "", + fakecontext.WithDockerfile(dockerfile), + fakecontext.WithFile("file", "some content"), + ) + defer ctx.Close() + + res, body, err := request.Post( + "/build", + request.RawContent(ctx.AsTarReader(c)), + request.ContentType("application/x-tar")) + c.Assert(err, checker.IsNil) + c.Assert(res.StatusCode, checker.Equals, http.StatusOK) + + out, err := testutil.ReadBody(body) + c.Assert(err, checker.IsNil) + c.Assert(string(out), checker.Contains, "Successfully built") +}