2018-02-05 16:05:59 -05:00
|
|
|
package dockerfile // import "github.com/docker/docker/builder/dockerfile"
|
2016-06-18 06:49:17 -04:00
|
|
|
|
|
|
|
import (
|
2017-04-21 15:08:11 -04:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2017-05-22 11:21:17 -04:00
|
|
|
"runtime"
|
|
|
|
"testing"
|
2017-04-13 18:44:36 -04:00
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-04-21 15:08:11 -04:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/strslice"
|
2017-04-04 13:40:37 -04:00
|
|
|
"github.com/docker/docker/builder"
|
2018-02-06 13:27:55 -05:00
|
|
|
"github.com/docker/docker/image"
|
2017-05-05 13:05:25 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2016-06-26 16:01:28 -04:00
|
|
|
"github.com/docker/go-connections/nat"
|
2018-06-02 12:46:53 -04:00
|
|
|
"github.com/moby/buildkit/frontend/dockerfile/instructions"
|
|
|
|
"github.com/moby/buildkit/frontend/dockerfile/shell"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
2016-06-18 06:49:17 -04:00
|
|
|
)
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
func newBuilderWithMockBackend() *Builder {
|
2017-03-27 21:36:28 -04:00
|
|
|
mockBackend := &MockBackend{}
|
2018-07-02 22:31:05 -04:00
|
|
|
opts := &types.ImageBuildOptions{}
|
2017-05-05 18:52:11 -04:00
|
|
|
ctx := context.Background()
|
2017-04-11 14:34:05 -04:00
|
|
|
b := &Builder{
|
2018-06-26 17:49:33 -04:00
|
|
|
options: opts,
|
2017-03-27 21:36:28 -04:00
|
|
|
docker: mockBackend,
|
2017-04-21 15:08:11 -04:00
|
|
|
Stdout: new(bytes.Buffer),
|
2017-05-05 18:52:11 -04:00
|
|
|
clientCtx: ctx,
|
2017-04-11 14:34:05 -04:00
|
|
|
disableCommit: true,
|
2017-05-05 18:52:11 -04:00
|
|
|
imageSources: newImageSources(ctx, builderOptions{
|
2018-06-26 17:49:33 -04:00
|
|
|
Options: opts,
|
2017-05-05 18:52:11 -04:00
|
|
|
Backend: mockBackend,
|
|
|
|
}),
|
2017-08-24 14:48:16 -04:00
|
|
|
imageProber: newImageProber(mockBackend, nil, false),
|
2017-04-13 18:44:36 -04:00
|
|
|
containerManager: newContainerManager(mockBackend),
|
2017-04-11 14:34:05 -04:00
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2016-06-18 06:49:17 -04:00
|
|
|
func TestEnv2Variables(t *testing.T) {
|
2017-04-04 12:28:59 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '\\', nil, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2017-05-22 11:21:17 -04:00
|
|
|
envCommand := &instructions.EnvCommand{
|
|
|
|
Env: instructions.KeyValuePairs{
|
|
|
|
instructions.KeyValuePair{Key: "var1", Value: "val1"},
|
|
|
|
instructions.KeyValuePair{Key: "var2", Value: "val2"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := dispatch(sb, envCommand)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2017-04-04 12:28:59 -04:00
|
|
|
expected := []string{
|
2017-05-22 11:21:17 -04:00
|
|
|
"var1=val1",
|
|
|
|
"var2=val2",
|
2016-06-18 06:49:17 -04:00
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(expected, sb.state.runConfig.Env))
|
2017-04-04 12:28:59 -04:00
|
|
|
}
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2017-04-04 12:28:59 -04:00
|
|
|
func TestEnvValueWithExistingRunConfigEnv(t *testing.T) {
|
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '\\', nil, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2017-05-22 11:21:17 -04:00
|
|
|
sb.state.runConfig.Env = []string{"var1=old", "var2=fromenv"}
|
|
|
|
envCommand := &instructions.EnvCommand{
|
|
|
|
Env: instructions.KeyValuePairs{
|
|
|
|
instructions.KeyValuePair{Key: "var1", Value: "val1"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := dispatch(sb, envCommand)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-04-04 12:28:59 -04:00
|
|
|
expected := []string{
|
2017-05-22 11:21:17 -04:00
|
|
|
"var1=val1",
|
2017-04-04 12:28:59 -04:00
|
|
|
"var2=fromenv",
|
2016-06-18 06:49:17 -04:00
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(expected, sb.state.runConfig.Env))
|
2016-06-18 06:49:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMaintainer(t *testing.T) {
|
|
|
|
maintainerEntry := "Some Maintainer <maintainer@example.com>"
|
2017-04-11 14:34:05 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '\\', nil, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2017-05-22 11:21:17 -04:00
|
|
|
cmd := &instructions.MaintainerCommand{Maintainer: maintainerEntry}
|
|
|
|
err := dispatch(sb, cmd)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(maintainerEntry, sb.state.maintainer))
|
2016-06-18 06:49:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLabel(t *testing.T) {
|
|
|
|
labelName := "label"
|
|
|
|
labelValue := "value"
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '\\', nil, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2017-05-22 11:21:17 -04:00
|
|
|
cmd := &instructions.LabelCommand{
|
|
|
|
Labels: instructions.KeyValuePairs{
|
|
|
|
instructions.KeyValuePair{Key: labelName, Value: labelValue},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := dispatch(sb, cmd)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Assert(t, is.Contains(sb.state.runConfig.Labels, labelName))
|
|
|
|
assert.Check(t, is.Equal(sb.state.runConfig.Labels[labelName], labelValue))
|
2017-04-04 13:40:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFromScratch(t *testing.T) {
|
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '\\', nil, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2017-05-22 11:21:17 -04:00
|
|
|
cmd := &instructions.Stage{
|
|
|
|
BaseName: "scratch",
|
|
|
|
}
|
|
|
|
err := initializeStage(sb, cmd)
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2017-05-17 20:08:01 -04:00
|
|
|
if runtime.GOOS == "windows" && !system.LCOWSupported() {
|
2018-07-02 22:31:05 -04:00
|
|
|
assert.Check(t, is.Error(err, "Linux containers are not supported on this system"))
|
2017-04-04 13:40:37 -04:00
|
|
|
return
|
|
|
|
}
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, sb.state.hasFromImage())
|
|
|
|
assert.Check(t, is.Equal("", sb.state.imageID))
|
2017-05-26 19:14:18 -04:00
|
|
|
expected := "PATH=" + system.DefaultPathEnv(runtime.GOOS)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual([]string{expected}, sb.state.runConfig.Env))
|
2017-04-04 13:40:37 -04:00
|
|
|
}
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2017-04-04 13:40:37 -04:00
|
|
|
func TestFromWithArg(t *testing.T) {
|
|
|
|
tag, expected := ":sometag", "expectedthisid"
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2018-02-16 16:50:57 -05:00
|
|
|
getImage := func(name string) (builder.Image, builder.ROLayer, error) {
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal("alpine"+tag, name))
|
2017-03-27 21:36:28 -04:00
|
|
|
return &mockImage{id: "expectedthisid"}, nil, nil
|
2017-04-04 13:40:37 -04:00
|
|
|
}
|
|
|
|
b := newBuilderWithMockBackend()
|
2017-03-27 21:36:28 -04:00
|
|
|
b.docker.(*MockBackend).getImageFunc = getImage
|
2018-05-07 13:49:13 -04:00
|
|
|
args := NewBuildArgs(make(map[string]*string))
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
val := "sometag"
|
2018-07-11 19:52:03 -04:00
|
|
|
metaArg := instructions.ArgCommand{KeyValuePairOptional: instructions.KeyValuePairOptional{
|
2017-05-22 11:21:17 -04:00
|
|
|
Key: "THETAG",
|
|
|
|
Value: &val,
|
2018-07-11 19:52:03 -04:00
|
|
|
}}
|
2017-05-22 11:21:17 -04:00
|
|
|
cmd := &instructions.Stage{
|
|
|
|
BaseName: "alpine:${THETAG}",
|
|
|
|
}
|
2018-01-30 18:58:21 -05:00
|
|
|
err := processMetaArg(metaArg, shell.NewLex('\\'), args)
|
2017-04-04 13:40:37 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
sb := newDispatchRequest(b, '\\', nil, args, newStagesBuildResults())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-05-22 11:21:17 -04:00
|
|
|
err = initializeStage(sb, cmd)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-05-22 11:21:17 -04:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(expected, sb.state.imageID))
|
|
|
|
assert.Check(t, is.Equal(expected, sb.state.baseImage.ImageID()))
|
|
|
|
assert.Check(t, is.Len(sb.state.buildArgs.GetAllAllowed(), 0))
|
|
|
|
assert.Check(t, is.Len(sb.state.buildArgs.GetAllMeta(), 1))
|
2017-04-04 13:40:37 -04:00
|
|
|
}
|
|
|
|
|
2018-07-04 20:23:15 -04:00
|
|
|
func TestFromWithArgButBuildArgsNotGiven(t *testing.T) {
|
|
|
|
b := newBuilderWithMockBackend()
|
|
|
|
args := NewBuildArgs(make(map[string]*string))
|
|
|
|
|
|
|
|
metaArg := instructions.ArgCommand{}
|
|
|
|
cmd := &instructions.Stage{
|
|
|
|
BaseName: "${THETAG}",
|
|
|
|
}
|
|
|
|
err := processMetaArg(metaArg, shell.NewLex('\\'), args)
|
|
|
|
|
|
|
|
sb := newDispatchRequest(b, '\\', nil, args, newStagesBuildResults())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
err = initializeStage(sb, cmd)
|
|
|
|
assert.Error(t, err, "base name (${THETAG}) should not be blank")
|
|
|
|
}
|
|
|
|
|
2017-04-04 13:40:37 -04:00
|
|
|
func TestFromWithUndefinedArg(t *testing.T) {
|
|
|
|
tag, expected := "sometag", "expectedthisid"
|
|
|
|
|
2018-02-16 16:50:57 -05:00
|
|
|
getImage := func(name string) (builder.Image, builder.ROLayer, error) {
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal("alpine", name))
|
2017-03-27 21:36:28 -04:00
|
|
|
return &mockImage{id: "expectedthisid"}, nil, nil
|
2016-06-18 06:49:17 -04:00
|
|
|
}
|
2017-04-04 13:40:37 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2017-03-27 21:36:28 -04:00
|
|
|
b.docker.(*MockBackend).getImageFunc = getImage
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '\\', nil, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2017-05-22 11:21:17 -04:00
|
|
|
|
2017-04-04 13:40:37 -04:00
|
|
|
b.options.BuildArgs = map[string]*string{"THETAG": &tag}
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
cmd := &instructions.Stage{
|
|
|
|
BaseName: "alpine${THETAG}",
|
|
|
|
}
|
|
|
|
err := initializeStage(sb, cmd)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(expected, sb.state.imageID))
|
2016-06-18 06:49:17 -04:00
|
|
|
}
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
func TestFromMultiStageWithNamedStage(t *testing.T) {
|
2017-05-03 14:02:46 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2017-05-22 11:21:17 -04:00
|
|
|
firstFrom := &instructions.Stage{BaseName: "someimg", Name: "base"}
|
|
|
|
secondFrom := &instructions.Stage{BaseName: "base"}
|
|
|
|
previousResults := newStagesBuildResults()
|
2018-05-07 13:49:13 -04:00
|
|
|
firstSB := newDispatchRequest(b, '\\', nil, NewBuildArgs(make(map[string]*string)), previousResults)
|
|
|
|
secondSB := newDispatchRequest(b, '\\', nil, NewBuildArgs(make(map[string]*string)), previousResults)
|
2017-05-22 11:21:17 -04:00
|
|
|
err := initializeStage(firstSB, firstFrom)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, firstSB.state.hasFromImage())
|
2017-05-22 11:21:17 -04:00
|
|
|
previousResults.indexed["base"] = firstSB.state.runConfig
|
|
|
|
previousResults.flat = append(previousResults.flat, firstSB.state.runConfig)
|
|
|
|
err = initializeStage(secondSB, secondFrom)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, secondSB.state.hasFromImage())
|
2016-06-18 06:49:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOnbuild(t *testing.T) {
|
2017-04-11 14:34:05 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '\\', nil, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2017-05-22 11:21:17 -04:00
|
|
|
cmd := &instructions.OnbuildCommand{
|
|
|
|
Expression: "ADD . /app/src",
|
|
|
|
}
|
|
|
|
err := dispatch(sb, cmd)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal("ADD . /app/src", sb.state.runConfig.OnBuild[0]))
|
2016-06-18 06:49:17 -04:00
|
|
|
}
|
2016-06-26 16:01:28 -04:00
|
|
|
|
|
|
|
func TestWorkdir(t *testing.T) {
|
2017-04-11 14:34:05 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '`', nil, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2017-10-03 14:32:54 -04:00
|
|
|
sb.state.baseImage = &mockImage{}
|
2016-06-26 16:01:28 -04:00
|
|
|
workingDir := "/app"
|
|
|
|
if runtime.GOOS == "windows" {
|
2017-05-22 11:21:17 -04:00
|
|
|
workingDir = "C:\\app"
|
|
|
|
}
|
|
|
|
cmd := &instructions.WorkdirCommand{
|
|
|
|
Path: workingDir,
|
2016-06-26 16:01:28 -04:00
|
|
|
}
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
err := dispatch(sb, cmd)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(workingDir, sb.state.runConfig.WorkingDir))
|
2016-06-26 16:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCmd(t *testing.T) {
|
2017-04-11 14:34:05 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '`', nil, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2017-10-04 17:26:56 -04:00
|
|
|
sb.state.baseImage = &mockImage{}
|
2016-06-26 16:01:28 -04:00
|
|
|
command := "./executable"
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
cmd := &instructions.CmdCommand{
|
|
|
|
ShellDependantCmdLine: instructions.ShellDependantCmdLine{
|
|
|
|
CmdLine: strslice.StrSlice{command},
|
|
|
|
PrependShell: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := dispatch(sb, cmd)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2016-06-26 16:01:28 -04:00
|
|
|
|
|
|
|
var expectedCommand strslice.StrSlice
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expectedCommand = strslice.StrSlice(append([]string{"cmd"}, "/S", "/C", command))
|
|
|
|
} else {
|
|
|
|
expectedCommand = strslice.StrSlice(append([]string{"/bin/sh"}, "-c", command))
|
|
|
|
}
|
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(expectedCommand, sb.state.runConfig.Cmd))
|
|
|
|
assert.Check(t, sb.state.cmdSet)
|
2016-06-26 16:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHealthcheckNone(t *testing.T) {
|
2017-04-11 14:34:05 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '`', nil, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2017-05-22 11:21:17 -04:00
|
|
|
cmd := &instructions.HealthCheckCommand{
|
|
|
|
Health: &container.HealthConfig{
|
|
|
|
Test: []string{"NONE"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := dispatch(sb, cmd)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2016-06-26 16:01:28 -04:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Assert(t, sb.state.runConfig.Healthcheck != nil)
|
|
|
|
assert.Check(t, is.DeepEqual([]string{"NONE"}, sb.state.runConfig.Healthcheck.Test))
|
2016-06-26 16:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHealthcheckCmd(t *testing.T) {
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '`', nil, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2017-05-22 11:21:17 -04:00
|
|
|
expectedTest := []string{"CMD-SHELL", "curl -f http://localhost/ || exit 1"}
|
|
|
|
cmd := &instructions.HealthCheckCommand{
|
|
|
|
Health: &container.HealthConfig{
|
|
|
|
Test: expectedTest,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := dispatch(sb, cmd)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2016-06-26 16:01:28 -04:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Assert(t, sb.state.runConfig.Healthcheck != nil)
|
|
|
|
assert.Check(t, is.DeepEqual(expectedTest, sb.state.runConfig.Healthcheck.Test))
|
2016-06-26 16:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEntrypoint(t *testing.T) {
|
2017-04-11 14:34:05 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '`', nil, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2017-10-04 17:26:56 -04:00
|
|
|
sb.state.baseImage = &mockImage{}
|
2016-06-26 16:01:28 -04:00
|
|
|
entrypointCmd := "/usr/sbin/nginx"
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
cmd := &instructions.EntrypointCommand{
|
|
|
|
ShellDependantCmdLine: instructions.ShellDependantCmdLine{
|
|
|
|
CmdLine: strslice.StrSlice{entrypointCmd},
|
|
|
|
PrependShell: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := dispatch(sb, cmd)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, sb.state.runConfig.Entrypoint != nil)
|
2016-06-26 16:01:28 -04:00
|
|
|
|
|
|
|
var expectedEntrypoint strslice.StrSlice
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expectedEntrypoint = strslice.StrSlice(append([]string{"cmd"}, "/S", "/C", entrypointCmd))
|
|
|
|
} else {
|
|
|
|
expectedEntrypoint = strslice.StrSlice(append([]string{"/bin/sh"}, "-c", entrypointCmd))
|
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(expectedEntrypoint, sb.state.runConfig.Entrypoint))
|
2016-06-26 16:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestExpose(t *testing.T) {
|
2017-04-11 14:34:05 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '`', nil, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2016-06-26 16:01:28 -04:00
|
|
|
|
|
|
|
exposedPort := "80"
|
2017-05-22 11:21:17 -04:00
|
|
|
cmd := &instructions.ExposeCommand{
|
|
|
|
Ports: []string{exposedPort},
|
|
|
|
}
|
|
|
|
err := dispatch(sb, cmd)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2016-06-26 16:01:28 -04:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Assert(t, sb.state.runConfig.ExposedPorts != nil)
|
|
|
|
assert.Assert(t, is.Len(sb.state.runConfig.ExposedPorts, 1))
|
2016-06-26 16:01:28 -04:00
|
|
|
|
|
|
|
portsMapping, err := nat.ParsePortSpec(exposedPort)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Contains(sb.state.runConfig.ExposedPorts, portsMapping[0].Port))
|
2016-06-26 16:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser(t *testing.T) {
|
2017-04-11 14:34:05 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '`', nil, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2016-06-26 16:01:28 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
cmd := &instructions.UserCommand{
|
|
|
|
User: "test",
|
|
|
|
}
|
|
|
|
err := dispatch(sb, cmd)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal("test", sb.state.runConfig.User))
|
2016-06-26 16:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestVolume(t *testing.T) {
|
2017-04-11 14:34:05 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '`', nil, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2016-06-26 16:01:28 -04:00
|
|
|
|
|
|
|
exposedVolume := "/foo"
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
cmd := &instructions.VolumeCommand{
|
|
|
|
Volumes: []string{exposedVolume},
|
|
|
|
}
|
|
|
|
err := dispatch(sb, cmd)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, sb.state.runConfig.Volumes != nil)
|
|
|
|
assert.Check(t, is.Len(sb.state.runConfig.Volumes, 1))
|
|
|
|
assert.Check(t, is.Contains(sb.state.runConfig.Volumes, exposedVolume))
|
2016-06-26 16:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStopSignal(t *testing.T) {
|
2017-05-22 11:21:17 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("Windows does not support stopsignal")
|
|
|
|
return
|
|
|
|
}
|
2017-04-11 14:34:05 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '`', nil, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2017-10-04 17:26:56 -04:00
|
|
|
sb.state.baseImage = &mockImage{}
|
2016-06-26 16:01:28 -04:00
|
|
|
signal := "SIGKILL"
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
cmd := &instructions.StopSignalCommand{
|
|
|
|
Signal: signal,
|
|
|
|
}
|
|
|
|
err := dispatch(sb, cmd)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(signal, sb.state.runConfig.StopSignal))
|
2016-06-26 16:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestArg(t *testing.T) {
|
2017-04-06 17:38:02 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '`', nil, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2016-06-26 16:01:28 -04:00
|
|
|
|
|
|
|
argName := "foo"
|
|
|
|
argVal := "bar"
|
2018-07-11 19:52:03 -04:00
|
|
|
cmd := &instructions.ArgCommand{KeyValuePairOptional: instructions.KeyValuePairOptional{Key: argName, Value: &argVal}}
|
2017-05-22 11:21:17 -04:00
|
|
|
err := dispatch(sb, cmd)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2016-06-26 16:01:28 -04:00
|
|
|
|
2017-04-06 17:38:02 -04:00
|
|
|
expected := map[string]string{argName: argVal}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(expected, sb.state.buildArgs.GetAllAllowed()))
|
2016-06-26 16:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShell(t *testing.T) {
|
2017-04-11 14:34:05 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '`', nil, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2016-06-26 16:01:28 -04:00
|
|
|
|
|
|
|
shellCmd := "powershell"
|
2017-05-22 11:21:17 -04:00
|
|
|
cmd := &instructions.ShellCommand{Shell: strslice.StrSlice{shellCmd}}
|
2016-06-26 16:01:28 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
err := dispatch(sb, cmd)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2016-06-26 16:01:28 -04:00
|
|
|
|
|
|
|
expectedShell := strslice.StrSlice([]string{shellCmd})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(expectedShell, sb.state.runConfig.Shell))
|
2017-04-21 15:08:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrependEnvOnCmd(t *testing.T) {
|
2018-05-07 13:49:13 -04:00
|
|
|
buildArgs := NewBuildArgs(nil)
|
2017-04-21 15:08:11 -04:00
|
|
|
buildArgs.AddArg("NO_PROXY", nil)
|
|
|
|
|
|
|
|
args := []string{"sorted=nope", "args=not", "http_proxy=foo", "NO_PROXY=YA"}
|
|
|
|
cmd := []string{"foo", "bar"}
|
|
|
|
cmdWithEnv := prependEnvOnCmd(buildArgs, args, cmd)
|
|
|
|
expected := strslice.StrSlice([]string{
|
|
|
|
"|3", "NO_PROXY=YA", "args=not", "sorted=nope", "foo", "bar"})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(expected, cmdWithEnv))
|
2017-04-21 15:08:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunWithBuildArgs(t *testing.T) {
|
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
args := NewBuildArgs(make(map[string]*string))
|
2017-05-22 11:21:17 -04:00
|
|
|
args.argsFromOptions["HTTP_PROXY"] = strPtr("FOO")
|
2017-04-21 15:08:11 -04:00
|
|
|
b.disableCommit = false
|
2017-05-22 11:21:17 -04:00
|
|
|
sb := newDispatchRequest(b, '`', nil, args, newStagesBuildResults())
|
2017-04-21 15:08:11 -04:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig := &container.Config{}
|
2017-04-21 15:08:11 -04:00
|
|
|
origCmd := strslice.StrSlice([]string{"cmd", "in", "from", "image"})
|
2017-06-20 18:08:58 -04:00
|
|
|
cmdWithShell := strslice.StrSlice(append(getShell(runConfig, runtime.GOOS), "echo foo"))
|
2017-04-21 15:08:11 -04:00
|
|
|
envVars := []string{"|1", "one=two"}
|
|
|
|
cachedCmd := strslice.StrSlice(append(envVars, cmdWithShell...))
|
|
|
|
|
|
|
|
imageCache := &mockImageCache{
|
|
|
|
getCacheFunc: func(parentID string, cfg *container.Config) (string, error) {
|
|
|
|
// Check the runConfig.Cmd sent to probeCache()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(cachedCmd, cfg.Cmd))
|
|
|
|
assert.Check(t, is.DeepEqual(strslice.StrSlice(nil), cfg.Entrypoint))
|
2017-04-21 15:08:11 -04:00
|
|
|
return "", nil
|
|
|
|
},
|
2017-04-10 21:58:31 -04:00
|
|
|
}
|
2017-04-21 15:08:11 -04:00
|
|
|
|
|
|
|
mockBackend := b.docker.(*MockBackend)
|
2017-08-24 14:48:16 -04:00
|
|
|
mockBackend.makeImageCacheFunc = func(_ []string) builder.ImageCache {
|
2017-04-13 18:44:36 -04:00
|
|
|
return imageCache
|
|
|
|
}
|
2017-08-24 14:48:16 -04:00
|
|
|
b.imageProber = newImageProber(mockBackend, nil, false)
|
2018-02-16 16:50:57 -05:00
|
|
|
mockBackend.getImageFunc = func(_ string) (builder.Image, builder.ROLayer, error) {
|
2017-03-27 21:36:28 -04:00
|
|
|
return &mockImage{
|
|
|
|
id: "abcdef",
|
|
|
|
config: &container.Config{Cmd: origCmd},
|
|
|
|
}, nil, nil
|
2017-04-21 15:08:11 -04:00
|
|
|
}
|
|
|
|
mockBackend.containerCreateFunc = func(config types.ContainerCreateConfig) (container.ContainerCreateCreatedBody, error) {
|
|
|
|
// Check the runConfig.Cmd sent to create()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(cmdWithShell, config.Config.Cmd))
|
|
|
|
assert.Check(t, is.Contains(config.Config.Env, "one=two"))
|
|
|
|
assert.Check(t, is.DeepEqual(strslice.StrSlice{""}, config.Config.Entrypoint))
|
2017-04-21 15:08:11 -04:00
|
|
|
return container.ContainerCreateCreatedBody{ID: "12345"}, nil
|
|
|
|
}
|
2018-02-06 13:27:55 -05:00
|
|
|
mockBackend.commitFunc = func(cfg backend.CommitConfig) (image.ID, error) {
|
2017-04-21 15:08:11 -04:00
|
|
|
// Check the runConfig.Cmd sent to commit()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(origCmd, cfg.Config.Cmd))
|
|
|
|
assert.Check(t, is.DeepEqual(cachedCmd, cfg.ContainerConfig.Cmd))
|
|
|
|
assert.Check(t, is.DeepEqual(strslice.StrSlice(nil), cfg.Config.Entrypoint))
|
2017-04-21 15:08:11 -04:00
|
|
|
return "", nil
|
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
from := &instructions.Stage{BaseName: "abcdef"}
|
|
|
|
err := initializeStage(sb, from)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-05-22 11:21:17 -04:00
|
|
|
sb.state.buildArgs.AddArg("one", strPtr("two"))
|
|
|
|
run := &instructions.RunCommand{
|
|
|
|
ShellDependantCmdLine: instructions.ShellDependantCmdLine{
|
|
|
|
CmdLine: strslice.StrSlice{"echo foo"},
|
|
|
|
PrependShell: true,
|
|
|
|
},
|
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, dispatch(sb, run))
|
2017-04-21 15:08:11 -04:00
|
|
|
|
|
|
|
// Check that runConfig.Cmd has not been modified by run
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(origCmd, sb.state.runConfig.Cmd))
|
2017-04-10 21:58:31 -04:00
|
|
|
}
|
2018-07-07 20:36:50 -04:00
|
|
|
|
|
|
|
func TestRunIgnoresHealthcheck(t *testing.T) {
|
|
|
|
b := newBuilderWithMockBackend()
|
|
|
|
args := NewBuildArgs(make(map[string]*string))
|
|
|
|
sb := newDispatchRequest(b, '`', nil, args, newStagesBuildResults())
|
|
|
|
b.disableCommit = false
|
|
|
|
|
|
|
|
origCmd := strslice.StrSlice([]string{"cmd", "in", "from", "image"})
|
|
|
|
|
|
|
|
imageCache := &mockImageCache{
|
|
|
|
getCacheFunc: func(parentID string, cfg *container.Config) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mockBackend := b.docker.(*MockBackend)
|
|
|
|
mockBackend.makeImageCacheFunc = func(_ []string) builder.ImageCache {
|
|
|
|
return imageCache
|
|
|
|
}
|
|
|
|
b.imageProber = newImageProber(mockBackend, nil, false)
|
|
|
|
mockBackend.getImageFunc = func(_ string) (builder.Image, builder.ROLayer, error) {
|
|
|
|
return &mockImage{
|
|
|
|
id: "abcdef",
|
|
|
|
config: &container.Config{Cmd: origCmd},
|
|
|
|
}, nil, nil
|
|
|
|
}
|
|
|
|
mockBackend.containerCreateFunc = func(config types.ContainerCreateConfig) (container.ContainerCreateCreatedBody, error) {
|
|
|
|
return container.ContainerCreateCreatedBody{ID: "12345"}, nil
|
|
|
|
}
|
|
|
|
mockBackend.commitFunc = func(cfg backend.CommitConfig) (image.ID, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
from := &instructions.Stage{BaseName: "abcdef"}
|
|
|
|
err := initializeStage(sb, from)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
expectedTest := []string{"CMD-SHELL", "curl -f http://localhost/ || exit 1"}
|
|
|
|
cmd := &instructions.HealthCheckCommand{
|
|
|
|
Health: &container.HealthConfig{
|
|
|
|
Test: expectedTest,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.NilError(t, dispatch(sb, cmd))
|
|
|
|
assert.Assert(t, sb.state.runConfig.Healthcheck != nil)
|
|
|
|
|
|
|
|
mockBackend.containerCreateFunc = func(config types.ContainerCreateConfig) (container.ContainerCreateCreatedBody, error) {
|
|
|
|
// Check the Healthcheck is disabled.
|
|
|
|
assert.Check(t, is.DeepEqual([]string{"NONE"}, config.Config.Healthcheck.Test))
|
|
|
|
return container.ContainerCreateCreatedBody{ID: "123456"}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.state.buildArgs.AddArg("one", strPtr("two"))
|
|
|
|
run := &instructions.RunCommand{
|
|
|
|
ShellDependantCmdLine: instructions.ShellDependantCmdLine{
|
|
|
|
CmdLine: strslice.StrSlice{"echo foo"},
|
|
|
|
PrependShell: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.NilError(t, dispatch(sb, run))
|
|
|
|
assert.Check(t, is.DeepEqual(expectedTest, sb.state.runConfig.Healthcheck.Test))
|
|
|
|
}
|