2016-06-18 06:49:17 -04:00
|
|
|
package dockerfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
2017-04-21 15:08:11 -04:00
|
|
|
"bytes"
|
|
|
|
"context"
|
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-04-26 18:24:41 -04:00
|
|
|
"github.com/docker/docker/builder/dockerfile/parser"
|
2017-08-23 17:35:09 -04:00
|
|
|
"github.com/docker/docker/internal/testutil"
|
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"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
type commandWithFunction struct {
|
|
|
|
name string
|
|
|
|
function func(args []string) error
|
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
func withArgs(f dispatcher) func([]string) error {
|
|
|
|
return func(args []string) error {
|
2017-04-26 17:45:16 -04:00
|
|
|
return f(dispatchRequest{args: args})
|
2017-04-11 14:34:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func withBuilderAndArgs(builder *Builder, f dispatcher) func([]string) error {
|
|
|
|
return func(args []string) error {
|
|
|
|
return f(defaultDispatchReq(builder, args...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultDispatchReq(builder *Builder, args ...string) dispatchRequest {
|
|
|
|
return dispatchRequest{
|
2017-04-26 17:45:16 -04:00
|
|
|
builder: builder,
|
|
|
|
args: args,
|
|
|
|
flags: NewBFlags(),
|
|
|
|
shlex: NewShellLex(parser.DefaultEscapeToken),
|
|
|
|
state: &dispatchState{runConfig: &container.Config{}},
|
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{
|
|
|
|
options: &types.ImageBuildOptions{},
|
2017-03-27 21:36:28 -04:00
|
|
|
docker: mockBackend,
|
2017-04-11 14:34:05 -04:00
|
|
|
buildArgs: newBuildArgs(make(map[string]*string)),
|
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{
|
|
|
|
Options: &types.ImageBuildOptions{},
|
|
|
|
Backend: mockBackend,
|
|
|
|
}),
|
2017-04-13 18:44:36 -04:00
|
|
|
buildStages: newBuildStages(),
|
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 TestCommandsExactlyOneArgument(t *testing.T) {
|
|
|
|
commands := []commandWithFunction{
|
2017-04-11 14:34:05 -04:00
|
|
|
{"MAINTAINER", withArgs(maintainer)},
|
|
|
|
{"WORKDIR", withArgs(workdir)},
|
|
|
|
{"USER", withArgs(user)},
|
|
|
|
{"STOPSIGNAL", withArgs(stopSignal)},
|
|
|
|
}
|
2016-06-18 06:49:17 -04:00
|
|
|
|
|
|
|
for _, command := range commands {
|
|
|
|
err := command.function([]string{})
|
2017-04-11 14:34:05 -04:00
|
|
|
assert.EqualError(t, err, errExactlyOneArgument(command.name).Error())
|
2016-06-18 06:49:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandsAtLeastOneArgument(t *testing.T) {
|
|
|
|
commands := []commandWithFunction{
|
2017-04-11 14:34:05 -04:00
|
|
|
{"ENV", withArgs(env)},
|
|
|
|
{"LABEL", withArgs(label)},
|
|
|
|
{"ONBUILD", withArgs(onbuild)},
|
|
|
|
{"HEALTHCHECK", withArgs(healthcheck)},
|
|
|
|
{"EXPOSE", withArgs(expose)},
|
|
|
|
{"VOLUME", withArgs(volume)},
|
|
|
|
}
|
2016-06-18 06:49:17 -04:00
|
|
|
|
|
|
|
for _, command := range commands {
|
|
|
|
err := command.function([]string{})
|
2017-04-11 14:34:05 -04:00
|
|
|
assert.EqualError(t, err, errAtLeastOneArgument(command.name).Error())
|
2016-06-18 06:49:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 10:08:55 -04:00
|
|
|
func TestCommandsAtLeastTwoArguments(t *testing.T) {
|
|
|
|
commands := []commandWithFunction{
|
2017-04-11 14:34:05 -04:00
|
|
|
{"ADD", withArgs(add)},
|
|
|
|
{"COPY", withArgs(dispatchCopy)}}
|
2016-08-08 10:08:55 -04:00
|
|
|
|
|
|
|
for _, command := range commands {
|
|
|
|
err := command.function([]string{"arg1"})
|
2017-04-11 14:34:05 -04:00
|
|
|
assert.EqualError(t, err, errAtLeastTwoArguments(command.name).Error())
|
2016-08-08 10:08:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-18 06:49:17 -04:00
|
|
|
func TestCommandsTooManyArguments(t *testing.T) {
|
|
|
|
commands := []commandWithFunction{
|
2017-04-11 14:34:05 -04:00
|
|
|
{"ENV", withArgs(env)},
|
|
|
|
{"LABEL", withArgs(label)}}
|
2016-06-18 06:49:17 -04:00
|
|
|
|
|
|
|
for _, command := range commands {
|
|
|
|
err := command.function([]string{"arg1", "arg2", "arg3"})
|
2017-04-11 14:34:05 -04:00
|
|
|
assert.EqualError(t, err, errTooManyArguments(command.name).Error())
|
2016-08-26 05:06:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
func TestCommandsBlankNames(t *testing.T) {
|
|
|
|
builder := newBuilderWithMockBackend()
|
2016-08-26 05:06:07 -04:00
|
|
|
commands := []commandWithFunction{
|
2017-04-11 14:34:05 -04:00
|
|
|
{"ENV", withBuilderAndArgs(builder, env)},
|
|
|
|
{"LABEL", withBuilderAndArgs(builder, label)},
|
2016-08-26 05:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, command := range commands {
|
|
|
|
err := command.function([]string{"", ""})
|
2017-04-11 14:34:05 -04:00
|
|
|
assert.EqualError(t, err, errBlankCommandNames(command.name).Error())
|
2016-06-18 06:49:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnv2Variables(t *testing.T) {
|
2017-04-04 12:28:59 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2017-04-04 12:28:59 -04:00
|
|
|
args := []string{"var1", "val1", "var2", "val2"}
|
2017-04-11 14:34:05 -04:00
|
|
|
req := defaultDispatchReq(b, args...)
|
|
|
|
err := env(req)
|
|
|
|
require.NoError(t, err)
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2017-04-04 12:28:59 -04:00
|
|
|
expected := []string{
|
|
|
|
fmt.Sprintf("%s=%s", args[0], args[1]),
|
|
|
|
fmt.Sprintf("%s=%s", args[2], args[3]),
|
2016-06-18 06:49:17 -04:00
|
|
|
}
|
2017-04-26 17:45:16 -04:00
|
|
|
assert.Equal(t, expected, req.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()
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2017-04-04 12:28:59 -04:00
|
|
|
args := []string{"var1", "val1"}
|
2017-04-11 14:34:05 -04:00
|
|
|
req := defaultDispatchReq(b, args...)
|
2017-04-26 17:45:16 -04:00
|
|
|
req.state.runConfig.Env = []string{"var1=old", "var2=fromenv"}
|
2017-04-11 14:34:05 -04:00
|
|
|
err := env(req)
|
|
|
|
require.NoError(t, err)
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2017-04-04 12:28:59 -04:00
|
|
|
expected := []string{
|
|
|
|
fmt.Sprintf("%s=%s", args[0], args[1]),
|
|
|
|
"var2=fromenv",
|
2016-06-18 06:49:17 -04:00
|
|
|
}
|
2017-04-26 17:45:16 -04:00
|
|
|
assert.Equal(t, expected, req.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-04-26 17:45:16 -04:00
|
|
|
req := defaultDispatchReq(b, maintainerEntry)
|
|
|
|
err := maintainer(req)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2017-04-26 17:45:16 -04:00
|
|
|
assert.Equal(t, maintainerEntry, req.state.maintainer)
|
2016-06-18 06:49:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLabel(t *testing.T) {
|
|
|
|
labelName := "label"
|
|
|
|
labelValue := "value"
|
|
|
|
|
|
|
|
labelEntry := []string{labelName, labelValue}
|
2017-04-11 14:34:05 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
|
|
|
req := defaultDispatchReq(b, labelEntry...)
|
|
|
|
err := label(req)
|
|
|
|
require.NoError(t, err)
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
require.Contains(t, req.state.runConfig.Labels, labelName)
|
|
|
|
assert.Equal(t, req.state.runConfig.Labels[labelName], labelValue)
|
2017-04-04 13:40:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFromScratch(t *testing.T) {
|
|
|
|
b := newBuilderWithMockBackend()
|
2017-04-26 17:45:16 -04:00
|
|
|
req := defaultDispatchReq(b, "scratch")
|
|
|
|
err := from(req)
|
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-05 13:05:25 -04:00
|
|
|
assert.True(t, req.state.hasFromImage())
|
2017-04-26 17:45:16 -04:00
|
|
|
assert.Equal(t, "", req.state.imageID)
|
2017-05-17 20:08:01 -04:00
|
|
|
// Windows does not set the default path. TODO @jhowardmsft LCOW support. This will need revisiting as we get further into the implementation
|
2017-05-26 19:14:18 -04:00
|
|
|
expected := "PATH=" + system.DefaultPathEnv(runtime.GOOS)
|
2017-05-17 20:08:01 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expected = ""
|
|
|
|
}
|
|
|
|
assert.Equal(t, []string{expected}, req.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
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, arg(defaultDispatchReq(b, "THETAG="+tag)))
|
2017-04-26 17:45:16 -04:00
|
|
|
req := defaultDispatchReq(b, "alpine${THETAG}")
|
|
|
|
err := from(req)
|
2017-04-04 13:40:37 -04:00
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2017-04-26 17:45:16 -04:00
|
|
|
assert.Equal(t, expected, req.state.imageID)
|
|
|
|
assert.Equal(t, expected, req.state.baseImage.ImageID())
|
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.Len(t, b.buildArgs.GetAllAllowed(), 0)
|
|
|
|
assert.Len(t, b.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-04-04 13:40:37 -04:00
|
|
|
b.options.BuildArgs = map[string]*string{"THETAG": &tag}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
req := defaultDispatchReq(b, "alpine${THETAG}")
|
|
|
|
err := from(req)
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
2017-04-26 17:45:16 -04:00
|
|
|
assert.Equal(t, expected, req.state.imageID)
|
2016-06-18 06:49:17 -04:00
|
|
|
}
|
|
|
|
|
2017-05-03 14:02:46 -04:00
|
|
|
func TestFromMultiStageWithScratchNamedStage(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("Windows does not support scratch")
|
|
|
|
}
|
|
|
|
b := newBuilderWithMockBackend()
|
|
|
|
req := defaultDispatchReq(b, "scratch", "AS", "base")
|
|
|
|
|
|
|
|
require.NoError(t, from(req))
|
|
|
|
assert.True(t, req.state.hasFromImage())
|
|
|
|
|
|
|
|
req.args = []string{"base"}
|
|
|
|
require.NoError(t, from(req))
|
|
|
|
assert.True(t, req.state.hasFromImage())
|
|
|
|
}
|
|
|
|
|
2016-06-18 06:49:17 -04:00
|
|
|
func TestOnbuildIllegalTriggers(t *testing.T) {
|
|
|
|
triggers := []struct{ command, expectedError string }{
|
|
|
|
{"ONBUILD", "Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed"},
|
|
|
|
{"MAINTAINER", "MAINTAINER isn't allowed as an ONBUILD trigger"},
|
|
|
|
{"FROM", "FROM isn't allowed as an ONBUILD trigger"}}
|
|
|
|
|
|
|
|
for _, trigger := range triggers {
|
2017-04-11 14:34:05 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
err := onbuild(defaultDispatchReq(b, trigger.command))
|
|
|
|
testutil.ErrorContains(t, err, trigger.expectedError)
|
2016-06-18 06:49:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOnbuild(t *testing.T) {
|
2017-04-11 14:34:05 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
req := defaultDispatchReq(b, "ADD", ".", "/app/src")
|
|
|
|
req.original = "ONBUILD ADD . /app/src"
|
2017-04-26 17:45:16 -04:00
|
|
|
req.state.runConfig = &container.Config{}
|
2016-06-18 06:49:17 -04:00
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
err := onbuild(req)
|
|
|
|
require.NoError(t, err)
|
2017-04-26 17:45:16 -04:00
|
|
|
assert.Equal(t, "ADD . /app/src", req.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()
|
2016-06-26 16:01:28 -04:00
|
|
|
workingDir := "/app"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
workingDir = "C:\app"
|
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
req := defaultDispatchReq(b, workingDir)
|
|
|
|
err := workdir(req)
|
|
|
|
require.NoError(t, err)
|
2017-04-26 17:45:16 -04:00
|
|
|
assert.Equal(t, workingDir, req.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()
|
2016-06-26 16:01:28 -04:00
|
|
|
command := "./executable"
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
req := defaultDispatchReq(b, command)
|
|
|
|
err := cmd(req)
|
|
|
|
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-04-26 17:45:16 -04:00
|
|
|
assert.Equal(t, expectedCommand, req.state.runConfig.Cmd)
|
|
|
|
assert.True(t, req.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()
|
2016-06-26 16:01:28 -04:00
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
req := defaultDispatchReq(b, "NONE")
|
|
|
|
err := healthcheck(req)
|
|
|
|
require.NoError(t, err)
|
2016-06-26 16:01:28 -04:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
require.NotNil(t, req.state.runConfig.Healthcheck)
|
|
|
|
assert.Equal(t, []string{"NONE"}, req.state.runConfig.Healthcheck.Test)
|
2016-06-26 16:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHealthcheckCmd(t *testing.T) {
|
2017-04-11 14:34:05 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2016-06-26 16:01:28 -04:00
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
args := []string{"CMD", "curl", "-f", "http://localhost/", "||", "exit", "1"}
|
|
|
|
req := defaultDispatchReq(b, args...)
|
|
|
|
err := healthcheck(req)
|
|
|
|
require.NoError(t, err)
|
2016-06-26 16:01:28 -04:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
require.NotNil(t, req.state.runConfig.Healthcheck)
|
2017-04-11 14:34:05 -04:00
|
|
|
expectedTest := []string{"CMD-SHELL", "curl -f http://localhost/ || exit 1"}
|
2017-04-26 17:45:16 -04:00
|
|
|
assert.Equal(t, expectedTest, req.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()
|
2016-06-26 16:01:28 -04:00
|
|
|
entrypointCmd := "/usr/sbin/nginx"
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
req := defaultDispatchReq(b, entrypointCmd)
|
|
|
|
err := entrypoint(req)
|
|
|
|
require.NoError(t, err)
|
2017-04-26 17:45:16 -04:00
|
|
|
require.NotNil(t, req.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-04-26 17:45:16 -04:00
|
|
|
assert.Equal(t, expectedEntrypoint, req.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()
|
2016-06-26 16:01:28 -04:00
|
|
|
|
|
|
|
exposedPort := "80"
|
2017-04-11 14:34:05 -04:00
|
|
|
req := defaultDispatchReq(b, exposedPort)
|
|
|
|
err := expose(req)
|
|
|
|
require.NoError(t, err)
|
2016-06-26 16:01:28 -04:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
require.NotNil(t, req.state.runConfig.ExposedPorts)
|
|
|
|
require.Len(t, req.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-04-26 17:45:16 -04:00
|
|
|
assert.Contains(t, req.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()
|
2016-06-26 16:01:28 -04:00
|
|
|
userCommand := "foo"
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
req := defaultDispatchReq(b, userCommand)
|
|
|
|
err := user(req)
|
|
|
|
require.NoError(t, err)
|
2017-04-26 17:45:16 -04:00
|
|
|
assert.Equal(t, userCommand, req.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()
|
2016-06-26 16:01:28 -04:00
|
|
|
|
|
|
|
exposedVolume := "/foo"
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
req := defaultDispatchReq(b, exposedVolume)
|
|
|
|
err := volume(req)
|
|
|
|
require.NoError(t, err)
|
2016-06-26 16:01:28 -04:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
require.NotNil(t, req.state.runConfig.Volumes)
|
|
|
|
assert.Len(t, req.state.runConfig.Volumes, 1)
|
|
|
|
assert.Contains(t, req.state.runConfig.Volumes, exposedVolume)
|
2016-06-26 16:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStopSignal(t *testing.T) {
|
2017-04-11 14:34:05 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2016-06-26 16:01:28 -04:00
|
|
|
signal := "SIGKILL"
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
req := defaultDispatchReq(b, signal)
|
|
|
|
err := stopSignal(req)
|
|
|
|
require.NoError(t, err)
|
2017-04-26 17:45:16 -04:00
|
|
|
assert.Equal(t, signal, req.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()
|
2016-06-26 16:01:28 -04:00
|
|
|
|
|
|
|
argName := "foo"
|
|
|
|
argVal := "bar"
|
|
|
|
argDef := fmt.Sprintf("%s=%s", argName, argVal)
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
err := arg(defaultDispatchReq(b, argDef))
|
|
|
|
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-04-11 14:34:05 -04:00
|
|
|
assert.Equal(t, expected, b.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()
|
2016-06-26 16:01:28 -04:00
|
|
|
|
|
|
|
shellCmd := "powershell"
|
2017-04-11 14:34:05 -04:00
|
|
|
req := defaultDispatchReq(b, shellCmd)
|
|
|
|
req.attributes = map[string]bool{"json": true}
|
2016-06-26 16:01:28 -04:00
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
err := shell(req)
|
|
|
|
require.NoError(t, err)
|
2016-06-26 16:01:28 -04:00
|
|
|
|
|
|
|
expectedShell := strslice.StrSlice([]string{shellCmd})
|
2017-04-26 17:45:16 -04:00
|
|
|
assert.Equal(t, expectedShell, req.state.runConfig.Shell)
|
2016-06-26 16:01:28 -04:00
|
|
|
}
|
2017-04-10 21:58:31 -04:00
|
|
|
|
|
|
|
func TestParseOptInterval(t *testing.T) {
|
|
|
|
flInterval := &Flag{
|
|
|
|
name: "interval",
|
|
|
|
flagType: stringType,
|
|
|
|
Value: "50ns",
|
|
|
|
}
|
|
|
|
_, err := parseOptInterval(flInterval)
|
2017-04-21 15:08:11 -04:00
|
|
|
testutil.ErrorContains(t, err, "cannot be less than 1ms")
|
2017-04-10 21:58:31 -04:00
|
|
|
|
|
|
|
flInterval.Value = "1ms"
|
|
|
|
_, err = parseOptInterval(flInterval)
|
2017-04-21 15:08:11 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
b.buildArgs.argsFromOptions["HTTP_PROXY"] = strPtr("FOO")
|
|
|
|
b.disableCommit = false
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
req := defaultDispatchReq(b, "abcdef")
|
|
|
|
require.NoError(t, from(req))
|
|
|
|
b.buildArgs.AddArg("one", strPtr("two"))
|
|
|
|
|
|
|
|
req.args = []string{"echo foo"}
|
|
|
|
require.NoError(t, run(req))
|
|
|
|
|
|
|
|
// Check that runConfig.Cmd has not been modified by run
|
2017-04-26 17:45:16 -04:00
|
|
|
assert.Equal(t, origCmd, req.state.runConfig.Cmd)
|
2017-04-10 21:58:31 -04:00
|
|
|
}
|