opentofu/opentofu

Crash when using provider-defined functions in tests

Closed this issue · 4 comments

psi commented

OpenTofu Version

OpenTofu v1.7.0
on darwin_amd64
+ provider registry.opentofu.org/northwood-labs/corefunc v1.4.0

OpenTofu Configuration Files

# main.tf
terraform {
  required_providers {
    corefunc = {
      source  = "northwood-labs/corefunc"
      version = "~> 1.4"
    }
  }
}

provider "corefunc" {}

output "result" {
  value = "foo bar"
}

# main.tftest.hcl
run "check_result" {
  command = plan

  assert {
    condition = provider::corefunc::str_snake(output.result) == "foo_bar"
    error_message = "Result does not match"
  }
}

Debug Output

https://gist.github.com/psi/578c61e857a3ad1288420299629434b5

Expected Behavior

I expected the test to pass without error. I realize the implementations are unrelated, but I also tested this in Terraform 1.8.2 and it does work there, but of course I wouldn't have access to dynamic go functions in that scenario.

Actual Behavior

I was playing around with provider-defined functions and terraform-provider-go and was curious to see if I was able to use functions in a test case. I observed a crash when trying to use a dynamic go provider function, and so tried a simpler case using corefunc, which also crashed. Interestingly, the output also includes Success! 1 passed, 0 failed., so it looks like the tests do run successfully, but the crash happens elsewhere.

Steps to Reproduce

  1. tofu init
  2. tofu test

Additional Context

Thanks for taking a look at this! I'm very excited about provider-defined functions and especially dynamic functions. My use case for testing this was to experiment with a Go function to run a Kubernetes job whose output can be asserted against in the interest of running some lightweight integration tests as part of a tofu test run.

References

No response

Looks like this was a miss on my part. I didn't realize our test code was creating it's own evaluation scope (test_context.go:95).

psi commented

Thank you, @cam72cam!

#1601 changes it to an error message instead of a panic. I need to dig a bit deeper into supporting provider functions in the test context.

Alright, I think I've got a good handle on the fix now and it is ready for review and then inclusion in 1.7.1.