-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
helper/resource: Skip data source states with TestStep.ImportStateChe…
…ck (#1089) * helper/resource: Skip data source states with TestStep.ImportStateCheck Reference: #1060 The `TestStep` type `ImportStateCheck` functionality is for verifying two specific scenarios over `ImportStateVerify`: - Resources which create multiple resources during import (an implementation detail which is an legacy anti-pattern that should no longer be present in resources). - Resources where importing may cause attributes to have syntactically different but semantically/functionally equivalent values that requires special logic to check. Terraform 1.3 and later can include data source states when importing resources. Rather than forcing provider developers to account for this Terraform version-specific behavior, the `ImportStateCheck` logic will now only receive managed resource states to match the intended usage. Previously with Terraform 1.2.9 and earlier: ``` --- PASS: TestTest_TestStep_ImportStateCheck_SkipDataSourceState (1.58s) ``` Previously with Terraform 1.3.4: ``` --- FAIL: TestTest_TestStep_ImportStateCheck_SkipDataSourceState (0.63s) /Users/bflad/src/github.com/hashicorp/terraform-plugin-sdk/helper/resource/testing_new_import_state_test.go:16: expected 1 state, got: 2 ``` Now with Terraform 1.3.4: ``` --- PASS: TestTest_TestStep_ImportStateCheck_SkipDataSourceState (0.91s) ``` * Update CHANGELOG for #1089
- Loading branch information
Showing
4 changed files
with
106 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:bug | ||
helper/resource: Fixed `TestStep` type `ImportStateCheck` field so that it only matches against resources following a change in behaviour in Terraform 1.3 that imports both resources and data sources into state | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package resource | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func TestTest_TestStep_ImportStateCheck_SkipDataSourceState(t *testing.T) { | ||
t.Parallel() | ||
|
||
UnitTest(t, TestCase{ | ||
ProviderFactories: map[string]func() (*schema.Provider, error){ | ||
"examplecloud": func() (*schema.Provider, error) { //nolint:unparam // required signature | ||
return &schema.Provider{ | ||
DataSourcesMap: map[string]*schema.Resource{ | ||
"examplecloud_thing": { | ||
ReadContext: func(_ context.Context, d *schema.ResourceData, _ interface{}) diag.Diagnostics { | ||
d.SetId("datasource-test") | ||
|
||
return nil | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
ResourcesMap: map[string]*schema.Resource{ | ||
"examplecloud_thing": { | ||
CreateContext: func(_ context.Context, d *schema.ResourceData, _ interface{}) diag.Diagnostics { | ||
d.SetId("resource-test") | ||
|
||
return nil | ||
}, | ||
DeleteContext: func(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { | ||
return nil | ||
}, | ||
ReadContext: func(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { | ||
return nil | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Computed: true, | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
}, | ||
}, | ||
}, nil | ||
}, | ||
}, | ||
Steps: []TestStep{ | ||
{ | ||
Config: ` | ||
data "examplecloud_thing" "test" {} | ||
resource "examplecloud_thing" "test" {} | ||
`, | ||
}, | ||
{ | ||
ResourceName: "examplecloud_thing.test", | ||
ImportState: true, | ||
ImportStateCheck: func(is []*terraform.InstanceState) error { | ||
if len(is) > 1 { | ||
return fmt.Errorf("expected 1 state, got: %d", len(is)) | ||
} | ||
|
||
return nil | ||
}, | ||
}, | ||
}, | ||
}) | ||
} |