terraform icon indicating copy to clipboard operation
terraform copied to clipboard

`terraform test`: Additional erroneous error message when referencing invalid attributes

Open liamcervante opened this issue 1 year ago • 4 comments

Terraform Version

v1.8.3

Terraform Configuration Files

# main.tf

variable "number" {
    type = number
}

data "external" "resource" {
    count = var.number

    program = [ "bash", "${path.module}/out.sh" ]
}

output "out" {
    value = data.external.resource
}
#!/bin/bash
# out.sh

echo "{\"hello\":\"world\"}"
# main.tftest.hcl

run "test" {
    variables {
        number = 1
    }

    assert {
        condition = data.external.resource[0].hello == "world"
        error_message = "wrong"
    }
}

Debug Output

N/A

Expected Behavior

main.tftest.hcl... in progress
  run "test"... fail
╷
│ Error: Unsupported attribute
│ 
│   on main.tftest.hcl line 8, in run "test":
│    8:         condition = data.external.resource[0].hello == "world"
│ 
│ This object has no argument, nested block, or exported attribute named "hello".
╵
main.tftest.hcl... tearing down
main.tftest.hcl... fail

Failure! 0 passed, 1 failed.

Actual Behavior

main.tftest.hcl... in progress
  run "test"... fail
╷
│ Error: Unknown variable
│ 
│   on main.tftest.hcl line 8, in run "test":
│    8:         condition = data.external.resource[0].hello == "world"
│ 
│ There is no variable named "data".
╵
╷
│ Error: Unsupported attribute
│ 
│   on main.tftest.hcl line 8, in run "test":
│    8:         condition = data.external.resource[0].hello == "world"
│ 
│ This object has no argument, nested block, or exported attribute named "hello".
╵
main.tftest.hcl... tearing down
main.tftest.hcl... fail

Failure! 0 passed, 1 failed.

Steps to Reproduce

  1. terraform init
  2. terraform test

Additional Context

No response

References

https://discuss.hashicorp.com/t/tf-test-1-8-cannot-test-custom-data-source-output/67461

liamcervante avatar May 31 '24 07:05 liamcervante

We are constructing a HCL evaluation context for the test command here. In this case, the actual construction of the context does some validation of its own which results in it returning the expected diagnostic along with an empty context. The error then comes as we are not exiting the evaluation early when the context isn't computed properly. Currently, we are just adding the diagnostics and then attempting to evaluate the condition anyway. This results in the confusing message we get as the context does not contain any variables.

The fix would be to skip the evaluation of the rule if we cannot build the context properly:

		hclCtx, moreDiags := scope.EvalContext(refs)
		ruleDiags = ruleDiags.Append(moreDiags)
                if moreDiags.HasErrors() {
                    // If we couldn't create the context properly then we can't
                    // evaluate the condition.
                    continue
                }

liamcervante avatar Jun 03 '24 11:06 liamcervante

Making changes for this issue! I was able to reproduced the behavior locally and working on the changes.

gogingersnap777 avatar Jun 03 '24 12:06 gogingersnap777

Hey can i take this issue up?

burnerlee avatar Jul 01 '24 07:07 burnerlee

Hi @burnerlee, I'll happily review a PR for this if you put one together.

liamcervante avatar Jul 01 '24 08:07 liamcervante

thanks

burnerlee avatar Jul 01 '24 15:07 burnerlee

@liamcervante opened a PR for the issue

burnerlee avatar Jul 01 '24 18:07 burnerlee

This is fixed and merged, will be released in v1.9.1. Thanks @burnerlee

liamcervante avatar Jul 02 '24 09:07 liamcervante

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

github-actions[bot] avatar Aug 02 '24 02:08 github-actions[bot]