2016-06-18 06:49:17 -04:00
|
|
|
package dockerfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"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-04-11 18:17:02 -04:00
|
|
|
"github.com/docker/docker/pkg/testutil"
|
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 {
|
|
|
|
return f(dispatchRequest{args: args, runConfig: &container.Config{}})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
|
|
|
builder: builder,
|
|
|
|
args: args,
|
|
|
|
flags: NewBFlags(),
|
|
|
|
runConfig: &container.Config{},
|
2017-04-26 18:24:41 -04:00
|
|
|
shlex: NewShellLex(parser.DefaultEscapeToken),
|
2017-04-11 14:34:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBuilderWithMockBackend() *Builder {
|
|
|
|
b := &Builder{
|
|
|
|
runConfig: &container.Config{},
|
|
|
|
options: &types.ImageBuildOptions{},
|
|
|
|
docker: &MockBackend{},
|
|
|
|
buildArgs: newBuildArgs(make(map[string]*string)),
|
|
|
|
disableCommit: true,
|
|
|
|
}
|
|
|
|
b.imageContexts = &imageContexts{b: b}
|
|
|
|
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-11 14:34:05 -04:00
|
|
|
assert.Equal(t, expected, req.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...)
|
|
|
|
req.runConfig.Env = []string{"var1=old", "var2=fromenv"}
|
|
|
|
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-11 14:34:05 -04:00
|
|
|
assert.Equal(t, expected, req.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()
|
|
|
|
err := maintainer(defaultDispatchReq(b, maintainerEntry))
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, maintainerEntry, b.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-11 14:34:05 -04:00
|
|
|
require.Contains(t, req.runConfig.Labels, labelName)
|
|
|
|
assert.Equal(t, req.runConfig.Labels[labelName], labelValue)
|
2017-04-04 13:40:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFromScratch(t *testing.T) {
|
|
|
|
b := newBuilderWithMockBackend()
|
2017-04-11 14:34:05 -04:00
|
|
|
err := from(defaultDispatchReq(b, "scratch"))
|
2016-06-18 06:49:17 -04:00
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
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)
|
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, "", b.image)
|
|
|
|
assert.Equal(t, true, b.noBaseImage)
|
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-04-04 13:40:37 -04:00
|
|
|
getImage := func(name string) (builder.Image, 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-04-04 13:40:37 -04:00
|
|
|
return &mockImage{id: "expectedthisid"}, nil
|
|
|
|
}
|
|
|
|
b := newBuilderWithMockBackend()
|
|
|
|
b.docker.(*MockBackend).getImageOnBuildFunc = 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)))
|
|
|
|
err := from(defaultDispatchReq(b, "alpine${THETAG}"))
|
2017-04-04 13:40:37 -04:00
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
require.NoError(t, err)
|
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, expected, b.image)
|
|
|
|
assert.Equal(t, expected, b.from.ImageID())
|
|
|
|
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"
|
|
|
|
|
|
|
|
getImage := func(name string) (builder.Image, 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-04-04 13:40:37 -04:00
|
|
|
return &mockImage{id: "expectedthisid"}, nil
|
2016-06-18 06:49:17 -04:00
|
|
|
}
|
2017-04-04 13:40:37 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
|
|
|
b.docker.(*MockBackend).getImageOnBuildFunc = getImage
|
|
|
|
b.options.BuildArgs = map[string]*string{"THETAG": &tag}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
err := from(defaultDispatchReq(b, "alpine${THETAG}"))
|
|
|
|
require.NoError(t, err)
|
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, expected, b.image)
|
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"
|
|
|
|
req.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)
|
|
|
|
assert.Equal(t, "ADD . /app/src", req.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)
|
|
|
|
assert.Equal(t, workingDir, req.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-11 14:34:05 -04:00
|
|
|
assert.Equal(t, expectedCommand, req.runConfig.Cmd)
|
|
|
|
assert.True(t, b.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-11 14:34:05 -04:00
|
|
|
require.NotNil(t, req.runConfig.Healthcheck)
|
|
|
|
assert.Equal(t, []string{"NONE"}, req.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-11 14:34:05 -04:00
|
|
|
require.NotNil(t, req.runConfig.Healthcheck)
|
|
|
|
expectedTest := []string{"CMD-SHELL", "curl -f http://localhost/ || exit 1"}
|
|
|
|
assert.Equal(t, expectedTest, req.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)
|
|
|
|
require.NotNil(t, req.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-11 14:34:05 -04:00
|
|
|
assert.Equal(t, expectedEntrypoint, req.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-11 14:34:05 -04:00
|
|
|
require.NotNil(t, req.runConfig.ExposedPorts)
|
|
|
|
require.Len(t, req.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)
|
|
|
|
assert.Contains(t, req.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)
|
|
|
|
assert.Equal(t, userCommand, req.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-11 14:34:05 -04:00
|
|
|
require.NotNil(t, req.runConfig.Volumes)
|
|
|
|
assert.Len(t, req.runConfig.Volumes, 1)
|
|
|
|
assert.Contains(t, req.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)
|
|
|
|
assert.Equal(t, signal, req.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-11 14:34:05 -04:00
|
|
|
assert.Equal(t, expectedShell, req.runConfig.Shell)
|
2016-06-26 16:01:28 -04:00
|
|
|
}
|