mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
bdcac8792d
Previously we were using the type name requested in the import to select the schema, but a provider is free to return additional objects of other types as part of an import result, and so it's important that we perform schema selection separately for each returned object. If we don't do this, we get confusing downstream errors where the resulting object decodes to the wrong type and breaks various invariants expected by Terraform Core. The testResourceImportOther test in the test provider didn't catch this previously because it happened to have an identical schema to the other resource type being imported. Now the schema is changed and also there's a computed attribute we can set as part of the refresh phase to make sure we're completing the Read call properly during import. Refresh was working correctly, but we didn't have any tests for it as part of the import flow.
56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
package test
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
)
|
|
|
|
func TestResourceImportOther(t *testing.T) {
|
|
resource.UnitTest(t, resource.TestCase{
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckResourceDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: strings.TrimSpace(`
|
|
resource "test_resource_import_other" "foo" {
|
|
}
|
|
`),
|
|
},
|
|
{
|
|
ImportState: true,
|
|
ResourceName: "test_resource_import_other.foo",
|
|
|
|
ImportStateCheck: func(iss []*terraform.InstanceState) error {
|
|
if got, want := len(iss), 2; got != want {
|
|
return fmt.Errorf("wrong number of resources %d; want %d", got, want)
|
|
}
|
|
|
|
byID := make(map[string]*terraform.InstanceState, len(iss))
|
|
for _, is := range iss {
|
|
byID[is.ID] = is
|
|
}
|
|
|
|
if is, ok := byID["import_other_main"]; !ok {
|
|
return fmt.Errorf("no instance with id import_other_main in state")
|
|
} else if got, want := is.Ephemeral.Type, "test_resource_import_other"; got != want {
|
|
return fmt.Errorf("import_other_main has wrong type %q; want %q", got, want)
|
|
} else if got, want := is.Attributes["computed"], "hello!"; got != want {
|
|
return fmt.Errorf("import_other_main has wrong value %q for its computed attribute; want %q", got, want)
|
|
}
|
|
if is, ok := byID["import_other_other"]; !ok {
|
|
return fmt.Errorf("no instance with id import_other_other in state")
|
|
} else if got, want := is.Ephemeral.Type, "test_resource_defaults"; got != want {
|
|
return fmt.Errorf("import_other_other has wrong type %q; want %q", got, want)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|