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
|
2016-11-05 19:53:12 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/addrs"
|
2016-11-05 19:53:12 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfigTransformer_nilModule(t *testing.T) {
|
2018-05-04 21:24:06 -05:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2016-11-05 19:53:12 -05:00
|
|
|
tf := &ConfigTransformer{}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(g.Vertices()) > 0 {
|
2016-11-08 16:04:57 -06:00
|
|
|
t.Fatalf("graph is not empty: %s", g.String())
|
2016-11-05 19:53:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigTransformer(t *testing.T) {
|
2018-05-04 21:24:06 -05:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
|
|
|
tf := &ConfigTransformer{Config: testModule(t, "graph-basic")}
|
2016-11-05 19:53:12 -05:00
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testConfigTransformerGraphBasicStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-22 14:57:19 -06:00
|
|
|
func TestConfigTransformer_mode(t *testing.T) {
|
2018-05-04 21:24:06 -05:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2017-01-22 14:57:19 -06:00
|
|
|
tf := &ConfigTransformer{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: testModule(t, "transform-config-mode-data"),
|
2017-01-22 14:57:19 -06:00
|
|
|
ModeFilter: true,
|
2018-05-04 21:24:06 -05:00
|
|
|
Mode: addrs.DataResourceMode,
|
2017-01-22 14:57:19 -06:00
|
|
|
}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(`
|
|
|
|
data.aws_ami.foo
|
|
|
|
`)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigTransformer_nonUnique(t *testing.T) {
|
2018-05-04 21:24:06 -05:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
|
|
|
g.Add(NewNodeAbstractResource(
|
2020-03-20 16:07:46 -05:00
|
|
|
addrs.RootModule.Resource(
|
2018-05-04 21:24:06 -05:00
|
|
|
addrs.ManagedResourceMode, "aws_instance", "web",
|
|
|
|
),
|
|
|
|
))
|
|
|
|
tf := &ConfigTransformer{Config: testModule(t, "graph-basic")}
|
2017-01-22 14:57:19 -06:00
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(`
|
|
|
|
aws_instance.web
|
|
|
|
aws_instance.web
|
|
|
|
aws_load_balancer.weblb
|
|
|
|
aws_security_group.firewall
|
|
|
|
openstack_floating_ip.random
|
|
|
|
`)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-05 19:53:12 -05:00
|
|
|
const testConfigTransformerGraphBasicStr = `
|
|
|
|
aws_instance.web
|
|
|
|
aws_load_balancer.weblb
|
|
|
|
aws_security_group.firewall
|
|
|
|
openstack_floating_ip.random
|
|
|
|
`
|