tests for destroy-then-update dependency ordering

This commit is contained in:
James Bardin 2021-03-22 14:18:36 -04:00
parent 8671f40768
commit 0bc64e3cc4
2 changed files with 133 additions and 2 deletions

View File

@ -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)
}
}

View File

@ -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)