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"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/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()
|
|
|
|
}
|
|
|
|
|
2019-08-27 11:26:15 -04:00
|
|
|
func TestDispatch(t *testing.T) {
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
skip.If(t, os.Getuid() != 0, "skipping test that requires root")
|
|
|
|
}
|
|
|
|
testCases := []dispatchTestCase{
|
2017-05-22 11:21:17 -04:00
|
|
|
{
|
|
|
|
name: "ADD multiple files to file",
|
|
|
|
cmd: &instructions.AddCommand{SourcesAndDest: instructions.SourcesAndDest{
|
2021-06-05 15:56:40 -04:00
|
|
|
SourcePaths: []string{"file1.txt", "file2.txt"},
|
|
|
|
DestPath: "test",
|
2017-05-22 11:21:17 -04:00
|
|
|
}},
|
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{
|
2021-06-05 15:56:40 -04:00
|
|
|
SourcePaths: []string{"file*.txt"},
|
|
|
|
DestPath: "test",
|
2017-05-22 11:21:17 -04:00
|
|
|
}},
|
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{
|
2021-06-05 15:56:40 -04:00
|
|
|
SourcePaths: []string{"file1.txt", "file2.txt"},
|
|
|
|
DestPath: "test",
|
2017-05-22 11:21:17 -04:00
|
|
|
}},
|
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{
|
2021-06-05 15:56:40 -04:00
|
|
|
SourcePaths: []string{"test file1.txt", "test file2.txt"},
|
|
|
|
DestPath: "test",
|
2017-05-22 11:21:17 -04:00
|
|
|
}},
|
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{
|
2021-06-05 15:56:40 -04:00
|
|
|
SourcePaths: []string{"test file1.txt", "test file2.txt"},
|
|
|
|
DestPath: "test",
|
2017-05-22 11:21:17 -04:00
|
|
|
}},
|
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{
|
2021-06-05 15:56:40 -04:00
|
|
|
SourcePaths: []string{"file*.txt"},
|
|
|
|
DestPath: "/tmp/",
|
2017-05-22 11:21:17 -04:00
|
|
|
}},
|
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{
|
2021-06-05 15:56:40 -04:00
|
|
|
SourcePaths: []string{"https://index.docker.io/robots.txt"},
|
|
|
|
DestPath: "/",
|
2017-05-22 11:21:17 -04:00
|
|
|
}},
|
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,
|
2019-08-27 11:26:15 -04:00
|
|
|
},
|
2018-03-08 12:53:27 -05:00
|
|
|
}
|
2016-05-08 13:22:06 -04:00
|
|
|
|
2019-08-27 11:26:15 -04:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
|
|
|
|
defer cleanup()
|
2016-05-08 13:22:06 -04:00
|
|
|
|
2019-08-27 11:26:15 -04:00
|
|
|
for filename, content := range tc.files {
|
|
|
|
createTestTempFile(t, contextDir, filename, content, 0777)
|
|
|
|
}
|
2016-05-08 13:22:06 -04:00
|
|
|
|
2019-08-27 11:26:15 -04:00
|
|
|
tarStream, err := archive.Tar(contextDir, archive.Uncompressed)
|
2016-05-21 12:33:16 -04:00
|
|
|
|
2019-08-27 11:26:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when creating tar stream: %s", err)
|
|
|
|
}
|
2016-05-08 13:22:06 -04:00
|
|
|
|
2019-08-27 11:26:15 -04:00
|
|
|
defer func() {
|
|
|
|
if err = tarStream.Close(); err != nil {
|
|
|
|
t.Fatalf("Error when closing tar stream: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
2016-05-08 13:22:06 -04:00
|
|
|
|
2019-08-27 11:26:15 -04:00
|
|
|
context, err := remotecontext.FromArchive(tarStream)
|
2016-05-08 13:22:06 -04:00
|
|
|
|
2019-08-27 11:26:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when creating tar context: %s", err)
|
|
|
|
}
|
2016-05-08 13:22:06 -04:00
|
|
|
|
2019-08-27 11:26:15 -04:00
|
|
|
defer func() {
|
|
|
|
if err = context.Close(); err != nil {
|
|
|
|
t.Fatalf("Error when closing tar context: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
2016-05-08 13:22:06 -04:00
|
|
|
|
2019-08-27 11:26:15 -04:00
|
|
|
b := newBuilderWithMockBackend()
|
|
|
|
sb := newDispatchRequest(b, '`', context, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
|
|
|
err = dispatch(sb, tc.cmd)
|
|
|
|
assert.Check(t, is.ErrorContains(err, tc.expectedError))
|
|
|
|
})
|
|
|
|
}
|
2016-05-08 13:22:06 -04:00
|
|
|
}
|