summaryrefslogtreecommitdiff
path: root/integrationtest
diff options
context:
space:
mode:
authorJames Bardin <j.bardin@gmail.com>2023-11-02 14:14:41 -0400
committerJames Bardin <j.bardin@gmail.com>2023-11-02 14:14:41 -0400
commit916ac487b471a8c6220be57d711710942c7dd29c (patch)
treec7e35877bc38420f9e47773a72323c11baf95ddd /integrationtest
parent1ce4917789ea5ed149c2c3254d85c65654c27cc4 (diff)
add scoped function integration tests
Add a locals blocks to the terraformlike tests, with normal and scoped function calls.
Diffstat (limited to 'integrationtest')
-rw-r--r--integrationtest/terraformlike_test.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/integrationtest/terraformlike_test.go b/integrationtest/terraformlike_test.go
index 8978346..ae86626 100644
--- a/integrationtest/terraformlike_test.go
+++ b/integrationtest/terraformlike_test.go
@@ -16,6 +16,7 @@ import (
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/json"
"github.com/zclconf/go-cty/cty"
+ "github.com/zclconf/go-cty/cty/function"
)
// TestTerraformLike parses both a native syntax and a JSON representation
@@ -53,10 +54,14 @@ func TestTerraformLike(t *testing.T) {
Name string `hcl:"name,label"`
Providers hcl.Expression `hcl:"providers"`
}
+ type Locals struct {
+ Config hcl.Body `hcl:",remain"`
+ }
type Root struct {
Variables []*Variable `hcl:"variable,block"`
Resources []*Resource `hcl:"resource,block"`
Modules []*Module `hcl:"module,block"`
+ Locals []*Locals `hcl:"locals,block"`
}
instanceDecode := &hcldec.ObjectSpec{
"image_id": &hcldec.AttrSpec{
@@ -343,6 +348,57 @@ func TestTerraformLike(t *testing.T) {
t.Errorf("wrong value traversal [1] type %T; want hcl.TraverseAttr", vt[1])
}
})
+
+ t.Run("locals", func(t *testing.T) {
+ locals := root.Locals[0]
+ attrs, diags := locals.Config.JustAttributes()
+ if diags.HasErrors() {
+ t.Fatal(diags)
+ }
+
+ ctx := &hcl.EvalContext{
+ Functions: map[string]function.Function{
+ "func": function.New(&function.Spec{
+ Params: []function.Parameter{{Type: cty.String}},
+ Type: function.StaticReturnType(cty.String),
+ Impl: func([]cty.Value, cty.Type) (cty.Value, error) {
+ return cty.StringVal("func_result"), nil
+ },
+ }),
+ "scoped::func": function.New(&function.Spec{
+ Params: []function.Parameter{{Type: cty.String}},
+ Type: function.StaticReturnType(cty.String),
+ Impl: func([]cty.Value, cty.Type) (cty.Value, error) {
+ return cty.StringVal("scoped::func_result"), nil
+ },
+ }),
+ },
+ }
+
+ res := attrs["func_result"]
+ funcVal, diags := res.Expr.Value(ctx)
+ if diags.HasErrors() {
+ t.Fatal(diags)
+ }
+
+ wantVal := cty.StringVal("func_result")
+
+ if !funcVal.RawEquals(wantVal) {
+ t.Errorf("expected %#v, got %#v", wantVal, funcVal)
+ }
+
+ res = attrs["scoped_func_result"]
+ funcVal, diags = res.Expr.Value(ctx)
+ if diags.HasErrors() {
+ t.Fatal(diags)
+ }
+
+ wantVal = cty.StringVal("scoped::func_result")
+
+ if !funcVal.RawEquals(wantVal) {
+ t.Errorf("expected %#v, got %#v", wantVal, funcVal)
+ }
+ })
})
}
}
@@ -352,6 +408,11 @@ const terraformLikeNativeSyntax = `
variable "image_id" {
}
+locals {
+ func_result = func("arg")
+ scoped_func_result = scoped::func("arg")
+}
+
resource "happycloud_instance" "test" {
instance_type = "z3.weedy"
image_id = var.image_id
@@ -400,6 +461,10 @@ const terraformLikeJSON = `
"variable": {
"image_id": {}
},
+ "locals": {
+ "func_result": "${func(\"arg\")}",
+ "scoped_func_result": "${scoped::func(\"arg\")}"
+ },
"resource": {
"happycloud_instance": {
"test": {