Fix: load local ComponentDefinitions recursively (#6414)

* fix: load local componentdefinitions recursively

Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>

* test: add dry-run offline def dir test

Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>

* test: fix unit tests

Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>

---------

Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>
This commit is contained in:
Tyler Gillson
2024-01-04 08:47:59 -08:00
committed by GitHub
parent 62efa9c787
commit 86dc53afab
4 changed files with 38 additions and 20 deletions
+23 -16
View File
@@ -133,7 +133,7 @@ func DryRunApplication(cmdOption *DryRunCmdOptions, c common.Args, namespace str
var objs []*unstructured.Unstructured
if cmdOption.DefinitionFile != "" {
objs, err = ReadDefinitionsFromFile(cmdOption.DefinitionFile)
objs, err = ReadDefinitionsFromFile(cmdOption.DefinitionFile, cmdOption.IOStreams)
if err != nil {
return buff, err
}
@@ -211,7 +211,7 @@ func readObj(path string) (*unstructured.Unstructured, error) {
}
// ReadDefinitionsFromFile will read objects from file or dir in the format of yaml
func ReadDefinitionsFromFile(path string) ([]*unstructured.Unstructured, error) {
func ReadDefinitionsFromFile(path string, io cmdutil.IOStreams) ([]*unstructured.Unstructured, error) {
fi, err := os.Stat(path)
if err != nil {
return nil, err
@@ -225,24 +225,31 @@ func ReadDefinitionsFromFile(path string) ([]*unstructured.Unstructured, error)
}
var objs []*unstructured.Unstructured
//nolint:gosec
fis, err := os.ReadDir(path)
if err != nil {
return nil, err
}
for _, fi := range fis {
if fi.IsDir() {
continue
err = filepath.WalkDir(path, func(path string, e os.DirEntry, err error) error {
if e == nil {
io.Errorf("failed to walk nil dir entry %s", path)
return nil
}
fileType := filepath.Ext(fi.Name())
if fileType != ".yaml" && fileType != ".yml" && fileType != ".cue" {
continue
}
obj, err := readObj(filepath.Join(path, fi.Name()))
if err != nil {
return nil, err
io.Errorf("failed to walk dir %s: %v", path, err)
return nil
}
if e.IsDir() {
return nil
}
fileType := filepath.Ext(e.Name())
if fileType != ".yaml" && fileType != ".yml" && fileType != ".cue" {
return nil
}
obj, err := readObj(path)
if err != nil {
return err
}
objs = append(objs, obj)
return nil
})
if err != nil {
return nil, err
}
return objs, nil
}
+14 -3
View File
@@ -223,9 +223,20 @@ var _ = Describe("Testing dry-run", func() {
Expect(buff.String()).Should(ContainSubstring("kind: Deployment"))
})
It("Testing dry-run offline", func() {
It("Testing dry-run offline with definition file", func() {
c := common2.Args{}
opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-6.yaml"}, DefinitionFile: "test-data/dry-run/testing-worker-def.yaml", OfflineMode: true}
opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-6.yaml"}, DefinitionFile: "test-data/dry-run/definitions/testing-worker-def.yaml", OfflineMode: true}
buff, err := DryRunApplication(&opt, c, "")
Expect(err).Should(BeNil())
Expect(buff.String()).Should(ContainSubstring("# Application(testing-app)"))
Expect(buff.String()).Should(ContainSubstring("name: testing-dryrun"))
Expect(buff.String()).Should(ContainSubstring("kind: Deployment"))
Expect(buff.String()).Should(ContainSubstring("workload.oam.dev/type: myworker"))
})
It("Testing dry-run offline with definition directory", func() {
c := common2.Args{}
opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-6.yaml"}, DefinitionFile: "test-data/dry-run/definitions", OfflineMode: true}
buff, err := DryRunApplication(&opt, c, "")
Expect(err).Should(BeNil())
Expect(buff.String()).Should(ContainSubstring("# Application(testing-app)"))
@@ -236,7 +247,7 @@ var _ = Describe("Testing dry-run", func() {
It("Testing dry-run offline with deploy workflow step", func() {
c := common2.Args{}
opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-7.yaml"}, DefinitionFile: "test-data/dry-run/testing-worker-def.yaml", OfflineMode: true}
opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-7.yaml"}, DefinitionFile: "test-data/dry-run/definitions/testing-worker-def.yaml", OfflineMode: true}
buff, err := DryRunApplication(&opt, c, "")
Expect(err).Should(BeNil())
Expect(buff.String()).Should(ContainSubstring("# Application(testing-app with topology target-prod)"))
+1 -1
View File
@@ -103,7 +103,7 @@ func LiveDiffApplication(cmdOption *LiveDiffCmdOptions, c common.Args) (bytes.Bu
}
var objs []*unstructured.Unstructured
if cmdOption.DefinitionFile != "" {
objs, err = ReadDefinitionsFromFile(cmdOption.DefinitionFile)
objs, err = ReadDefinitionsFromFile(cmdOption.DefinitionFile, cmdOption.IOStreams)
if err != nil {
return buff, err
}