Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(TFECO-8375) Add test for stacks schema merger #429

Merged
merged 3 commits into from
Dec 2, 2024
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
9 changes: 3 additions & 6 deletions schema/stacks/deploy_schema_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,15 @@ func (m *DeploySchemaMerger) SchemaForDeployment(meta *stack.Meta) (*schema.Body

mergedSchema := m.coreSchema.Copy()

constr, err := constraintForDeploymentInputs(*meta)
if err != nil {
return mergedSchema, err
}
constr := constraintForDeploymentInputs(*meta)

// TODO: isOptional should be set to true if at least one input is required
mergedSchema.Blocks["deployment"].Body.Attributes["inputs"].Constraint = constr

return mergedSchema, nil
}

func constraintForDeploymentInputs(stackMeta stack.Meta) (schema.Constraint, error) {
func constraintForDeploymentInputs(stackMeta stack.Meta) schema.Constraint {
inputs := make(map[string]*schema.AttributeSchema, 0)

for name, variable := range stackMeta.Variables {
Expand Down Expand Up @@ -72,7 +69,7 @@ func constraintForDeploymentInputs(stackMeta stack.Meta) (schema.Constraint, err

return schema.Object{
Attributes: inputs,
}, nil
}
}

func StackVarToAttribute(stackVar stack.Variable) *schema.AttributeSchema {
Expand Down
155 changes: 155 additions & 0 deletions schema/stacks/deploy_schema_merge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package schema

import (
"errors"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
tfschema "github.com/hashicorp/terraform-schema/schema"
"github.com/hashicorp/terraform-schema/stack"
"github.com/zclconf/go-cty-debug/ctydebug"
"github.com/zclconf/go-cty/cty"
)

func TestDeploySchemaMerger_SchemaForDeployment_noCoreSchema(t *testing.T) {
sm := NewDeploySchemaMerger(nil)

_, err := sm.SchemaForDeployment(nil)
if err == nil {
t.Fatal("expected error for nil core schema")
}

if !errors.Is(err, tfschema.CoreSchemaRequiredErr{}) {
t.Fatalf("unexpected error: %#v", err)
}
}

func TestDeploySchemaMerger_SchemaForDeployment_no_inputs(t *testing.T) {
testCoreSchema := &schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"deployment": {
Labels: []*schema.LabelSchema{
{Name: "name"},
},
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"inputs": {
Description: lang.Markdown("A mapping of stack variable names to values for this deployment. The keys of this map must correspond to the names of variables defined for the stack. The values must be valid HCL literals meeting the type constraint of those variables. Values are also expressions, currently with access to identity token references only"),
IsOptional: true,
Constraint: schema.Map{
Name: "map of variable references",
Elem: schema.AnyExpression{OfType: cty.DynamicPseudoType},
},
},
},
},
},
},
}

sm := NewDeploySchemaMerger(testCoreSchema)

givenBodySchema, err := sm.SchemaForDeployment(&stack.Meta{
Variables: map[string]stack.Variable{},
})
if err != nil {
t.Fatal(err)
}

expectedBodySchema := &schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"deployment": {
Labels: []*schema.LabelSchema{
{Name: "name"},
},
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"inputs": {
Description: lang.Markdown("A mapping of stack variable names to values for this deployment. The keys of this map must correspond to the names of variables defined for the stack. The values must be valid HCL literals meeting the type constraint of those variables. Values are also expressions, currently with access to identity token references only"),
IsOptional: true,
Constraint: schema.Object{Attributes: schema.ObjectAttributes{}},
},
},
},
},
},
}

if diff := cmp.Diff(expectedBodySchema, givenBodySchema, ctydebug.CmpOptions); diff != "" {
t.Fatalf("schema mismatch: %s", diff)
}
}

func TestDeploySchemaMerger_SchemaForDeployment_inputs(t *testing.T) {
testCoreSchema := &schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"deployment": {
Labels: []*schema.LabelSchema{
{Name: "name"},
},
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"inputs": {
Description: lang.Markdown("A mapping of stack variable names to values for this deployment. The keys of this map must correspond to the names of variables defined for the stack. The values must be valid HCL literals meeting the type constraint of those variables. Values are also expressions, currently with access to identity token references only"),
IsOptional: true,
Constraint: schema.Map{
Name: "map of variable references",
Elem: schema.AnyExpression{OfType: cty.DynamicPseudoType},
},
},
},
},
},
},
}

sm := NewDeploySchemaMerger(testCoreSchema)

givenBodySchema, err := sm.SchemaForDeployment(&stack.Meta{
Variables: map[string]stack.Variable{
"foo": {
Type: cty.String,
},
},
})
if err != nil {
t.Fatal(err)
}

expectedBodySchema := &schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"deployment": {
Labels: []*schema.LabelSchema{
{Name: "name"},
},
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"inputs": {
Description: lang.Markdown("A mapping of stack variable names to values for this deployment. The keys of this map must correspond to the names of variables defined for the stack. The values must be valid HCL literals meeting the type constraint of those variables. Values are also expressions, currently with access to identity token references only"),
IsOptional: true,
Constraint: schema.Object{Attributes: schema.ObjectAttributes{
"foo": {
IsRequired: true,
Constraint: schema.AnyExpression{OfType: cty.String},
OriginForTarget: &schema.PathTarget{
Address: schema.Address{schema.StaticStep{Name: "var"}, schema.AttrNameStep{}},
Path: lang.Path{LanguageID: "terraform-stack"},
Constraints: schema.Constraints{ScopeId: "variable", Type: cty.String},
},
},
}},
},
},
},
},
},
}

if diff := cmp.Diff(expectedBodySchema, givenBodySchema, ctydebug.CmpOptions); diff != "" {
t.Fatalf("schema mismatch: %s", diff)
}
}
Loading