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

object or map of objects attribute of object variable has incorrect default value in tests #35801

Closed
berahtlv opened this issue Oct 2, 2024 · 2 comments
Labels
bug new new issue not yet triaged

Comments

@berahtlv
Copy link

berahtlv commented Oct 2, 2024

Terraform Version

Terraform v1.9.6

Terraform Configuration Files

main.tf

variable "object" {
  type = object({
    disks = optional(map(object({
      size = optional(number)
    })))
    root_disk = optional(object({
      size = optional(number)
    }))
  })
}

main.tftest.hcl

run "object" {

  command = plan

  variables {
    object = {
    }
  }

  assert {
    condition = var.object == {
      disks = tomap(null)
      root_disk = null
    }
    error_message = "Value is incorrect"
  }
}

Debug Output

terraform console

> var.object
{
  "disks" = tomap(null) /* of object */
  "root_disk" = null /* object */
}

terraform test

main.tftest.hcl... in progress
  run "object"... fail
╷
│ Error: Test assertion failed
│ 
│   on main.tftest.hcl line 57, in run "object":
│   57:     condition = var.object == {
│   58:       disks = tomap(null)
│   59:       root_disk = null
│   60:     }
│     ├────────────────
│     │ var.object is object with 2 attributes
│ 
│ Value is incorrect
╵
main.tftest.hcl... tearing down
main.tftest.hcl... fail

Failure! 0 passed, 1 failed.

Expected Behavior

The test is passing.

Actual Behavior

The test fails. An issue arises if a nested object or map of objects has a null value. In the case of assigning the size attribute test is passing.

Updated variables block:

  variables {
    object = {
      root_disk = {
        size = 45
      }
      disks = {
        data = {
          size = 125
        }
      }
    }
  }

Update assert block:

  assert {
    condition = var.object == {
      disks = tomap({
        data = {
          size = 125
        }
      })
      root_disk = {
        size = 45
      }
    }
    error_message = "Value is incorrect"
  }

Steps to Reproduce

terraform test

Additional Context

No response

References

No response

@berahtlv berahtlv added bug new new issue not yet triaged labels Oct 2, 2024
@jbardin
Copy link
Member

jbardin commented Oct 2, 2024

Hi @berahtlv,

The code is working here as expected, because equality always compares precise types first, and you have not created an object of the same type. The object literal in your condition has the type:

object({
    disks: map(dynamic),
    root_disk: dynamic,
})

which is not going to match the. There are no default values, because you are not passing a value into the input variable, which is where the type constraint with the defaults is defined. The only way to get the exact same type conversion right now would be to create a module with the same type constraints and pass the value through for comparison. In general it's more reliable to compare the attributes you want directly rather than as a group in nested literals.

We do have open enhancement requests already for easier inline type conversions, like #23562, #26164, #27643, etc.

Thanks!

@jbardin jbardin closed this as completed Oct 2, 2024
Copy link
Contributor

github-actions bot commented Nov 2, 2024

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 github-actions bot locked as resolved and limited conversation to collaborators Nov 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug new new issue not yet triaged
Projects
None yet
Development

No branches or pull requests

2 participants