cf2611f3 |
package dockerfile
import ( |
830584b0 |
"fmt" |
fe7b4d8f |
"runtime" |
cf2611f3 |
"testing"
|
0296797f |
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/backend" |
0d9e66b9 |
"github.com/docker/docker/api/types/container" |
cf2611f3 |
"github.com/docker/docker/builder" |
d1faf3df |
"github.com/docker/docker/builder/remotecontext" |
cf2611f3 |
"github.com/docker/docker/pkg/archive" |
9bcd5d25 |
"github.com/docker/go-connections/nat" |
6052f2b3 |
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" |
cf2611f3 |
)
func TestEmptyDockerfile(t *testing.T) {
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
defer cleanup()
createTestTempFile(t, contextDir, builder.DefaultDockerfileName, "", 0777)
|
d1faf3df |
readAndCheckDockerfile(t, "emptyDockerfile", contextDir, "", "the Dockerfile (Dockerfile) cannot be empty") |
830584b0 |
}
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)
}
|
9408b205 |
func TestDockerfileOutsideTheBuildContext(t *testing.T) {
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
defer cleanup() |
cf2611f3 |
|
6052f2b3 |
expectedError := "Forbidden path outside the build context: ../../Dockerfile ()" |
cf2611f3 |
|
9408b205 |
readAndCheckDockerfile(t, "DockerfileOutsideTheBuildContext", contextDir, "../../Dockerfile", expectedError) |
cf2611f3 |
} |
fb175bb3 |
|
9408b205 |
func TestNonExistingDockerfile(t *testing.T) { |
fb175bb3 |
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
defer cleanup()
|
9408b205 |
expectedError := "Cannot locate specified Dockerfile: Dockerfile"
readAndCheckDockerfile(t, "NonExistingDockerfile", contextDir, "Dockerfile", expectedError)
}
func readAndCheckDockerfile(t *testing.T, testName, contextDir, dockerfilePath, expectedError string) { |
fb175bb3 |
tarStream, err := archive.Tar(contextDir, archive.Uncompressed) |
6052f2b3 |
require.NoError(t, err) |
fb175bb3 |
defer func() {
if err = tarStream.Close(); err != nil {
t.Fatalf("Error when closing tar stream: %s", err)
}
}()
|
d1faf3df |
if dockerfilePath == "" { // handled in BuildWithContext
dockerfilePath = builder.DefaultDockerfileName |
fb175bb3 |
}
|
0296797f |
config := backend.BuildConfig{
Options: &types.ImageBuildOptions{Dockerfile: dockerfilePath},
Source: tarStream,
}
_, _, err = remotecontext.Detect(config) |
6052f2b3 |
assert.EqualError(t, err, expectedError) |
fb175bb3 |
} |
0d9e66b9 |
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", |
fe7b4d8f |
modifiers: []runConfigModifier{withCmdComment("comment", runtime.GOOS)}, |
0d9e66b9 |
expected: &container.Config{ |
0380fbff |
Cmd: append(defaultShellForOS(runtime.GOOS), "#(nop) ", "comment"), |
0d9e66b9 |
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...)
assert.Equal(t, testcase.expected, runConfigCopy, testcase.doc)
// Assert the original was not modified
assert.NotEqual(t, runConfig, runConfigCopy, testcase.doc)
}
} |
19a29f6f |
|
9bcd5d25 |
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)
assert.Equal(t, fullMutableRunConfig(), copy)
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"
assert.Equal(t, fullMutableRunConfig(), runConfig)
} |