2024-02-08 03:48:59 -06:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 10:33:06 -05:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-09-20 07:16:53 -05:00
|
|
|
package tofu
|
2015-02-27 21:18:04 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-05-04 21:24:06 -05:00
|
|
|
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/addrs"
|
|
|
|
"github.com/opentofu/opentofu/internal/configs/configschema"
|
|
|
|
"github.com/opentofu/opentofu/internal/providers"
|
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{
|
2023-07-06 09:35:33 -05:00
|
|
|
Plugins: schemaOnlyProvidersForTesting(map[addrs.Provider]providers.ProviderSchema{
|
2021-08-31 18:36:27 -05:00
|
|
|
addrs.NewDefaultProvider("aws"): {
|
2023-07-05 16:31:39 -05:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2021-08-31 18:36:27 -05:00
|
|
|
"aws_instance": {
|
2023-07-05 16:31:39 -05:00
|
|
|
Block: &configschema.Block{
|
|
|
|
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
|
|
|
},
|
2024-04-10 07:04:08 -05:00
|
|
|
}, t),
|
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
|
|
|
|
`
|