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

Test config with tuple output value results in a panic #310

Closed
austinvalle opened this issue Mar 22, 2024 · 1 comment · Fixed by #314
Closed

Test config with tuple output value results in a panic #310

austinvalle opened this issue Mar 22, 2024 · 1 comment · Fixed by #314
Assignees
Labels
bug Something isn't working
Milestone

Comments

@austinvalle
Copy link
Member

terraform-plugin-testing version

v1.7.0

Actual Behavior

The following test causes a panic when the testing framework attempts to shim the output from terraform show -json

func TestRecreateTuplePanic(t *testing.T) {
    resource.UnitTest(t, resource.TestCase{
        ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
            "framework": providerserver.NewProtocol6WithError(New()),
        },
        Steps: []resource.TestStep{
            {
                Config: `
                output "test" {
                    value = [true, "hello tuple!"]
                }`,
            },
        },
    })
}

Terraform State JSON output

{
  "format_version": "1.0",
  "terraform_version": "1.8.0",
  "values": {
    "outputs": {
      "test": {
        "sensitive": false,
        "value": [
          true,
          "hello tuple!"
        ],
        "type": [
          "tuple",
          [
            "bool",
            "string"
          ]
        ]
      }
    },
    "root_module": {}
  }
}

Relevant stack trace

2024-03-22T15:19:40.253-0400 [TRACE] sdk.helper_resource: Called Terraform CLI show command for JSON state: test_working_directory=/var/folders/t8/1tjpvj_d24x8yl0st5qswzf80000gp/T/plugintest66285806 test_step_number=1 test_name=TestRecreateTuplePanic test_terraform_path=/usr/local/bin/terraform
--- FAIL: TestRecreateTuplePanic (0.38s)
panic: interface conversion: interface {} is string, not bool [recovered]
	panic: interface conversion: interface {} is string, not bool

goroutine 35 [running]:
testing.tRunner.func1.2({0x105248400, 0x1400064aed0})
	/opt/homebrew/Cellar/go/1.22.0/libexec/src/testing/testing.go:1631 +0x1c4
testing.tRunner.func1()
	/opt/homebrew/Cellar/go/1.22.0/libexec/src/testing/testing.go:1634 +0x33c
panic({0x105248400?, 0x1400064aed0?})
	/opt/homebrew/Cellar/go/1.22.0/libexec/src/runtime/panic.go:770 +0x124
github.com/hashicorp/terraform-plugin-testing/helper/resource.shimOutputState(0x1400064a9c0)
	/Users/austin.valle/go/pkg/mod/github.com/hashicorp/[email protected]/helper/resource/state_shim.go:77 +0x574
github.com/hashicorp/terraform-plugin-testing/helper/resource.shimStateFromJson(0x140000f23c0)
	/Users/austin.valle/go/pkg/mod/github.com/hashicorp/[email protected]/helper/resource/state_shim.go:33 +0xe8
github.com/hashicorp/terraform-plugin-testing/helper/resource.getState({0x105380e90, 0x140003ac7b0}, {0x10538b4a8, 0x140001396c0}, 0x140000f2280)
	/Users/austin.valle/go/pkg/mod/github.com/hashicorp/[email protected]/helper/resource/testing_new.go:471 +0x50
github.com/hashicorp/terraform-plugin-testing/helper/resource.runNewTest.func1.1()
	/Users/austin.valle/go/pkg/mod/github.com/hashicorp/[email protected]/helper/resource/testing_new.go:71 +0x44
github.com/hashicorp/terraform-plugin-testing/helper/resource.runProviderCommand({0x105380e90, 0x140003ac7b0}, {0x10538b4a8, 0x140001396c0}, 0x140002975e8, 0x140000f2280, 0x140002997f8)
	/Users/austin.valle/go/pkg/mod/github.com/hashicorp/[email protected]/helper/resource/plugin.go:438 +0x1ddc
github.com/hashicorp/terraform-plugin-testing/helper/resource.runNewTest.func1()
	/Users/austin.valle/go/pkg/mod/github.com/hashicorp/[email protected]/helper/resource/testing_new.go:70 +0xc8
github.com/hashicorp/terraform-plugin-testing/helper/resource.runNewTest({0x105380e90, 0x140003ac7b0}, {0x10538b4a8, 0x140001396c0}, {0x1, 0x0, {0x0, 0x0, 0x0}, 0x0, ...}, ...)
	/Users/austin.valle/go/pkg/mod/github.com/hashicorp/[email protected]/helper/resource/testing_new.go:462 +0x11d0
github.com/hashicorp/terraform-plugin-testing/helper/resource.Test({0x10538b4a8, 0x140001396c0}, {0x1, 0x0, {0x0, 0x0, 0x0}, 0x0, 0x0, 0x140002adb30, ...})
	/Users/austin.valle/go/pkg/mod/github.com/hashicorp/[email protected]/helper/resource/testing.go:922 +0x59c
github.com/hashicorp/terraform-plugin-testing/helper/resource.UnitTest({0x10538b4a8, 0x140001396c0}, {0x1, 0x0, {0x0, 0x0, 0x0}, 0x0, 0x0, 0x140002adb30, ...})
	/Users/austin.valle/go/pkg/mod/github.com/hashicorp/[email protected]/helper/resource/testing.go:936 +0x68
github.com/hashicorp/terraform-provider-corner/internal/framework6provider.TestRecreateTuplePanic(0x140001396c0)
	/Users/austin.valle/code/terraform-provider-corner/internal/framework6provider/dynamic_function_test.go:44 +0x154
testing.tRunner(0x140001396c0, 0x1053702b8)
	/opt/homebrew/Cellar/go/1.22.0/libexec/src/testing/testing.go:1689 +0xec
created by testing.(*T).Run in goroutine 1
	/opt/homebrew/Cellar/go/1.22.0/libexec/src/testing/testing.go:1742 +0x318
FAIL	github.com/hashicorp/terraform-provider-corner/internal/framework6provider	0.902s
FAIL

Expected Behavior

The state shimming logic needs to be updated to support tuple values that might have different types in an array 😅

case []interface{}:
os.Type = "list"
if len(v) == 0 {
os.Value = v
return os, nil
}
switch firstElem := v[0].(type) {
case string:
elements := make([]interface{}, len(v))
for i, el := range v {
//nolint:forcetypeassert // Guaranteed by type switch
elements[i] = el.(string)
}
os.Value = elements
case bool:
elements := make([]interface{}, len(v))
for i, el := range v {
//nolint:forcetypeassert // Guaranteed by type switch
elements[i] = el.(bool)
}
os.Value = elements
// unmarshalled number from JSON will always be json.Number
case json.Number:
elements := make([]interface{}, len(v))
for i, el := range v {
//nolint:forcetypeassert // Guaranteed by type switch
elements[i] = el.(json.Number)
}
os.Value = elements
case []interface{}:
os.Value = v
case map[string]interface{}:
os.Value = v
default:
return nil, fmt.Errorf("unexpected output list element type: %T", firstElem)
}
return os, nil

References

Copy link

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 Apr 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
1 participant