diff --git a/terraform/context_apply2_test.go b/terraform/context_apply2_test.go index a739db2ad4..e0484197c7 100644 --- a/terraform/context_apply2_test.go +++ b/terraform/context_apply2_test.go @@ -3,7 +3,9 @@ package terraform import ( "errors" "fmt" + "sync" "testing" + "time" "github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/providers" @@ -177,3 +179,72 @@ output "data" { t.Fatal(diags.Err()) } } + +func TestContext2Apply_destroyThenUpdate(t *testing.T) { + m := testModuleInline(t, map[string]string{ + "main.tf": ` +resource "test_instance" "a" { + value = "udpated" +} +`, + }) + + p := testProvider("test") + p.PlanResourceChangeFn = testDiffFn + + var orderMu sync.Mutex + var order []string + p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) (resp providers.ApplyResourceChangeResponse) { + id := req.PriorState.GetAttr("id").AsString() + if id == "b" { + // slow down the b destroy, since a should wait for it + time.Sleep(100 * time.Millisecond) + } + + orderMu.Lock() + order = append(order, id) + orderMu.Unlock() + + resp.NewState = req.PlannedState + return resp + } + + addrA := mustResourceInstanceAddr(`test_instance.a`) + addrB := mustResourceInstanceAddr(`test_instance.b`) + + state := states.BuildState(func(s *states.SyncState) { + s.SetResourceInstanceCurrent(addrA, &states.ResourceInstanceObjectSrc{ + AttrsJSON: []byte(`{"id":"a","value":"old","type":"test"}`), + Status: states.ObjectReady, + }, mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`)) + + // test_instance.b depended on test_instance.a, and therefor should be + // destroyed before any changes to test_instance.a + s.SetResourceInstanceCurrent(addrB, &states.ResourceInstanceObjectSrc{ + AttrsJSON: []byte(`{"id":"b"}`), + Status: states.ObjectReady, + Dependencies: []addrs.ConfigResource{addrA.ContainingResource().Config()}, + }, mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`)) + }) + + ctx := testContext2(t, &ContextOpts{ + Config: m, + State: state, + Providers: map[addrs.Provider]providers.Factory{ + addrs.NewDefaultProvider("test"): testProviderFuncFixed(p), + }, + }) + + if _, diags := ctx.Plan(); diags.HasErrors() { + t.Fatal(diags.Err()) + } + + _, diags := ctx.Apply() + if diags.HasErrors() { + t.Fatal(diags.Err()) + } + + if order[0] != "b" { + t.Fatalf("expected apply order [b, a], got: %v\n", order) + } +} diff --git a/terraform/transform_destroy_edge_test.go b/terraform/transform_destroy_edge_test.go index 4d0c63ebb7..92b2448fde 100644 --- a/terraform/transform_destroy_edge_test.go +++ b/terraform/transform_destroy_edge_test.go @@ -260,14 +260,74 @@ module.child[1].test_object.c (destroy) } } +func TestDestroyEdgeTransformer_destroyThenUpdate(t *testing.T) { + g := Graph{Path: addrs.RootModuleInstance} + g.Add(testUpdateNode("test_object.A")) + g.Add(testDestroyNode("test_object.B")) + + state := states.NewState() + root := state.EnsureModule(addrs.RootModuleInstance) + root.SetResourceInstanceCurrent( + mustResourceInstanceAddr("test_object.A").Resource, + &states.ResourceInstanceObjectSrc{ + Status: states.ObjectReady, + AttrsJSON: []byte(`{"id":"A","test_string":"old"}`), + }, + mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`), + ) + root.SetResourceInstanceCurrent( + mustResourceInstanceAddr("test_object.B").Resource, + &states.ResourceInstanceObjectSrc{ + Status: states.ObjectReady, + AttrsJSON: []byte(`{"id":"B","test_string":"x"}`), + Dependencies: []addrs.ConfigResource{mustConfigResourceAddr("test_object.A")}, + }, + mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`), + ) + + if err := (&AttachStateTransformer{State: state}).Transform(&g); err != nil { + t.Fatal(err) + } + + m := testModuleInline(t, map[string]string{ + "main.tf": ` +resource "test_instance" "a" { + test_string = "udpated" +} +`, + }) + tf := &DestroyEdgeTransformer{ + Config: m, + Schemas: simpleTestSchemas(), + } + if err := tf.Transform(&g); err != nil { + t.Fatalf("err: %s", err) + } + + expected := strings.TrimSpace(` +test_object.A + test_object.B (destroy) +test_object.B (destroy) +`) + actual := strings.TrimSpace(g.String()) + + if actual != expected { + t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected) + } +} + func testDestroyNode(addrString string) GraphNodeDestroyer { instAddr := mustResourceInstanceAddr(addrString) - inst := NewNodeAbstractResourceInstance(instAddr) - return &NodeDestroyResourceInstance{NodeAbstractResourceInstance: inst} } +func testUpdateNode(addrString string) GraphNodeCreator { + instAddr := mustResourceInstanceAddr(addrString) + inst := NewNodeAbstractResourceInstance(instAddr) + return &NodeApplyableResourceInstance{NodeAbstractResourceInstance: inst} +} + const testTransformDestroyEdgeBasicStr = ` test_object.A (destroy) test_object.B (destroy)