terraform/internal/lang/globalref/analyzer_meta_references_te...

171 lines
2.8 KiB
Go
Raw Normal View History

lang/globalref: Global reference analysis utilities Our existing functionality for dealing with references generally only has to concern itself with one level of references at a time, and only within one module, because we use it to draw a dependency graph which then ends up reflecting the broader context. However, there are some situations where it's handy to be able to ask questions about the indirect contributions to a particular expression in the configuration, particularly for additional hints in the user interface where we're just providing some extra context rather than changing behavior. This new "globalref" package therefore aims to be the home for algorithms for use-cases like this. It introduces its own special "Reference" type that wraps addrs.Reference to annotate it also with the usually-implied context about where the references would be evaluated. With that building block we can therefore ask questions whose answers might involve discussing references in multiple packages at once, such as "which resources directly or indirectly contribute to this expression?", including indirect hops through input variables or output values which would therefore change the evaluation context. The current implementations of this are around mapping references onto the static configuration expressions that they refer to, which is a pretty broad and conservative approach that unfortunately therefore loses accuracy when confronted with complex expressions that might take dynamic actions on the contents of an object. My hunch is that this'll be good enough to get some initial small use-cases solved, though there's plenty room for improvement in accuracy. It's somewhat ironic that this sort of "what is this value built from?" question is the use-case I had in mind when I designed the "marks" feature in cty, yet we've ended up putting it to an unexpected but still valid use in Terraform for sensitivity analysis and our currently handling of that isn't really tight enough to permit other concurrent uses of marks for other use-cases. I expect we can address that later and so maybe we'll try for a more accurate version of these analyses at a later date, but my hunch is that this'll be good enough for us to still get some good use out of it in the near future, particular related to helping understand where unknown values came from and in tailoring our refresh results in plan output to deemphasize detected changes that couldn't possibly have contributed to the proposed plan.
2021-06-09 21:11:44 +02:00
package globalref
import (
"sort"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform/internal/addrs"
)
func TestAnalyzerMetaReferences(t *testing.T) {
tests := []struct {
InputContainer string
InputRef string
WantRefs []string
}{
{
``,
`local.a`,
nil,
},
2022-02-03 23:31:23 +01:00
{
``,
`local.single`,
[]string{
"::test_thing.single.id",
},
},
lang/globalref: Global reference analysis utilities Our existing functionality for dealing with references generally only has to concern itself with one level of references at a time, and only within one module, because we use it to draw a dependency graph which then ends up reflecting the broader context. However, there are some situations where it's handy to be able to ask questions about the indirect contributions to a particular expression in the configuration, particularly for additional hints in the user interface where we're just providing some extra context rather than changing behavior. This new "globalref" package therefore aims to be the home for algorithms for use-cases like this. It introduces its own special "Reference" type that wraps addrs.Reference to annotate it also with the usually-implied context about where the references would be evaluated. With that building block we can therefore ask questions whose answers might involve discussing references in multiple packages at once, such as "which resources directly or indirectly contribute to this expression?", including indirect hops through input variables or output values which would therefore change the evaluation context. The current implementations of this are around mapping references onto the static configuration expressions that they refer to, which is a pretty broad and conservative approach that unfortunately therefore loses accuracy when confronted with complex expressions that might take dynamic actions on the contents of an object. My hunch is that this'll be good enough to get some initial small use-cases solved, though there's plenty room for improvement in accuracy. It's somewhat ironic that this sort of "what is this value built from?" question is the use-case I had in mind when I designed the "marks" feature in cty, yet we've ended up putting it to an unexpected but still valid use in Terraform for sensitivity analysis and our currently handling of that isn't really tight enough to permit other concurrent uses of marks for other use-cases. I expect we can address that later and so maybe we'll try for a more accurate version of these analyses at a later date, but my hunch is that this'll be good enough for us to still get some good use out of it in the near future, particular related to helping understand where unknown values came from and in tailoring our refresh results in plan output to deemphasize detected changes that couldn't possibly have contributed to the proposed plan.
2021-06-09 21:11:44 +02:00
{
``,
`test_thing.single`,
[]string{
"::local.a",
"::local.b",
},
},
{
``,
`test_thing.single.string`,
[]string{
"::local.a",
},
},
{
``,
`test_thing.for_each`,
[]string{
"::local.a",
"::test_thing.single.string",
},
},
{
``,
`test_thing.for_each["whatever"]`,
[]string{
"::local.a",
"::test_thing.single.string",
},
},
{
``,
`test_thing.for_each["whatever"].single`,
[]string{
"::test_thing.single.string",
},
},
{
``,
`test_thing.for_each["whatever"].single.z`,
[]string{
"::test_thing.single.string",
},
},
{
``,
`test_thing.count`,
[]string{
"::local.a",
},
},
{
``,
`test_thing.count[0]`,
[]string{
"::local.a",
},
},
{
``,
`module.single.a`,
[]string{
"module.single::test_thing.foo",
"module.single::var.a",
},
},
{
``,
`module.for_each["whatever"].a`,
[]string{
`module.for_each["whatever"]::test_thing.foo`,
`module.for_each["whatever"]::var.a`,
},
},
{
``,
`module.count[0].a`,
[]string{
`module.count[0]::test_thing.foo`,
`module.count[0]::var.a`,
},
},
{
`module.single`,
`var.a`,
[]string{
"::test_thing.single",
},
},
{
`module.single`,
`test_thing.foo`,
[]string{
"module.single::var.a",
},
},
}
azr := testAnalyzer(t, "assorted")
for _, test := range tests {
name := test.InputRef
if test.InputContainer != "" {
name = test.InputContainer + " " + test.InputRef
}
t.Run(name, func(t *testing.T) {
t.Logf("testing %s", name)
var containerAddr addrs.Targetable
containerAddr = addrs.RootModuleInstance
if test.InputContainer != "" {
moduleAddrTarget, diags := addrs.ParseTargetStr(test.InputContainer)
if diags.HasErrors() {
t.Fatalf("input module address is invalid: %s", diags.Err())
}
containerAddr = moduleAddrTarget.Subject
}
localRef, diags := addrs.ParseRefStr(test.InputRef)
if diags.HasErrors() {
t.Fatalf("input reference is invalid: %s", diags.Err())
}
ref := Reference{
ContainerAddr: containerAddr,
LocalRef: localRef,
}
refs := azr.MetaReferences(ref)
want := test.WantRefs
var got []string
for _, ref := range refs {
got = append(got, ref.DebugString())
}
sort.Strings(got)
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("wrong references\n%s", diff)
}
})
}
}