core: Fix panic on invalid depends_on root. (#21589)

Report an invalid reference to the user instead of crashing when there is in error traversing the `depends_on` attribute.

Fixes #21590
This commit is contained in:
tinocode 2019-06-06 14:43:22 +02:00 committed by Kristin Laemmert
parent e71e3d85a9
commit 51a4055198
2 changed files with 19 additions and 3 deletions

View File

@ -373,7 +373,7 @@ func (n *EvalValidateResource) Eval(ctx EvalContext) (interface{}, error) {
for _, traversal := range n.Config.DependsOn {
ref, refDiags := addrs.ParseRef(traversal)
diags = diags.Append(refDiags)
if len(ref.Remaining) != 0 {
if !refDiags.HasErrors() && len(ref.Remaining) != 0 {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid depends_on reference",

View File

@ -328,8 +328,8 @@ func TestEvalValidateResource_invalidDependsOn(t *testing.T) {
t.Fatalf("error for supposedly-valid config: %s", err)
}
// No we'll make it invalid, but adding additional traversal steps
// on the end of what we're referencing. This is intended to catch the
// Now we'll make it invalid by adding additional traversal steps at
// the end of what we're referencing. This is intended to catch the
// situation where the user tries to depend on e.g. a specific resource
// attribute, rather than the whole resource, like aws_instance.foo.id.
rc.DependsOn = append(rc.DependsOn, hcl.Traversal{
@ -351,6 +351,22 @@ func TestEvalValidateResource_invalidDependsOn(t *testing.T) {
if got, want := err.Error(), "Invalid depends_on reference"; !strings.Contains(got, want) {
t.Fatalf("wrong error\ngot: %s\nwant: Message containing %q", got, want)
}
// Test for handling an unknown root without attribute, like a
// typo that omits the dot inbetween "path.module".
rc.DependsOn = append(rc.DependsOn, hcl.Traversal{
hcl.TraverseRoot{
Name: "pathmodule",
},
})
_, err = node.Eval(ctx)
if err == nil {
t.Fatal("no error for invalid depends_on")
}
if got, want := err.Error(), "Invalid depends_on reference"; !strings.Contains(got, want) {
t.Fatalf("wrong error\ngot: %s\nwant: Message containing %q", got, want)
}
}
func TestEvalValidateProvisioner_valid(t *testing.T) {