@@ -17,17 +17,20 @@ import (
1717)
1818
1919const (
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 = "---"
2527)
2628
2729var (
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 )
3134)
3235
3336func setupTestFilesystem (t * testing.T ) testutil.TestFilesystem {
@@ -37,6 +40,8 @@ func setupTestFilesystem(t *testing.T) testutil.TestFilesystem {
3740 tf := testutil .Setup (t , packageDir )
3841 t .Logf ("Adding File: %s" , inventoryFilePath )
3942 tf .WriteFile (t , inventoryFilePath , inventoryConfigMap )
43+ t .Logf ("Adding File: %s" , secondInventoryFilePath )
44+ tf .WriteFile (t , secondInventoryFilePath , secondInventoryConfigMap )
4045 t .Logf ("Adding File: %s" , podAFilePath )
4146 tf .WriteFile (t , podAFilePath , podA )
4247 t .Logf ("Adding File: %s" , podBFilePath )
@@ -54,6 +59,16 @@ metadata:
5459 cli-utils.sigs.k8s.io/inventory-id: test-inventory
5560` )
5661
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+
5772var podA = []byte (`
5873apiVersion: v1
5974kind: Pod
@@ -218,6 +233,72 @@ func TestFilterInputFile(t *testing.T) {
218233 }
219234}
220235
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+
221302func TestExpandDirErrors (t * testing.T ) {
222303 tf := setupTestFilesystem (t )
223304 defer tf .Clean ()
0 commit comments