Skip to content

Commit 2b8badf

Browse files
committed
add test
1 parent fdfdcd7 commit 2b8badf

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

helper/resource/importstate/examplecloud_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,3 +596,27 @@ func examplecloudResourceWithNullIdentityAttr() testprovider.Resource {
596596
},
597597
}
598598
}
599+
600+
// This example resource, on update plans, will plan a different identity to test that
601+
// our testing framework assertions catch an identity that differs after import/refresh.
602+
func examplecloudResourceWithChangingIdentity() testprovider.Resource {
603+
exampleCloudResource := examplecloudResource()
604+
605+
exampleCloudResource.PlanChangeFunc = func(ctx context.Context, req resource.PlanChangeRequest, resp *resource.PlanChangeResponse) {
606+
// Only on update
607+
if !req.PriorState.IsNull() && !req.ProposedNewState.IsNull() {
608+
resp.PlannedIdentity = teststep.Pointer(tftypes.NewValue(
609+
tftypes.Object{
610+
AttributeTypes: map[string]tftypes.Type{
611+
"id": tftypes.String,
612+
},
613+
},
614+
map[string]tftypes.Value{
615+
"id": tftypes.NewValue(tftypes.String, "easteurope/someothervalue"),
616+
},
617+
))
618+
}
619+
}
620+
621+
return exampleCloudResource
622+
}

helper/resource/importstate/import_block_with_resource_identity_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,39 @@ func TestImportBlock_WithResourceIdentity_WithEveryType(t *testing.T) {
119119
})
120120
}
121121

122+
func TestImportBlock_WithResourceIdentity_ChangingIdentityError(t *testing.T) {
123+
t.Parallel()
124+
125+
r.UnitTest(t, r.TestCase{
126+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
127+
tfversion.SkipBelow(tfversion.Version1_12_0), // ImportBlockWithResourceIdentity requires Terraform 1.12.0 or later
128+
},
129+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
130+
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
131+
Resources: map[string]testprovider.Resource{
132+
"examplecloud_container": examplecloudResourceWithChangingIdentity(),
133+
},
134+
}),
135+
},
136+
Steps: []r.TestStep{
137+
{
138+
Config: `
139+
resource "examplecloud_container" "test" {
140+
location = "westeurope"
141+
name = "somevalue"
142+
}`,
143+
},
144+
{
145+
ResourceName: "examplecloud_container.test",
146+
ImportState: true,
147+
ImportStateKind: r.ImportBlockWithResourceIdentity,
148+
// The plan following the import will produce a different identity value then test step 1
149+
ExpectError: regexp.MustCompile(`expected identity values map\[id:westeurope/somevalue\], got map\[id:easteurope/someothervalue\]`),
150+
},
151+
},
152+
})
153+
}
154+
122155
func TestImportBlock_WithResourceIdentity_RequiresVersion1_12_0(t *testing.T) {
123156
t.Parallel()
124157

0 commit comments

Comments
 (0)