1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/builder/dockerignore_test.go
Tomasz Kopczynski ab2baf08f0 Builder unit tests refactoring
Signed-off-by: Tomasz Kopczynski <tomek@kopczynski.net.pl>
2016-05-03 19:00:35 +02:00

95 lines
2.6 KiB
Go

package builder
import (
"io/ioutil"
"log"
"os"
"sort"
"testing"
)
const shouldStayFilename = "should_stay"
func extractFilenames(files []os.FileInfo) []string {
filenames := make([]string, len(files), len(files))
for i, file := range files {
filenames[i] = file.Name()
}
return filenames
}
func checkDirectory(t *testing.T, dir string, expectedFiles []string) {
files, err := ioutil.ReadDir(dir)
if err != nil {
t.Fatalf("Could not read directory: %s", err)
}
if len(files) != len(expectedFiles) {
log.Fatalf("Directory should contain exactly %d file(s), got %d", len(expectedFiles), len(files))
}
filenames := extractFilenames(files)
sort.Strings(filenames)
sort.Strings(expectedFiles)
for i, filename := range filenames {
if filename != expectedFiles[i] {
t.Fatalf("File %s should be in the directory, got: %s", expectedFiles[i], filename)
}
}
}
func executeProcess(t *testing.T, contextDir string) {
modifiableCtx := &tarSumContext{root: contextDir}
ctx := DockerIgnoreContext{ModifiableContext: modifiableCtx}
err := ctx.Process([]string{DefaultDockerfileName})
if err != nil {
t.Fatalf("Error when executing Process: %s", err)
}
}
func TestProcessShouldRemoveDockerfileDockerignore(t *testing.T) {
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerignore-process-test")
defer cleanup()
createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777)
createTestTempFile(t, contextDir, dockerignoreFilename, "Dockerfile\n.dockerignore", 0777)
createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777)
executeProcess(t, contextDir)
checkDirectory(t, contextDir, []string{shouldStayFilename})
}
func TestProcessNoDockerignore(t *testing.T) {
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerignore-process-test")
defer cleanup()
createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777)
createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777)
executeProcess(t, contextDir)
checkDirectory(t, contextDir, []string{shouldStayFilename, DefaultDockerfileName})
}
func TestProcessShouldLeaveAllFiles(t *testing.T) {
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerignore-process-test")
defer cleanup()
createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777)
createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777)
createTestTempFile(t, contextDir, dockerignoreFilename, "input1\ninput2", 0777)
executeProcess(t, contextDir)
checkDirectory(t, contextDir, []string{shouldStayFilename, DefaultDockerfileName, dockerignoreFilename})
}