1
0
mirror of https://github.com/moby/moby.git synced 2025-07-30 18:23:29 +03:00

Fix Cache with ONBUILD

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin
2017-06-05 11:12:26 -04:00
parent c8141a1fb1
commit f1ade82d82
4 changed files with 85 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"archive/tar"
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"regexp"
@ -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) {
@ -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
}