Skip to content

Commit

Permalink
Merge pull request moby#33524 from dnephin/fix-onbuild-copy-cache
Browse files Browse the repository at this point in the history
[Builder] Fix Cache with ONBUILD
  • Loading branch information
mlaventure authored Jun 6, 2017
2 parents 84149ac + f1ade82 commit f502499
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
6 changes: 2 additions & 4 deletions builder/dockerfile/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,9 @@ func (b *Builder) dispatch(options dispatchOptions) (*dispatchState, error) {
buildsFailed.WithValues(metricsUnknownInstructionError).Inc()
return nil, fmt.Errorf("unknown instruction: %s", upperCasedCmd)
}
if err := f(newDispatchRequestFromOptions(options, b, args)); err != nil {
return nil, err
}
options.state.updateRunConfig()
return options.state, nil
err = f(newDispatchRequestFromOptions(options, b, args))
return options.state, err
}

type dispatchOptions struct {
Expand Down
2 changes: 1 addition & 1 deletion integration-cli/docker_api_attach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (s *DockerSuite) TestGetContainersAttachWebsocket(c *check.C) {

// regression gh14320
func (s *DockerSuite) TestPostContainersAttachContainerNotFound(c *check.C) {
client, err := request.NewClient(daemonHost())
client, err := request.NewHTTPClient(daemonHost())
c.Assert(err, checker.IsNil)
req, err := request.New(daemonHost(), "/containers/doesnotexist/attach", request.Method(http.MethodPost))
resp, err := client.Do(req)
Expand Down
69 changes: 69 additions & 0 deletions integration-cli/docker_api_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"archive/tar"
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"regexp"
Expand All @@ -15,6 +16,9 @@ import (
"github.com/docker/docker/integration-cli/request"
"github.com/docker/docker/pkg/testutil"
"github.com/go-check/check"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/context"
)

func (s *DockerSuite) TestBuildAPIDockerFileRemote(c *check.C) {
Expand Down Expand Up @@ -274,3 +278,68 @@ func (s *DockerSuite) TestBuildOnBuildWithCopy(c *check.C) {
c.Assert(err, checker.IsNil)
c.Assert(string(out), checker.Contains, "Successfully built")
}

func (s *DockerSuite) TestBuildOnBuildCache(c *check.C) {
build := func(dockerfile string) []byte {
ctx := fakecontext.New(c, "",
fakecontext.WithDockerfile(dockerfile),
)
defer ctx.Close()

res, body, err := request.Post(
"/build",
request.RawContent(ctx.AsTarReader(c)),
request.ContentType("application/x-tar"))
require.NoError(c, err)
assert.Equal(c, http.StatusOK, res.StatusCode)

out, err := testutil.ReadBody(body)
require.NoError(c, err)
assert.Contains(c, string(out), "Successfully built")
return out
}

dockerfile := `
FROM ` + minimalBaseImage() + ` as onbuildbase
ENV something=bar
ONBUILD ENV foo=bar
`
build(dockerfile)

dockerfile += "FROM onbuildbase"
out := build(dockerfile)

imageIDs := getImageIDsFromBuild(c, out)
assert.Len(c, imageIDs, 2)
parentID, childID := imageIDs[0], imageIDs[1]

client, err := request.NewClient()
require.NoError(c, err)

// check parentID is correct
image, _, err := client.ImageInspectWithRaw(context.Background(), childID)
require.NoError(c, err)
assert.Equal(c, parentID, image.Parent)
}

type buildLine struct {
Stream string
Aux struct {
ID string
}
}

func getImageIDsFromBuild(c *check.C, output []byte) []string {
ids := []string{}
for _, line := range bytes.Split(output, []byte("\n")) {
if len(line) == 0 {
continue
}
entry := buildLine{}
require.NoError(c, json.Unmarshal(line, &entry))
if entry.Aux.ID != "" {
ids = append(ids, entry.Aux.ID)
}
}
return ids
}
16 changes: 13 additions & 3 deletions integration-cli/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func DoOnHost(host, endpoint string, modifiers ...func(*http.Request) error) (*h
if err != nil {
return nil, nil, err
}
client, err := NewClient(host)
client, err := NewHTTPClient(host)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -140,8 +140,8 @@ func New(host, endpoint string, modifiers ...func(*http.Request) error) (*http.R
return req, nil
}

// NewClient creates an http client for the specific host
func NewClient(host string) (*http.Client, error) {
// NewHTTPClient creates an http client for the specific host
func NewHTTPClient(host string) (*http.Client, error) {
// FIXME(vdemeester) 10*time.Second timeout of SockRequest… ?
proto, addr, _, err := dclient.ParseHost(host)
if err != nil {
Expand All @@ -163,6 +163,16 @@ func NewClient(host string) (*http.Client, error) {
}, err
}

// NewClient returns a new Docker API client
func NewClient() (dclient.APIClient, error) {
host := DaemonHost()
httpClient, err := NewHTTPClient(host)
if err != nil {
return nil, err
}
return dclient.NewClient(host, "", httpClient, nil)
}

// FIXME(vdemeester) httputil.ClientConn is deprecated, use http.Client instead (closer to actual client)
// Deprecated: Use New instead of NewRequestClient
// Deprecated: use request.Do (or Get, Delete, Post) instead
Expand Down

0 comments on commit f502499

Please sign in to comment.