@@ -17,6 +17,18 @@ import (
17
17
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
18
18
)
19
19
20
+ const validResource = `
21
+ apiVersion: v1
22
+ kind: Service
23
+ metadata:
24
+ name: myService
25
+ spec:
26
+ selector:
27
+ backend: bungie
28
+ ports:
29
+ - port: 7002
30
+ `
31
+
20
32
func TestTargetMustHaveKustomizationFile (t * testing.T ) {
21
33
th := kusttest_test .MakeHarness (t )
22
34
th .WriteF ("service.yaml" , `
@@ -63,17 +75,7 @@ func TestBaseMustHaveKustomizationFile(t *testing.T) {
63
75
resources:
64
76
- base
65
77
` )
66
- th .WriteF ("base/service.yaml" , `
67
- apiVersion: v1
68
- kind: Service
69
- metadata:
70
- name: myService
71
- spec:
72
- selector:
73
- backend: bungie
74
- ports:
75
- - port: 7002
76
- ` )
78
+ th .WriteF ("base/service.yaml" , validResource )
77
79
err := th .RunWithErr ("." , th .MakeDefaultOptions ())
78
80
if err == nil {
79
81
t .Fatalf ("expected an error" )
@@ -167,12 +169,35 @@ spec:
167
169
168
170
func TestAccumulateResourcesErrors (t * testing.T ) {
169
171
type testcase struct {
170
- name string
171
- resource string
172
- // regex error message that is output when kustomize tries to
173
- // accumulate resource as file and dir, respectively
172
+ name string
173
+ resource string
174
+ isAbsolute bool
175
+ files map [string ]string
176
+ // errFile, errDir are regex for the expected error message output
177
+ // when kustomize tries to accumulate resource as file and dir,
178
+ // respectively. The test substitutes occurrences of "%s" in the
179
+ // error strings with the absolute path where kustomize looks for it.
174
180
errFile , errDir string
175
181
}
182
+ populateAbsolutePaths := func (tc testcase , dir string ) testcase {
183
+ filePaths := make (map [string ]string , len (tc .files )+ 1 )
184
+ for file , content := range tc .files {
185
+ filePaths [filepath .Join (dir , file )] = content
186
+ }
187
+ resourcePath := filepath .Join (dir , tc .resource )
188
+ if tc .isAbsolute {
189
+ tc .resource = resourcePath
190
+ }
191
+ filePaths [filepath .Join (dir , "kustomization.yaml" )] = fmt .Sprintf (`
192
+ resources:
193
+ - %s
194
+ ` , tc .resource )
195
+ tc .files = filePaths
196
+ regPath := regexp .QuoteMeta (resourcePath )
197
+ tc .errFile = strings .ReplaceAll (tc .errFile , "%s" , regPath )
198
+ tc .errDir = strings .ReplaceAll (tc .errDir , "%s" , regPath )
199
+ return tc
200
+ }
176
201
buildError := func (tc testcase ) string {
177
202
const (
178
203
prefix = "accumulating resources"
@@ -196,39 +221,89 @@ func TestAccumulateResourcesErrors(t *testing.T) {
196
221
for _ , test := range []testcase {
197
222
{
198
223
name : "remote file not considered repo" ,
199
- // This url is too short to be considered a remote repo.
200
- resource : "https://raw.githubusercontent.com/kustomize" ,
201
- // It is acceptable that the error for a remote file-like reference
202
- // (that is not long enough to be considered a repo) does not
203
- // indicate said reference is not a local directory.
204
- // Though it is possible for the remote file-like reference to be
205
- // a local directory, it is very unlikely.
206
- errFile : `HTTP Error: status code 400 \(Bad Request \)\z` ,
224
+ // The example.com second-level domain is reserved and
225
+ // safe to access, see RFC 2606.
226
+ resource : "https://example.com/segments-too-few-to-be-repo" ,
227
+ // It's acceptable for the error output of a remote file-like
228
+ // resource to not indicate the resource's status as a
229
+ // local directory. Though it is possible for a remote file-like
230
+ // resource to be a local directory, it is very unlikely.
231
+ errFile : `HTTP Error: status code 404 \(Not Found \)\z` ,
207
232
},
208
233
{
209
234
name : "remote file qualifies as repo" ,
210
- resource : "https://raw.githubusercontent. com/kubernetes-sigs/kustomize/kustomize/v5.0.0/examples/helloWorld/configMap " ,
235
+ resource : "https://example. com/long/enough/to/have/org/and/repo " ,
211
236
// TODO(4788): This error message is technically wrong. Just
212
- // because we fail to GET a reference does not mean the reference is
213
- // not a remote file. We should return the GET status code instead .
237
+ // because we fail to GET a resource does not mean the resource is
238
+ // not a remote file. We should return the GET status code as well .
214
239
errFile : "URL is a git repository" ,
215
240
errDir : `failed to run \S+/git fetch --depth=1 .+` ,
216
241
},
242
+ {
243
+ name : "local file qualifies as repo" ,
244
+ // The .example top level domain is reserved for example purposes,
245
+ // see RFC 2606.
246
+ resource :
"[email protected] /configs/base" ,
247
+ errFile : `evalsymlink failure on '%s' .+` ,
248
+ errDir : `failed to run \S+/git fetch --depth=1 .+` ,
249
+ },
250
+ {
251
+ name : "relative path does not exist" ,
252
+ resource : "file-or-directory" ,
253
+ errFile : `evalsymlink failure on '%s' .+` ,
254
+ errDir : `must build at directory: not a valid directory: evalsymlink failure .+` ,
255
+ },
256
+ {
257
+ name : "absolute path does not exist" ,
258
+ resource : "file-or-directory" ,
259
+ isAbsolute : true ,
260
+ errFile : `evalsymlink failure on '%s' .+` ,
261
+ errDir : `new root '%s' cannot be absolute` ,
262
+ },
263
+ {
264
+ name : "relative file violates restrictions" ,
265
+ resource : "../base/resource.yaml" ,
266
+ files : map [string ]string {
267
+ "../base/resource.yaml" : validResource ,
268
+ },
269
+ errFile : "security; file '%s' is not in or below .+" ,
270
+ // TODO(4348): Over-inclusion of directory error message when we
271
+ // know resource is file.
272
+ errDir : "must build at directory: '%s': file is not directory" ,
273
+ },
274
+ {
275
+ name : "absolute file violates restrictions" ,
276
+ resource : "../base/resource.yaml" ,
277
+ isAbsolute : true ,
278
+ files : map [string ]string {
279
+ "../base/resource.yaml" : validResource ,
280
+ },
281
+ errFile : "security; file '%s' is not in or below .+" ,
282
+ // TODO(4348): Over-inclusion of directory error message when we
283
+ // know resource is file.
284
+ errDir : `new root '%s' cannot be absolute` ,
285
+ },
217
286
} {
218
287
t .Run (test .name , func (t * testing.T ) {
219
- // should use real file system to indicate that we are creating
220
- // new temporary directories on disk when we attempt to fetch repos
221
- fSys , tmpDir := kusttest_test .CreateKustDir (t , fmt .Sprintf (`
222
- resources:
223
- - %s
224
- ` , test .resource ))
288
+ // Should use real file system to indicate that we are creating
289
+ // new temporary directories on disk when we attempt to fetch repos.
290
+ fs , tmpDir := kusttest_test .Setup (t )
291
+ root := tmpDir .Join ("root" )
292
+ require .NoError (t , fs .Mkdir (root ))
293
+
294
+ test = populateAbsolutePaths (test , root )
295
+ for file , content := range test .files {
296
+ dir := filepath .Dir (file )
297
+ require .NoError (t , fs .MkdirAll (dir ))
298
+ require .NoError (t , fs .WriteFile (file , []byte (content )))
299
+ }
300
+
225
301
b := krusty .MakeKustomizer (krusty .MakeDefaultOptions ())
226
- _ , err := b .Run (fSys , tmpDir . String () )
302
+ _ , err := b .Run (fs , root )
227
303
require .Regexp (t , buildError (test ), err .Error ())
228
304
})
229
305
}
230
306
// TODO(annasong): add tests that check accumulateResources errors for
231
- // - local files
232
307
// - repos
233
308
// - local directories
234
309
// - files that yield malformed yaml errors
0 commit comments