Merge pull request #22485 from tkopczynski/test-utils

Builder unit tests refactoring
This commit is contained in:
Vincent Demeester 2016-05-06 22:05:02 +02:00
commit 16d0a89593
5 changed files with 234 additions and 444 deletions

View file

@ -5,7 +5,6 @@ import (
"bytes" "bytes"
"io" "io"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
@ -14,46 +13,24 @@ import (
"github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/archive"
) )
const ( var prepareEmpty = func(t *testing.T) (string, func()) {
dockerfileTestName = "Dockerfile-test" return "", func() {}
dockerfileContent = "FROM busybox"
)
var prepareEmpty = func(t *testing.T) string {
return ""
} }
var prepareNoFiles = func(t *testing.T) string { var prepareNoFiles = func(t *testing.T) (string, func()) {
contextDir, err := ioutil.TempDir("", "builder-context-test") return createTestTempDir(t, "", "builder-context-test")
if err != nil {
t.Fatalf("Error when creating temporary directory: %s", err)
}
return contextDir
} }
var prepareOneFile = func(t *testing.T) string { var prepareOneFile = func(t *testing.T) (string, func()) {
contextDir, err := ioutil.TempDir("", "builder-context-test") contextDir, cleanup := createTestTempDir(t, "", "builder-context-test")
createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777)
if err != nil { return contextDir, cleanup
t.Fatalf("Error when creating temporary directory: %s", err)
}
dockerfileFilename := filepath.Join(contextDir, dockerfileTestName)
err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777)
if err != nil {
t.Fatalf("Error with writing to file: %s", err)
}
return contextDir
} }
func testValidateContextDirectory(t *testing.T, prepare func(t *testing.T) string, excludes []string) { func testValidateContextDirectory(t *testing.T, prepare func(t *testing.T) (string, func()), excludes []string) {
contextDir := prepare(t) contextDir, cleanup := prepare(t)
defer cleanup()
defer os.RemoveAll(contextDir)
err := ValidateContextDirectory(contextDir, excludes) err := ValidateContextDirectory(contextDir, excludes)
if err != nil { if err != nil {
@ -62,13 +39,8 @@ func testValidateContextDirectory(t *testing.T, prepare func(t *testing.T) strin
} }
func TestGetContextFromLocalDirNoDockerfile(t *testing.T) { func TestGetContextFromLocalDirNoDockerfile(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-context-test") contextDir, cleanup := createTestTempDir(t, "", "builder-context-test")
defer cleanup()
defer os.RemoveAll(contextDir)
if err != nil {
t.Fatalf("Error with creating temporary directory: %s", err)
}
absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, "") absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, "")
@ -86,13 +58,8 @@ func TestGetContextFromLocalDirNoDockerfile(t *testing.T) {
} }
func TestGetContextFromLocalDirNotExistingDir(t *testing.T) { func TestGetContextFromLocalDirNotExistingDir(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-context-test") contextDir, cleanup := createTestTempDir(t, "", "builder-context-test")
defer cleanup()
if err != nil {
t.Fatalf("Error with creating temporary directory: %s", err)
}
defer os.RemoveAll(contextDir)
fakePath := filepath.Join(contextDir, "fake") fakePath := filepath.Join(contextDir, "fake")
@ -112,13 +79,8 @@ func TestGetContextFromLocalDirNotExistingDir(t *testing.T) {
} }
func TestGetContextFromLocalDirNotExistingDockerfile(t *testing.T) { func TestGetContextFromLocalDirNotExistingDockerfile(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-context-test") contextDir, cleanup := createTestTempDir(t, "", "builder-context-test")
defer cleanup()
if err != nil {
t.Fatalf("Error with creating temporary directory: %s", err)
}
defer os.RemoveAll(contextDir)
fakePath := filepath.Join(contextDir, "fake") fakePath := filepath.Join(contextDir, "fake")
@ -138,34 +100,14 @@ func TestGetContextFromLocalDirNotExistingDockerfile(t *testing.T) {
} }
func TestGetContextFromLocalDirWithNoDirectory(t *testing.T) { func TestGetContextFromLocalDirWithNoDirectory(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-context-test") contextDir, dirCleanup := createTestTempDir(t, "", "builder-context-test")
defer dirCleanup()
if err != nil { createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777)
t.Fatalf("Error with creating temporary directory: %s", err)
}
defer os.RemoveAll(contextDir) chdirCleanup := chdir(t, contextDir)
defer chdirCleanup()
dockerfileFilename := filepath.Join(contextDir, DefaultDockerfileName)
err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777)
if err != nil {
t.Fatalf("Error when writing file (%s) contents: %s", dockerfileFilename, err)
}
workingDirectory, err := os.Getwd()
if err != nil {
t.Fatalf("Error when retrieving working directory: %s", err)
}
defer os.Chdir(workingDirectory)
err = os.Chdir(contextDir)
if err != nil {
t.Fatalf("Error when changing directory to %s: %s", contextDir, err)
}
absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, "") absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, "")
if err != nil { if err != nil {
@ -182,20 +124,10 @@ func TestGetContextFromLocalDirWithNoDirectory(t *testing.T) {
} }
func TestGetContextFromLocalDirWithDockerfile(t *testing.T) { func TestGetContextFromLocalDirWithDockerfile(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-context-test") contextDir, cleanup := createTestTempDir(t, "", "builder-context-test")
defer cleanup()
if err != nil { createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777)
t.Fatalf("Error with creating temporary directory: %s", err)
}
defer os.RemoveAll(contextDir)
dockerfileFilename := filepath.Join(contextDir, DefaultDockerfileName)
err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777)
if err != nil {
t.Fatalf("Error when writing file (%s) contents: %s", dockerfileFilename, err)
}
absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, "") absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, "")
@ -213,28 +145,11 @@ func TestGetContextFromLocalDirWithDockerfile(t *testing.T) {
} }
func TestGetContextFromLocalDirLocalFile(t *testing.T) { func TestGetContextFromLocalDirLocalFile(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-context-test") contextDir, cleanup := createTestTempDir(t, "", "builder-context-test")
defer cleanup()
if err != nil { createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777)
t.Fatalf("Error with creating temporary directory: %s", err) testFilename := createTestTempFile(t, contextDir, "tmpTest", "test", 0777)
}
defer os.RemoveAll(contextDir)
dockerfileFilename := filepath.Join(contextDir, DefaultDockerfileName)
err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777)
if err != nil {
t.Fatalf("Error when writing file (%s) contents: %s", dockerfileFilename, err)
}
testFilename := filepath.Join(contextDir, "tmpTest")
testContent := "test"
err = ioutil.WriteFile(testFilename, []byte(testContent), 0777)
if err != nil {
t.Fatalf("Error when writing file (%s) contents: %s", testFilename, err)
}
absContextDir, relDockerfile, err := GetContextFromLocalDir(testFilename, "") absContextDir, relDockerfile, err := GetContextFromLocalDir(testFilename, "")
@ -252,36 +167,15 @@ func TestGetContextFromLocalDirLocalFile(t *testing.T) {
} }
func TestGetContextFromLocalDirWithCustomDockerfile(t *testing.T) { func TestGetContextFromLocalDirWithCustomDockerfile(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-context-test") contextDir, cleanup := createTestTempDir(t, "", "builder-context-test")
defer cleanup()
if err != nil { chdirCleanup := chdir(t, contextDir)
t.Fatalf("Error with creating temporary directory: %s", err) defer chdirCleanup()
}
defer os.RemoveAll(contextDir) createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777)
workingDirectory, err := os.Getwd() absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, DefaultDockerfileName)
if err != nil {
t.Fatalf("Error when retrieving working directory: %s", err)
}
defer os.Chdir(workingDirectory)
err = os.Chdir(contextDir)
if err != nil {
t.Fatalf("Error when changing directory to %s: %s", contextDir, err)
}
dockerfileFilename := filepath.Join(contextDir, dockerfileTestName)
err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777)
if err != nil {
t.Fatalf("Error when writing file (%s) contents: %s", dockerfileFilename, err)
}
absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, dockerfileTestName)
if err != nil { if err != nil {
t.Fatalf("Error when getting context from local dir: %s", err) t.Fatalf("Error when getting context from local dir: %s", err)
@ -291,14 +185,14 @@ func TestGetContextFromLocalDirWithCustomDockerfile(t *testing.T) {
t.Fatalf("Absolute directory path should be equal to %s, got: %s", contextDir, absContextDir) t.Fatalf("Absolute directory path should be equal to %s, got: %s", contextDir, absContextDir)
} }
if relDockerfile != dockerfileTestName { if relDockerfile != DefaultDockerfileName {
t.Fatalf("Relative path to dockerfile should be equal to %s, got: %s", dockerfileTestName, relDockerfile) t.Fatalf("Relative path to dockerfile should be equal to %s, got: %s", DefaultDockerfileName, relDockerfile)
} }
} }
func TestGetContextFromReaderString(t *testing.T) { func TestGetContextFromReaderString(t *testing.T) {
tarArchive, relDockerfile, err := GetContextFromReader(ioutil.NopCloser(strings.NewReader(dockerfileContent)), "") tarArchive, relDockerfile, err := GetContextFromReader(ioutil.NopCloser(strings.NewReader(dockerfileContents)), "")
if err != nil { if err != nil {
t.Fatalf("Error when executing GetContextFromReader: %s", err) t.Fatalf("Error when executing GetContextFromReader: %s", err)
@ -326,8 +220,8 @@ func TestGetContextFromReaderString(t *testing.T) {
t.Fatalf("Error when closing tar stream: %s", err) t.Fatalf("Error when closing tar stream: %s", err)
} }
if dockerfileContent != contents { if dockerfileContents != contents {
t.Fatalf("Uncompressed tar archive does not equal: %s, got: %s", dockerfileContent, contents) t.Fatalf("Uncompressed tar archive does not equal: %s, got: %s", dockerfileContents, contents)
} }
if relDockerfile != DefaultDockerfileName { if relDockerfile != DefaultDockerfileName {
@ -336,20 +230,10 @@ func TestGetContextFromReaderString(t *testing.T) {
} }
func TestGetContextFromReaderTar(t *testing.T) { func TestGetContextFromReaderTar(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-context-test") contextDir, cleanup := createTestTempDir(t, "", "builder-context-test")
defer cleanup()
if err != nil { createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777)
t.Fatalf("Error with creating temporary directory: %s", err)
}
defer os.RemoveAll(contextDir)
dockerfileFilename := filepath.Join(contextDir, dockerfileTestName)
err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777)
if err != nil {
t.Fatalf("Error when writing file (%s) contents: %s", dockerfileFilename, err)
}
tarStream, err := archive.Tar(contextDir, archive.Uncompressed) tarStream, err := archive.Tar(contextDir, archive.Uncompressed)
@ -357,7 +241,7 @@ func TestGetContextFromReaderTar(t *testing.T) {
t.Fatalf("Error when creating tar: %s", err) t.Fatalf("Error when creating tar: %s", err)
} }
tarArchive, relDockerfile, err := GetContextFromReader(tarStream, dockerfileTestName) tarArchive, relDockerfile, err := GetContextFromReader(tarStream, DefaultDockerfileName)
if err != nil { if err != nil {
t.Fatalf("Error when executing GetContextFromReader: %s", err) t.Fatalf("Error when executing GetContextFromReader: %s", err)
@ -371,8 +255,8 @@ func TestGetContextFromReaderTar(t *testing.T) {
t.Fatalf("Error when reading tar archive: %s", err) t.Fatalf("Error when reading tar archive: %s", err)
} }
if header.Name != dockerfileTestName { if header.Name != DefaultDockerfileName {
t.Fatalf("Dockerfile name should be: %s, got: %s", dockerfileTestName, header.Name) t.Fatalf("Dockerfile name should be: %s, got: %s", DefaultDockerfileName, header.Name)
} }
buff := new(bytes.Buffer) buff := new(bytes.Buffer)
@ -389,12 +273,12 @@ func TestGetContextFromReaderTar(t *testing.T) {
t.Fatalf("Error when closing tar stream: %s", err) t.Fatalf("Error when closing tar stream: %s", err)
} }
if dockerfileContent != contents { if dockerfileContents != contents {
t.Fatalf("Uncompressed tar archive does not equal: %s, got: %s", dockerfileContent, contents) t.Fatalf("Uncompressed tar archive does not equal: %s, got: %s", dockerfileContents, contents)
} }
if relDockerfile != dockerfileTestName { if relDockerfile != DefaultDockerfileName {
t.Fatalf("Relative path not equals %s, got: %s", dockerfileTestName, relDockerfile) t.Fatalf("Relative path not equals %s, got: %s", DefaultDockerfileName, relDockerfile)
} }
} }
@ -419,5 +303,5 @@ func TestValidateContextDirectoryWithOneFile(t *testing.T) {
} }
func TestValidateContextDirectoryWithOneFileExcludes(t *testing.T) { func TestValidateContextDirectoryWithOneFileExcludes(t *testing.T) {
testValidateContextDirectory(t, prepareOneFile, []string{dockerfileTestName}) testValidateContextDirectory(t, prepareOneFile, []string{DefaultDockerfileName})
} }

View file

@ -4,174 +4,92 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"path/filepath" "sort"
"testing" "testing"
) )
func TestProcessShouldRemoveDockerfileDockerignore(t *testing.T) { const shouldStayFilename = "should_stay"
contextDir, err := ioutil.TempDir("", "builder-dockerignore-process-test")
if err != nil { func extractFilenames(files []os.FileInfo) []string {
t.Fatalf("Error with creating temporary directory: %s", err) filenames := make([]string, len(files), len(files))
for i, file := range files {
filenames[i] = file.Name()
} }
defer os.RemoveAll(contextDir) return filenames
}
testFilename := filepath.Join(contextDir, "should_stay") func checkDirectory(t *testing.T, dir string, expectedFiles []string) {
testContent := "test" files, err := ioutil.ReadDir(dir)
err = ioutil.WriteFile(testFilename, []byte(testContent), 0777)
if err != nil {
t.Fatalf("Error when creating should_stay file: %s", err)
}
dockerignoreFilename := filepath.Join(contextDir, ".dockerignore")
dockerignoreContent := "Dockerfile\n.dockerignore"
err = ioutil.WriteFile(dockerignoreFilename, []byte(dockerignoreContent), 0777)
if err != nil {
t.Fatalf("Error when creating .dockerignore file: %s", err)
}
dockerfileFilename := filepath.Join(contextDir, DefaultDockerfileName)
dockerfileContent := "FROM busybox"
err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777)
if err != nil {
t.Fatalf("Error when creating Dockerfile file: %s", err)
}
modifiableCtx := &tarSumContext{root: contextDir}
ctx := DockerIgnoreContext{ModifiableContext: modifiableCtx}
err = ctx.Process([]string{DefaultDockerfileName})
if err != nil {
t.Fatalf("Error when executing Process: %s", err)
}
files, err := ioutil.ReadDir(contextDir)
if err != nil { if err != nil {
t.Fatalf("Could not read directory: %s", err) t.Fatalf("Could not read directory: %s", err)
} }
if len(files) != 1 { if len(files) != len(expectedFiles) {
log.Fatal("Directory should contain exactly one file") log.Fatalf("Directory should contain exactly %d file(s), got %d", len(expectedFiles), len(files))
} }
for _, file := range files { filenames := extractFilenames(files)
if "should_stay" != file.Name() { sort.Strings(filenames)
log.Fatalf("File %s should not be in the directory", file.Name()) 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) { func TestProcessNoDockerignore(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-dockerignore-process-test") contextDir, cleanup := createTestTempDir(t, "", "builder-dockerignore-process-test")
defer cleanup()
if err != nil { createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777)
t.Fatalf("Error with creating temporary directory: %s", err) createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777)
}
defer os.RemoveAll(contextDir) executeProcess(t, contextDir)
testFilename := filepath.Join(contextDir, "should_stay") checkDirectory(t, contextDir, []string{shouldStayFilename, DefaultDockerfileName})
testContent := "test"
err = ioutil.WriteFile(testFilename, []byte(testContent), 0777)
if err != nil {
t.Fatalf("Error when creating should_stay file: %s", err)
}
dockerfileFilename := filepath.Join(contextDir, DefaultDockerfileName)
dockerfileContent := "FROM busybox"
err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777)
if err != nil {
t.Fatalf("Error when creating Dockerfile file: %s", err)
}
modifiableCtx := &tarSumContext{root: contextDir}
ctx := DockerIgnoreContext{ModifiableContext: modifiableCtx}
ctx.Process([]string{DefaultDockerfileName})
files, err := ioutil.ReadDir(contextDir)
if err != nil {
t.Fatalf("Could not read directory: %s", err)
}
if len(files) != 2 {
log.Fatal("Directory should contain exactly two files")
}
for _, file := range files {
if "should_stay" != file.Name() && DefaultDockerfileName != file.Name() {
log.Fatalf("File %s should not be in the directory", file.Name())
}
}
} }
func TestProcessShouldLeaveAllFiles(t *testing.T) { func TestProcessShouldLeaveAllFiles(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-dockerignore-process-test") contextDir, cleanup := createTestTempDir(t, "", "builder-dockerignore-process-test")
defer cleanup()
if err != nil { createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777)
t.Fatalf("Error with creating temporary directory: %s", err) createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777)
} createTestTempFile(t, contextDir, dockerignoreFilename, "input1\ninput2", 0777)
defer os.RemoveAll(contextDir) executeProcess(t, contextDir)
testFilename := filepath.Join(contextDir, "should_stay") checkDirectory(t, contextDir, []string{shouldStayFilename, DefaultDockerfileName, dockerignoreFilename})
testContent := "test"
err = ioutil.WriteFile(testFilename, []byte(testContent), 0777)
if err != nil {
t.Fatalf("Error when creating should_stay file: %s", err)
}
dockerignoreFilename := filepath.Join(contextDir, ".dockerignore")
dockerignoreContent := "input1\ninput2"
err = ioutil.WriteFile(dockerignoreFilename, []byte(dockerignoreContent), 0777)
if err != nil {
t.Fatalf("Error when creating .dockerignore file: %s", err)
}
dockerfileFilename := filepath.Join(contextDir, DefaultDockerfileName)
dockerfileContent := "FROM busybox"
err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777)
if err != nil {
t.Fatalf("Error when creating Dockerfile file: %s", err)
}
modifiableCtx := &tarSumContext{root: contextDir}
ctx := DockerIgnoreContext{ModifiableContext: modifiableCtx}
err = ctx.Process([]string{DefaultDockerfileName})
if err != nil {
t.Fatalf("Error when executing Process: %s", err)
}
files, err := ioutil.ReadDir(contextDir)
if err != nil {
t.Fatalf("Could not read directory: %s", err)
}
if len(files) != 3 {
log.Fatal("Directory should contain exactly three files")
}
for _, file := range files {
if "should_stay" != file.Name() && DefaultDockerfileName != file.Name() && ".dockerignore" != file.Name() {
log.Fatalf("File %s should not be in the directory", file.Name())
}
}
} }

View file

@ -7,15 +7,12 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"os"
"path/filepath"
"testing" "testing"
"github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/httputils" "github.com/docker/docker/pkg/httputils"
) )
var textPlainDockerfile = "FROM busybox"
var binaryContext = []byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00} //xz magic var binaryContext = []byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00} //xz magic
func TestSelectAcceptableMIME(t *testing.T) { func TestSelectAcceptableMIME(t *testing.T) {
@ -95,10 +92,10 @@ func TestInspectResponseBinary(t *testing.T) {
} }
func TestResponseUnsupportedContentType(t *testing.T) { func TestResponseUnsupportedContentType(t *testing.T) {
content := []byte(textPlainDockerfile) content := []byte(dockerfileContents)
ct := "application/json" ct := "application/json"
br := ioutil.NopCloser(bytes.NewReader(content)) br := ioutil.NopCloser(bytes.NewReader(content))
contentType, bReader, err := inspectResponse(ct, br, int64(len(textPlainDockerfile))) contentType, bReader, err := inspectResponse(ct, br, int64(len(dockerfileContents)))
if err == nil { if err == nil {
t.Fatal("Should have returned an error on content-type 'application/json'") t.Fatal("Should have returned an error on content-type 'application/json'")
@ -110,13 +107,13 @@ func TestResponseUnsupportedContentType(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if string(body) != textPlainDockerfile { if string(body) != dockerfileContents {
t.Fatalf("Corrupted response body %s", body) t.Fatalf("Corrupted response body %s", body)
} }
} }
func TestInspectResponseTextSimple(t *testing.T) { func TestInspectResponseTextSimple(t *testing.T) {
content := []byte(textPlainDockerfile) content := []byte(dockerfileContents)
ct := "text/plain" ct := "text/plain"
br := ioutil.NopCloser(bytes.NewReader(content)) br := ioutil.NopCloser(bytes.NewReader(content))
contentType, bReader, err := inspectResponse(ct, br, int64(len(content))) contentType, bReader, err := inspectResponse(ct, br, int64(len(content)))
@ -130,13 +127,13 @@ func TestInspectResponseTextSimple(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if string(body) != textPlainDockerfile { if string(body) != dockerfileContents {
t.Fatalf("Corrupted response body %s", body) t.Fatalf("Corrupted response body %s", body)
} }
} }
func TestInspectResponseEmptyContentType(t *testing.T) { func TestInspectResponseEmptyContentType(t *testing.T) {
content := []byte(textPlainDockerfile) content := []byte(dockerfileContents)
br := ioutil.NopCloser(bytes.NewReader(content)) br := ioutil.NopCloser(bytes.NewReader(content))
contentType, bodyReader, err := inspectResponse("", br, int64(len(content))) contentType, bodyReader, err := inspectResponse("", br, int64(len(content)))
if err != nil { if err != nil {
@ -149,26 +146,16 @@ func TestInspectResponseEmptyContentType(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if string(body) != textPlainDockerfile { if string(body) != dockerfileContents {
t.Fatalf("Corrupted response body %s", body) t.Fatalf("Corrupted response body %s", body)
} }
} }
func TestMakeRemoteContext(t *testing.T) { func TestMakeRemoteContext(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-remote-test") contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
defer cleanup()
if err != nil { createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777)
t.Fatalf("Error with creating temporary directory: %s", err)
}
defer os.RemoveAll(contextDir)
testFilename := filepath.Join(contextDir, DefaultDockerfileName)
err = ioutil.WriteFile(testFilename, []byte(textPlainDockerfile), 0777)
if err != nil {
t.Fatalf("Error when writing file (%s) contents: %s", testFilename, err)
}
mux := http.NewServeMux() mux := http.NewServeMux()
server := httptest.NewServer(mux) server := httptest.NewServer(mux)

View file

@ -45,20 +45,10 @@ func TestCloseRootDirectory(t *testing.T) {
} }
func TestOpenFile(t *testing.T) { func TestOpenFile(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-tarsum-test") contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
defer cleanup()
if err != nil { createTestTempFile(t, contextDir, filename, contents, 0777)
t.Fatalf("Error with creating temporary directory: %s", err)
}
defer os.RemoveAll(contextDir)
testFilename := filepath.Join(contextDir, filename)
err = ioutil.WriteFile(testFilename, []byte(contents), 0777)
if err != nil {
t.Fatalf("Error when writing file (%s) contents: %s", testFilename, err)
}
tarSum := &tarSumContext{root: contextDir} tarSum := &tarSumContext{root: contextDir}
@ -84,13 +74,8 @@ func TestOpenFile(t *testing.T) {
} }
func TestOpenNotExisting(t *testing.T) { func TestOpenNotExisting(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-tarsum-test") contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
defer cleanup()
if err != nil {
t.Fatalf("Error with creating temporary directory: %s", err)
}
defer os.RemoveAll(contextDir)
tarSum := &tarSumContext{root: contextDir} tarSum := &tarSumContext{root: contextDir}
@ -106,20 +91,10 @@ func TestOpenNotExisting(t *testing.T) {
} }
func TestStatFile(t *testing.T) { func TestStatFile(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-tarsum-test") contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
defer cleanup()
if err != nil { testFilename := createTestTempFile(t, contextDir, filename, contents, 0777)
t.Fatalf("Error with creating temporary directory: %s", err)
}
defer os.RemoveAll(contextDir)
testFilename := filepath.Join(contextDir, filename)
err = ioutil.WriteFile(testFilename, []byte(contents), 0777)
if err != nil {
t.Fatalf("Error when writing file (%s) contents: %s", testFilename, err)
}
tarSum := &tarSumContext{root: contextDir} tarSum := &tarSumContext{root: contextDir}
@ -139,26 +114,12 @@ func TestStatFile(t *testing.T) {
} }
func TestStatSubdir(t *testing.T) { func TestStatSubdir(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-tarsum-test") contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
defer cleanup()
if err != nil { contextSubdir := createTestTempSubdir(t, contextDir, "builder-tarsum-test-subdir")
t.Fatalf("Error with creating temporary directory: %s", err)
}
defer os.RemoveAll(contextDir) testFilename := createTestTempFile(t, contextSubdir, filename, contents, 0777)
contextSubdir, err := ioutil.TempDir(contextDir, "builder-tarsum-test-subdir")
if err != nil {
t.Fatalf("Error with creating temporary subdirectory: %s", err)
}
testFilename := filepath.Join(contextSubdir, filename)
err = ioutil.WriteFile(testFilename, []byte(contents), 0777)
if err != nil {
t.Fatalf("Error when writing file (%s) contents: %s", testFilename, err)
}
tarSum := &tarSumContext{root: contextDir} tarSum := &tarSumContext{root: contextDir}
@ -184,13 +145,8 @@ func TestStatSubdir(t *testing.T) {
} }
func TestStatNotExisting(t *testing.T) { func TestStatNotExisting(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-tarsum-test") contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
defer cleanup()
if err != nil {
t.Fatalf("Error with creating temporary directory: %s", err)
}
defer os.RemoveAll(contextDir)
tarSum := &tarSumContext{root: contextDir} tarSum := &tarSumContext{root: contextDir}
@ -210,19 +166,10 @@ func TestStatNotExisting(t *testing.T) {
} }
func TestRemoveDirectory(t *testing.T) { func TestRemoveDirectory(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-tarsum-test") contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
defer cleanup()
if err != nil { contextSubdir := createTestTempSubdir(t, contextDir, "builder-tarsum-test-subdir")
t.Fatalf("Error with creating temporary directory: %s", err)
}
defer os.RemoveAll(contextDir)
contextSubdir, err := ioutil.TempDir(contextDir, "builder-tarsum-test-subdir")
if err != nil {
t.Fatalf("Error with creating temporary subdirectory: %s", err)
}
relativePath, err := filepath.Rel(contextDir, contextSubdir) relativePath, err := filepath.Rel(contextDir, contextSubdir)
@ -246,20 +193,10 @@ func TestRemoveDirectory(t *testing.T) {
} }
func TestMakeSumTarContext(t *testing.T) { func TestMakeSumTarContext(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-tarsum-test") contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
defer cleanup()
if err != nil { createTestTempFile(t, contextDir, filename, contents, 0777)
t.Fatalf("Error with creating temporary directory: %s", err)
}
defer os.RemoveAll(contextDir)
testFilename := filepath.Join(contextDir, filename)
err = ioutil.WriteFile(testFilename, []byte(contents), 0644)
if err != nil {
t.Fatalf("Error when writing file (%s) contents: %s", testFilename, err)
}
tarStream, err := archive.Tar(contextDir, archive.Uncompressed) tarStream, err := archive.Tar(contextDir, archive.Uncompressed)
@ -281,26 +218,12 @@ func TestMakeSumTarContext(t *testing.T) {
} }
func TestWalkWithoutError(t *testing.T) { func TestWalkWithoutError(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-tarsum-test") contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
defer cleanup()
if err != nil { contextSubdir := createTestTempSubdir(t, contextDir, "builder-tarsum-test-subdir")
t.Fatalf("Error with creating temporary directory: %s", err)
}
defer os.RemoveAll(contextDir) createTestTempFile(t, contextSubdir, filename, contents, 0777)
contextSubdir, err := ioutil.TempDir(contextDir, "builder-tarsum-test-subdir")
if err != nil {
t.Fatalf("Error with creating temporary subdirectory: %s", err)
}
testFilename := filepath.Join(contextSubdir, filename)
err = ioutil.WriteFile(testFilename, []byte(contents), 0777)
if err != nil {
t.Fatalf("Error when writing file (%s) contents: %s", testFilename, err)
}
tarSum := &tarSumContext{root: contextDir} tarSum := &tarSumContext{root: contextDir}
@ -308,7 +231,7 @@ func TestWalkWithoutError(t *testing.T) {
return nil return nil
} }
err = tarSum.Walk(contextSubdir, walkFun) err := tarSum.Walk(contextSubdir, walkFun)
if err != nil { if err != nil {
t.Fatalf("Error when executing Walk: %s", err) t.Fatalf("Error when executing Walk: %s", err)
@ -323,19 +246,10 @@ func (we WalkError) Error() string {
} }
func TestWalkWithError(t *testing.T) { func TestWalkWithError(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-tarsum-test") contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
defer cleanup()
if err != nil { contextSubdir := createTestTempSubdir(t, contextDir, "builder-tarsum-test-subdir")
t.Fatalf("Error with creating temporary directory: %s", err)
}
defer os.RemoveAll(contextDir)
contextSubdir, err := ioutil.TempDir(contextDir, "builder-tarsum-test-subdir")
if err != nil {
t.Fatalf("Error with creating temporary subdirectory: %s", err)
}
tarSum := &tarSumContext{root: contextDir} tarSum := &tarSumContext{root: contextDir}
@ -343,7 +257,7 @@ func TestWalkWithError(t *testing.T) {
return WalkError{} return WalkError{}
} }
err = tarSum.Walk(contextSubdir, walkFun) err := tarSum.Walk(contextSubdir, walkFun)
if err == nil { if err == nil {
t.Fatalf("Error should not be nil") t.Fatalf("Error should not be nil")

87
builder/utils_test.go Normal file
View file

@ -0,0 +1,87 @@
package builder
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
const (
dockerfileContents = "FROM busybox"
dockerignoreFilename = ".dockerignore"
testfileContents = "test"
)
// createTestTempDir creates a temporary directory for testing.
// It returns the created path and a cleanup function which is meant to be used as deferred call.
// When an error occurs, it terminates the test.
func createTestTempDir(t *testing.T, dir, prefix string) (string, func()) {
path, err := ioutil.TempDir(dir, prefix)
if err != nil {
t.Fatalf("Error when creating directory %s with prefix %s: %s", dir, prefix, err)
}
return path, func() {
err = os.RemoveAll(path)
if err != nil {
t.Fatalf("Error when removing directory %s: %s", path, err)
}
}
}
// createTestTempSubdir creates a temporary directory for testing.
// It returns the created path but doesn't provide a cleanup function,
// so createTestTempSubdir should be used only for creating temporary subdirectories
// whose parent directories are properly cleaned up.
// When an error occurs, it terminates the test.
func createTestTempSubdir(t *testing.T, dir, prefix string) string {
path, err := ioutil.TempDir(dir, prefix)
if err != nil {
t.Fatalf("Error when creating directory %s with prefix %s: %s", dir, prefix, err)
}
return path
}
// createTestTempFile creates a temporary file within dir with specific contents and permissions.
// When an error occurs, it terminates the test
func createTestTempFile(t *testing.T, dir, filename, contents string, perm os.FileMode) string {
filePath := filepath.Join(dir, filename)
err := ioutil.WriteFile(filePath, []byte(contents), perm)
if err != nil {
t.Fatalf("Error when creating %s file: %s", filename, err)
}
return filePath
}
// chdir changes current working directory to dir.
// It returns a function which changes working directory back to the previous one.
// This function is meant to be executed as a deferred call.
// When an error occurs, it terminates the test.
func chdir(t *testing.T, dir string) func() {
workingDirectory, err := os.Getwd()
if err != nil {
t.Fatalf("Error when retrieving working directory: %s", err)
}
err = os.Chdir(dir)
if err != nil {
t.Fatalf("Error when changing directory to %s: %s", dir, err)
}
return func() {
err = os.Chdir(workingDirectory)
if err != nil {
t.Fatalf("Error when changing back to working directory (%s): %s", workingDirectory, err)
}
}
}