From 83cfb503d2d4b7b2b98c5c32e5b36e03b28d2f80 Mon Sep 17 00:00:00 2001 From: Tejal Desai Date: Fri, 15 Mar 2019 14:54:53 -0700 Subject: [PATCH] Add test case for skaffold init walk In this change: 1. Refactor walk into its own function 2. Add sample directory structure and tests. --- pkg/skaffold/initializer/init.go | 64 ++++++----- pkg/skaffold/initializer/init_test.go | 147 ++++++++++++++++++++++++++ 2 files changed, 182 insertions(+), 29 deletions(-) diff --git a/pkg/skaffold/initializer/init.go b/pkg/skaffold/initializer/init.go index 5189572ba54..5a7c81bd10a 100644 --- a/pkg/skaffold/initializer/init.go +++ b/pkg/skaffold/initializer/init.go @@ -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 } @@ -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 +} diff --git a/pkg/skaffold/initializer/init_test.go b/pkg/skaffold/initializer/init_test.go index 9e0c5440c7e..611323d3367 100644 --- a/pkg/skaffold/initializer/init_test.go +++ b/pkg/skaffold/initializer/init_test.go @@ -18,6 +18,10 @@ package initializer import ( "bytes" + "io/ioutil" + "os" + "path/filepath" + "strings" "testing" "github.com/GoogleContainerTools/skaffold/testutil" @@ -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 +}