Add tests for AttachResourceConfigTransformer (#1570)

Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
This commit is contained in:
Christian Mesh 2024-04-29 11:36:31 -04:00 committed by GitHub
parent cdb10b12b0
commit 25149e1f6e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,6 @@
data "aws_instance" "child_data_instance" {
data_config = 30
}
resource "aws_instance" "child_resource_instance" {
data_config = 40
}

View File

@ -0,0 +1,9 @@
data "aws_instance" "data_instance" {
data_config = 10
}
resource "aws_instance" "resource_instance" {
resource_config = 20
}
module "child" {
source = "./child"
}

View File

@ -0,0 +1,66 @@
package tofu
import (
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/opentofu/opentofu/internal/addrs"
"github.com/zclconf/go-cty/cty/gocty"
)
func TestModuleTransformAttachConfigTransformer(t *testing.T) {
g := Graph{Path: addrs.RootModuleInstance}
module := testModule(t, "transform-attach-config")
err := (&ConfigTransformer{Config: module}).Transform(&g)
if err != nil {
t.Fatal(err)
}
err = (&AttachResourceConfigTransformer{Config: module}).Transform(&g)
if err != nil {
t.Fatal(err)
}
verts := g.Vertices()
if len(verts) != 4 {
t.Fatalf("Expected 4 vertices, got %v", len(verts))
}
expected := map[string]map[string]int{
"data.aws_instance.data_instance": map[string]int{
"data_config": 10,
},
"aws_instance.resource_instance": map[string]int{
"resource_config": 20,
},
"module.child.data.aws_instance.child_data_instance": map[string]int{
"data_config": 30,
},
"module.child.aws_instance.child_resource_instance": map[string]int{
"data_config": 40,
},
}
got := make(map[string]map[string]int)
for _, v := range verts {
ar := v.(*NodeAbstractResource)
attrs, _ := ar.Config.Config.JustAttributes()
values := make(map[string]int)
for _, attr := range attrs {
val, _ := attr.Expr.Value(nil)
var target int
gocty.FromCtyValue(val, &target)
values[attr.Name] = target
}
got[ar.ResourceAddr().String()] = values
}
if !reflect.DeepEqual(expected, got) {
t.Fatalf("Expected %s, got %s", spew.Sdump(expected), spew.Sdump(got))
}
}