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

Testing: When mocking a resource, wrong type for the resource field doesn't fail, simply ignores mock #1839

Closed
RLRabinowitz opened this issue Jul 21, 2024 · 4 comments · Fixed by #2220
Assignees
Labels
accepted This issue has been accepted for implementation. bug Something isn't working
Milestone

Comments

@RLRabinowitz
Copy link
Contributor

OpenTofu Version

1.8.0-beta2

OpenTofu Configuration Files

main.tf

provider "aws" {
  region = "us-west-2"
}

resource "aws_acm_certificate" "this" {
  domain_name       = "example.com"
  validation_method = "DNS"
}

tests/my-test.tftest.hcl

mock_provider "aws" {
  mock_resource "aws_acm_certificate" {
    defaults = {
      domain_validation_options = 2
    }
  }
}

run "test" {
  command = apply
  assert {
    condition = one(aws_acm_certificate.this.domain_validation_options) == 2
    error_message = "domain validation options is empty?"
  }
}

Debug Output

.

Expected Behavior

Should error, saying that the mocked domain_validation_options has an incompatible type

Actual Behavior

Test continues to run, and the mock is ignored

╷
│ Warning: Ignored mock/override field `domain_validation_options`
│ 
│   with aws_acm_certificate.this,
│   on main.tf line 5, in resource "aws_acm_certificate" "this":
│    5: resource "aws_acm_certificate" "this" {
│ 
│ Values provided for override / mock must match resource fields types: set of object required, but received number.
│ 
│ (and one more similar warning elsewhere)

Steps to Reproduce

  1. tofu init
  2. tofu test

Additional Context

No response

References

No response

@RLRabinowitz RLRabinowitz added bug Something isn't working pending-decision This issue has not been accepted for implementation nor rejected. It's still open to discussion. labels Jul 21, 2024
@cam72cam cam72cam added accepted This issue has been accepted for implementation. and removed pending-decision This issue has not been accepted for implementation nor rejected. It's still open to discussion. labels Jul 26, 2024
@cam72cam cam72cam added this to the 1.9.0 milestone Jul 26, 2024
@pooriaghaedi
Copy link
Contributor

@cam72cam Can you please assign it to me and also give me some directions?

@ollevche
Copy link
Member

ollevche commented Aug 2, 2024

Hey @pooriaghaedi!

Thank you for volunteering, I've assigned you. Please read the "I've been assigned an issue, now what?" section of the contribution guide before you begin working.

In mocks and overrides, we use MockValueComposer to generate resulting values. It is defined in hcl2shim package.

You can go through the diags creation in MockValueComposer's functions and make sure those diags have relevant Warning / Error types. Feel free to ask more questions!

@pooriaghaedi
Copy link
Contributor

Hi @ollevche,

I have tested the same code with v1.9.0-alpha1 version and I get the following response:

tests/my-test.tftest.hcl... fail
  run "test"... fail
╷
│ Warning: Ignored mock/override field `domain_validation_options`
│ 
│   with aws_acm_certificate.this,
│   on main.tf line 5, in resource "aws_acm_certificate" "this":
│    5: resource "aws_acm_certificate" "this" {
│ 
│ Values provided for override / mock must match resource fields types: set of object required, but received number.
│ 
│ (and one more similar warning elsewhere)
╵
╷
│ Error: Test assertion failed
│ 
│   on tests/my-test.tftest.hcl line 13, in run "test":13:     condition = one(aws_acm_certificate.this.domain_validation_options) == 2
│     ├────────────────
│     │ aws_acm_certificate.this.domain_validation_options is empty set of object
│ 
│ domain validation options is empty?
╵

Failure! 0 passed, 1 failed.

Should we consider this as resolved or does it still need investigation?

@ollevche
Copy link
Member

ollevche commented Aug 3, 2024

Hey @pooriaghaedi, the test assertion fails because the condition has not been met. If you change the condition to check against the empty set of objects, it will succeed.

We want tofu to fail due to mismatched mock / override types, which currently results in a warning. We should also review other warnings in MockValueComposer and turn some of them into errors.

pooriaghaedi added a commit to pooriaghaedi/opentofu that referenced this issue Aug 11, 2024
pooriaghaedi added a commit to pooriaghaedi/opentofu that referenced this issue Aug 11, 2024
pooriaghaedi added a commit to pooriaghaedi/opentofu that referenced this issue Aug 30, 2024
pooriaghaedi added a commit to pooriaghaedi/opentofu that referenced this issue Aug 30, 2024
pooriaghaedi added a commit to pooriaghaedi/opentofu that referenced this issue Aug 31, 2024
pooriaghaedi added a commit to pooriaghaedi/opentofu that referenced this issue Sep 15, 2024
pooriaghaedi added a commit to pooriaghaedi/opentofu that referenced this issue Sep 17, 2024
pooriaghaedi added a commit to pooriaghaedi/opentofu that referenced this issue Sep 17, 2024
@ollevche ollevche self-assigned this Nov 19, 2024
ollevche pushed a commit that referenced this issue Nov 27, 2024
ollevche pushed a commit that referenced this issue Nov 27, 2024
ollevche pushed a commit that referenced this issue Nov 27, 2024
ollevche pushed a commit that referenced this issue Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This issue has been accepted for implementation. bug Something isn't working
Projects
None yet
4 participants