Skip to content

Commit 4b0ee1f

Browse files
committed
mostly organizing test code + wrote some tests for stripTags
1 parent cc9a7f1 commit 4b0ee1f

13 files changed

+1026
-837
lines changed

pkg/skaffold/initializer/analyze.go

+22-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@ type analysis struct {
3131
builderAnalyzer *builderAnalyzer
3232
}
3333

34+
// newAnalysis sets up the analysis of the directory based on the initializer configuration
35+
func newAnalysis(c Config) *analysis {
36+
var builders *builderAnalyzer
37+
if !c.SkipBuild {
38+
builders = &builderAnalyzer{
39+
findBuilders: !c.SkipBuild,
40+
enableJibInit: c.EnableJibInit,
41+
enableBuildpackInit: c.EnableBuildpackInit,
42+
}
43+
}
44+
a := &analysis{
45+
kubectlAnalyzer: &kubectlAnalyzer{},
46+
builderAnalyzer: builders,
47+
skaffoldAnalyzer: &skaffoldConfigAnalyzer{
48+
force: c.Force,
49+
},
50+
}
51+
return a
52+
}
53+
3454
// analyze recursively walks a directory and notifies the analyzers of files and enterDir and exitDir events
3555
// at the end of the analyze function the analysis struct's analyzers should contain the state that we can
3656
// use to do further computation.
@@ -105,14 +125,14 @@ type directoryAnalyzer struct {
105125
currentDir string
106126
}
107127

108-
func (a *directoryAnalyzer) analyzeFile(filePath string) error {
128+
func (a *directoryAnalyzer) analyzeFile(_ string) error {
109129
return nil
110130
}
111131

112132
func (a *directoryAnalyzer) enterDir(dir string) {
113133
a.currentDir = dir
114134
}
115135

116-
func (a *directoryAnalyzer) exitDir(dir string) {
136+
func (a *directoryAnalyzer) exitDir(_ string) {
117137
//pass
118138
}
+285
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
/*
2+
Copyright 2020 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package initializer
18+
19+
import (
20+
"strings"
21+
"testing"
22+
23+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/jib"
24+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
25+
"github.com/GoogleContainerTools/skaffold/testutil"
26+
)
27+
28+
func TestAnalyze(t *testing.T) {
29+
emptyFile := ""
30+
validK8sManifest := "apiVersion: v1\nkind: Service\nmetadata:\n name: test\n"
31+
32+
tests := []struct {
33+
description string
34+
filesWithContents map[string]string
35+
expectedConfigs []string
36+
expectedPaths []string
37+
config Config
38+
shouldErr bool
39+
}{
40+
{
41+
description: "should return correct k8 configs and build files (backwards compatibility)",
42+
filesWithContents: map[string]string{
43+
"config/test.yaml": validK8sManifest,
44+
"config/invalid.yaml": emptyFile,
45+
"k8pod.yml": validK8sManifest,
46+
"README": emptyFile,
47+
"deploy/Dockerfile": emptyFile,
48+
"deploy/Dockerfile.dev": emptyFile,
49+
"deploy/dev.Dockerfile": emptyFile,
50+
"deploy/test.dockerfile": emptyFile,
51+
"gradle/build.gradle": emptyFile,
52+
"maven/pom.xml": emptyFile,
53+
"Dockerfile": emptyFile,
54+
},
55+
config: Config{
56+
Force: false,
57+
EnableBuildpackInit: false,
58+
EnableJibInit: false,
59+
},
60+
expectedConfigs: []string{
61+
"k8pod.yml",
62+
"config/test.yaml",
63+
},
64+
expectedPaths: []string{
65+
"Dockerfile",
66+
"deploy/Dockerfile",
67+
"deploy/Dockerfile.dev",
68+
"deploy/dev.Dockerfile",
69+
"deploy/test.dockerfile",
70+
},
71+
shouldErr: false,
72+
},
73+
{
74+
description: "should return correct k8 configs and build files",
75+
filesWithContents: map[string]string{
76+
"config/test.yaml": validK8sManifest,
77+
"config/invalid.yaml": emptyFile,
78+
"k8pod.yml": validK8sManifest,
79+
"README": emptyFile,
80+
"deploy/Dockerfile": emptyFile,
81+
"gradle/build.gradle": emptyFile,
82+
"maven/pom.xml": emptyFile,
83+
"Dockerfile": emptyFile,
84+
"node/package.json": emptyFile,
85+
},
86+
config: Config{
87+
Force: false,
88+
EnableBuildpackInit: true,
89+
EnableJibInit: true,
90+
},
91+
expectedConfigs: []string{
92+
"k8pod.yml",
93+
"config/test.yaml",
94+
},
95+
expectedPaths: []string{
96+
"Dockerfile",
97+
"deploy/Dockerfile",
98+
"gradle/build.gradle",
99+
"maven/pom.xml",
100+
"node/package.json",
101+
},
102+
shouldErr: false,
103+
},
104+
{
105+
description: "skip validating nested jib configs",
106+
filesWithContents: map[string]string{
107+
"config/test.yaml": validK8sManifest,
108+
"k8pod.yml": validK8sManifest,
109+
"gradle/build.gradle": emptyFile,
110+
"gradle/subproject/build.gradle": emptyFile,
111+
"maven/asubproject/pom.xml": emptyFile,
112+
"maven/pom.xml": emptyFile,
113+
},
114+
config: Config{
115+
Force: false,
116+
EnableBuildpackInit: false,
117+
EnableJibInit: true,
118+
},
119+
expectedConfigs: []string{
120+
"k8pod.yml",
121+
"config/test.yaml",
122+
},
123+
expectedPaths: []string{
124+
"gradle/build.gradle",
125+
"maven/pom.xml",
126+
},
127+
shouldErr: false,
128+
},
129+
{
130+
description: "multiple builders in same directory",
131+
filesWithContents: map[string]string{
132+
"build.gradle": emptyFile,
133+
"ignored-builder/build.gradle": emptyFile,
134+
"not-ignored-config/test.yaml": validK8sManifest,
135+
"Dockerfile": emptyFile,
136+
"k8pod.yml": validK8sManifest,
137+
"pom.xml": emptyFile,
138+
},
139+
config: Config{
140+
Force: false,
141+
EnableBuildpackInit: false,
142+
EnableJibInit: true,
143+
},
144+
expectedConfigs: []string{
145+
"k8pod.yml",
146+
"not-ignored-config/test.yaml",
147+
},
148+
expectedPaths: []string{
149+
"Dockerfile",
150+
"build.gradle",
151+
"pom.xml",
152+
},
153+
shouldErr: false,
154+
},
155+
{
156+
description: "should skip hidden dir",
157+
filesWithContents: map[string]string{
158+
".hidden/test.yaml": validK8sManifest,
159+
"k8pod.yml": validK8sManifest,
160+
"README": emptyFile,
161+
".hidden/Dockerfile": emptyFile,
162+
"Dockerfile": emptyFile,
163+
},
164+
config: Config{
165+
Force: false,
166+
EnableBuildpackInit: false,
167+
EnableJibInit: true,
168+
},
169+
expectedConfigs: []string{
170+
"k8pod.yml",
171+
},
172+
expectedPaths: []string{
173+
"Dockerfile",
174+
},
175+
shouldErr: false,
176+
},
177+
{
178+
description: "should not error when skaffold.config present and force = true",
179+
filesWithContents: map[string]string{
180+
"skaffold.yaml": `apiVersion: skaffold/v1beta6
181+
kind: Config
182+
deploy:
183+
kustomize: {}`,
184+
"config/test.yaml": validK8sManifest,
185+
"k8pod.yml": validK8sManifest,
186+
"README": emptyFile,
187+
"deploy/Dockerfile": emptyFile,
188+
"Dockerfile": emptyFile,
189+
},
190+
config: Config{
191+
Force: true,
192+
EnableBuildpackInit: false,
193+
EnableJibInit: true,
194+
},
195+
expectedConfigs: []string{
196+
"k8pod.yml",
197+
"config/test.yaml",
198+
},
199+
expectedPaths: []string{
200+
"Dockerfile",
201+
"deploy/Dockerfile",
202+
},
203+
shouldErr: false,
204+
},
205+
{
206+
description: "should error when skaffold.config present and force = false",
207+
filesWithContents: map[string]string{
208+
"config/test.yaml": validK8sManifest,
209+
"k8pod.yml": validK8sManifest,
210+
"README": emptyFile,
211+
"deploy/Dockerfile": emptyFile,
212+
"Dockerfile": emptyFile,
213+
"skaffold.yaml": `apiVersion: skaffold/v1beta6
214+
kind: Config
215+
deploy:
216+
kustomize: {}`,
217+
},
218+
config: Config{
219+
Force: false,
220+
EnableBuildpackInit: false,
221+
EnableJibInit: true,
222+
},
223+
expectedConfigs: nil,
224+
expectedPaths: nil,
225+
shouldErr: true,
226+
},
227+
{
228+
description: "should error when skaffold.config present with jib config",
229+
filesWithContents: map[string]string{
230+
"config/test.yaml": validK8sManifest,
231+
"k8pod.yml": validK8sManifest,
232+
"README": emptyFile,
233+
"pom.xml": emptyFile,
234+
"skaffold.yaml": `apiVersion: skaffold/v1beta6
235+
kind: Config
236+
deploy:
237+
kustomize: {}`,
238+
},
239+
config: Config{
240+
Force: false,
241+
EnableBuildpackInit: false,
242+
EnableJibInit: true,
243+
},
244+
expectedConfigs: nil,
245+
expectedPaths: nil,
246+
shouldErr: true,
247+
},
248+
}
249+
for _, test := range tests {
250+
testutil.Run(t, test.description, func(t *testutil.T) {
251+
tmpDir := t.NewTempDir().WriteFiles(test.filesWithContents)
252+
253+
t.Override(&docker.Validate, fakeValidateDockerfile)
254+
t.Override(&jib.Validate, fakeValidateJibConfig)
255+
256+
a := newAnalysis(test.config)
257+
err := a.analyze(tmpDir.Root())
258+
259+
t.CheckError(test.shouldErr, err)
260+
if test.shouldErr {
261+
return
262+
}
263+
264+
t.CheckDeepEqual(tmpDir.Paths(test.expectedConfigs...), a.kubectlAnalyzer.kubernetesManifests)
265+
t.CheckDeepEqual(len(test.expectedPaths), len(a.builderAnalyzer.foundBuilders))
266+
for i := range a.builderAnalyzer.foundBuilders {
267+
t.CheckDeepEqual(tmpDir.Path(test.expectedPaths[i]), a.builderAnalyzer.foundBuilders[i].Path())
268+
}
269+
})
270+
}
271+
}
272+
273+
func fakeValidateDockerfile(path string) bool {
274+
return strings.Contains(strings.ToLower(path), "dockerfile")
275+
}
276+
277+
func fakeValidateJibConfig(path string) []jib.ArtifactConfig {
278+
if strings.HasSuffix(path, "build.gradle") {
279+
return []jib.ArtifactConfig{{BuilderName: jib.PluginName(jib.JibGradle), File: path}}
280+
}
281+
if strings.HasSuffix(path, "pom.xml") {
282+
return []jib.ArtifactConfig{{BuilderName: jib.PluginName(jib.JibMaven), File: path}}
283+
}
284+
return nil
285+
}

0 commit comments

Comments
 (0)