2016-03-05 20:35:06 +00:00
|
|
|
package builder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2016-05-03 09:10:51 +00:00
|
|
|
"sort"
|
2016-03-05 20:35:06 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-05-03 09:10:51 +00:00
|
|
|
const shouldStayFilename = "should_stay"
|
2016-03-05 20:35:06 +00:00
|
|
|
|
2016-05-03 09:10:51 +00:00
|
|
|
func extractFilenames(files []os.FileInfo) []string {
|
|
|
|
filenames := make([]string, len(files), len(files))
|
|
|
|
|
|
|
|
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) {
|
|
|
|
files, err := ioutil.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) {
|
2016-03-05 20:35:06 +00:00
|
|
|
modifiableCtx := &tarSumContext{root: contextDir}
|
|
|
|
ctx := DockerIgnoreContext{ModifiableContext: modifiableCtx}
|
|
|
|
|
2016-05-03 09:10:51 +00:00
|
|
|
err := ctx.Process([]string{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)
|
|
|
|
createTestTempFile(t, contextDir, 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)
|
|
|
|
createTestTempFile(t, contextDir, 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, 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)
|
|
|
|
createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777)
|
|
|
|
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
|
|
|
|
2016-05-03 09:10:51 +00:00
|
|
|
checkDirectory(t, contextDir, []string{shouldStayFilename, DefaultDockerfileName, dockerignoreFilename})
|
2016-03-05 20:35:06 +00:00
|
|
|
|
|
|
|
}
|