From 500dca1d5c885c12abd637581b48edc3d03cfb19 Mon Sep 17 00:00:00 2001 From: Sean Sullivan Date: Mon, 26 Oct 2020 16:09:43 -0700 Subject: [PATCH] Export ExpandDir function and add an extra return value --- pkg/common/path.go | 23 ++++++---- pkg/common/path_test.go | 97 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 103 insertions(+), 17 deletions(-) diff --git a/pkg/common/path.go b/pkg/common/path.go index 5a10eb48..64f24a46 100644 --- a/pkg/common/path.go +++ b/pkg/common/path.go @@ -80,7 +80,7 @@ func ExpandPackageDir(f genericclioptions.FileNameFlags) (genericclioptions.File return f, fmt.Errorf("expand package directory should pass one package directory. "+ "Passed the following paths: %v", f.Filenames) } - configFilepaths, err := expandDir((*f.Filenames)[0]) + _, configFilepaths, err := ExpandDir((*f.Filenames)[0]) if err != nil { return f, err } @@ -136,29 +136,34 @@ func FilterInputFile(in io.Reader, tmpDir string) error { return w.Write(filteredNodes) } -// expandDir takes a single package directory as a parameter, and returns -// an array of config file paths excluding the inventory object. Returns -// an error if one occurred while processing the paths. -func expandDir(dir string) ([]string, error) { +// ExpandDir takes a single package directory as a parameter, and returns +// the inventory template filepath and an array of config file paths. If no +// inventory template file, then the first return value is an empty string. +// Returns an error if one occurred while processing the paths. +func ExpandDir(dir string) (string, []string, error) { filepaths := []string{} r := kio.LocalPackageReader{PackagePath: dir} nodes, err := r.Read() if err != nil { - return filepaths, err + return "", filepaths, err } + var invFilepath string for _, node := range nodes { meta, err := node.GetMeta() if err != nil { continue } + path := meta.Annotations[kioutil.PathAnnotation] + path = filepath.Join(dir, path) // If object has inventory label, skip it. labels := meta.Labels if _, exists := labels[InventoryLabel]; exists { + if invFilepath == "" { + invFilepath = path + } continue } - path := meta.Annotations[kioutil.PathAnnotation] - path = filepath.Join(dir, path) filepaths = append(filepaths, path) } - return filepaths, nil + return invFilepath, filepaths, nil } diff --git a/pkg/common/path_test.go b/pkg/common/path_test.go index 7c05c699..031f08d4 100644 --- a/pkg/common/path_test.go +++ b/pkg/common/path_test.go @@ -17,17 +17,20 @@ import ( ) const ( - packageDir = "test-pkg-dir" - inventoryFilename = "inventory.yaml" - podAFilename = "pod-a.yaml" - podBFilename = "pod-b.yaml" - configSeparator = "---" + packageDir = "test-pkg-dir" + subFolder = "sub-folder" + inventoryFilename = "inventory.yaml" + secondInventoryFilename = "inventory-2.yaml" + podAFilename = "pod-a.yaml" + podBFilename = "pod-b.yaml" + configSeparator = "---" ) var ( - inventoryFilePath = filepath.Join(packageDir, inventoryFilename) - podAFilePath = filepath.Join(packageDir, podAFilename) - podBFilePath = filepath.Join(packageDir, podBFilename) + inventoryFilePath = filepath.Join(packageDir, inventoryFilename) + secondInventoryFilePath = filepath.Join(packageDir, subFolder, secondInventoryFilename) + podAFilePath = filepath.Join(packageDir, podAFilename) + podBFilePath = filepath.Join(packageDir, podBFilename) ) func setupTestFilesystem(t *testing.T) testutil.TestFilesystem { @@ -37,6 +40,8 @@ func setupTestFilesystem(t *testing.T) testutil.TestFilesystem { tf := testutil.Setup(t, packageDir) t.Logf("Adding File: %s", inventoryFilePath) tf.WriteFile(t, inventoryFilePath, inventoryConfigMap) + t.Logf("Adding File: %s", secondInventoryFilePath) + tf.WriteFile(t, secondInventoryFilePath, secondInventoryConfigMap) t.Logf("Adding File: %s", podAFilePath) tf.WriteFile(t, podAFilePath, podA) t.Logf("Adding File: %s", podBFilePath) @@ -54,6 +59,16 @@ metadata: cli-utils.sigs.k8s.io/inventory-id: test-inventory `) +var secondInventoryConfigMap = []byte(` +apiVersion: v1 +kind: ConfigMap +metadata: + namespace: test-namespace + name: inventory-2 + labels: + cli-utils.sigs.k8s.io/inventory-id: test-inventory +`) + var podA = []byte(` apiVersion: v1 kind: Pod @@ -218,6 +233,72 @@ func TestFilterInputFile(t *testing.T) { } } +func TestExpandDir(t *testing.T) { + tf := setupTestFilesystem(t) + defer tf.Clean() + + testCases := map[string]struct { + packageDirPath string + expandedInventory string + expandedPaths []string + isError bool + }{ + "empty path is error": { + packageDirPath: "", + isError: true, + }, + "path that is not dir is error": { + packageDirPath: "fakedir1", + isError: true, + }, + "root package dir excludes inventory object": { + packageDirPath: tf.GetRootDir(), + expandedInventory: "inventory.yaml", + expandedPaths: []string{ + "pod-a.yaml", + "pod-b.yaml", + }, + isError: false, + }, + } + + for tn, tc := range testCases { + t.Run(tn, func(t *testing.T) { + actualInventory, actualPaths, err := ExpandDir(tc.packageDirPath) + if tc.isError { + if err == nil { + t.Fatalf("expected error but received none") + } + return + } + if err != nil { + t.Fatalf("received unexpected error %#v", err) + return + } + actualFilename := filepath.Base(actualInventory) + if tc.expandedInventory != actualFilename { + t.Errorf("expected inventory template filepath (%s), got (%s)", tc.expandedInventory, actualFilename) + } + if len(tc.expandedPaths) != len(actualPaths) { + t.Errorf("expected (%d) resource filepaths, got (%d)", len(tc.expandedPaths), len(actualPaths)) + } + for _, expectedPath := range tc.expandedPaths { + found := false + for _, actualPath := range actualPaths { + actualFilename := filepath.Base(actualPath) + if expectedPath == actualFilename { + found = true + break + } + } + if !found { + t.Errorf("expected filename (%s) not found", expectedPath) + } + } + }) + } +} + func TestExpandDirErrors(t *testing.T) { tf := setupTestFilesystem(t) defer tf.Clean()