Skip to content

Commit 7174184

Browse files
committed
Add test case for skaffold init walk
In this change: 1. Refactor walk into its own function 2. Add sample directory structure and tests.
1 parent 891f9b0 commit 7174184

File tree

2 files changed

+182
-29
lines changed

2 files changed

+182
-29
lines changed

pkg/skaffold/initializer/init.go

+35-29
Original file line numberDiff line numberDiff line change
@@ -77,35 +77,7 @@ func DoInit(out io.Writer, c Config) error {
7777
}
7878
}
7979

80-
var potentialConfigs, dockerfiles []string
81-
82-
err := filepath.Walk(rootDir, func(path string, f os.FileInfo, e error) error {
83-
if f.IsDir() && util.IsHiddenDir(f.Name()) {
84-
logrus.Debugf("skip walking hidden dir %s", f.Name())
85-
return filepath.SkipDir
86-
}
87-
if f.IsDir() || util.IsHiddenFile(f.Name()) {
88-
return nil
89-
}
90-
if IsSkaffoldConfig(path) {
91-
if !c.Force {
92-
return fmt.Errorf("pre-existing %s found", path)
93-
}
94-
logrus.Debugf("%s is a valid skaffold configuration: continuing since --force=true", path)
95-
return nil
96-
}
97-
if IsSupportedKubernetesFileExtension(path) {
98-
potentialConfigs = append(potentialConfigs, path)
99-
return nil
100-
}
101-
// try and parse dockerfile
102-
if docker.ValidateDockerfile(path) {
103-
logrus.Infof("existing dockerfile found: %s", path)
104-
dockerfiles = append(dockerfiles, path)
105-
}
106-
return nil
107-
})
108-
80+
potentialConfigs, dockerfiles, err := walk(rootDir, c.Force, docker.ValidateDockerfile)
10981
if err != nil {
11082
return err
11183
}
@@ -309,3 +281,37 @@ type dockerfilePair struct {
309281
Dockerfile string
310282
ImageName string
311283
}
284+
285+
func walk(dir string, force bool, validateDockerfile func(string) bool) ([]string, []string, error) {
286+
var dockerfiles, potentialConfigs []string
287+
err := filepath.Walk(dir, func(path string, f os.FileInfo, e error) error {
288+
if f.IsDir() && util.IsHiddenDir(f.Name()) {
289+
logrus.Debugf("skip walking hidden dir %s", f.Name())
290+
return filepath.SkipDir
291+
}
292+
if f.IsDir() || util.IsHiddenFile(f.Name()) {
293+
return nil
294+
}
295+
if IsSkaffoldConfig(path) {
296+
if !force {
297+
return fmt.Errorf("pre-existing %s found", path)
298+
}
299+
logrus.Debugf("%s is a valid skaffold configuration: continuing since --force=true", path)
300+
return nil
301+
}
302+
if IsSupportedKubernetesFileExtension(path) {
303+
potentialConfigs = append(potentialConfigs, path)
304+
return nil
305+
}
306+
// try and parse dockerfile
307+
if validateDockerfile(path) {
308+
logrus.Infof("existing dockerfile found: %s", path)
309+
dockerfiles = append(dockerfiles, path)
310+
}
311+
return nil
312+
})
313+
if err != nil {
314+
return nil, nil, err
315+
}
316+
return potentialConfigs, dockerfiles, nil
317+
}

pkg/skaffold/initializer/init_test.go

+147
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ package initializer
1818

1919
import (
2020
"bytes"
21+
"io/ioutil"
22+
"os"
23+
"path/filepath"
24+
"strings"
2125
"testing"
2226

2327
"github.com/GoogleContainerTools/skaffold/testutil"
@@ -59,3 +63,146 @@ func TestPrintAnalyzeJSON(t *testing.T) {
5963
})
6064
}
6165
}
66+
67+
func TestWalk(t *testing.T) {
68+
emptyFile := []byte("")
69+
tests := []struct {
70+
name string
71+
filesWithContents map[string][]byte
72+
expectedConfigs []string
73+
expectedDockerfiles []string
74+
force bool
75+
err bool
76+
}{
77+
{
78+
name: "shd return correct k8 configs and dockerfiles",
79+
filesWithContents: map[string][]byte{
80+
"config/test.yaml": emptyFile,
81+
"k8pod.yml": emptyFile,
82+
"README": emptyFile,
83+
"deploy/Dockerfile": emptyFile,
84+
"Dockerfile": emptyFile,
85+
},
86+
force: false,
87+
expectedConfigs: []string{
88+
"config/test.yaml",
89+
"k8pod.yml",
90+
},
91+
expectedDockerfiles: []string{
92+
"Dockerfile",
93+
"deploy/Dockerfile",
94+
},
95+
err: false,
96+
},
97+
{
98+
name: "shd skip hidden dir",
99+
filesWithContents: map[string][]byte{
100+
".hidden/test.yaml": emptyFile,
101+
"k8pod.yml": emptyFile,
102+
"README": emptyFile,
103+
".hidden/Dockerfile": emptyFile,
104+
"Dockerfile": emptyFile,
105+
},
106+
force: false,
107+
expectedConfigs: []string{
108+
"k8pod.yml",
109+
},
110+
expectedDockerfiles: []string{
111+
"Dockerfile",
112+
},
113+
err: false,
114+
},
115+
{
116+
name: "shd not error when skaffold.config present and force = true",
117+
filesWithContents: map[string][]byte{
118+
"skaffold.yaml": []byte(`apiVersion: skaffold/v1beta6
119+
kind: Config
120+
deploy:
121+
kustomize: {}`),
122+
"config/test.yaml": emptyFile,
123+
"k8pod.yml": emptyFile,
124+
"README": emptyFile,
125+
"deploy/Dockerfile": emptyFile,
126+
"Dockerfile": emptyFile,
127+
},
128+
force: true,
129+
expectedConfigs: []string{
130+
"config/test.yaml",
131+
"k8pod.yml",
132+
},
133+
expectedDockerfiles: []string{
134+
"Dockerfile",
135+
"deploy/Dockerfile",
136+
},
137+
err: false,
138+
},
139+
{
140+
name: "shd error when skaffold.config present and force = false",
141+
filesWithContents: map[string][]byte{
142+
"config/test.yaml": emptyFile,
143+
"k8pod.yml": emptyFile,
144+
"README": emptyFile,
145+
"deploy/Dockerfile": emptyFile,
146+
"Dockerfile": emptyFile,
147+
"skaffold.yaml": []byte(`apiVersion: skaffold/v1beta6
148+
kind: Config
149+
deploy:
150+
kustomize: {}`),
151+
},
152+
force: false,
153+
expectedConfigs: nil,
154+
expectedDockerfiles: nil,
155+
err: true,
156+
},
157+
}
158+
for _, test := range tests {
159+
t.Run(test.name, func(t *testing.T) {
160+
rootDir, err := ioutil.TempDir("", "test")
161+
if err != nil {
162+
t.Fatal(err)
163+
}
164+
createDirStructure(t, rootDir, test.filesWithContents)
165+
potentialConfigs, dockerfiles, err := walk(rootDir, test.force, testValidDocker)
166+
testutil.CheckErrorAndDeepEqual(t, test.err, err,
167+
convertToAbsPath(rootDir, test.expectedConfigs), potentialConfigs)
168+
testutil.CheckErrorAndDeepEqual(t, test.err, err,
169+
convertToAbsPath(rootDir, test.expectedDockerfiles), dockerfiles)
170+
os.Remove(rootDir)
171+
})
172+
}
173+
}
174+
175+
func testValidDocker(path string) bool {
176+
return strings.HasSuffix(path, "Dockerfile")
177+
}
178+
179+
func createDirStructure(t *testing.T, dir string, filesWithContents map[string][]byte) {
180+
t.Helper()
181+
for file, content := range filesWithContents {
182+
// Create Directory path if it does not exist.
183+
absPath := filepath.Join(dir, filepath.Dir(file))
184+
if _, err := os.Stat(absPath); err != nil {
185+
if err := os.MkdirAll(absPath, os.ModePerm); err != nil {
186+
t.Fatal(err)
187+
}
188+
}
189+
// Create filepath with contents
190+
f, err := os.Create(filepath.Join(dir, file))
191+
if err != nil {
192+
t.Fatal(err)
193+
}
194+
f.Write(content)
195+
f.Close()
196+
}
197+
}
198+
199+
func convertToAbsPath(dir string, files []string) []string {
200+
if files == nil {
201+
return files
202+
}
203+
absPaths := make([]string, len(files))
204+
for i, file := range files {
205+
absPaths[i] = filepath.Join(dir, file)
206+
}
207+
return absPaths
208+
}

0 commit comments

Comments
 (0)