2018-02-05 16:05:59 -05:00
|
|
|
package dockerfile // import "github.com/docker/docker/builder/dockerfile"
|
2016-05-08 13:22:06 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-20 05:59:08 -04:00
|
|
|
"os"
|
2018-03-08 12:53:27 -05:00
|
|
|
"runtime"
|
2016-05-08 13:22:06 -04:00
|
|
|
"testing"
|
|
|
|
|
2017-03-20 18:22:29 -04:00
|
|
|
"github.com/docker/docker/builder/remotecontext"
|
2016-05-08 13:22:06 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2018-06-02 12:46:53 -04:00
|
|
|
"github.com/moby/buildkit/frontend/dockerfile/instructions"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
|
|
|
"gotest.tools/skip"
|
2016-05-08 13:22:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type dispatchTestCase struct {
|
2017-05-22 11:21:17 -04:00
|
|
|
name, expectedError string
|
|
|
|
cmd instructions.Command
|
|
|
|
files map[string]string
|
2016-05-08 13:22:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
reexec.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func initDispatchTestCases() []dispatchTestCase {
|
2017-05-22 11:21:17 -04:00
|
|
|
dispatchTestCases := []dispatchTestCase{
|
|
|
|
{
|
|
|
|
name: "ADD multiple files to file",
|
|
|
|
cmd: &instructions.AddCommand{SourcesAndDest: instructions.SourcesAndDest{
|
|
|
|
"file1.txt",
|
|
|
|
"file2.txt",
|
|
|
|
"test",
|
|
|
|
}},
|
2016-05-21 12:33:16 -04:00
|
|
|
expectedError: "When using ADD with more than one source file, the destination must be a directory and end with a /",
|
|
|
|
files: map[string]string{"file1.txt": "test1", "file2.txt": "test2"},
|
|
|
|
},
|
|
|
|
{
|
2017-05-22 11:21:17 -04:00
|
|
|
name: "Wildcard ADD multiple files to file",
|
|
|
|
cmd: &instructions.AddCommand{SourcesAndDest: instructions.SourcesAndDest{
|
|
|
|
"file*.txt",
|
|
|
|
"test",
|
|
|
|
}},
|
2016-05-21 12:33:16 -04:00
|
|
|
expectedError: "When using ADD with more than one source file, the destination must be a directory and end with a /",
|
|
|
|
files: map[string]string{"file1.txt": "test1", "file2.txt": "test2"},
|
|
|
|
},
|
|
|
|
{
|
2017-05-22 11:21:17 -04:00
|
|
|
name: "COPY multiple files to file",
|
|
|
|
cmd: &instructions.CopyCommand{SourcesAndDest: instructions.SourcesAndDest{
|
|
|
|
"file1.txt",
|
|
|
|
"file2.txt",
|
|
|
|
"test",
|
|
|
|
}},
|
2016-05-21 12:33:16 -04:00
|
|
|
expectedError: "When using COPY with more than one source file, the destination must be a directory and end with a /",
|
|
|
|
files: map[string]string{"file1.txt": "test1", "file2.txt": "test2"},
|
|
|
|
},
|
|
|
|
{
|
2017-05-22 11:21:17 -04:00
|
|
|
name: "ADD multiple files to file with whitespace",
|
|
|
|
cmd: &instructions.AddCommand{SourcesAndDest: instructions.SourcesAndDest{
|
|
|
|
"test file1.txt",
|
|
|
|
"test file2.txt",
|
|
|
|
"test",
|
|
|
|
}},
|
2016-05-21 12:33:16 -04:00
|
|
|
expectedError: "When using ADD with more than one source file, the destination must be a directory and end with a /",
|
|
|
|
files: map[string]string{"test file1.txt": "test1", "test file2.txt": "test2"},
|
|
|
|
},
|
|
|
|
{
|
2017-05-22 11:21:17 -04:00
|
|
|
name: "COPY multiple files to file with whitespace",
|
|
|
|
cmd: &instructions.CopyCommand{SourcesAndDest: instructions.SourcesAndDest{
|
|
|
|
"test file1.txt",
|
|
|
|
"test file2.txt",
|
|
|
|
"test",
|
|
|
|
}},
|
2016-05-21 12:33:16 -04:00
|
|
|
expectedError: "When using COPY with more than one source file, the destination must be a directory and end with a /",
|
|
|
|
files: map[string]string{"test file1.txt": "test1", "test file2.txt": "test2"},
|
|
|
|
},
|
|
|
|
{
|
2017-05-22 11:21:17 -04:00
|
|
|
name: "COPY wildcard no files",
|
|
|
|
cmd: &instructions.CopyCommand{SourcesAndDest: instructions.SourcesAndDest{
|
|
|
|
"file*.txt",
|
|
|
|
"/tmp/",
|
|
|
|
}},
|
2017-05-07 14:37:46 -04:00
|
|
|
expectedError: "COPY failed: no source files were specified",
|
2016-05-21 12:33:16 -04:00
|
|
|
files: nil,
|
|
|
|
},
|
|
|
|
{
|
2017-05-22 11:21:17 -04:00
|
|
|
name: "COPY url",
|
|
|
|
cmd: &instructions.CopyCommand{SourcesAndDest: instructions.SourcesAndDest{
|
|
|
|
"https://index.docker.io/robots.txt",
|
|
|
|
"/",
|
|
|
|
}},
|
2017-05-07 14:37:46 -04:00
|
|
|
expectedError: "source can't be a URL for COPY",
|
2016-05-21 12:33:16 -04:00
|
|
|
files: nil,
|
2016-05-08 13:22:06 -04:00
|
|
|
}}
|
|
|
|
|
|
|
|
return dispatchTestCases
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDispatch(t *testing.T) {
|
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-08 13:22:06 -04:00
|
|
|
testCases := initDispatchTestCases()
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
executeTestCase(t, testCase)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func executeTestCase(t *testing.T, testCase dispatchTestCase) {
|
|
|
|
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
|
|
|
|
defer cleanup()
|
|
|
|
|
2016-05-21 12:33:16 -04:00
|
|
|
for filename, content := range testCase.files {
|
|
|
|
createTestTempFile(t, contextDir, filename, content, 0777)
|
|
|
|
}
|
|
|
|
|
2016-05-08 13:22:06 -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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
context, err := remotecontext.FromArchive(tarStream)
|
2016-05-08 13:22:06 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
2018-05-07 13:49:13 -04:00
|
|
|
sb := newDispatchRequest(b, '`', context, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
2017-05-22 11:21:17 -04:00
|
|
|
err = dispatch(sb, testCase.cmd)
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, testCase.expectedError))
|
2016-05-08 13:22:06 -04:00
|
|
|
}
|