update test states that need dependency info

A number of tests had no, or incomplete state for the transformations
they wanted to test. Add states state with the correct dependencies for
these tests.
This commit is contained in:
James Bardin 2019-11-19 17:41:45 -05:00
parent 23112e198a
commit c47f100e56
4 changed files with 248 additions and 61 deletions

View File

@ -9175,33 +9175,48 @@ func TestContext2Apply_createBefore_depends(t *testing.T) {
p := testProvider("aws") p := testProvider("aws")
p.ApplyFn = testApplyFn p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn p.DiffFn = testDiffFn
state := MustShimLegacyState(&State{ state := states.NewState()
Modules: []*ModuleState{ root := state.EnsureModule(addrs.RootModuleInstance)
&ModuleState{ root.SetResourceInstanceCurrent(
Path: rootModulePath, addrs.Resource{
Resources: map[string]*ResourceState{ Mode: addrs.ManagedResourceMode,
"aws_instance.web": &ResourceState{ Type: "aws_instance",
Name: "web",
}.Instance(addrs.NoKey),
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"bar","require_new":"ami-old"}`),
},
addrs.ProviderConfig{
Type: "aws",
}.Absolute(addrs.RootModuleInstance),
)
root.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "lb",
}.Instance(addrs.NoKey),
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"baz","instance":"bar"}`),
Dependencies: []addrs.AbsResource{
addrs.AbsResource{
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance", Type: "aws_instance",
Primary: &InstanceState{ Name: "web",
ID: "bar",
Attributes: map[string]string{
"require_new": "ami-old",
},
},
},
"aws_instance.lb": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "baz",
Attributes: map[string]string{
"instance": "bar",
},
},
}, },
Module: addrs.RootModuleInstance,
}, },
}, },
}, },
}) addrs.ProviderConfig{
Type: "aws",
}.Absolute(addrs.RootModuleInstance),
)
ctx := testContext2(t, &ContextOpts{ ctx := testContext2(t, &ContextOpts{
Config: m, Config: m,
Hooks: []Hook{h}, Hooks: []Hook{h},
@ -9241,17 +9256,18 @@ func TestContext2Apply_createBefore_depends(t *testing.T) {
// Test that things were managed _in the right order_ // Test that things were managed _in the right order_
order := h.States order := h.States
diffs := h.Diffs diffs := h.Diffs
if !order[0].IsNull() || diffs[0].Action == plans.Delete { if !order[0].IsNull() || diffs[0].Action == plans.Delete {
t.Fatalf("should create new instance first: %#v", order) t.Fatalf("should create new instance first: %#v", order)
} }
if order[1].GetAttr("id").AsString() != "baz" { if order[1].GetAttr("id").AsString() != "baz" {
t.Fatalf("update must happen after create: %#v", order) t.Fatalf("update must happen after create: %#v", order[1])
} }
if order[2].GetAttr("id").AsString() != "bar" || diffs[2].Action != plans.Delete { if order[2].GetAttr("id").AsString() != "bar" || diffs[2].Action != plans.Delete {
t.Fatalf("destroy must happen after update: %#v", order) t.Fatalf("destroy must happen after update: %#v", order[2])
} }
} }
@ -9290,33 +9306,48 @@ func TestContext2Apply_singleDestroy(t *testing.T) {
return testApplyFn(info, s, d) return testApplyFn(info, s, d)
} }
p.DiffFn = testDiffFn p.DiffFn = testDiffFn
state := MustShimLegacyState(&State{ state := states.NewState()
Modules: []*ModuleState{ root := state.EnsureModule(addrs.RootModuleInstance)
&ModuleState{ root.SetResourceInstanceCurrent(
Path: rootModulePath, addrs.Resource{
Resources: map[string]*ResourceState{ Mode: addrs.ManagedResourceMode,
"aws_instance.web": &ResourceState{ Type: "aws_instance",
Name: "web",
}.Instance(addrs.NoKey),
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"bar","require_new":"ami-old"}`),
},
addrs.ProviderConfig{
Type: "aws",
}.Absolute(addrs.RootModuleInstance),
)
root.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "lb",
}.Instance(addrs.NoKey),
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"baz","instance":"bar"}`),
Dependencies: []addrs.AbsResource{
addrs.AbsResource{
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance", Type: "aws_instance",
Primary: &InstanceState{ Name: "web",
ID: "bar",
Attributes: map[string]string{
"require_new": "ami-old",
},
},
},
"aws_instance.lb": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "baz",
Attributes: map[string]string{
"instance": "bar",
},
},
}, },
Module: addrs.RootModuleInstance,
}, },
}, },
}, },
}) addrs.ProviderConfig{
Type: "aws",
}.Absolute(addrs.RootModuleInstance),
)
ctx := testContext2(t, &ContextOpts{ ctx := testContext2(t, &ContextOpts{
Config: m, Config: m,
Hooks: []Hook{h}, Hooks: []Hook{h},

View File

@ -89,11 +89,32 @@ func TestApplyGraphBuilder_depCbd(t *testing.T) {
}, },
} }
state := states.NewState()
root := state.EnsureModule(addrs.RootModuleInstance)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_object.A").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"A"}`),
},
mustProviderConfig("provider.test"),
)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_object.B").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"B","test_list":["x"]}`),
Dependencies: []addrs.AbsResource{mustResourceAddr("test_object.A")},
},
mustProviderConfig("provider.test"),
)
b := &ApplyGraphBuilder{ b := &ApplyGraphBuilder{
Config: testModule(t, "graph-builder-apply-dep-cbd"), Config: testModule(t, "graph-builder-apply-dep-cbd"),
Changes: changes, Changes: changes,
Components: simpleMockComponentFactory(), Components: simpleMockComponentFactory(),
Schemas: simpleTestSchemas(), Schemas: simpleTestSchemas(),
State: state,
} }
g, err := b.Build(addrs.RootModuleInstance) g, err := b.Build(addrs.RootModuleInstance)

View File

@ -210,6 +210,22 @@ func mustResourceInstanceAddr(s string) addrs.AbsResourceInstance {
return addr return addr
} }
func mustResourceAddr(s string) addrs.AbsResource {
addr, diags := addrs.ParseAbsResourceStr(s)
if diags.HasErrors() {
panic(diags.Err())
}
return addr
}
func mustProviderConfig(s string) addrs.AbsProviderConfig {
p, diags := addrs.ParseAbsProviderConfigStr(s)
if diags.HasErrors() {
panic(diags.Err())
}
return p
}
func instanceObjectIdForTests(obj *states.ResourceInstanceObject) string { func instanceObjectIdForTests(obj *states.ResourceInstanceObject) string {
v := obj.Value v := obj.Value
if v.IsNull() || !v.IsKnown() { if v.IsNull() || !v.IsKnown() {

View File

@ -7,9 +7,10 @@ import (
"github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/plans" "github.com/hashicorp/terraform/plans"
"github.com/hashicorp/terraform/states"
) )
func cbdTestGraph(t *testing.T, mod string, changes *plans.Changes) *Graph { func cbdTestGraph(t *testing.T, mod string, changes *plans.Changes, state *states.State) *Graph {
module := testModule(t, mod) module := testModule(t, mod)
applyBuilder := &ApplyGraphBuilder{ applyBuilder := &ApplyGraphBuilder{
@ -17,6 +18,7 @@ func cbdTestGraph(t *testing.T, mod string, changes *plans.Changes) *Graph {
Changes: changes, Changes: changes,
Components: simpleMockComponentFactory(), Components: simpleMockComponentFactory(),
Schemas: simpleTestSchemas(), Schemas: simpleTestSchemas(),
State: state,
} }
g, err := (&BasicGraphBuilder{ g, err := (&BasicGraphBuilder{
Steps: cbdTestSteps(applyBuilder.Steps()), Steps: cbdTestSteps(applyBuilder.Steps()),
@ -77,7 +79,27 @@ func TestCBDEdgeTransformer(t *testing.T) {
}, },
} }
g := cbdTestGraph(t, "transform-destroy-cbd-edge-basic", changes) state := states.NewState()
root := state.EnsureModule(addrs.RootModuleInstance)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_object.A").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"A"}`),
},
mustProviderConfig("provider.test"),
)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_object.B").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"B","test_list":["x"]}`),
Dependencies: []addrs.AbsResource{mustResourceAddr("test_object.A")},
},
mustProviderConfig("provider.test"),
)
g := cbdTestGraph(t, "transform-destroy-cbd-edge-basic", changes, state)
g = filterInstances(g) g = filterInstances(g)
actual := strings.TrimSpace(g.String()) actual := strings.TrimSpace(g.String())
@ -119,7 +141,38 @@ func TestCBDEdgeTransformerMulti(t *testing.T) {
}, },
} }
g := cbdTestGraph(t, "transform-destroy-cbd-edge-multi", changes) state := states.NewState()
root := state.EnsureModule(addrs.RootModuleInstance)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_object.A").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"A"}`),
},
mustProviderConfig("provider.test"),
)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_object.B").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"B"}`),
},
mustProviderConfig("provider.test"),
)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_object.C").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"C","test_list":["x"]}`),
Dependencies: []addrs.AbsResource{
mustResourceAddr("test_object.A"),
mustResourceAddr("test_object.B"),
},
},
mustProviderConfig("provider.test"),
)
g := cbdTestGraph(t, "transform-destroy-cbd-edge-multi", changes, state)
g = filterInstances(g) g = filterInstances(g)
actual := strings.TrimSpace(g.String()) actual := strings.TrimSpace(g.String())
@ -166,7 +219,36 @@ func TestCBDEdgeTransformer_depNonCBDCount(t *testing.T) {
}, },
} }
g := cbdTestGraph(t, "transform-cbd-destroy-edge-count", changes) state := states.NewState()
root := state.EnsureModule(addrs.RootModuleInstance)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_object.A").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"A"}`),
},
mustProviderConfig("provider.test"),
)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_object.B[0]").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"B","test_list":["x"]}`),
Dependencies: []addrs.AbsResource{mustResourceAddr("test_object.A")},
},
mustProviderConfig("provider.test"),
)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_object.B[1]").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"B","test_list":["x"]}`),
Dependencies: []addrs.AbsResource{mustResourceAddr("test_object.A")},
},
mustProviderConfig("provider.test"),
)
g := cbdTestGraph(t, "transform-cbd-destroy-edge-count", changes, state)
actual := strings.TrimSpace(g.String()) actual := strings.TrimSpace(g.String())
expected := regexp.MustCompile(strings.TrimSpace(` expected := regexp.MustCompile(strings.TrimSpace(`
@ -215,22 +297,59 @@ func TestCBDEdgeTransformer_depNonCBDCountBoth(t *testing.T) {
}, },
} }
g := cbdTestGraph(t, "transform-cbd-destroy-edge-both-count", changes) state := states.NewState()
root := state.EnsureModule(addrs.RootModuleInstance)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_object.A[0]").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"A"}`),
},
mustProviderConfig("provider.test"),
)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_object.A[1]").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"A"}`),
},
mustProviderConfig("provider.test"),
)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_object.B[0]").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"B","test_list":["x"]}`),
Dependencies: []addrs.AbsResource{mustResourceAddr("test_object.A")},
},
mustProviderConfig("provider.test"),
)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_object.B[1]").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"B","test_list":["x"]}`),
Dependencies: []addrs.AbsResource{mustResourceAddr("test_object.A")},
},
mustProviderConfig("provider.test"),
)
g := cbdTestGraph(t, "transform-cbd-destroy-edge-both-count", changes, state)
actual := strings.TrimSpace(g.String()) actual := strings.TrimSpace(g.String())
expected := regexp.MustCompile(strings.TrimSpace(` expected := regexp.MustCompile(strings.TrimSpace(`
test_object.A \(destroy deposed \w+\)
test_object.A\[0\]
test_object.A\[1\]
test_object.B\[0\]
test_object.B\[1\]
test_object.A \(destroy deposed \w+\)
test_object.A\[0\]
test_object.A\[1\]
test_object.B\[0\]
test_object.B\[1\]
test_object.A\[0\] test_object.A\[0\]
test_object.A\[0\] \(destroy deposed \w+\)
test_object.A\[0\]
test_object.A\[1\]
test_object.B\[0\]
test_object.B\[1\]
test_object.A\[1\] test_object.A\[1\]
test_object.A\[1\] \(destroy deposed \w+\)
test_object.A\[0\]
test_object.A\[1\]
test_object.B\[0\]
test_object.B\[1\]
test_object.B\[0\] test_object.B\[0\]
test_object.A\[0\] test_object.A\[0\]
test_object.A\[1\] test_object.A\[1\]