2016-05-21 12:33:16 -04:00
|
|
|
package dockerfile
|
|
|
|
|
|
|
|
import (
|
2016-05-29 16:28:24 -04:00
|
|
|
"fmt"
|
2016-05-21 12:33:16 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-05-21 12:33:16 -04:00
|
|
|
"github.com/docker/docker/builder"
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEmptyDockerfile(t *testing.T) {
|
|
|
|
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
createTestTempFile(t, contextDir, builder.DefaultDockerfileName, "", 0777)
|
|
|
|
|
2016-05-29 16:28:24 -04:00
|
|
|
readAndCheckDockerfile(t, "emptyDockefile", contextDir, "", "The Dockerfile (Dockerfile) cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-06-26 16:01:28 -04:00
|
|
|
expectedError := "Forbidden path outside the build context"
|
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) {
|
2016-05-29 12:56:49 -04:00
|
|
|
tarStream, err := archive.Tar(contextDir, archive.Uncompressed)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when creating tar stream: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err = tarStream.Close(); err != nil {
|
|
|
|
t.Fatalf("Error when closing tar stream: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
context, err := builder.MakeTarSumContext(tarStream)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when creating tar context: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err = context.Close(); err != nil {
|
|
|
|
t.Fatalf("Error when closing tar context: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
options := &types.ImageBuildOptions{
|
2016-06-26 16:01:28 -04:00
|
|
|
Dockerfile: dockerfilePath,
|
2016-05-29 12:56:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
b := &Builder{options: options, context: context}
|
|
|
|
|
|
|
|
err = b.readDockerfile()
|
|
|
|
|
|
|
|
if err == nil {
|
2016-06-26 16:01:28 -04:00
|
|
|
t.Fatalf("No error when executing test: %s", testName)
|
2016-05-29 12:56:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(err.Error(), expectedError) {
|
|
|
|
t.Fatalf("Wrong error message. Should be \"%s\". Got \"%s\"", expectedError, err.Error())
|
|
|
|
}
|
|
|
|
}
|