Fixing lint issues. Adding tests for COPY command. Fixing issue with copying files out of snapshots

This commit is contained in:
Don McCasland 2019-09-16 10:41:59 -07:00
parent e003bae87d
commit 1bb5a41d7d
No known key found for this signature in database
GPG Key ID: 2B332A542CE5FE1B
21 changed files with 104 additions and 40 deletions

View File

@ -19,12 +19,11 @@ package commands
import ( import (
"path/filepath" "path/filepath"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/GoogleContainerTools/kaniko/pkg/util" "github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )

View File

@ -19,7 +19,7 @@ package commands
import ( import (
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/GoogleContainerTools/kaniko/pkg/util" "github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/google/go-containerregistry/pkg/v1" v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/instructions"
) )

View File

@ -18,7 +18,7 @@ package commands
import ( import (
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/google/go-containerregistry/pkg/v1" v1 "github.com/google/go-containerregistry/pkg/v1"
) )
type BaseCommand struct { type BaseCommand struct {

View File

@ -20,8 +20,8 @@ import (
"strings" "strings"
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/instructions"
) )

View File

@ -19,7 +19,7 @@ import (
"testing" "testing"
"github.com/GoogleContainerTools/kaniko/testutil" "github.com/GoogleContainerTools/kaniko/testutil"
"github.com/google/go-containerregistry/pkg/v1" v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/instructions"
) )

View File

@ -18,7 +18,7 @@ package commands
import ( import (
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/google/go-containerregistry/pkg/v1" v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"

View File

@ -27,7 +27,7 @@ import (
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/GoogleContainerTools/kaniko/pkg/util" "github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/google/go-containerregistry/pkg/v1" v1 "github.com/google/go-containerregistry/pkg/v1"
) )
type CopyCommand struct { type CopyCommand struct {

View File

@ -16,41 +16,103 @@ limitations under the License.
package commands package commands
import ( import (
"io/ioutil"
"os"
"path/filepath"
"testing" "testing"
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/GoogleContainerTools/kaniko/testutil" "github.com/GoogleContainerTools/kaniko/testutil"
"github.com/google/go-containerregistry/pkg/v1" v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/sirupsen/logrus"
) )
var copyTests = []struct { var copyTests = []struct {
SourcesAndDest []string name string
expectedDest []string SourcesAndDest []string
expectedDest []string
}{ }{
{ {
SourcesAndDest: []string{"/usr/bin/bash", "/tmp"}, name: "copy foo into tempCopyExecuteTest/",
expectedDest: []string{"/tmp/bash"}, SourcesAndDest: []string{"foo", "tempCopyExecuteTest/"},
expectedDest: []string{"foo"},
}, },
{ {
SourcesAndDest: []string{"/usr/bin/bash", "/tmp/"}, name: "copy foo into tempCopyExecuteTest",
expectedDest: []string{"/tmp/bash"}, SourcesAndDest: []string{"foo", "tempCopyExecuteTest"},
expectedDest: []string{"tempCopyExecuteTest"},
}, },
} }
func TestCopyExecuteCmd(t *testing.T) { func TestCopyExecuteCmd(t *testing.T) {
cfg := &v1.Config{ cfg := &v1.Config{
Cmd: nil, Cmd: nil,
Env: []string{},
WorkingDir: "../../integration/context/",
} }
for _, test := range copyTests { for _, test := range copyTests {
cmd := CopyCommand{ t.Run(test.name, func(t *testing.T) {
cmd: &instructions.CopyCommand{ dirList := []string{}
SourcesAndDest: instructions.SourcesAndDest{
SourcesAndDest: test.SourcesAndDest, logrus.Infof("Running test: %v", test.name)
},
} cmd := CopyCommand{
err := cmd.ExecuteCommand(cfg, nil) cmd: &instructions.CopyCommand{
testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedDest, cfg.WorkingDir) SourcesAndDest: test.SourcesAndDest,
},
buildcontext: "../../integration/context/",
}
buildArgs := copySetUpBuildArgs()
dest := cfg.WorkingDir + test.SourcesAndDest[len(test.SourcesAndDest)-1]
os.RemoveAll(dest)
err := cmd.ExecuteCommand(cfg, buildArgs)
fi, ferr := os.Open(dest)
if ferr != nil {
t.Error()
}
defer fi.Close()
fstat, ferr := fi.Stat()
if ferr != nil {
t.Error()
}
if fstat.IsDir() {
files, ferr := ioutil.ReadDir(dest)
if ferr != nil {
t.Error()
}
for _, file := range files {
logrus.Infof("file: %v", file.Name())
dirList = append(dirList, file.Name())
}
} else {
dirList = append(dirList, filepath.Base(dest))
}
//dir, err := os.Getwd()
// logrus.Infof("CWD: %v", dir)
// logrus.Infof("test.SourcesAndDest: %v", test.SourcesAndDest)
logrus.Infof("dest: %v", dest)
logrus.Infof("test.expectedDest: %v", test.expectedDest)
logrus.Infof("dirList: %v", dirList)
testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedDest, dirList)
os.RemoveAll(dest)
})
} }
} }
func copySetUpBuildArgs() *dockerfile.BuildArgs {
buildArgs := dockerfile.NewBuildArgs([]string{
"buildArg1=foo",
"buildArg2=foo2",
})
buildArgs.AddArg("buildArg1", nil)
d := "default"
buildArgs.AddArg("buildArg2", &d)
return buildArgs
}

View File

@ -20,8 +20,8 @@ import (
"strings" "strings"
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/instructions"
) )

View File

@ -19,7 +19,7 @@ import (
"testing" "testing"
"github.com/GoogleContainerTools/kaniko/testutil" "github.com/GoogleContainerTools/kaniko/testutil"
"github.com/google/go-containerregistry/pkg/v1" v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/instructions"
) )

View File

@ -18,9 +18,9 @@ package commands
import ( import (
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/GoogleContainerTools/kaniko/pkg/util" "github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/instructions"
) )

View File

@ -20,7 +20,7 @@ import (
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/GoogleContainerTools/kaniko/testutil" "github.com/GoogleContainerTools/kaniko/testutil"
"github.com/google/go-containerregistry/pkg/v1" v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/instructions"
) )

View File

@ -21,9 +21,9 @@ import (
"strings" "strings"
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/GoogleContainerTools/kaniko/pkg/util" "github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )

View File

@ -20,9 +20,9 @@ import (
"testing" "testing"
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/GoogleContainerTools/kaniko/testutil" "github.com/GoogleContainerTools/kaniko/testutil"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/instructions"
) )

View File

@ -18,7 +18,7 @@ package commands
import ( import (
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/google/go-containerregistry/pkg/v1" v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/instructions"
) )

View File

@ -18,9 +18,9 @@ package commands
import ( import (
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/GoogleContainerTools/kaniko/pkg/util" "github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )

View File

@ -21,7 +21,7 @@ import (
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/GoogleContainerTools/kaniko/testutil" "github.com/GoogleContainerTools/kaniko/testutil"
"github.com/google/go-containerregistry/pkg/v1" v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/instructions"
) )

View File

@ -18,7 +18,7 @@ package commands
import ( import (
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/google/go-containerregistry/pkg/v1" v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )

View File

@ -20,9 +20,9 @@ import (
"testing" "testing"
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/GoogleContainerTools/kaniko/testutil" "github.com/GoogleContainerTools/kaniko/testutil"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/instructions"
) )

View File

@ -32,7 +32,7 @@ import (
"github.com/GoogleContainerTools/kaniko/pkg/constants" "github.com/GoogleContainerTools/kaniko/pkg/constants"
"github.com/docker/docker/builder/dockerignore" "github.com/docker/docker/builder/dockerignore"
"github.com/docker/docker/pkg/fileutils" "github.com/docker/docker/pkg/fileutils"
"github.com/google/go-containerregistry/pkg/v1" v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -188,9 +188,12 @@ func extractFile(dest string, hdr *tar.Header, tr io.Reader) error {
switch hdr.Typeflag { switch hdr.Typeflag {
case tar.TypeReg: case tar.TypeReg:
logrus.Debugf("creating file %s", path) logrus.Debugf("creating file %s", path)
// It's possible a file is in the tar before its directory. // It's possible a file is in the tar before its directory,
if _, err := os.Stat(dir); os.IsNotExist(err) { // or a file was copied over a directory prior to now
fi, err := os.Stat(dir)
if os.IsNotExist(err) || !fi.IsDir() {
logrus.Debugf("base %s for file %s does not exist. Creating.", base, path) logrus.Debugf("base %s for file %s does not exist. Creating.", base, path)
os.RemoveAll(dir)
if err := os.MkdirAll(dir, 0755); err != nil { if err := os.MkdirAll(dir, 0755); err != nil {
return err return err
} }

View File

@ -28,7 +28,7 @@ import (
"github.com/GoogleContainerTools/kaniko/pkg/creds" "github.com/GoogleContainerTools/kaniko/pkg/creds"
"github.com/google/go-containerregistry/pkg/name" "github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1" v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty" "github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/remote" "github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/tarball" "github.com/google/go-containerregistry/pkg/v1/tarball"