Refactoring to add use of a tempdir

This commit is contained in:
Don McCasland 2019-10-07 12:56:34 -07:00
parent 4e1639c030
commit 2eace1d511
No known key found for this signature in database
GPG Key ID: 2B332A542CE5FE1B
1 changed files with 67 additions and 19 deletions

View File

@ -16,9 +16,11 @@ limitations under the License.
package commands package commands
import ( import (
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
@ -45,45 +47,97 @@ var copyTests = []struct {
}, },
} }
func setupTestTemp() string {
tempDir, err := ioutil.TempDir("", "")
if err != nil {
logrus.Fatalf("error creating temp dir %s", err)
}
//defer os.RemoveAll(tempDir)
logrus.Infof("Tempdir: %s", tempDir)
srcPath, err := filepath.Abs("../../integration/context")
cperr := filepath.Walk(srcPath,
func(path string, info os.FileInfo, err error) error {
if path != srcPath {
if err != nil {
return err
}
tempPath := strings.TrimPrefix(path, srcPath)
fileInfo, err := os.Stat(path)
if err != nil {
return err
}
if fileInfo.IsDir() {
os.MkdirAll(tempDir+"/"+tempPath, 0777)
} else {
out, err := os.Create(tempDir + "/" + tempPath)
defer out.Close()
if err != nil {
return err
}
in, err := os.Open(path)
defer in.Close()
if err != nil {
return err
}
_, err = io.Copy(out, in)
if err != nil {
return err
}
}
}
return nil
})
if cperr != nil {
logrus.Fatalf("error populating temp dir %s", cperr)
}
return tempDir
}
func TestCopyExecuteCmd(t *testing.T) { func TestCopyExecuteCmd(t *testing.T) {
tempDir := setupTestTemp()
cfg := &v1.Config{ cfg := &v1.Config{
Cmd: nil, Cmd: nil,
Env: []string{}, Env: []string{},
WorkingDir: "../../integration/context/", WorkingDir: tempDir,
} }
for _, test := range copyTests { for _, test := range copyTests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
dirList := []string{} dirList := []string{}
logrus.Infof("Running test: %v", test.name)
cmd := CopyCommand{ cmd := CopyCommand{
cmd: &instructions.CopyCommand{ cmd: &instructions.CopyCommand{
SourcesAndDest: test.sourcesAndDest, SourcesAndDest: test.sourcesAndDest,
}, },
buildcontext: "../../integration/context/", buildcontext: tempDir,
} }
buildArgs := copySetUpBuildArgs() buildArgs := copySetUpBuildArgs()
dest := cfg.WorkingDir + test.sourcesAndDest[len(test.sourcesAndDest)-1] dest := cfg.WorkingDir + "/" + test.sourcesAndDest[len(test.sourcesAndDest)-1]
logrus.Infof("dest dir: %s", dest)
os.RemoveAll(dest) //os.RemoveAll(dest)
err := cmd.ExecuteCommand(cfg, buildArgs) err := cmd.ExecuteCommand(cfg, buildArgs)
if err != nil {
t.Error()
}
fi, ferr := os.Open(dest) fi, err := os.Open(dest)
if ferr != nil { if err != nil {
t.Error() t.Error()
} }
defer fi.Close() defer fi.Close()
fstat, ferr := fi.Stat() fstat, err := fi.Stat()
if ferr != nil { if err != nil {
t.Error() t.Error()
} }
if fstat.IsDir() { if fstat.IsDir() {
files, ferr := ioutil.ReadDir(dest) files, err := ioutil.ReadDir(dest)
if ferr != nil { if err != nil {
t.Error() t.Error()
} }
for _, file := range files { for _, file := range files {
@ -93,12 +147,6 @@ func TestCopyExecuteCmd(t *testing.T) {
} else { } else {
dirList = append(dirList, filepath.Base(dest)) 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) testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedDest, dirList)
os.RemoveAll(dest) os.RemoveAll(dest)