2018-02-05 16:05:59 -05:00
|
|
|
package dockerfile // import "github.com/docker/docker/builder/dockerfile"
|
2016-05-21 12:33:16 -04:00
|
|
|
|
|
|
|
import (
|
2016-05-29 16:28:24 -04:00
|
|
|
"fmt"
|
2018-04-20 05:59:08 -04:00
|
|
|
"os"
|
2017-06-20 18:08:58 -04:00
|
|
|
"runtime"
|
2016-05-21 12:33:16 -04:00
|
|
|
"testing"
|
|
|
|
|
2017-04-13 14:37:32 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/backend"
|
2017-04-21 14:11:21 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2016-05-21 12:33:16 -04:00
|
|
|
"github.com/docker/docker/builder"
|
2017-03-20 18:22:29 -04:00
|
|
|
"github.com/docker/docker/builder/remotecontext"
|
2019-04-26 18:12:43 -04:00
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/layer"
|
2016-05-21 12:33:16 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2017-11-09 16:20:51 -05:00
|
|
|
"github.com/docker/go-connections/nat"
|
2019-04-26 18:12:43 -04:00
|
|
|
"github.com/opencontainers/go-digest"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/skip"
|
2016-05-21 12:33:16 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEmptyDockerfile(t *testing.T) {
|
|
|
|
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
createTestTempFile(t, contextDir, builder.DefaultDockerfileName, "", 0777)
|
|
|
|
|
2017-03-20 18:22:29 -04:00
|
|
|
readAndCheckDockerfile(t, "emptyDockerfile", contextDir, "", "the Dockerfile (Dockerfile) cannot be empty")
|
2016-05-29 16:28:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSymlinkDockerfile(t *testing.T) {
|
|
|
|
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
createTestSymlink(t, contextDir, builder.DefaultDockerfileName, "/etc/passwd")
|
|
|
|
|
|
|
|
// The reason the error is "Cannot locate specified Dockerfile" is because
|
|
|
|
// in the builder, the symlink is resolved within the context, therefore
|
|
|
|
// Dockerfile -> /etc/passwd becomes etc/passwd from the context which is
|
|
|
|
// a nonexistent file.
|
|
|
|
expectedError := fmt.Sprintf("Cannot locate specified Dockerfile: %s", builder.DefaultDockerfileName)
|
|
|
|
|
|
|
|
readAndCheckDockerfile(t, "symlinkDockerfile", contextDir, builder.DefaultDockerfileName, expectedError)
|
|
|
|
}
|
|
|
|
|
2016-06-26 16:01:28 -04:00
|
|
|
func TestDockerfileOutsideTheBuildContext(t *testing.T) {
|
|
|
|
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
|
|
|
|
defer cleanup()
|
2016-05-21 12:33:16 -04:00
|
|
|
|
2020-08-07 14:51:23 -04:00
|
|
|
expectedError := "path outside the build context: ../../Dockerfile ()"
|
2018-03-08 12:53:27 -05:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expectedError = "failed to resolve scoped path ../../Dockerfile ()"
|
|
|
|
}
|
2016-05-21 12:33:16 -04:00
|
|
|
|
2016-06-26 16:01:28 -04:00
|
|
|
readAndCheckDockerfile(t, "DockerfileOutsideTheBuildContext", contextDir, "../../Dockerfile", expectedError)
|
2016-05-21 12:33:16 -04:00
|
|
|
}
|
2016-05-29 12:56:49 -04:00
|
|
|
|
2016-06-26 16:01:28 -04:00
|
|
|
func TestNonExistingDockerfile(t *testing.T) {
|
2016-05-29 12:56:49 -04:00
|
|
|
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
|
|
|
|
defer cleanup()
|
|
|
|
|
2016-06-26 16:01:28 -04:00
|
|
|
expectedError := "Cannot locate specified Dockerfile: Dockerfile"
|
|
|
|
|
|
|
|
readAndCheckDockerfile(t, "NonExistingDockerfile", contextDir, "Dockerfile", expectedError)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readAndCheckDockerfile(t *testing.T, testName, contextDir, dockerfilePath, expectedError string) {
|
2018-03-08 12:53:27 -05:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
skip.If(t, os.Getuid() != 0, "skipping test that requires root")
|
|
|
|
}
|
2016-05-29 12:56:49 -04:00
|
|
|
tarStream, err := archive.Tar(contextDir, archive.Uncompressed)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2016-05-29 12:56:49 -04:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err = tarStream.Close(); err != nil {
|
|
|
|
t.Fatalf("Error when closing tar stream: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-03-20 18:22:29 -04:00
|
|
|
if dockerfilePath == "" { // handled in BuildWithContext
|
|
|
|
dockerfilePath = builder.DefaultDockerfileName
|
2016-05-29 12:56:49 -04:00
|
|
|
}
|
|
|
|
|
2017-04-13 14:37:32 -04:00
|
|
|
config := backend.BuildConfig{
|
|
|
|
Options: &types.ImageBuildOptions{Dockerfile: dockerfilePath},
|
|
|
|
Source: tarStream,
|
|
|
|
}
|
|
|
|
_, _, err = remotecontext.Detect(config)
|
2018-03-08 12:53:27 -05:00
|
|
|
assert.Check(t, is.ErrorContains(err, expectedError))
|
2016-05-29 12:56:49 -04:00
|
|
|
}
|
2017-04-21 14:11:21 -04:00
|
|
|
|
|
|
|
func TestCopyRunConfig(t *testing.T) {
|
|
|
|
defaultEnv := []string{"foo=1"}
|
|
|
|
defaultCmd := []string{"old"}
|
|
|
|
|
|
|
|
var testcases = []struct {
|
|
|
|
doc string
|
|
|
|
modifiers []runConfigModifier
|
|
|
|
expected *container.Config
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "Set the command",
|
|
|
|
modifiers: []runConfigModifier{withCmd([]string{"new"})},
|
|
|
|
expected: &container.Config{
|
|
|
|
Cmd: []string{"new"},
|
|
|
|
Env: defaultEnv,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "Set the command to a comment",
|
2017-06-20 18:08:58 -04:00
|
|
|
modifiers: []runConfigModifier{withCmdComment("comment", runtime.GOOS)},
|
2017-04-21 14:11:21 -04:00
|
|
|
expected: &container.Config{
|
2017-08-08 15:43:48 -04:00
|
|
|
Cmd: append(defaultShellForOS(runtime.GOOS), "#(nop) ", "comment"),
|
2017-04-21 14:11:21 -04:00
|
|
|
Env: defaultEnv,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "Set the command and env",
|
|
|
|
modifiers: []runConfigModifier{
|
|
|
|
withCmd([]string{"new"}),
|
|
|
|
withEnv([]string{"one", "two"}),
|
|
|
|
},
|
|
|
|
expected: &container.Config{
|
|
|
|
Cmd: []string{"new"},
|
|
|
|
Env: []string{"one", "two"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
runConfig := &container.Config{
|
|
|
|
Cmd: defaultCmd,
|
|
|
|
Env: defaultEnv,
|
|
|
|
}
|
|
|
|
runConfigCopy := copyRunConfig(runConfig, testcase.modifiers...)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(testcase.expected, runConfigCopy), testcase.doc)
|
2017-04-21 14:11:21 -04:00
|
|
|
// Assert the original was not modified
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, runConfig != runConfigCopy, testcase.doc)
|
2017-04-21 14:11:21 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-26 12:05:55 -04:00
|
|
|
|
2017-11-09 16:20:51 -05:00
|
|
|
func fullMutableRunConfig() *container.Config {
|
|
|
|
return &container.Config{
|
|
|
|
Cmd: []string{"command", "arg1"},
|
|
|
|
Env: []string{"env1=foo", "env2=bar"},
|
|
|
|
ExposedPorts: nat.PortSet{
|
|
|
|
"1000/tcp": {},
|
|
|
|
"1001/tcp": {},
|
|
|
|
},
|
|
|
|
Volumes: map[string]struct{}{
|
|
|
|
"one": {},
|
|
|
|
"two": {},
|
|
|
|
},
|
|
|
|
Entrypoint: []string{"entry", "arg1"},
|
|
|
|
OnBuild: []string{"first", "next"},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"label1": "value1",
|
|
|
|
"label2": "value2",
|
|
|
|
},
|
|
|
|
Shell: []string{"shell", "-c"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeepCopyRunConfig(t *testing.T) {
|
|
|
|
runConfig := fullMutableRunConfig()
|
|
|
|
copy := copyRunConfig(runConfig)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(fullMutableRunConfig(), copy))
|
2017-11-09 16:20:51 -05:00
|
|
|
|
|
|
|
copy.Cmd[1] = "arg2"
|
|
|
|
copy.Env[1] = "env2=new"
|
|
|
|
copy.ExposedPorts["10002"] = struct{}{}
|
|
|
|
copy.Volumes["three"] = struct{}{}
|
|
|
|
copy.Entrypoint[1] = "arg2"
|
|
|
|
copy.OnBuild[0] = "start"
|
|
|
|
copy.Labels["label3"] = "value3"
|
|
|
|
copy.Shell[0] = "sh"
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(fullMutableRunConfig(), runConfig))
|
2017-11-09 16:20:51 -05:00
|
|
|
}
|
2019-04-26 18:12:43 -04:00
|
|
|
|
|
|
|
type MockRWLayer struct{}
|
|
|
|
|
2022-09-23 14:21:31 -04:00
|
|
|
func (l *MockRWLayer) Release() error { return nil }
|
|
|
|
func (l *MockRWLayer) Root() string { return "" }
|
2019-04-26 18:12:43 -04:00
|
|
|
func (l *MockRWLayer) Commit() (builder.ROLayer, error) {
|
|
|
|
return &MockROLayer{
|
|
|
|
diffID: layer.DiffID(digest.Digest("sha256:1234")),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type MockROLayer struct {
|
|
|
|
diffID layer.DiffID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *MockROLayer) Release() error { return nil }
|
|
|
|
func (l *MockROLayer) NewRWLayer() (builder.RWLayer, error) { return nil, nil }
|
|
|
|
func (l *MockROLayer) DiffID() layer.DiffID { return l.diffID }
|
|
|
|
|
|
|
|
func getMockBuildBackend() builder.Backend {
|
|
|
|
return &MockBackend{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExportImage(t *testing.T) {
|
|
|
|
ds := newDispatchState(NewBuildArgs(map[string]*string{}))
|
|
|
|
layer := &MockRWLayer{}
|
|
|
|
parentImage := &image.Image{
|
|
|
|
V1Image: image.V1Image{
|
|
|
|
OS: "linux",
|
|
|
|
Architecture: "arm64",
|
|
|
|
Variant: "v8",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
runConfig := &container.Config{}
|
|
|
|
|
|
|
|
b := &Builder{
|
|
|
|
imageSources: getMockImageSource(nil, nil, nil),
|
|
|
|
docker: getMockBuildBackend(),
|
|
|
|
}
|
|
|
|
err := b.exportImage(ds, layer, parentImage, runConfig)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|