hashicorp / hashicorp/terraform-plugin-sdk
TestCheckOutput does not expose data for testing
- Dominant language
- Go
- Stars
- 485
- Forks
- 244
- Avg merge
- 19h 57m
- Merged PRs (30d)
- 4
Description
### SDK version
```
github.com/hashicorp/terraform-plugin-sdk/v2 v2.14.0
```
### Relevant provider source code
`helper/resource/testing.go`
```go
// TestCheckOutput checks an output in the Terraform configuration
func TestCheckOutput(name, value string) TestCheckFunc {
return func(s *terraform.State) error {
ms := s.RootModule()
rs, ok := ms.Outputs[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
if rs.Value != value {
return fmt.Errorf(
"Output '%s': expected %#v, got %#v",
name,
value,
rs)
}
return nil
}
}
...
```
### Terraform Configuration Files
```hcl
data "nautobot_manufacturers" "list" {}
variable "filter" {
type = string
default = "Juniper"
}
output "vendor" {
value = [
for manufacturer in data.nautobot_manufacturers.list.manufacturers :
manufacturer.slug
if manufacturer.name == var.filter
]
}
```
### Debug Output
I'm trying to run a test case, where I know a computed attribute (`slug`) should return a specific value.
```go
func TestAccDataSourceManufacturers(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceManufacturer,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckOutput("vendor", "juniper"),
),
},
},
})
}
```
However, the value in the output is in a slice of empty interfaces (`[]interface {}{"juniper"}`), so it always fail and I can't find any resources in the package to get the concrete value of an element of this slice.
```bash
=== RUN TestAccDataSourceManufacturers
data_source_manufacturers_test.go:12: Step 1/1 error: Check failed: Check 1/1 error: Output 'vendor': expected "juniper", got &terraform.OutputState{Sensitive:false, Type:"list", Value:[]interface {}{"juniper"}, mu:sync.Mutex{state:0, sema:0x0}}
```
### Expected Behavior
I might be missing something, but maybe the `TestCheckOutput` should do reflection to expose the string value?
### Actual Behavior
The string I pass to the function `"juniper"` is compared against `"[]interface {}{"juniper"}"`, so my test fails.
### Steps to Reproduce
It's a test case.
### References
I couldn't find any previous issues nor test cases for `TestCheckOutput`.
Thanks
Contributor guide
Research direction
Start in helper/resource/testing.go and inspect TestCheckOutput, then reproduce the reported case using the TestAccDataSourceManufacturers example and its vendor output. Compare the helper's handling of scalar and list output values; done means the intended output value can be checked without the reported mismatch, with coverage for this behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100