Skip to content

Commit

Permalink
Add test case for skaffold init walk
Browse files Browse the repository at this point in the history
In this change:
1. Refactor walk into its own function
2. Add sample directory structure and tests.
  • Loading branch information
tejal29 committed Mar 18, 2019
1 parent be9b932 commit 83cfb50
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 29 deletions.
64 changes: 35 additions & 29 deletions pkg/skaffold/initializer/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,35 +77,7 @@ func DoInit(out io.Writer, c Config) error {
}
}

var potentialConfigs, dockerfiles []string

err := filepath.Walk(rootDir, func(path string, f os.FileInfo, e error) error {
if f.IsDir() && util.IsHiddenDir(f.Name()) {
logrus.Debugf("skip walking hidden dir %s", f.Name())
return filepath.SkipDir
}
if f.IsDir() || util.IsHiddenFile(f.Name()) {
return nil
}
if IsSkaffoldConfig(path) {
if !c.Force {
return fmt.Errorf("pre-existing %s found", path)
}
logrus.Debugf("%s is a valid skaffold configuration: continuing since --force=true", path)
return nil
}
if IsSupportedKubernetesFileExtension(path) {
potentialConfigs = append(potentialConfigs, path)
return nil
}
// try and parse dockerfile
if docker.ValidateDockerfile(path) {
logrus.Infof("existing dockerfile found: %s", path)
dockerfiles = append(dockerfiles, path)
}
return nil
})

potentialConfigs, dockerfiles, err := walk(rootDir, c.Force, docker.ValidateDockerfile)
if err != nil {
return err
}
Expand Down Expand Up @@ -309,3 +281,37 @@ type dockerfilePair struct {
Dockerfile string
ImageName string
}

func walk(dir string, force bool, validateDockerfile func(string) bool) ([]string, []string, error) {
var dockerfiles, potentialConfigs []string
err := filepath.Walk(dir, func(path string, f os.FileInfo, e error) error {
if f.IsDir() && util.IsHiddenDir(f.Name()) {
logrus.Debugf("skip walking hidden dir %s", f.Name())
return filepath.SkipDir
}
if f.IsDir() || util.IsHiddenFile(f.Name()) {
return nil
}
if IsSkaffoldConfig(path) {
if !force {
return fmt.Errorf("pre-existing %s found", path)
}
logrus.Debugf("%s is a valid skaffold configuration: continuing since --force=true", path)
return nil
}
if IsSupportedKubernetesFileExtension(path) {
potentialConfigs = append(potentialConfigs, path)
return nil
}
// try and parse dockerfile
if validateDockerfile(path) {
logrus.Infof("existing dockerfile found: %s", path)
dockerfiles = append(dockerfiles, path)
}
return nil
})
if err != nil {
return nil, nil, err
}
return potentialConfigs, dockerfiles, nil
}
147 changes: 147 additions & 0 deletions pkg/skaffold/initializer/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ package initializer

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

"github.com/GoogleContainerTools/skaffold/testutil"
Expand Down Expand Up @@ -59,3 +63,146 @@ func TestPrintAnalyzeJSON(t *testing.T) {
})
}
}

func TestWalk(t *testing.T) {
emptyFile := []byte("")
tests := []struct {
name string
filesWithContents map[string][]byte
expectedConfigs []string
expectedDockerfiles []string
force bool
err bool
}{
{
name: "shd return correct k8 configs and dockerfiles",
filesWithContents: map[string][]byte{
"config/test.yaml": emptyFile,
"k8pod.yml": emptyFile,
"README": emptyFile,
"deploy/Dockerfile": emptyFile,
"Dockerfile": emptyFile,
},
force: false,
expectedConfigs: []string{
"config/test.yaml",
"k8pod.yml",
},
expectedDockerfiles: []string{
"Dockerfile",
"deploy/Dockerfile",
},
err: false,
},
{
name: "shd skip hidden dir",
filesWithContents: map[string][]byte{
".hidden/test.yaml": emptyFile,
"k8pod.yml": emptyFile,
"README": emptyFile,
".hidden/Dockerfile": emptyFile,
"Dockerfile": emptyFile,
},
force: false,
expectedConfigs: []string{
"k8pod.yml",
},
expectedDockerfiles: []string{
"Dockerfile",
},
err: false,
},
{
name: "shd not error when skaffold.config present and force = true",
filesWithContents: map[string][]byte{
"skaffold.yaml": []byte(`apiVersion: skaffold/v1beta6
kind: Config
deploy:
kustomize: {}`),
"config/test.yaml": emptyFile,
"k8pod.yml": emptyFile,
"README": emptyFile,
"deploy/Dockerfile": emptyFile,
"Dockerfile": emptyFile,
},
force: true,
expectedConfigs: []string{
"config/test.yaml",
"k8pod.yml",
},
expectedDockerfiles: []string{
"Dockerfile",
"deploy/Dockerfile",
},
err: false,
},
{
name: "shd error when skaffold.config present and force = false",
filesWithContents: map[string][]byte{
"config/test.yaml": emptyFile,
"k8pod.yml": emptyFile,
"README": emptyFile,
"deploy/Dockerfile": emptyFile,
"Dockerfile": emptyFile,
"skaffold.yaml": []byte(`apiVersion: skaffold/v1beta6
kind: Config
deploy:
kustomize: {}`),
},
force: false,
expectedConfigs: nil,
expectedDockerfiles: nil,
err: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
rootDir, err := ioutil.TempDir("", "test")
if err != nil {
t.Fatal(err)
}
createDirStructure(t, rootDir, test.filesWithContents)
potentialConfigs, dockerfiles, err := walk(rootDir, test.force, testValidDocker)
testutil.CheckErrorAndDeepEqual(t, test.err, err,
convertToAbsPath(rootDir, test.expectedConfigs), potentialConfigs)
testutil.CheckErrorAndDeepEqual(t, test.err, err,
convertToAbsPath(rootDir, test.expectedDockerfiles), dockerfiles)
os.Remove(rootDir)
})
}
}

func testValidDocker(path string) bool {
return strings.HasSuffix(path, "Dockerfile")
}

func createDirStructure(t *testing.T, dir string, filesWithContents map[string][]byte) {
t.Helper()
for file, content := range filesWithContents {
// Create Directory path if it does not exist.
absPath := filepath.Join(dir, filepath.Dir(file))
if _, err := os.Stat(absPath); err != nil {
if err := os.MkdirAll(absPath, os.ModePerm); err != nil {
t.Fatal(err)
}
}
// Create filepath with contents
f, err := os.Create(filepath.Join(dir, file))
if err != nil {
t.Fatal(err)
}
f.Write(content)
f.Close()
}
}

func convertToAbsPath(dir string, files []string) []string {
if files == nil {
return files
}
absPaths := make([]string, len(files))
for i, file := range files {
absPaths[i] = filepath.Join(dir, file)
}
return absPaths
}

0 comments on commit 83cfb50

Please sign in to comment.