Fixed relative filepath and unit test

This commit is contained in:
Priya Wadhwa 2018-03-15 11:11:39 -07:00
parent 140d49d506
commit 5ebf156d94
No known key found for this signature in database
GPG Key ID: 0D0DAFD8F7AA73AE
8 changed files with 43 additions and 39 deletions

View File

@ -0,0 +1 @@
hello

View File

View File

@ -10,3 +10,5 @@ COPY . newdir
COPY context/bar /baz/ COPY context/bar /baz/
COPY ["context/foo", "/tmp/foo" ] COPY ["context/foo", "/tmp/foo" ]
COPY context/b* /baz/ COPY context/b* /baz/
COPY context/foo context/bar/ba? /test/
COPY context/arr[[]0].txt /mydir/

View File

@ -41,7 +41,7 @@ func (c *CopyCommand) ExecuteCommand(config *manifest.Schema2Config) error {
logrus.Infof("dest: %s", dest) logrus.Infof("dest: %s", dest)
// Get a map of [src]:[files rooted at src] // Get a map of [src]:[files rooted at src]
srcMap, err := util.ResolveSources(c.cmd.SourcesAndDest, c.buildcontext, config.WorkingDir) srcMap, err := util.ResolveSources(c.cmd.SourcesAndDest, c.buildcontext)
if err != nil { if err != nil {
return err return err
} }
@ -88,7 +88,7 @@ func (c *CopyCommand) FilesToSnapshot() []string {
return c.snapshotFiles return c.snapshotFiles
} }
// Author returns some information about the command for the image config // CreatedBy returns some information about the command for the image config
func (c *CopyCommand) CreatedBy() string { func (c *CopyCommand) CreatedBy() string {
return strings.Join(c.cmd.SourcesAndDest, " ") return strings.Join(c.cmd.SourcesAndDest, " ")
} }

View File

@ -19,6 +19,7 @@ package util
import ( import (
"github.com/docker/docker/builder/dockerfile/instructions" "github.com/docker/docker/builder/dockerfile/instructions"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -38,21 +39,22 @@ func ContainsWildcards(paths []string) bool {
return false return false
} }
// ResolveSources resolves the given sources if the sources contains wildcard // ResolveSources resolves the given sources if the sources contains wildcards
// It returns a map of [src]:[files rooted at src] // It returns a map of [src]:[files rooted at src]
func ResolveSources(srcsAndDest instructions.SourcesAndDest, root, cwd string) (map[string][]string, error) { func ResolveSources(srcsAndDest instructions.SourcesAndDest, root string) (map[string][]string, error) {
srcs := srcsAndDest[:len(srcsAndDest)-1] srcs := srcsAndDest[:len(srcsAndDest)-1]
// If sources contain wildcards, we first need to resolve them to actual paths // If sources contain wildcards, we first need to resolve them to actual paths
wildcard := ContainsWildcards(srcs) if ContainsWildcards(srcs) {
if wildcard { logrus.Debugf("Resolving srcs %v...", srcs)
files, err := Files("", root) files, err := RelativeFiles("", root)
if err != nil { if err != nil {
return nil, err return nil, err
} }
srcs, err = matchSources(srcs, files, cwd) srcs, err = matchSources(srcs, files)
if err != nil { if err != nil {
return nil, err return nil, err
} }
logrus.Debugf("Resolved sources to %v", srcs)
} }
// Now, get a map of [src]:[files rooted at src] // Now, get a map of [src]:[files rooted at src]
srcMap, err := SourcesToFilesMap(srcs, root) srcMap, err := SourcesToFilesMap(srcs, root)
@ -63,8 +65,8 @@ func ResolveSources(srcsAndDest instructions.SourcesAndDest, root, cwd string) (
return srcMap, IsSrcsValid(srcsAndDest, srcMap) return srcMap, IsSrcsValid(srcsAndDest, srcMap)
} }
// matchSources returns a map of [src]:[matching filepaths], used to resolve wildcards // matchSources returns a list of sources that match wildcards
func matchSources(srcs, files []string, cwd string) ([]string, error) { func matchSources(srcs, files []string) ([]string, error) {
var matchedSources []string var matchedSources []string
for _, src := range srcs { for _, src := range srcs {
src = filepath.Clean(src) src = filepath.Clean(src)
@ -73,15 +75,9 @@ func matchSources(srcs, files []string, cwd string) ([]string, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Check cwd if matched {
matchedRoot, err := filepath.Match(filepath.Join(cwd, src), file) matchedSources = append(matchedSources, file)
if err != nil {
return nil, err
} }
if !(matched || matchedRoot) {
continue
}
matchedSources = append(matchedSources, file)
} }
} }
return matchedSources, nil return matchedSources, nil
@ -91,13 +87,17 @@ func IsDestDir(path string) bool {
return strings.HasSuffix(path, "/") return strings.HasSuffix(path, "/")
} }
// RelativeFilepath returns the relative filepath func IsAbsoluteFilepath(path string) bool {
// If source is a file: return strings.HasPrefix(path, "/")
// If dest is a dir, copy it to /cwd/dest/relpath }
// If dest is a file, copy directly to /cwd/dest
// RelativeFilepath returns the relative filepath from the build context to the image filesystem
// If source is a file:
// If dest is a dir, copy it to /dest/relpath
// If dest is a file, copy directly to dest
// If source is a dir: // If source is a dir:
// Assume dest is also a dir, and copy to /cwd/dest/relpath // Assume dest is also a dir, and copy to dest/relpath
// If dest is not an absolute filepath, add /cwd to the beginning
func RelativeFilepath(filename, srcName, dest, cwd, buildcontext string) (string, error) { func RelativeFilepath(filename, srcName, dest, cwd, buildcontext string) (string, error) {
fi, err := os.Stat(filepath.Join(buildcontext, filename)) fi, err := os.Stat(filepath.Join(buildcontext, filename))
if err != nil { if err != nil {
@ -115,8 +115,14 @@ func RelativeFilepath(filename, srcName, dest, cwd, buildcontext string) (string
if relPath == "." && !fi.IsDir() { if relPath == "." && !fi.IsDir() {
relPath = filepath.Base(filename) relPath = filepath.Base(filename)
} }
destPath := filepath.Join(cwd, dest, relPath) destPath := filepath.Join(dest, relPath)
return destPath, nil if IsAbsoluteFilepath(dest) {
return destPath, nil
}
return filepath.Join(cwd, destPath), nil
}
if IsAbsoluteFilepath(dest) {
return dest, nil
} }
return filepath.Join(cwd, dest), nil return filepath.Join(cwd, dest), nil
} }
@ -126,7 +132,7 @@ func SourcesToFilesMap(srcs []string, root string) (map[string][]string, error)
srcMap := make(map[string][]string) srcMap := make(map[string][]string)
for _, src := range srcs { for _, src := range srcs {
src = filepath.Clean(src) src = filepath.Clean(src)
files, err := Files(src, root) files, err := RelativeFiles(src, root)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -79,7 +79,7 @@ var relativeFilepathTests = []struct {
filename: "context/empty", filename: "context/empty",
cwd: "/dir", cwd: "/dir",
dest: "/empty", dest: "/empty",
expectedFilepath: "/dir/empty", expectedFilepath: "/empty",
}, },
{ {
srcName: "./", srcName: "./",
@ -121,7 +121,6 @@ func Test_RelativeFilepath(t *testing.T) {
var matchSourcesTests = []struct { var matchSourcesTests = []struct {
srcs []string srcs []string
files []string files []string
cwd string
expectedFiles []string expectedFiles []string
}{ }{
{ {
@ -135,18 +134,16 @@ var matchSourcesTests = []struct {
"pkg/b/d/", "pkg/b/d/",
"dir/", "dir/",
}, },
cwd: "/",
expectedFiles: []string{ expectedFiles: []string{
"pkg/a", "pkg/a",
"pkg/b", "pkg/b",
"/pkg/d",
}, },
}, },
} }
func Test_MatchSources(t *testing.T) { func Test_MatchSources(t *testing.T) {
for _, test := range matchSourcesTests { for _, test := range matchSourcesTests {
actualFiles, err := matchSources(test.srcs, test.files, test.cwd) actualFiles, err := matchSources(test.srcs, test.files)
sort.Strings(actualFiles) sort.Strings(actualFiles)
sort.Strings(test.expectedFiles) sort.Strings(test.expectedFiles)
testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedFiles, actualFiles) testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedFiles, actualFiles)
@ -253,7 +250,6 @@ func Test_IsSrcsValid(t *testing.T) {
var testResolveSources = []struct { var testResolveSources = []struct {
srcsAndDest []string srcsAndDest []string
cwd string
expectedMap map[string][]string expectedMap map[string][]string
}{ }{
{ {
@ -262,7 +258,6 @@ var testResolveSources = []struct {
"context/b*", "context/b*",
"dest/", "dest/",
}, },
cwd: "/",
expectedMap: map[string][]string{ expectedMap: map[string][]string{
"context/foo": { "context/foo": {
"context/foo", "context/foo",
@ -280,7 +275,7 @@ var testResolveSources = []struct {
func Test_ResolveSources(t *testing.T) { func Test_ResolveSources(t *testing.T) {
for _, test := range testResolveSources { for _, test := range testResolveSources {
actualMap, err := ResolveSources(test.srcsAndDest, buildContextPath, test.cwd) actualMap, err := ResolveSources(test.srcsAndDest, buildContextPath)
testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedMap, actualMap) testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedMap, actualMap)
} }
} }

View File

@ -98,8 +98,8 @@ func fileSystemWhitelist(path string) ([]string, error) {
return whitelist, nil return whitelist, nil
} }
// Files returns a list of all files at the filepath relative to root // RelativeFiles returns a list of all files at the filepath relative to root
func Files(fp string, root string) ([]string, error) { func RelativeFiles(fp string, root string) ([]string, error) {
var files []string var files []string
fullPath := filepath.Join(root, fp) fullPath := filepath.Join(root, fp)
logrus.Debugf("Getting files and contents at root %s", fullPath) logrus.Debugf("Getting files and contents at root %s", fullPath)
@ -112,7 +112,7 @@ func Files(fp string, root string) ([]string, error) {
return err return err
} }
files = append(files, relPath) files = append(files, relPath)
return err return nil
}) })
return files, err return files, err
} }

View File

@ -114,7 +114,7 @@ var tests = []struct {
}, },
} }
func Test_Files(t *testing.T) { func Test_RelativeFiles(t *testing.T) {
for _, test := range tests { for _, test := range tests {
testDir, err := ioutil.TempDir("", "") testDir, err := ioutil.TempDir("", "")
if err != nil { if err != nil {
@ -124,7 +124,7 @@ func Test_Files(t *testing.T) {
if err := testutil.SetupFiles(testDir, test.files); err != nil { if err := testutil.SetupFiles(testDir, test.files); err != nil {
t.Fatalf("err setting up files: %v", err) t.Fatalf("err setting up files: %v", err)
} }
actualFiles, err := Files(test.directory, testDir) actualFiles, err := RelativeFiles(test.directory, testDir)
sort.Strings(actualFiles) sort.Strings(actualFiles)
sort.Strings(test.expectedFiles) sort.Strings(test.expectedFiles)
testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedFiles, actualFiles) testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedFiles, actualFiles)