From 7087b86421cb2516e643fb7ef5c90cb20e1e2b43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20K=C3=A4sser?= Date: Sun, 6 Oct 2024 19:38:56 +0200 Subject: [PATCH] Fix ordering of input-manifests being scrambled, closes #29 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tobias Kässer --- internal/prepare.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/internal/prepare.go b/internal/prepare.go index f9f2e53..7e9d8e8 100644 --- a/internal/prepare.go +++ b/internal/prepare.go @@ -35,6 +35,11 @@ var ( caseDirectory = "case" ) +type injectedManifest struct { + Path string + Manifest string +} + type preparerOption func(*preparer) func withDataSource(path string) preparerOption { @@ -72,7 +77,7 @@ func (p *preparer) prepareManifests() ([]config.Manifest, error) { if err := os.RemoveAll(caseDirectory); err != nil { return nil, errors.Wrapf(err, "cannot clean directory %s", caseDirectory) } - if err := os.MkdirAll(caseDirectory, os.ModePerm); err != nil { + if err := os.MkdirAll(caseDirectory, os.ModePerm); err != nil { //nolint:gosec // directory permissions are not critical here return nil, errors.Wrapf(err, "cannot create directory %s", caseDirectory) } @@ -82,8 +87,8 @@ func (p *preparer) prepareManifests() ([]config.Manifest, error) { } manifests := make([]config.Manifest, 0, len(injectedFiles)) - for path, data := range injectedFiles { - decoder := kyaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(data), 1024) + for _, data := range injectedFiles { + decoder := kyaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(data.Manifest), 1024) for { u := &unstructured.Unstructured{} if err := decoder.Decode(&u); err != nil { @@ -102,7 +107,7 @@ func (p *preparer) prepareManifests() ([]config.Manifest, error) { return nil, errors.Wrapf(err, "cannot marshal manifest for \"%s/%s\"", u.GetObjectKind(), u.GetName()) } manifests = append(manifests, config.Manifest{ - FilePath: path, + FilePath: data.Path, Object: u, YAML: string(y), }) @@ -112,7 +117,7 @@ func (p *preparer) prepareManifests() ([]config.Manifest, error) { return manifests, nil } -func (p *preparer) injectVariables() (map[string]string, error) { +func (p *preparer) injectVariables() ([]injectedManifest, error) { dataSourceMap := make(map[string]string) if p.dataSourcePath != "" { dataSource, err := os.ReadFile(p.dataSourcePath) @@ -124,13 +129,16 @@ func (p *preparer) injectVariables() (map[string]string, error) { } } - inputs := make(map[string]string, len(p.testFilePaths)) - for _, f := range p.testFilePaths { + inputs := make([]injectedManifest, len(p.testFilePaths)) + for i, f := range p.testFilePaths { manifestData, err := os.ReadFile(filepath.Clean(f)) if err != nil { return nil, errors.Wrapf(err, "cannot read %s", f) } - inputs[f] = p.injectValues(string(manifestData), dataSourceMap) + inputs[i] = injectedManifest{ + Path: f, + Manifest: p.injectValues(string(manifestData), dataSourceMap), + } } return inputs, nil }