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