2016-12-22 16:25:02 -05:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
2018-12-23 07:05:23 -05:00
|
|
|
"github.com/docker/docker/pkg/fileutils"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-12-22 16:25:02 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const dockerfileContents = "FROM busybox"
|
|
|
|
|
2022-02-25 07:10:34 -05:00
|
|
|
func prepareEmpty(t *testing.T) string {
|
|
|
|
return ""
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
2022-02-25 07:10:34 -05:00
|
|
|
func prepareNoFiles(t *testing.T) string {
|
|
|
|
return createTestTempDir(t)
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
2022-02-25 07:10:34 -05:00
|
|
|
func prepareOneFile(t *testing.T) string {
|
|
|
|
contextDir := createTestTempDir(t)
|
2019-04-02 07:08:04 -04:00
|
|
|
createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents)
|
2022-02-25 07:10:34 -05:00
|
|
|
return contextDir
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
2022-02-25 07:10:34 -05:00
|
|
|
func testValidateContextDirectory(t *testing.T, prepare func(t *testing.T) string, excludes []string) {
|
|
|
|
contextDir := prepare(t)
|
2016-12-22 16:25:02 -05:00
|
|
|
err := ValidateContextDirectory(contextDir, excludes)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContextFromLocalDirNoDockerfile(t *testing.T) {
|
2022-02-25 07:10:34 -05:00
|
|
|
contextDir := createTestTempDir(t)
|
2017-04-06 15:36:15 -04:00
|
|
|
_, _, err := GetContextFromLocalDir(contextDir, "")
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, err, "Dockerfile")
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContextFromLocalDirNotExistingDir(t *testing.T) {
|
2022-02-25 07:10:34 -05:00
|
|
|
contextDir := createTestTempDir(t)
|
2016-12-22 16:25:02 -05:00
|
|
|
fakePath := filepath.Join(contextDir, "fake")
|
|
|
|
|
2017-04-16 16:06:16 -04:00
|
|
|
_, _, err := GetContextFromLocalDir(fakePath, "")
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, err, "fake")
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContextFromLocalDirNotExistingDockerfile(t *testing.T) {
|
2022-02-25 07:10:34 -05:00
|
|
|
contextDir := createTestTempDir(t)
|
2016-12-22 16:25:02 -05:00
|
|
|
fakePath := filepath.Join(contextDir, "fake")
|
|
|
|
|
2017-04-06 15:36:15 -04:00
|
|
|
_, _, err := GetContextFromLocalDir(contextDir, fakePath)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, err, "fake")
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContextFromLocalDirWithNoDirectory(t *testing.T) {
|
2022-02-25 07:10:34 -05:00
|
|
|
contextDir := createTestTempDir(t)
|
2019-04-02 07:08:04 -04:00
|
|
|
createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents)
|
2016-12-22 16:25:02 -05:00
|
|
|
|
2022-02-25 07:10:34 -05:00
|
|
|
chdir(t, contextDir)
|
2016-12-22 16:25:02 -05:00
|
|
|
|
|
|
|
absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, "")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-22 16:25:02 -05:00
|
|
|
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(contextDir, absContextDir))
|
|
|
|
assert.Check(t, is.Equal(DefaultDockerfileName, relDockerfile))
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContextFromLocalDirWithDockerfile(t *testing.T) {
|
2022-02-25 07:10:34 -05:00
|
|
|
contextDir := createTestTempDir(t)
|
2019-04-02 07:08:04 -04:00
|
|
|
createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents)
|
2016-12-22 16:25:02 -05:00
|
|
|
|
|
|
|
absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, "")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-22 16:25:02 -05:00
|
|
|
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(contextDir, absContextDir))
|
|
|
|
assert.Check(t, is.Equal(DefaultDockerfileName, relDockerfile))
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContextFromLocalDirLocalFile(t *testing.T) {
|
2022-02-25 07:10:34 -05:00
|
|
|
contextDir := createTestTempDir(t)
|
2019-04-02 07:08:04 -04:00
|
|
|
createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents)
|
|
|
|
testFilename := createTestTempFile(t, contextDir, "tmpTest", "test")
|
2016-12-22 16:25:02 -05:00
|
|
|
|
|
|
|
absContextDir, relDockerfile, err := GetContextFromLocalDir(testFilename, "")
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Error should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if absContextDir != "" {
|
|
|
|
t.Fatalf("Absolute directory path should be empty, got: %s", absContextDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
if relDockerfile != "" {
|
|
|
|
t.Fatalf("Relative path to Dockerfile should be empty, got: %s", relDockerfile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContextFromLocalDirWithCustomDockerfile(t *testing.T) {
|
2022-02-25 07:10:34 -05:00
|
|
|
contextDir := createTestTempDir(t)
|
|
|
|
chdir(t, contextDir)
|
2016-12-22 16:25:02 -05:00
|
|
|
|
2019-04-02 07:08:04 -04:00
|
|
|
createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents)
|
2016-12-22 16:25:02 -05:00
|
|
|
|
|
|
|
absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, DefaultDockerfileName)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-22 16:25:02 -05:00
|
|
|
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(contextDir, absContextDir))
|
|
|
|
assert.Check(t, is.Equal(DefaultDockerfileName, relDockerfile))
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContextFromReaderString(t *testing.T) {
|
2022-02-25 07:10:34 -05:00
|
|
|
tarArchive, relDockerfile, err := GetContextFromReader(io.NopCloser(strings.NewReader(dockerfileContents)), "")
|
2016-12-22 16:25:02 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when executing GetContextFromReader: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tarReader := tar.NewReader(tarArchive)
|
|
|
|
|
|
|
|
_, err = tarReader.Next()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when reading tar archive: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buff := new(bytes.Buffer)
|
|
|
|
buff.ReadFrom(tarReader)
|
|
|
|
contents := buff.String()
|
|
|
|
|
|
|
|
_, err = tarReader.Next()
|
|
|
|
|
|
|
|
if err != io.EOF {
|
|
|
|
t.Fatalf("Tar stream too long: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, tarArchive.Close())
|
2016-12-22 16:25:02 -05:00
|
|
|
|
|
|
|
if dockerfileContents != contents {
|
|
|
|
t.Fatalf("Uncompressed tar archive does not equal: %s, got: %s", dockerfileContents, contents)
|
|
|
|
}
|
|
|
|
|
|
|
|
if relDockerfile != DefaultDockerfileName {
|
|
|
|
t.Fatalf("Relative path not equals %s, got: %s", DefaultDockerfileName, relDockerfile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContextFromReaderTar(t *testing.T) {
|
2022-02-25 07:10:34 -05:00
|
|
|
contextDir := createTestTempDir(t)
|
2019-04-02 07:08:04 -04:00
|
|
|
createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents)
|
2016-12-22 16:25:02 -05:00
|
|
|
|
|
|
|
tarStream, err := archive.Tar(contextDir, archive.Uncompressed)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-22 16:25:02 -05:00
|
|
|
|
|
|
|
tarArchive, relDockerfile, err := GetContextFromReader(tarStream, DefaultDockerfileName)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-22 16:25:02 -05:00
|
|
|
|
|
|
|
tarReader := tar.NewReader(tarArchive)
|
|
|
|
|
|
|
|
header, err := tarReader.Next()
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-22 16:25:02 -05:00
|
|
|
|
|
|
|
if header.Name != DefaultDockerfileName {
|
|
|
|
t.Fatalf("Dockerfile name should be: %s, got: %s", DefaultDockerfileName, header.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
buff := new(bytes.Buffer)
|
|
|
|
buff.ReadFrom(tarReader)
|
|
|
|
contents := buff.String()
|
|
|
|
|
|
|
|
_, err = tarReader.Next()
|
|
|
|
|
|
|
|
if err != io.EOF {
|
|
|
|
t.Fatalf("Tar stream too long: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, tarArchive.Close())
|
2016-12-22 16:25:02 -05:00
|
|
|
|
|
|
|
if dockerfileContents != contents {
|
|
|
|
t.Fatalf("Uncompressed tar archive does not equal: %s, got: %s", dockerfileContents, contents)
|
|
|
|
}
|
|
|
|
|
|
|
|
if relDockerfile != DefaultDockerfileName {
|
|
|
|
t.Fatalf("Relative path not equals %s, got: %s", DefaultDockerfileName, relDockerfile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateContextDirectoryEmptyContext(t *testing.T) {
|
|
|
|
// This isn't a valid test on Windows. See https://play.golang.org/p/RR6z6jxR81.
|
|
|
|
// The test will ultimately end up calling filepath.Abs(""). On Windows,
|
|
|
|
// golang will error. On Linux, golang will return /. Due to there being
|
|
|
|
// drive letters on Windows, this is probably the correct behaviour for
|
|
|
|
// Windows.
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("Invalid test on Windows")
|
|
|
|
}
|
|
|
|
testValidateContextDirectory(t, prepareEmpty, []string{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateContextDirectoryContextWithNoFiles(t *testing.T) {
|
|
|
|
testValidateContextDirectory(t, prepareNoFiles, []string{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateContextDirectoryWithOneFile(t *testing.T) {
|
|
|
|
testValidateContextDirectory(t, prepareOneFile, []string{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateContextDirectoryWithOneFileExcludes(t *testing.T) {
|
|
|
|
testValidateContextDirectory(t, prepareOneFile, []string{DefaultDockerfileName})
|
|
|
|
}
|
|
|
|
|
2022-02-25 07:10:34 -05:00
|
|
|
// createTestTempDir creates a temporary directory for testing. It returns the
|
|
|
|
// created path. When an error occurs, it terminates the test.
|
|
|
|
func createTestTempDir(t *testing.T) string {
|
|
|
|
t.Helper()
|
|
|
|
path := t.TempDir()
|
|
|
|
|
|
|
|
// Eval Symlinks is needed to account for macOS TMP using symlinks
|
|
|
|
path, err := filepath.EvalSymlinks(path)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2022-02-25 07:10:34 -05:00
|
|
|
return path
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// createTestTempFile creates a temporary file within dir with specific contents and permissions.
|
|
|
|
// When an error occurs, it terminates the test
|
2019-04-02 07:08:04 -04:00
|
|
|
func createTestTempFile(t *testing.T, dir, filename, contents string) string {
|
2022-02-25 07:10:34 -05:00
|
|
|
t.Helper()
|
2016-12-22 16:25:02 -05:00
|
|
|
filePath := filepath.Join(dir, filename)
|
2022-02-25 07:10:34 -05:00
|
|
|
err := os.WriteFile(filePath, []byte(contents), 0777)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-22 16:25:02 -05:00
|
|
|
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.
|
2022-02-25 07:10:34 -05:00
|
|
|
func chdir(t *testing.T, dir string) {
|
2016-12-22 16:25:02 -05:00
|
|
|
workingDirectory, err := os.Getwd()
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.NilError(t, os.Chdir(dir))
|
2022-02-25 07:10:34 -05:00
|
|
|
t.Cleanup(func() {
|
|
|
|
assert.NilError(t, os.Chdir(workingDirectory))
|
|
|
|
})
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
2017-06-08 16:08:11 -04:00
|
|
|
|
|
|
|
func TestIsArchive(t *testing.T) {
|
|
|
|
var testcases = []struct {
|
|
|
|
doc string
|
|
|
|
header []byte
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "nil is not a valid header",
|
|
|
|
header: nil,
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "invalid header bytes",
|
|
|
|
header: []byte{0x00, 0x01, 0x02},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "header for bzip2 archive",
|
|
|
|
header: []byte{0x42, 0x5A, 0x68},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "header for 7zip archive is not supported",
|
|
|
|
header: []byte{0x50, 0x4b, 0x03, 0x04},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testcase := range testcases {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(testcase.expected, IsArchive(testcase.header)), testcase.doc)
|
2017-06-08 16:08:11 -04:00
|
|
|
}
|
|
|
|
}
|
2018-10-19 11:37:58 -04:00
|
|
|
|
|
|
|
func TestDetectArchiveReader(t *testing.T) {
|
|
|
|
var testcases = []struct {
|
|
|
|
file string
|
|
|
|
desc string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
file: "../testdata/tar.test",
|
|
|
|
desc: "tar file without pax headers",
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
file: "../testdata/gittar.test",
|
|
|
|
desc: "tar file with pax headers",
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
file: "../testdata/Dockerfile.test",
|
|
|
|
desc: "not a tar file",
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
content, err := os.Open(testcase.file)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer content.Close()
|
|
|
|
|
|
|
|
_, isArchive, err := DetectArchiveReader(content)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(testcase.expected, isArchive), testcase.file)
|
|
|
|
}
|
|
|
|
}
|
2018-12-23 07:05:23 -05:00
|
|
|
|
|
|
|
func mustPatternMatcher(t *testing.T, patterns []string) *fileutils.PatternMatcher {
|
|
|
|
t.Helper()
|
|
|
|
pm, err := fileutils.NewPatternMatcher(patterns)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("failed to construct pattern matcher: ", err)
|
|
|
|
}
|
|
|
|
return pm
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWildcardMatches(t *testing.T) {
|
|
|
|
match, _ := filepathMatches(mustPatternMatcher(t, []string{"*"}), "fileutils.go")
|
|
|
|
if !match {
|
|
|
|
t.Errorf("failed to get a wildcard match, got %v", match)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A simple pattern match should return true.
|
|
|
|
func TestPatternMatches(t *testing.T) {
|
|
|
|
match, _ := filepathMatches(mustPatternMatcher(t, []string{"*.go"}), "fileutils.go")
|
|
|
|
if !match {
|
|
|
|
t.Errorf("failed to get a match, got %v", match)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// An exclusion followed by an inclusion should return true.
|
|
|
|
func TestExclusionPatternMatchesPatternBefore(t *testing.T) {
|
|
|
|
match, _ := filepathMatches(mustPatternMatcher(t, []string{"!fileutils.go", "*.go"}), "fileutils.go")
|
|
|
|
if !match {
|
|
|
|
t.Errorf("failed to get true match on exclusion pattern, got %v", match)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A folder pattern followed by an exception should return false.
|
|
|
|
func TestPatternMatchesFolderExclusions(t *testing.T) {
|
|
|
|
match, _ := filepathMatches(mustPatternMatcher(t, []string{"docs", "!docs/README.md"}), "docs/README.md")
|
|
|
|
if match {
|
|
|
|
t.Errorf("failed to get a false match on exclusion pattern, got %v", match)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A folder pattern followed by an exception should return false.
|
|
|
|
func TestPatternMatchesFolderWithSlashExclusions(t *testing.T) {
|
|
|
|
match, _ := filepathMatches(mustPatternMatcher(t, []string{"docs/", "!docs/README.md"}), "docs/README.md")
|
|
|
|
if match {
|
|
|
|
t.Errorf("failed to get a false match on exclusion pattern, got %v", match)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A folder pattern followed by an exception should return false.
|
|
|
|
func TestPatternMatchesFolderWildcardExclusions(t *testing.T) {
|
|
|
|
match, _ := filepathMatches(mustPatternMatcher(t, []string{"docs/*", "!docs/README.md"}), "docs/README.md")
|
|
|
|
if match {
|
|
|
|
t.Errorf("failed to get a false match on exclusion pattern, got %v", match)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A pattern followed by an exclusion should return false.
|
|
|
|
func TestExclusionPatternMatchesPatternAfter(t *testing.T) {
|
|
|
|
match, _ := filepathMatches(mustPatternMatcher(t, []string{"*.go", "!fileutils.go"}), "fileutils.go")
|
|
|
|
if match {
|
|
|
|
t.Errorf("failed to get false match on exclusion pattern, got %v", match)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A filename evaluating to . should return false.
|
|
|
|
func TestExclusionPatternMatchesWholeDirectory(t *testing.T) {
|
|
|
|
match, _ := filepathMatches(mustPatternMatcher(t, []string{"*.go"}), ".")
|
|
|
|
if match {
|
|
|
|
t.Errorf("failed to get false match on ., got %v", match)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matches with no patterns
|
|
|
|
func TestMatchesWithNoPatterns(t *testing.T) {
|
|
|
|
matches, err := filepathMatches(mustPatternMatcher(t, []string{}), "/any/path/there")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if matches {
|
|
|
|
t.Fatalf("Should not have match anything")
|
|
|
|
}
|
|
|
|
}
|