2015-02-28 04:18:04 +01:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-05-05 04:24:06 +02:00
|
|
|
|
2021-05-17 21:00:50 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 21:17:09 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
2018-05-08 02:20:01 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2015-02-28 04:18:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTransitiveReductionTransformer(t *testing.T) {
|
|
|
|
mod := testModule(t, "transform-trans-reduce-basic")
|
|
|
|
|
2018-05-05 04:24:06 +02:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2015-02-28 04:18:04 +01:00
|
|
|
{
|
2018-05-05 04:24:06 +02:00
|
|
|
tf := &ConfigTransformer{Config: mod}
|
2015-02-28 04:18:04 +01:00
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-05-08 02:20:01 +02:00
|
|
|
t.Logf("graph after ConfigTransformer:\n%s", g.String())
|
2015-02-28 04:18:04 +01:00
|
|
|
}
|
|
|
|
|
2017-01-27 04:56:16 +01:00
|
|
|
{
|
2018-05-05 04:24:06 +02:00
|
|
|
transform := &AttachResourceConfigTransformer{Config: mod}
|
2017-01-27 04:56:16 +01:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 02:20:01 +02:00
|
|
|
{
|
|
|
|
transform := &AttachSchemaTransformer{
|
2018-05-31 21:39:45 +02:00
|
|
|
Schemas: &Schemas{
|
2020-02-03 14:18:04 +01:00
|
|
|
Providers: map[addrs.Provider]*ProviderSchema{
|
2020-04-01 19:13:40 +02:00
|
|
|
addrs.NewDefaultProvider("aws"): {
|
2018-05-31 21:39:45 +02:00
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"A": {
|
|
|
|
Type: cty.String,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"B": {
|
|
|
|
Type: cty.String,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-05-08 02:20:01 +02:00
|
|
|
},
|
|
|
|
},
|
2018-05-31 21:39:45 +02:00
|
|
|
},
|
|
|
|
},
|
2018-05-08 02:20:01 +02:00
|
|
|
}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 04:56:16 +01:00
|
|
|
{
|
|
|
|
transform := &ReferenceTransformer{}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-05-08 02:20:01 +02:00
|
|
|
t.Logf("graph after ReferenceTransformer:\n%s", g.String())
|
2017-01-27 04:56:16 +01:00
|
|
|
}
|
|
|
|
|
2015-02-28 04:18:04 +01:00
|
|
|
{
|
|
|
|
transform := &TransitiveReductionTransformer{}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-05-08 02:20:01 +02:00
|
|
|
t.Logf("graph after TransitiveReductionTransformer:\n%s", g.String())
|
2015-02-28 04:18:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformTransReduceBasicStr)
|
|
|
|
if actual != expected {
|
2018-05-08 02:20:01 +02:00
|
|
|
t.Errorf("wrong result\ngot:\n%s\n\nwant:\n%s", actual, expected)
|
2015-02-28 04:18:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testTransformTransReduceBasicStr = `
|
|
|
|
aws_instance.A
|
|
|
|
aws_instance.B
|
|
|
|
aws_instance.A
|
|
|
|
aws_instance.C
|
|
|
|
aws_instance.B
|
|
|
|
`
|