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