Skip to content

Commit 500dca1

Browse files
committed
Export ExpandDir function and add an extra return value
1 parent 43b5215 commit 500dca1

File tree

2 files changed

+103
-17
lines changed

2 files changed

+103
-17
lines changed

pkg/common/path.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func ExpandPackageDir(f genericclioptions.FileNameFlags) (genericclioptions.File
8080
return f, fmt.Errorf("expand package directory should pass one package directory. "+
8181
"Passed the following paths: %v", f.Filenames)
8282
}
83-
configFilepaths, err := expandDir((*f.Filenames)[0])
83+
_, configFilepaths, err := ExpandDir((*f.Filenames)[0])
8484
if err != nil {
8585
return f, err
8686
}
@@ -136,29 +136,34 @@ func FilterInputFile(in io.Reader, tmpDir string) error {
136136
return w.Write(filteredNodes)
137137
}
138138

139-
// expandDir takes a single package directory as a parameter, and returns
140-
// an array of config file paths excluding the inventory object. Returns
141-
// an error if one occurred while processing the paths.
142-
func expandDir(dir string) ([]string, error) {
139+
// ExpandDir takes a single package directory as a parameter, and returns
140+
// the inventory template filepath and an array of config file paths. If no
141+
// inventory template file, then the first return value is an empty string.
142+
// Returns an error if one occurred while processing the paths.
143+
func ExpandDir(dir string) (string, []string, error) {
143144
filepaths := []string{}
144145
r := kio.LocalPackageReader{PackagePath: dir}
145146
nodes, err := r.Read()
146147
if err != nil {
147-
return filepaths, err
148+
return "", filepaths, err
148149
}
150+
var invFilepath string
149151
for _, node := range nodes {
150152
meta, err := node.GetMeta()
151153
if err != nil {
152154
continue
153155
}
156+
path := meta.Annotations[kioutil.PathAnnotation]
157+
path = filepath.Join(dir, path)
154158
// If object has inventory label, skip it.
155159
labels := meta.Labels
156160
if _, exists := labels[InventoryLabel]; exists {
161+
if invFilepath == "" {
162+
invFilepath = path
163+
}
157164
continue
158165
}
159-
path := meta.Annotations[kioutil.PathAnnotation]
160-
path = filepath.Join(dir, path)
161166
filepaths = append(filepaths, path)
162167
}
163-
return filepaths, nil
168+
return invFilepath, filepaths, nil
164169
}

pkg/common/path_test.go

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,20 @@ import (
1717
)
1818

1919
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 = "---"
2527
)
2628

2729
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)
3134
)
3235

3336
func 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+
5772
var podA = []byte(`
5873
apiVersion: v1
5974
kind: 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+
221302
func TestExpandDirErrors(t *testing.T) {
222303
tf := setupTestFilesystem(t)
223304
defer tf.Clean()

0 commit comments

Comments
 (0)