Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions pkg/common/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work for packages with subfolders? It might be worth adding a test for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added unit test for this case.

// 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
}
97 changes: 89 additions & 8 deletions pkg/common/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down