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
19 changes: 2 additions & 17 deletions pkg/composer/blueprint/blueprint_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,17 +441,6 @@ func (b *BaseBlueprintHandler) GetLocalTemplateData() (map[string][]byte, error)
}
}

if existingValues, exists := templateData["substitutions"]; exists {
var ociSubstitutionValues map[string]any
if err := b.shims.YamlUnmarshal(existingValues, &ociSubstitutionValues); err == nil {
if substitutionValues == nil {
substitutionValues = ociSubstitutionValues
} else {
substitutionValues = b.deepMergeMaps(ociSubstitutionValues, substitutionValues)
}
}
}

if len(substitutionValues) > 0 {
substitutionYAML, err := b.shims.YamlMarshal(substitutionValues)
if err != nil {
Expand Down Expand Up @@ -769,9 +758,8 @@ func (b *BaseBlueprintHandler) getKustomizations() []blueprintv1alpha1.Kustomiza

// walkAndCollectTemplates recursively traverses the specified template directory and collects all files into the
// templateData map. It adds the contents of each file by a normalized relative path key prefixed with "_template/".
// Special files "schema.yaml", "blueprint.yaml", and "substitutions" are also stored under canonical keys
// ("schema", "blueprint", "substitutions"). Directory entries are processed recursively. Any file or directory
// traversal errors are returned.
// Special files "schema.yaml" and "blueprint.yaml" are also stored under canonical keys ("schema", "blueprint").
// Directory entries are processed recursively. Any file or directory traversal errors are returned.
func (b *BaseBlueprintHandler) walkAndCollectTemplates(templateDir string, templateData map[string][]byte) error {
entries, err := b.shims.ReadDir(templateDir)
if err != nil {
Expand Down Expand Up @@ -806,9 +794,6 @@ func (b *BaseBlueprintHandler) walkAndCollectTemplates(templateDir string, templ
case "blueprint.yaml":
templateData["blueprint"] = content
templateData[key] = content
case "substitutions":
templateData["substitutions"] = content
templateData[key] = content
default:
templateData[key] = content
}
Expand Down
19 changes: 7 additions & 12 deletions pkg/composer/blueprint/blueprint_handler_private_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,6 @@ func TestBaseBlueprintHandler_walkAndCollectTemplates(t *testing.T) {
if _, exists := templateData["blueprint"]; !exists {
t.Error("Expected 'blueprint' key to exist")
}
if _, exists := templateData["substitutions"]; !exists {
t.Error("Expected 'substitutions' key to exist")
}

if _, exists := templateData["_template/schema.yaml"]; !exists {
t.Error("Expected '_template/schema.yaml' key to exist")
Expand Down Expand Up @@ -2473,7 +2470,6 @@ kustomizations:
}
})


t.Run("HandlesRepositoryField", func(t *testing.T) {
handler := setup(t)
blueprintData := []byte(`kind: Blueprint
Expand Down Expand Up @@ -2660,7 +2656,7 @@ terraform:
`)

templateData := map[string][]byte{
"blueprint.yaml": baseBlueprint,
"blueprint.yaml": baseBlueprint,
"_template/features/generic.yaml": genericFeature,
}

Expand Down Expand Up @@ -3630,7 +3626,7 @@ terraform:
key2: "value"
`)
templateData := map[string][]byte{
"blueprint.yaml": baseBlueprint,
"blueprint.yaml": baseBlueprint,
"_template/features/test.yaml": feature,
}
config := map[string]any{}
Expand Down Expand Up @@ -3673,7 +3669,7 @@ terraform:
new: "value"
`)
templateData := map[string][]byte{
"blueprint.yaml": baseBlueprint,
"blueprint.yaml": baseBlueprint,
"_template/features/test.yaml": feature,
}
config := map[string]any{}
Expand Down Expand Up @@ -3716,7 +3712,7 @@ kustomize:
- new-component
`)
templateData := map[string][]byte{
"blueprint.yaml": baseBlueprint,
"blueprint.yaml": baseBlueprint,
"_template/features/test.yaml": feature,
}
config := map[string]any{}
Expand Down Expand Up @@ -3761,7 +3757,7 @@ kustomize:
key: ${value}
`)
templateData := map[string][]byte{
"blueprint.yaml": baseBlueprint,
"blueprint.yaml": baseBlueprint,
"_template/features/test.yaml": feature,
}
config := map[string]any{
Expand Down Expand Up @@ -3790,7 +3786,6 @@ kustomize:
}
})


t.Run("HandlesInterpolateStringError", func(t *testing.T) {
handler := setup(t)
baseBlueprint := []byte(`kind: Blueprint
Expand All @@ -3808,7 +3803,7 @@ kustomize:
- patch: ${invalid expression [[[
`)
templateData := map[string][]byte{
"blueprint.yaml": baseBlueprint,
"blueprint.yaml": baseBlueprint,
"_template/features/test.yaml": feature,
}
config := map[string]any{}
Expand Down Expand Up @@ -5611,7 +5606,7 @@ metadata:
}

templateData := map[string][]byte{
"_metadata_name": []byte("custom-artifact"),
"_metadata_name": []byte("custom-artifact"),
"_template/blueprint.yaml": []byte(`kind: Blueprint
apiVersion: blueprints.windsorcli.dev/v1alpha1
metadata:
Expand Down
62 changes: 7 additions & 55 deletions pkg/composer/blueprint/blueprint_handler_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3644,16 +3644,6 @@ terraform:
t.Fatalf("Failed to create template directory: %v", err)
}

substitutionsContent := []byte(`common:
key1: value1
key2: value2
kustomization1:
key3: value3
`)
if err := os.WriteFile(filepath.Join(mocks.Runtime.TemplateRoot, "substitutions"), substitutionsContent, 0644); err != nil {
t.Fatalf("Failed to write substitutions file: %v", err)
}

handler.shims.Stat = os.Stat
handler.shims.ReadDir = os.ReadDir
handler.shims.ReadFile = os.ReadFile
Expand All @@ -3664,69 +3654,31 @@ kustomization1:
return map[string]any{
"substitutions": map[string]any{
"common": map[string]any{
"key1": "merged-value1",
"key1": "value1",
"key2": "value2",
},
"kustomization1": map[string]any{
"key4": "value4",
"key3": "value3",
},
},
}, nil
}

templateData, err := handler.GetLocalTemplateData()

_, err = handler.GetLocalTemplateData()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}

if len(handler.commonSubstitutions) == 0 {
t.Error("Expected common substitutions to be processed")
}
if len(handler.featureSubstitutions) == 0 {
t.Error("Expected feature substitutions to be processed")
}
if _, exists := templateData["substitutions"]; !exists {
t.Error("Expected substitutions to be in templateData")
}
})

t.Run("HandlesSubstitutionUnmarshalError", func(t *testing.T) {
mocks := setupBlueprintMocks(t)
mockArtifactBuilder := artifact.NewMockArtifact()
handler, err := NewBlueprintHandler(mocks.Runtime, mockArtifactBuilder)
if err != nil {
t.Fatalf("NewBlueprintHandler() failed: %v", err)
}
handler.shims = mocks.Shims

tmpDir := t.TempDir()
mocks.Runtime.ProjectRoot = tmpDir
mocks.Runtime.TemplateRoot = filepath.Join(tmpDir, "contexts", "_template")
mocks.Runtime.ConfigRoot = tmpDir

if err := os.MkdirAll(mocks.Runtime.TemplateRoot, 0755); err != nil {
t.Fatalf("Failed to create template directory: %v", err)
}

invalidSubstitutions := []byte("invalid: yaml: [")
if err := os.WriteFile(filepath.Join(mocks.Runtime.TemplateRoot, "substitutions"), invalidSubstitutions, 0644); err != nil {
t.Fatalf("Failed to write substitutions file: %v", err)
}

handler.shims.Stat = os.Stat
handler.shims.ReadDir = os.ReadDir
handler.shims.ReadFile = os.ReadFile
handler.shims.YamlUnmarshal = yaml.Unmarshal
handler.shims.YamlMarshal = yaml.Marshal

mocks.ConfigHandler.(*config.MockConfigHandler).GetContextValuesFunc = func() (map[string]any, error) {
return map[string]any{}, nil
}

_, err = handler.GetLocalTemplateData()

if err != nil {
t.Fatalf("Expected no error (unmarshal error should be ignored), got %v", err)
}
t.Skip("Substitutions are now only in values.yaml (contextValues) or Features, not in files")
})

t.Run("HandlesSubstitutionMarshalError", func(t *testing.T) {
Expand Down Expand Up @@ -3775,7 +3727,7 @@ kustomization1:
t.Fatal("Expected error when YamlMarshal fails for substitutions")
}
if !strings.Contains(err.Error(), "failed to marshal substitution values") {
t.Errorf("Expected error about marshaling substitution values, got: %v", err)
t.Errorf("Expected error about marshaling substitutions, got: %v", err)
}
})
}
Expand Down
Loading