Skip to content

Commit

Permalink
Fix ordering of input-manifests being scrambled, closes crossplane#29
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Kässer <[email protected]>
  • Loading branch information
kaessert committed Oct 6, 2024
1 parent a8fc648 commit 7087b86
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions internal/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ var (
caseDirectory = "case"
)

type injectedManifest struct {
Path string
Manifest string
}

type preparerOption func(*preparer)

func withDataSource(path string) preparerOption {
Expand Down Expand Up @@ -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)
}

Expand All @@ -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 {
Expand All @@ -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),
})
Expand All @@ -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)
Expand All @@ -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
}
Expand Down

0 comments on commit 7087b86

Please sign in to comment.