-
Notifications
You must be signed in to change notification settings - Fork 9.6k
/
node_count_boundary_test.go
72 lines (63 loc) · 1.92 KB
/
node_count_boundary_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package terraform
import (
"testing"
"github.com/hashicorp/hcl/v2/hcltest"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/states"
"github.com/zclconf/go-cty/cty"
)
func TestNodeCountBoundaryExecute(t *testing.T) {
// Create a state with a single instance (addrs.NoKey) of test_instance.foo
state := states.NewState()
state.Module(addrs.RootModuleInstance).SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_instance",
Name: "foo",
}.Instance(addrs.NoKey),
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"type":"string","value":"hello"}`),
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
// Create a config that uses count to create 2 instances of test_instance.foo
rc := &configs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_instance",
Name: "foo",
Count: hcltest.MockExprLiteral(cty.NumberIntVal(2)),
Config: configs.SynthBody("", map[string]cty.Value{
"test_string": cty.StringVal("hello"),
}),
}
config := &configs.Config{
Module: &configs.Module{
ManagedResources: map[string]*configs.Resource{
"test_instance.foo": rc,
},
},
}
ctx := &MockEvalContext{
StateState: state.SyncWrapper(),
}
node := NodeCountBoundary{Config: config}
diags := node.Execute(ctx, walkApply)
if diags.HasErrors() {
t.Fatalf("unexpected error: %s", diags.Err())
}
if !state.HasResources() {
t.Fatal("resources missing from state")
}
// verify that the resource changed from test_instance.foo to
// test_instance.foo.0 in the state
actual := state.String()
expected := "test_instance.foo.0:\n ID = \n provider = provider[\"registry.terraform.io/hashicorp/test\"]\n type = string\n value = hello"
if actual != expected {
t.Fatalf("wrong result: %s", actual)
}
}