Skip to content

Commit

Permalink
tests for destroy-then-update dependency ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
jbardin committed Mar 30, 2021
1 parent cc4d5e7 commit b2382b7
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 2 deletions.
71 changes: 71 additions & 0 deletions terraform/context_apply2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package terraform
import (
"errors"
"fmt"
"sync"
"testing"
"time"

"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/providers"
Expand Down Expand Up @@ -178,6 +180,75 @@ output "data" {
}
}

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

// verify that dependencies are updated in the state during refresh and apply
func TestApply_updateDependencies(t *testing.T) {
state := states.NewState()
Expand Down
64 changes: 62 additions & 2 deletions terraform/transform_destroy_edge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b2382b7

Please sign in to comment.