2018-03-30 07:10:02 -04:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2018-12-24 10:55:22 -05:00
|
|
|
dclient "github.com/docker/docker/client"
|
2018-03-30 07:10:02 -04:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2019-08-29 16:52:40 -04:00
|
|
|
"github.com/docker/docker/testutil/daemon"
|
|
|
|
"github.com/docker/docker/testutil/fakecontext"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/skip"
|
2018-03-30 07:10:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuildSquashParent(t *testing.T) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
2018-03-30 07:10:02 -04:00
|
|
|
|
2018-12-24 10:55:22 -05:00
|
|
|
var client dclient.APIClient
|
|
|
|
if !testEnv.DaemonInfo.ExperimentalBuild {
|
|
|
|
skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon")
|
|
|
|
|
2019-09-30 08:23:56 -04:00
|
|
|
d := daemon.New(t, daemon.WithExperimental())
|
2018-12-24 10:55:22 -05:00
|
|
|
d.StartWithBusybox(t)
|
|
|
|
defer d.Stop(t)
|
|
|
|
client = d.NewClientT(t)
|
|
|
|
} else {
|
|
|
|
client = testEnv.APIClient()
|
|
|
|
}
|
2018-03-30 07:10:02 -04:00
|
|
|
|
|
|
|
dockerfile := `
|
|
|
|
FROM busybox
|
|
|
|
RUN echo hello > /hello
|
|
|
|
RUN echo world >> /hello
|
|
|
|
RUN echo hello > /remove_me
|
|
|
|
ENV HELLO world
|
|
|
|
RUN rm /remove_me
|
|
|
|
`
|
|
|
|
|
|
|
|
// build and get the ID that we can use later for history comparison
|
|
|
|
ctx := context.Background()
|
|
|
|
source := fakecontext.New(t, "", fakecontext.WithDockerfile(dockerfile))
|
|
|
|
defer source.Close()
|
|
|
|
|
2020-05-18 08:42:29 -04:00
|
|
|
name := strings.ToLower(t.Name())
|
2018-03-30 07:10:02 -04:00
|
|
|
resp, err := client.ImageBuild(ctx,
|
|
|
|
source.AsTarReader(t),
|
|
|
|
types.ImageBuildOptions{
|
|
|
|
Remove: true,
|
|
|
|
ForceRemove: true,
|
|
|
|
Tags: []string{name},
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
2021-08-24 06:10:50 -04:00
|
|
|
_, err = io.Copy(io.Discard, resp.Body)
|
2018-03-30 07:10:02 -04:00
|
|
|
resp.Body.Close()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
inspect, _, err := client.ImageInspectWithRaw(ctx, name)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
origID := inspect.ID
|
|
|
|
|
|
|
|
// build with squash
|
|
|
|
resp, err = client.ImageBuild(ctx,
|
|
|
|
source.AsTarReader(t),
|
|
|
|
types.ImageBuildOptions{
|
|
|
|
Remove: true,
|
|
|
|
ForceRemove: true,
|
|
|
|
Squash: true,
|
|
|
|
Tags: []string{name},
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
2021-08-24 06:10:50 -04:00
|
|
|
_, err = io.Copy(io.Discard, resp.Body)
|
2018-03-30 07:10:02 -04:00
|
|
|
resp.Body.Close()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2019-06-06 07:15:31 -04:00
|
|
|
cid := container.Run(ctx, t, client,
|
2018-03-30 07:10:02 -04:00
|
|
|
container.WithImage(name),
|
|
|
|
container.WithCmd("/bin/sh", "-c", "cat /hello"),
|
|
|
|
)
|
|
|
|
reader, err := client.ContainerLogs(ctx, cid, types.ContainerLogsOptions{
|
|
|
|
ShowStdout: true,
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
actualStdout := new(bytes.Buffer)
|
2021-08-24 06:10:50 -04:00
|
|
|
actualStderr := io.Discard
|
2018-03-30 07:10:02 -04:00
|
|
|
_, err = stdcopy.StdCopy(actualStdout, actualStderr, reader)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(strings.TrimSpace(actualStdout.String()), "hello\nworld"))
|
|
|
|
|
2019-06-06 07:15:31 -04:00
|
|
|
container.Run(ctx, t, client,
|
2018-03-30 07:10:02 -04:00
|
|
|
container.WithImage(name),
|
|
|
|
container.WithCmd("/bin/sh", "-c", "[ ! -f /remove_me ]"),
|
|
|
|
)
|
2019-06-06 07:15:31 -04:00
|
|
|
container.Run(ctx, t, client,
|
2018-03-30 07:10:02 -04:00
|
|
|
container.WithImage(name),
|
2019-09-02 04:52:39 -04:00
|
|
|
container.WithCmd("/bin/sh", "-c", `[ "$(echo $HELLO)" = "world" ]`),
|
2018-03-30 07:10:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
origHistory, err := client.ImageHistory(ctx, origID)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
testHistory, err := client.ImageHistory(ctx, name)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
inspect, _, err = client.ImageInspectWithRaw(ctx, name)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Len(testHistory, len(origHistory)+1))
|
|
|
|
assert.Check(t, is.Len(inspect.RootFS.Layers, 2))
|
|
|
|
}
|