mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-27 09:21:14 -06:00
7bdf4a925d
The previous behavior of targets was that targeting a particular node would implicitly target everything it depends on. This makes sense when the dependencies in question are between resources, since we need to make sure all of a resource's dependencies are in place before we can create or update it. However, it had the undesirable side-effect that targeting a resource would _exclude_ any outputs referring to it, since the dependency edge goes from output to resource. This then causes the output to be "stale", which is problematic when outputs are being consumed by downstream configs using terraform_remote_state. GraphNodeTargetDownstream allows nodes to opt-in to a new behavior where they can be targeted by _inverted_ dependency edges. That is, it allows outputs to be considered targeted if anything they directly depend on is targeted. This is different than the implied targeting behavior in the other direction because transitive dependencies are not considered unless the intermediate nodes themselves have TargetDownstream. This means that an output1→output2→resource chain can implicitly target both outputs, but an output→resource1→resource2 chain _won't_ target the output if only resource2 is targeted. This behavior creates a scenario where an output can be visited before all of its dependencies are ready, since it may have a mixture of both targeted and untargeted dependencies. This is fine for outputs because they silently ignore any errors encountered during interpolation anyway, but other hypothetical future implementers of this interface may need to be more careful. This fixes #14186.
163 lines
3.6 KiB
Go
163 lines
3.6 KiB
Go
package terraform
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestTargetsTransformer(t *testing.T) {
|
|
mod := testModule(t, "transform-targets-basic")
|
|
|
|
g := Graph{Path: RootModulePath}
|
|
{
|
|
tf := &ConfigTransformer{Module: mod}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &AttachResourceConfigTransformer{Module: mod}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &ReferenceTransformer{}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &TargetsTransformer{Targets: []string{"aws_instance.me"}}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
expected := strings.TrimSpace(`
|
|
aws_instance.me
|
|
aws_subnet.me
|
|
aws_subnet.me
|
|
aws_vpc.me
|
|
aws_vpc.me
|
|
`)
|
|
if actual != expected {
|
|
t.Fatalf("bad:\n\nexpected:\n%s\n\ngot:\n%s\n", expected, actual)
|
|
}
|
|
}
|
|
|
|
func TestTargetsTransformer_downstream(t *testing.T) {
|
|
mod := testModule(t, "transform-targets-downstream")
|
|
|
|
g := Graph{Path: RootModulePath}
|
|
{
|
|
transform := &ConfigTransformer{Module: mod}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("%T failed: %s", transform, err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &AttachResourceConfigTransformer{Module: mod}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("%T failed: %s", transform, err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &AttachResourceConfigTransformer{Module: mod}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("%T failed: %s", transform, err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &OutputTransformer{Module: mod}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("%T failed: %s", transform, err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &ReferenceTransformer{}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &TargetsTransformer{Targets: []string{"module.child.module.grandchild.aws_instance.foo"}}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("%T failed: %s", transform, err)
|
|
}
|
|
}
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
// Even though we only asked to target the grandchild resource, all of the
|
|
// outputs that descend from it are also targeted.
|
|
expected := strings.TrimSpace(`
|
|
module.child.module.grandchild.aws_instance.foo
|
|
module.child.module.grandchild.output.id
|
|
module.child.module.grandchild.aws_instance.foo
|
|
module.child.output.grandchild_id
|
|
module.child.module.grandchild.output.id
|
|
output.grandchild_id
|
|
module.child.output.grandchild_id
|
|
`)
|
|
if actual != expected {
|
|
t.Fatalf("bad:\n\nexpected:\n%s\n\ngot:\n%s\n", expected, actual)
|
|
}
|
|
}
|
|
|
|
func TestTargetsTransformer_destroy(t *testing.T) {
|
|
mod := testModule(t, "transform-targets-destroy")
|
|
|
|
g := Graph{Path: RootModulePath}
|
|
{
|
|
tf := &ConfigTransformer{Module: mod}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &AttachResourceConfigTransformer{Module: mod}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &ReferenceTransformer{}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &TargetsTransformer{
|
|
Targets: []string{"aws_instance.me"},
|
|
Destroy: true,
|
|
}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
expected := strings.TrimSpace(`
|
|
aws_elb.me
|
|
aws_instance.me
|
|
aws_instance.me
|
|
aws_instance.metoo
|
|
aws_instance.me
|
|
`)
|
|
if actual != expected {
|
|
t.Fatalf("bad:\n\nexpected:\n%s\n\ngot:\n%s\n", expected, actual)
|
|
}
|
|
}
|