2018-02-05 16:05:59 -05:00
|
|
|
package remotecontext // import "github.com/docker/docker/builder/remotecontext"
|
2016-03-05 15:35:06 -05:00
|
|
|
|
|
|
|
import (
|
2017-03-20 18:22:29 -04:00
|
|
|
"errors"
|
2016-03-05 15:35:06 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2016-05-03 05:10:51 -04:00
|
|
|
"sort"
|
2016-03-05 15:35:06 -05:00
|
|
|
"testing"
|
2017-03-20 18:22:29 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/builder"
|
2017-08-03 20:22:00 -04:00
|
|
|
"github.com/docker/docker/pkg/containerfs"
|
2017-03-20 18:22:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dockerfileContents = "FROM busybox"
|
|
|
|
dockerignoreFilename = ".dockerignore"
|
|
|
|
testfileContents = "test"
|
2016-03-05 15:35:06 -05:00
|
|
|
)
|
|
|
|
|
2016-05-03 05:10:51 -04:00
|
|
|
const shouldStayFilename = "should_stay"
|
2016-03-05 15:35:06 -05:00
|
|
|
|
2016-05-03 05:10:51 -04:00
|
|
|
func extractFilenames(files []os.FileInfo) []string {
|
2017-09-11 14:55:05 -04:00
|
|
|
filenames := make([]string, len(files))
|
2016-05-03 05:10:51 -04:00
|
|
|
|
|
|
|
for i, file := range files {
|
|
|
|
filenames[i] = file.Name()
|
2016-03-05 15:35:06 -05:00
|
|
|
}
|
|
|
|
|
2016-05-03 05:10:51 -04:00
|
|
|
return filenames
|
|
|
|
}
|
2016-03-05 15:35:06 -05:00
|
|
|
|
2016-05-03 05:10:51 -04:00
|
|
|
func checkDirectory(t *testing.T, dir string, expectedFiles []string) {
|
|
|
|
files, err := ioutil.ReadDir(dir)
|
2016-03-05 15:35:06 -05:00
|
|
|
|
|
|
|
if err != nil {
|
2016-05-03 05:10:51 -04:00
|
|
|
t.Fatalf("Could not read directory: %s", err)
|
2016-03-05 15:35:06 -05:00
|
|
|
}
|
|
|
|
|
2016-05-03 05:10:51 -04:00
|
|
|
if len(files) != len(expectedFiles) {
|
|
|
|
log.Fatalf("Directory should contain exactly %d file(s), got %d", len(expectedFiles), len(files))
|
2016-03-05 15:35:06 -05:00
|
|
|
}
|
|
|
|
|
2016-05-03 05:10:51 -04:00
|
|
|
filenames := extractFilenames(files)
|
|
|
|
sort.Strings(filenames)
|
|
|
|
sort.Strings(expectedFiles)
|
2016-03-05 15:35:06 -05:00
|
|
|
|
2016-05-03 05:10:51 -04:00
|
|
|
for i, filename := range filenames {
|
|
|
|
if filename != expectedFiles[i] {
|
|
|
|
t.Fatalf("File %s should be in the directory, got: %s", expectedFiles[i], filename)
|
|
|
|
}
|
2016-03-05 15:35:06 -05:00
|
|
|
}
|
2016-05-03 05:10:51 -04:00
|
|
|
}
|
2016-03-05 15:35:06 -05:00
|
|
|
|
2016-05-03 05:10:51 -04:00
|
|
|
func executeProcess(t *testing.T, contextDir string) {
|
2017-08-03 20:22:00 -04:00
|
|
|
modifiableCtx := &stubRemote{root: containerfs.NewLocalContainerFS(contextDir)}
|
2016-03-05 15:35:06 -05:00
|
|
|
|
2017-03-20 18:22:29 -04:00
|
|
|
err := removeDockerfile(modifiableCtx, builder.DefaultDockerfileName)
|
2016-03-05 15:35:06 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when executing Process: %s", err)
|
|
|
|
}
|
2016-05-03 05:10:51 -04:00
|
|
|
}
|
2016-03-05 15:35:06 -05:00
|
|
|
|
2016-05-03 05:10:51 -04:00
|
|
|
func TestProcessShouldRemoveDockerfileDockerignore(t *testing.T) {
|
|
|
|
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerignore-process-test")
|
|
|
|
defer cleanup()
|
2016-03-05 15:35:06 -05:00
|
|
|
|
2016-05-03 05:10:51 -04:00
|
|
|
createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777)
|
|
|
|
createTestTempFile(t, contextDir, dockerignoreFilename, "Dockerfile\n.dockerignore", 0777)
|
2017-03-20 18:22:29 -04:00
|
|
|
createTestTempFile(t, contextDir, builder.DefaultDockerfileName, dockerfileContents, 0777)
|
2016-03-05 15:35:06 -05:00
|
|
|
|
2016-05-03 05:10:51 -04:00
|
|
|
executeProcess(t, contextDir)
|
2016-03-05 15:35:06 -05:00
|
|
|
|
2016-05-03 05:10:51 -04:00
|
|
|
checkDirectory(t, contextDir, []string{shouldStayFilename})
|
2016-03-05 15:35:06 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessNoDockerignore(t *testing.T) {
|
2016-05-03 05:10:51 -04:00
|
|
|
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerignore-process-test")
|
|
|
|
defer cleanup()
|
2016-03-05 15:35:06 -05:00
|
|
|
|
2016-05-03 05:10:51 -04:00
|
|
|
createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777)
|
2017-03-20 18:22:29 -04:00
|
|
|
createTestTempFile(t, contextDir, builder.DefaultDockerfileName, dockerfileContents, 0777)
|
2016-03-05 15:35:06 -05:00
|
|
|
|
2016-05-03 05:10:51 -04:00
|
|
|
executeProcess(t, contextDir)
|
2016-03-05 15:35:06 -05:00
|
|
|
|
2017-03-20 18:22:29 -04:00
|
|
|
checkDirectory(t, contextDir, []string{shouldStayFilename, builder.DefaultDockerfileName})
|
2016-03-05 15:35:06 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessShouldLeaveAllFiles(t *testing.T) {
|
2016-05-03 05:10:51 -04:00
|
|
|
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerignore-process-test")
|
|
|
|
defer cleanup()
|
2016-03-05 15:35:06 -05:00
|
|
|
|
2016-05-03 05:10:51 -04:00
|
|
|
createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777)
|
2017-03-20 18:22:29 -04:00
|
|
|
createTestTempFile(t, contextDir, builder.DefaultDockerfileName, dockerfileContents, 0777)
|
2016-05-03 05:10:51 -04:00
|
|
|
createTestTempFile(t, contextDir, dockerignoreFilename, "input1\ninput2", 0777)
|
2016-03-05 15:35:06 -05:00
|
|
|
|
2016-05-03 05:10:51 -04:00
|
|
|
executeProcess(t, contextDir)
|
2016-03-05 15:35:06 -05:00
|
|
|
|
2017-03-20 18:22:29 -04:00
|
|
|
checkDirectory(t, contextDir, []string{shouldStayFilename, builder.DefaultDockerfileName, dockerignoreFilename})
|
2016-03-05 15:35:06 -05:00
|
|
|
|
|
|
|
}
|
2017-03-20 18:22:29 -04:00
|
|
|
|
|
|
|
// TODO: remove after moving to a separate pkg
|
|
|
|
type stubRemote struct {
|
2017-08-03 20:22:00 -04:00
|
|
|
root containerfs.ContainerFS
|
2017-03-20 18:22:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *stubRemote) Hash(path string) (string, error) {
|
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
func (r *stubRemote) Root() containerfs.ContainerFS {
|
2017-03-20 18:22:29 -04:00
|
|
|
return r.root
|
|
|
|
}
|
|
|
|
func (r *stubRemote) Close() error {
|
|
|
|
return errors.New("not implemented")
|
|
|
|
}
|
|
|
|
func (r *stubRemote) Remove(p string) error {
|
2017-08-03 20:22:00 -04:00
|
|
|
return r.root.Remove(r.root.Join(r.root.Path(), p))
|
2017-03-20 18:22:29 -04:00
|
|
|
}
|