@@ -17,17 +17,20 @@ import (
17
17
)
18
18
19
19
const (
20
- packageDir = "test-pkg-dir"
21
- inventoryFilename = "inventory.yaml"
22
- podAFilename = "pod-a.yaml"
23
- podBFilename = "pod-b.yaml"
24
- configSeparator = "---"
20
+ packageDir = "test-pkg-dir"
21
+ subFolder = "sub-folder"
22
+ inventoryFilename = "inventory.yaml"
23
+ secondInventoryFilename = "inventory-2.yaml"
24
+ podAFilename = "pod-a.yaml"
25
+ podBFilename = "pod-b.yaml"
26
+ configSeparator = "---"
25
27
)
26
28
27
29
var (
28
- inventoryFilePath = filepath .Join (packageDir , inventoryFilename )
29
- podAFilePath = filepath .Join (packageDir , podAFilename )
30
- podBFilePath = filepath .Join (packageDir , podBFilename )
30
+ inventoryFilePath = filepath .Join (packageDir , inventoryFilename )
31
+ secondInventoryFilePath = filepath .Join (packageDir , subFolder , secondInventoryFilename )
32
+ podAFilePath = filepath .Join (packageDir , podAFilename )
33
+ podBFilePath = filepath .Join (packageDir , podBFilename )
31
34
)
32
35
33
36
func setupTestFilesystem (t * testing.T ) testutil.TestFilesystem {
@@ -37,6 +40,8 @@ func setupTestFilesystem(t *testing.T) testutil.TestFilesystem {
37
40
tf := testutil .Setup (t , packageDir )
38
41
t .Logf ("Adding File: %s" , inventoryFilePath )
39
42
tf .WriteFile (t , inventoryFilePath , inventoryConfigMap )
43
+ t .Logf ("Adding File: %s" , secondInventoryFilePath )
44
+ tf .WriteFile (t , secondInventoryFilePath , secondInventoryConfigMap )
40
45
t .Logf ("Adding File: %s" , podAFilePath )
41
46
tf .WriteFile (t , podAFilePath , podA )
42
47
t .Logf ("Adding File: %s" , podBFilePath )
@@ -54,6 +59,16 @@ metadata:
54
59
cli-utils.sigs.k8s.io/inventory-id: test-inventory
55
60
` )
56
61
62
+ var secondInventoryConfigMap = []byte (`
63
+ apiVersion: v1
64
+ kind: ConfigMap
65
+ metadata:
66
+ namespace: test-namespace
67
+ name: inventory-2
68
+ labels:
69
+ cli-utils.sigs.k8s.io/inventory-id: test-inventory
70
+ ` )
71
+
57
72
var podA = []byte (`
58
73
apiVersion: v1
59
74
kind: Pod
@@ -218,6 +233,72 @@ func TestFilterInputFile(t *testing.T) {
218
233
}
219
234
}
220
235
236
+ func TestExpandDir (t * testing.T ) {
237
+ tf := setupTestFilesystem (t )
238
+ defer tf .Clean ()
239
+
240
+ testCases := map [string ]struct {
241
+ packageDirPath string
242
+ expandedInventory string
243
+ expandedPaths []string
244
+ isError bool
245
+ }{
246
+ "empty path is error" : {
247
+ packageDirPath : "" ,
248
+ isError : true ,
249
+ },
250
+ "path that is not dir is error" : {
251
+ packageDirPath : "fakedir1" ,
252
+ isError : true ,
253
+ },
254
+ "root package dir excludes inventory object" : {
255
+ packageDirPath : tf .GetRootDir (),
256
+ expandedInventory : "inventory.yaml" ,
257
+ expandedPaths : []string {
258
+ "pod-a.yaml" ,
259
+ "pod-b.yaml" ,
260
+ },
261
+ isError : false ,
262
+ },
263
+ }
264
+
265
+ for tn , tc := range testCases {
266
+ t .Run (tn , func (t * testing.T ) {
267
+ actualInventory , actualPaths , err := ExpandDir (tc .packageDirPath )
268
+ if tc .isError {
269
+ if err == nil {
270
+ t .Fatalf ("expected error but received none" )
271
+ }
272
+ return
273
+ }
274
+ if err != nil {
275
+ t .Fatalf ("received unexpected error %#v" , err )
276
+ return
277
+ }
278
+ actualFilename := filepath .Base (actualInventory )
279
+ if tc .expandedInventory != actualFilename {
280
+ t .Errorf ("expected inventory template filepath (%s), got (%s)" , tc .expandedInventory , actualFilename )
281
+ }
282
+ if len (tc .expandedPaths ) != len (actualPaths ) {
283
+ t .Errorf ("expected (%d) resource filepaths, got (%d)" , len (tc .expandedPaths ), len (actualPaths ))
284
+ }
285
+ for _ , expectedPath := range tc .expandedPaths {
286
+ found := false
287
+ for _ , actualPath := range actualPaths {
288
+ actualFilename := filepath .Base (actualPath )
289
+ if expectedPath == actualFilename {
290
+ found = true
291
+ break
292
+ }
293
+ }
294
+ if ! found {
295
+ t .Errorf ("expected filename (%s) not found" , expectedPath )
296
+ }
297
+ }
298
+ })
299
+ }
300
+ }
301
+
221
302
func TestExpandDirErrors (t * testing.T ) {
222
303
tf := setupTestFilesystem (t )
223
304
defer tf .Clean ()
0 commit comments