2016-06-18 06:49:17 -04:00
|
|
|
package dockerfile
|
|
|
|
|
|
|
|
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"
|
2017-05-22 11:21:17 -04:00
|
|
|
"github.com/docker/docker/builder/dockerfile/instructions"
|
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"
|
2017-08-08 15:43:48 -04:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-04-11 14:34:05 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
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{}
|
2017-05-05 18:52:11 -04:00
|
|
|
ctx := context.Background()
|
2017-04-11 14:34:05 -04:00
|
|
|
b := &Builder{
|
2017-08-08 15:43:48 -04:00
|
|
|
options: &types.ImageBuildOptions{Platform: specs.Platform{OS: runtime.GOOS}},
|
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{
|
2017-08-08 15:43:48 -04:00
|
|
|
Options: &types.ImageBuildOptions{Platform: specs.Platform{OS: runtime.GOOS}},
|
2017-05-05 18:52:11 -04:00
|
|
|
Backend: mockBackend,
|
|
|
|
}),
|
2017-05-31 12:28:56 -04:00
|
|
|
imageProber: newImageProber(mockBackend, nil, runtime.GOOS, 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()
|
2017-05-22 11:21:17 -04:00
|
|
|
sb := newDispatchRequest(b, '\\', nil, newBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
|
|
|
envCommand := &instructions.EnvCommand{
|
|
|
|
Env: instructions.KeyValuePairs{
|
|
|
|
instructions.KeyValuePair{Key: "var1", Value: "val1"},
|
|
|
|
instructions.KeyValuePair{Key: "var2", Value: "val2"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := dispatch(sb, envCommand)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(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
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
assert.Equal(t, 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()
|
2017-05-22 11:21:17 -04:00
|
|
|
sb := newDispatchRequest(b, '\\', nil, newBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
|
|
|
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)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(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
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
assert.Equal(t, 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()
|
2017-05-22 11:21:17 -04:00
|
|
|
sb := newDispatchRequest(b, '\\', nil, newBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
|
|
|
cmd := &instructions.MaintainerCommand{Maintainer: maintainerEntry}
|
|
|
|
err := dispatch(sb, cmd)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2017-05-22 11:21:17 -04:00
|
|
|
assert.Equal(t, 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()
|
2017-05-22 11:21:17 -04:00
|
|
|
sb := newDispatchRequest(b, '\\', nil, newBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
|
|
|
cmd := &instructions.LabelCommand{
|
|
|
|
Labels: instructions.KeyValuePairs{
|
|
|
|
instructions.KeyValuePair{Key: labelName, Value: labelValue},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := dispatch(sb, cmd)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
require.Contains(t, sb.state.runConfig.Labels, labelName)
|
|
|
|
assert.Equal(t, sb.state.runConfig.Labels[labelName], labelValue)
|
2017-04-04 13:40:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFromScratch(t *testing.T) {
|
|
|
|
b := newBuilderWithMockBackend()
|
2017-05-22 11:21:17 -04:00
|
|
|
sb := newDispatchRequest(b, '\\', nil, newBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
|
|
|
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() {
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.EqualError(t, err, "Windows does not support FROM scratch")
|
2017-04-04 13:40:37 -04:00
|
|
|
return
|
|
|
|
}
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2017-05-22 11:21:17 -04:00
|
|
|
assert.True(t, sb.state.hasFromImage())
|
|
|
|
assert.Equal(t, "", sb.state.imageID)
|
2017-05-26 19:14:18 -04:00
|
|
|
expected := "PATH=" + system.DefaultPathEnv(runtime.GOOS)
|
2017-05-22 11:21:17 -04:00
|
|
|
assert.Equal(t, []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
|
|
|
|
2017-03-27 21:36:28 -04:00
|
|
|
getImage := func(name string) (builder.Image, builder.ReleaseableLayer, error) {
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.Equal(t, "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
|
2017-05-22 11:21:17 -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"
|
|
|
|
metaArg := instructions.ArgCommand{
|
|
|
|
Key: "THETAG",
|
|
|
|
Value: &val,
|
|
|
|
}
|
|
|
|
cmd := &instructions.Stage{
|
|
|
|
BaseName: "alpine:${THETAG}",
|
|
|
|
}
|
|
|
|
err := processMetaArg(metaArg, NewShellLex('\\'), args)
|
2017-04-04 13:40:37 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
sb := newDispatchRequest(b, '\\', nil, args, newStagesBuildResults())
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2017-05-22 11:21:17 -04:00
|
|
|
err = initializeStage(sb, cmd)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, expected, sb.state.imageID)
|
|
|
|
assert.Equal(t, expected, sb.state.baseImage.ImageID())
|
|
|
|
assert.Len(t, sb.state.buildArgs.GetAllAllowed(), 0)
|
|
|
|
assert.Len(t, sb.state.buildArgs.GetAllMeta(), 1)
|
2017-04-04 13:40:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFromWithUndefinedArg(t *testing.T) {
|
|
|
|
tag, expected := "sometag", "expectedthisid"
|
|
|
|
|
2017-03-27 21:36:28 -04:00
|
|
|
getImage := func(name string) (builder.Image, builder.ReleaseableLayer, error) {
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.Equal(t, "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
|
2017-05-22 11:21:17 -04:00
|
|
|
sb := newDispatchRequest(b, '\\', nil, newBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
|
|
|
|
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)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2017-05-22 11:21:17 -04:00
|
|
|
assert.Equal(t, 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()
|
|
|
|
firstSB := newDispatchRequest(b, '\\', nil, newBuildArgs(make(map[string]*string)), previousResults)
|
|
|
|
secondSB := newDispatchRequest(b, '\\', nil, newBuildArgs(make(map[string]*string)), previousResults)
|
|
|
|
err := initializeStage(firstSB, firstFrom)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, firstSB.state.hasFromImage())
|
|
|
|
previousResults.indexed["base"] = firstSB.state.runConfig
|
|
|
|
previousResults.flat = append(previousResults.flat, firstSB.state.runConfig)
|
|
|
|
err = initializeStage(secondSB, secondFrom)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(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()
|
2017-05-22 11:21:17 -04:00
|
|
|
sb := newDispatchRequest(b, '\\', nil, newBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
|
|
|
cmd := &instructions.OnbuildCommand{
|
|
|
|
Expression: "ADD . /app/src",
|
|
|
|
}
|
|
|
|
err := dispatch(sb, cmd)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2017-05-22 11:21:17 -04:00
|
|
|
assert.Equal(t, "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()
|
2017-05-22 11:21:17 -04:00
|
|
|
sb := newDispatchRequest(b, '`', nil, newBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
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)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2017-05-22 11:21:17 -04:00
|
|
|
assert.Equal(t, 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()
|
2017-05-22 11:21:17 -04:00
|
|
|
sb := newDispatchRequest(b, '`', nil, newBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
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)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(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))
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
assert.Equal(t, expectedCommand, sb.state.runConfig.Cmd)
|
|
|
|
assert.True(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()
|
2017-05-22 11:21:17 -04:00
|
|
|
sb := newDispatchRequest(b, '`', nil, newBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
|
|
|
cmd := &instructions.HealthCheckCommand{
|
|
|
|
Health: &container.HealthConfig{
|
|
|
|
Test: []string{"NONE"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := dispatch(sb, cmd)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2016-06-26 16:01:28 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
require.NotNil(t, sb.state.runConfig.Healthcheck)
|
|
|
|
assert.Equal(t, []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()
|
|
|
|
sb := newDispatchRequest(b, '`', nil, newBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
|
|
|
expectedTest := []string{"CMD-SHELL", "curl -f http://localhost/ || exit 1"}
|
|
|
|
cmd := &instructions.HealthCheckCommand{
|
|
|
|
Health: &container.HealthConfig{
|
|
|
|
Test: expectedTest,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := dispatch(sb, cmd)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2016-06-26 16:01:28 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
require.NotNil(t, sb.state.runConfig.Healthcheck)
|
|
|
|
assert.Equal(t, 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()
|
2017-05-22 11:21:17 -04:00
|
|
|
sb := newDispatchRequest(b, '`', nil, newBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
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)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2017-05-22 11:21:17 -04:00
|
|
|
require.NotNil(t, sb.state.runConfig.Entrypoint)
|
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))
|
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
assert.Equal(t, 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()
|
2017-05-22 11:21:17 -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)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2016-06-26 16:01:28 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
require.NotNil(t, sb.state.runConfig.ExposedPorts)
|
|
|
|
require.Len(t, sb.state.runConfig.ExposedPorts, 1)
|
2016-06-26 16:01:28 -04:00
|
|
|
|
|
|
|
portsMapping, err := nat.ParsePortSpec(exposedPort)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2017-05-22 11:21:17 -04:00
|
|
|
assert.Contains(t, 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()
|
2017-05-22 11:21:17 -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)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2017-05-22 11:21:17 -04:00
|
|
|
assert.Equal(t, "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()
|
2017-05-22 11:21:17 -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)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2017-05-22 11:21:17 -04:00
|
|
|
require.NotNil(t, sb.state.runConfig.Volumes)
|
|
|
|
assert.Len(t, sb.state.runConfig.Volumes, 1)
|
|
|
|
assert.Contains(t, 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()
|
2017-05-22 11:21:17 -04:00
|
|
|
sb := newDispatchRequest(b, '`', nil, newBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
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)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2017-05-22 11:21:17 -04:00
|
|
|
assert.Equal(t, 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()
|
2017-05-22 11:21:17 -04:00
|
|
|
sb := newDispatchRequest(b, '`', nil, newBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2016-06-26 16:01:28 -04:00
|
|
|
|
|
|
|
argName := "foo"
|
|
|
|
argVal := "bar"
|
2017-05-22 11:21:17 -04:00
|
|
|
cmd := &instructions.ArgCommand{Key: argName, Value: &argVal}
|
|
|
|
err := dispatch(sb, cmd)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2016-06-26 16:01:28 -04:00
|
|
|
|
2017-04-06 17:38:02 -04:00
|
|
|
expected := map[string]string{argName: argVal}
|
2017-05-22 11:21:17 -04:00
|
|
|
assert.Equal(t, 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()
|
2017-05-22 11:21:17 -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)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2016-06-26 16:01:28 -04:00
|
|
|
|
|
|
|
expectedShell := strslice.StrSlice([]string{shellCmd})
|
2017-05-22 11:21:17 -04:00
|
|
|
assert.Equal(t, expectedShell, sb.state.runConfig.Shell)
|
2017-04-21 15:08:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrependEnvOnCmd(t *testing.T) {
|
|
|
|
buildArgs := newBuildArgs(nil)
|
|
|
|
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"})
|
|
|
|
assert.Equal(t, expected, cmdWithEnv)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunWithBuildArgs(t *testing.T) {
|
|
|
|
b := newBuilderWithMockBackend()
|
2017-05-22 11:21:17 -04:00
|
|
|
args := newBuildArgs(make(map[string]*string))
|
|
|
|
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()
|
|
|
|
assert.Equal(t, cachedCmd, cfg.Cmd)
|
2017-04-20 15:46:12 -04:00
|
|
|
assert.Equal(t, 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-05-31 12:28:56 -04:00
|
|
|
mockBackend.makeImageCacheFunc = func(_ []string, _ string) builder.ImageCache {
|
2017-04-13 18:44:36 -04:00
|
|
|
return imageCache
|
|
|
|
}
|
2017-05-31 12:28:56 -04:00
|
|
|
b.imageProber = newImageProber(mockBackend, nil, runtime.GOOS, false)
|
2017-03-27 21:36:28 -04:00
|
|
|
mockBackend.getImageFunc = func(_ string) (builder.Image, builder.ReleaseableLayer, error) {
|
|
|
|
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()
|
|
|
|
assert.Equal(t, cmdWithShell, config.Config.Cmd)
|
|
|
|
assert.Contains(t, config.Config.Env, "one=two")
|
2017-04-20 15:46:12 -04:00
|
|
|
assert.Equal(t, strslice.StrSlice{""}, config.Config.Entrypoint)
|
2017-04-21 15:08:11 -04:00
|
|
|
return container.ContainerCreateCreatedBody{ID: "12345"}, nil
|
|
|
|
}
|
|
|
|
mockBackend.commitFunc = func(cID string, cfg *backend.ContainerCommitConfig) (string, error) {
|
|
|
|
// Check the runConfig.Cmd sent to commit()
|
|
|
|
assert.Equal(t, origCmd, cfg.Config.Cmd)
|
|
|
|
assert.Equal(t, cachedCmd, cfg.ContainerConfig.Cmd)
|
2017-04-20 15:46:12 -04:00
|
|
|
assert.Equal(t, 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)
|
|
|
|
require.NoError(t, err)
|
|
|
|
sb.state.buildArgs.AddArg("one", strPtr("two"))
|
|
|
|
run := &instructions.RunCommand{
|
|
|
|
ShellDependantCmdLine: instructions.ShellDependantCmdLine{
|
|
|
|
CmdLine: strslice.StrSlice{"echo foo"},
|
|
|
|
PrependShell: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.NoError(t, dispatch(sb, run))
|
2017-04-21 15:08:11 -04:00
|
|
|
|
|
|
|
// Check that runConfig.Cmd has not been modified by run
|
2017-05-22 11:21:17 -04:00
|
|
|
assert.Equal(t, origCmd, sb.state.runConfig.Cmd)
|
2017-04-10 21:58:31 -04:00
|
|
|
}
|