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
Родитель a603fa33d5 ab2baf08f0
Коммит 16d0a89593
5 изменённых файлов: 234 добавлений и 444 удалений

Просмотреть файл

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

Просмотреть файл

@ -4,174 +4,92 @@ import (
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"testing"
)
func TestProcessShouldRemoveDockerfileDockerignore(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-dockerignore-process-test")
const shouldStayFilename = "should_stay"
if err != nil {
t.Fatalf("Error with creating temporary directory: %s", err)
func extractFilenames(files []os.FileInfo) []string {
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")
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 := "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)
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) != 1 {
log.Fatal("Directory should contain exactly one file")
if len(files) != len(expectedFiles) {
log.Fatalf("Directory should contain exactly %d file(s), got %d", len(expectedFiles), len(files))
}
for _, file := range files {
if "should_stay" != file.Name() {
log.Fatalf("File %s should not be in the directory", file.Name())
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, err := ioutil.TempDir("", "builder-dockerignore-process-test")
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerignore-process-test")
defer cleanup()
if err != nil {
t.Fatalf("Error with creating temporary directory: %s", err)
}
createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777)
createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777)
defer os.RemoveAll(contextDir)
executeProcess(t, contextDir)
testFilename := filepath.Join(contextDir, "should_stay")
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())
}
}
checkDirectory(t, contextDir, []string{shouldStayFilename, DefaultDockerfileName})
}
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 {
t.Fatalf("Error with creating temporary directory: %s", err)
}
createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777)
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")
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())
}
}
checkDirectory(t, contextDir, []string{shouldStayFilename, DefaultDockerfileName, dockerignoreFilename})
}

Просмотреть файл

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

Просмотреть файл

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

87
builder/utils_test.go Normal 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)
}
}
}