Skip to content

Commit 3280649

Browse files
committed
Add test with import identity
1 parent 6379ce7 commit 3280649

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

internal/terraform/context_apply2_test.go

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2114,7 +2114,7 @@ resource "unused_resource" "test" {
21142114
assertNoErrors(t, diags)
21152115
}
21162116

2117-
func TestContext2Apply_import(t *testing.T) {
2117+
func TestContext2Apply_import_ID(t *testing.T) {
21182118
m := testModuleInline(t, map[string]string{
21192119
"main.tf": `
21202120
resource "test_resource" "a" {
@@ -2188,6 +2188,82 @@ import {
21882188
}
21892189
}
21902190

2191+
func TestContext2Apply_import_identity(t *testing.T) {
2192+
m := testModuleInline(t, map[string]string{
2193+
"main.tf": `
2194+
resource "test_resource" "a" {
2195+
id = "importable"
2196+
}
2197+
2198+
import {
2199+
to = test_resource.a
2200+
identity = {
2201+
id = "importable"
2202+
}
2203+
}
2204+
`,
2205+
})
2206+
2207+
p := testProvider("test")
2208+
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&providerSchema{
2209+
ResourceTypes: map[string]*configschema.Block{
2210+
"test_resource": {
2211+
Attributes: map[string]*configschema.Attribute{
2212+
"id": {
2213+
Type: cty.String,
2214+
Required: true,
2215+
},
2216+
},
2217+
},
2218+
},
2219+
})
2220+
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
2221+
return providers.PlanResourceChangeResponse{
2222+
PlannedState: req.ProposedNewState,
2223+
}
2224+
}
2225+
p.ImportResourceStateFn = func(req providers.ImportResourceStateRequest) providers.ImportResourceStateResponse {
2226+
return providers.ImportResourceStateResponse{
2227+
ImportedResources: []providers.ImportedResource{
2228+
{
2229+
TypeName: "test_instance",
2230+
State: cty.ObjectVal(map[string]cty.Value{
2231+
"id": cty.StringVal("importable"),
2232+
}),
2233+
},
2234+
},
2235+
}
2236+
}
2237+
hook := new(MockHook)
2238+
ctx := testContext2(t, &ContextOpts{
2239+
Hooks: []Hook{hook},
2240+
Providers: map[addrs.Provider]providers.Factory{
2241+
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
2242+
},
2243+
})
2244+
plan, diags := ctx.Plan(m, states.NewState(), &PlanOpts{
2245+
Mode: plans.NormalMode,
2246+
})
2247+
assertNoErrors(t, diags)
2248+
2249+
_, diags = ctx.Apply(plan, m, nil)
2250+
assertNoErrors(t, diags)
2251+
2252+
if !hook.PreApplyImportCalled {
2253+
t.Fatalf("PreApplyImport hook not called")
2254+
}
2255+
if addr, wantAddr := hook.PreApplyImportAddr, mustResourceInstanceAddr("test_resource.a"); !addr.Equal(wantAddr) {
2256+
t.Errorf("expected addr to be %s, but was %s", wantAddr, addr)
2257+
}
2258+
2259+
if !hook.PostApplyImportCalled {
2260+
t.Fatalf("PostApplyImport hook not called")
2261+
}
2262+
if addr, wantAddr := hook.PostApplyImportAddr, mustResourceInstanceAddr("test_resource.a"); !addr.Equal(wantAddr) {
2263+
t.Errorf("expected addr to be %s, but was %s", wantAddr, addr)
2264+
}
2265+
}
2266+
21912267
func TestContext2Apply_destroySkipsVariableValidations(t *testing.T) {
21922268
m := testModuleInline(t, map[string]string{
21932269
"main.tf": `

0 commit comments

Comments
 (0)