Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform test: Additional erroneous error message when referencing invalid attributes #35265

Open
liamcervante opened this issue May 31, 2024 · 2 comments
Labels
bug confirmed a Terraform Core team member has reproduced this issue explained a Terraform Core team member has described the root cause of this issue in code good first issue terraform test v1.8 Issues (primarily bugs) reported against v1.8 releases

Comments

@liamcervante
Copy link
Member

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 liamcervante added bug new new issue not yet triaged confirmed a Terraform Core team member has reproduced this issue terraform test and removed new new issue not yet triaged labels May 31, 2024
@liamcervante liamcervante self-assigned this Jun 3, 2024
@liamcervante
Copy link
Member Author

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 liamcervante removed their assignment Jun 3, 2024
@liamcervante liamcervante added good first issue explained a Terraform Core team member has described the root cause of this issue in code labels Jun 3, 2024
@gogingersnap777
Copy link

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

@apparentlymart apparentlymart added the v1.8 Issues (primarily bugs) reported against v1.8 releases label Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug confirmed a Terraform Core team member has reproduced this issue explained a Terraform Core team member has described the root cause of this issue in code good first issue terraform test v1.8 Issues (primarily bugs) reported against v1.8 releases
Projects
None yet
Development

No branches or pull requests

3 participants