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
12 changes: 12 additions & 0 deletions pkg/composer/blueprint/blueprint_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,12 @@ func (b *BaseBlueprintHandler) processOCIArtifact(templateData map[string][]byte
return err
}

contextName := b.runtime.ContextName
if contextName != "" {
b.blueprint.Metadata.Name = contextName
b.blueprint.Metadata.Description = fmt.Sprintf("Blueprint for %s context", contextName)
}

ociInfo, _ := artifact.ParseOCIReference(blueprintRef)
if ociInfo != nil {
b.setOCISource(ociInfo)
Expand Down Expand Up @@ -1363,6 +1369,12 @@ func (b *BaseBlueprintHandler) processLocalArtifact(templateData map[string][]by
return err
}

contextName := b.runtime.ContextName
if contextName != "" {
b.blueprint.Metadata.Name = contextName
b.blueprint.Metadata.Description = fmt.Sprintf("Blueprint for %s context", contextName)
}

fileSource := blueprintv1alpha1.Source{
Name: metadataName,
Url: "file://" + relativeArtifactPath,
Expand Down
126 changes: 126 additions & 0 deletions pkg/composer/blueprint/blueprint_handler_private_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5371,6 +5371,69 @@ metadata:
t.Errorf("Expected error about processing features, got: %v", err)
}
})

t.Run("SetsMetadataNameAndDescriptionFromContextName", func(t *testing.T) {
handler, mocks := setup(t)
handler.runtime.ContextName = "production"
mocks.ConfigHandler.(*config.MockConfigHandler).GetContextValuesFunc = func() (map[string]any, error) {
return map[string]any{"test": "value"}, nil
}
mocks.ConfigHandler.(*config.MockConfigHandler).LoadSchemaFromBytesFunc = func(data []byte) error {
return nil
}

templateData := map[string][]byte{
"_template/blueprint.yaml": []byte(`kind: Blueprint
apiVersion: blueprints.windsorcli.dev/v1alpha1
metadata:
name: template
description: Base blueprint template for core services`),
}

err := handler.processOCIArtifact(templateData, "oci://ghcr.io/test/repo:latest")

if err != nil {
t.Fatalf("Expected no error, got: %v", err)
}
if handler.blueprint.Metadata.Name != "production" {
t.Errorf("Expected metadata name to be 'production', got: %s", handler.blueprint.Metadata.Name)
}
expectedDescription := "Blueprint for production context"
if handler.blueprint.Metadata.Description != expectedDescription {
t.Errorf("Expected metadata description to be '%s', got: %s", expectedDescription, handler.blueprint.Metadata.Description)
}
})

t.Run("DoesNotSetMetadataWhenContextNameIsEmpty", func(t *testing.T) {
handler, mocks := setup(t)
handler.runtime.ContextName = ""
mocks.ConfigHandler.(*config.MockConfigHandler).GetContextValuesFunc = func() (map[string]any, error) {
return map[string]any{"test": "value"}, nil
}
mocks.ConfigHandler.(*config.MockConfigHandler).LoadSchemaFromBytesFunc = func(data []byte) error {
return nil
}

templateData := map[string][]byte{
"_template/blueprint.yaml": []byte(`kind: Blueprint
apiVersion: blueprints.windsorcli.dev/v1alpha1
metadata:
name: template
description: Base blueprint template for core services`),
}

err := handler.processOCIArtifact(templateData, "oci://ghcr.io/test/repo:latest")

if err != nil {
t.Fatalf("Expected no error, got: %v", err)
}
if handler.blueprint.Metadata.Name != "template" {
t.Errorf("Expected metadata name to remain 'template', got: %s", handler.blueprint.Metadata.Name)
}
if handler.blueprint.Metadata.Description != "Base blueprint template for core services" {
t.Errorf("Expected metadata description to remain unchanged, got: %s", handler.blueprint.Metadata.Description)
}
})
}

func TestBaseBlueprintHandler_pullOCISources(t *testing.T) {
Expand Down Expand Up @@ -5685,6 +5748,69 @@ metadata:
t.Errorf("Expected kustomization source to remain 'existing-source', got: %s", handler.blueprint.Kustomizations[0].Source)
}
})

t.Run("SetsMetadataNameAndDescriptionFromContextName", func(t *testing.T) {
handler, mocks := setup(t)
handler.runtime.ContextName = "staging"
mocks.ConfigHandler.(*config.MockConfigHandler).GetContextValuesFunc = func() (map[string]any, error) {
return map[string]any{"test": "value"}, nil
}
mocks.ConfigHandler.(*config.MockConfigHandler).LoadSchemaFromBytesFunc = func(data []byte) error {
return nil
}

templateData := map[string][]byte{
"_template/blueprint.yaml": []byte(`kind: Blueprint
apiVersion: blueprints.windsorcli.dev/v1alpha1
metadata:
name: template
description: Base blueprint template for core services`),
}

err := handler.processLocalArtifact(templateData, "../test.tar.gz")

if err != nil {
t.Fatalf("Expected no error, got: %v", err)
}
if handler.blueprint.Metadata.Name != "staging" {
t.Errorf("Expected metadata name to be 'staging', got: %s", handler.blueprint.Metadata.Name)
}
expectedDescription := "Blueprint for staging context"
if handler.blueprint.Metadata.Description != expectedDescription {
t.Errorf("Expected metadata description to be '%s', got: %s", expectedDescription, handler.blueprint.Metadata.Description)
}
})

t.Run("DoesNotSetMetadataWhenContextNameIsEmpty", func(t *testing.T) {
handler, mocks := setup(t)
handler.runtime.ContextName = ""
mocks.ConfigHandler.(*config.MockConfigHandler).GetContextValuesFunc = func() (map[string]any, error) {
return map[string]any{"test": "value"}, nil
}
mocks.ConfigHandler.(*config.MockConfigHandler).LoadSchemaFromBytesFunc = func(data []byte) error {
return nil
}

templateData := map[string][]byte{
"_template/blueprint.yaml": []byte(`kind: Blueprint
apiVersion: blueprints.windsorcli.dev/v1alpha1
metadata:
name: template
description: Base blueprint template for core services`),
}

err := handler.processLocalArtifact(templateData, "../test.tar.gz")

if err != nil {
t.Fatalf("Expected no error, got: %v", err)
}
if handler.blueprint.Metadata.Name != "template" {
t.Errorf("Expected metadata name to remain 'template', got: %s", handler.blueprint.Metadata.Name)
}
if handler.blueprint.Metadata.Description != "Base blueprint template for core services" {
t.Errorf("Expected metadata description to remain unchanged, got: %s", handler.blueprint.Metadata.Description)
}
})
}

func TestBaseBlueprintHandler_getSourceRef(t *testing.T) {
Expand Down
Loading