Skip to content

Allow expect_failure to assert on an error message #37835

@bebold-jhr

Description

@bebold-jhr

Terraform Version

Terraform v1.13.4
on darwin_arm64

Use Cases

I'm doing a lot of input validation. However I would like to be able to not only check that validation fails by using:

expect_failures = [
    var.input,
  ]

I wan to be able to pinpoint the exact cause.
The reason is that the current expect_failure does not guarantee that my test setup doesn't have a different invalid value. For complex objects with various validations this can lead to an invalid test not verifying the correct validation under test.

Attempted Solutions

I don't see a workaround.

Proposal

For a variable with complex setup and multiple validations:

variable "input" {
  type = object({
    name = string
   // anything else
  })

  validation {
    condition = var.input != null && startswith(var.input.name, " ") ])
    error_message = "Validation error in {input.name}: Must not start with a whitespace"
  }

// further validations

Maybe something like this:

expect_failures = {
    "Validation error in {input.name}: Must not start with a whitespace" = var.input
}

Where the key is the expected error_message and the value is the reference.

You could add multiple entries, but only one per message.

expect_failures = {
    "Validation error in {input.name}: Must not start with a whitespace" = var.input
    "Validation error in {input.id}: is not a valid UUID" = var.input
}

To solve that maybe something like:

expect_failures = {
    "Validation error in {input.name}: Must not start with a whitespace" = [
      var.input,
    ]
    "ID not a valid UUID" = [
      var.input,
      var.target,
    ]
}

References

Other programming languages give you the ability to check for the message of an exception.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions