-
Notifications
You must be signed in to change notification settings - Fork 9.6k
/
node_module_expand_test.go
116 lines (101 loc) · 2.82 KB
/
node_module_expand_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package terraform
import (
"testing"
"github.com/hashicorp/hcl/v2/hcltest"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/instances"
"github.com/hashicorp/terraform/states"
"github.com/zclconf/go-cty/cty"
)
func TestNodeExpandModuleExecute(t *testing.T) {
ctx := &MockEvalContext{
InstanceExpanderExpander: instances.NewExpander(),
}
ctx.installSimpleEval()
node := nodeExpandModule{
Addr: addrs.Module{"child"},
ModuleCall: &configs.ModuleCall{
Count: hcltest.MockExprLiteral(cty.NumberIntVal(2)),
},
}
err := node.Execute(ctx, walkApply)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !ctx.InstanceExpanderCalled {
t.Fatal("did not expand")
}
}
func TestNodeCloseModuleExecute(t *testing.T) {
t.Run("walkApply", func(t *testing.T) {
state := states.NewState()
state.EnsureModule(addrs.RootModuleInstance.Child("child", addrs.NoKey))
ctx := &MockEvalContext{
StateState: state.SyncWrapper(),
}
node := nodeCloseModule{addrs.Module{"child"}}
diags := node.Execute(ctx, walkApply)
if diags.HasErrors() {
t.Fatalf("unexpected error: %s", diags.Err())
}
// Since module.child has no resources, it should be removed
if _, ok := state.Modules["module.child"]; ok {
t.Fatal("module.child was not removed from state")
}
})
// walkImport is a no-op
t.Run("walkImport", func(t *testing.T) {
state := states.NewState()
state.EnsureModule(addrs.RootModuleInstance.Child("child", addrs.NoKey))
ctx := &MockEvalContext{
StateState: state.SyncWrapper(),
}
node := nodeCloseModule{addrs.Module{"child"}}
diags := node.Execute(ctx, walkImport)
if diags.HasErrors() {
t.Fatalf("unexpected error: %s", diags.Err())
}
if _, ok := state.Modules["module.child"]; !ok {
t.Fatal("module.child was removed from state, expected no-op")
}
})
}
func TestNodeValidateModuleExecute(t *testing.T) {
t.Run("success", func(t *testing.T) {
ctx := &MockEvalContext{
InstanceExpanderExpander: instances.NewExpander(),
}
ctx.installSimpleEval()
node := nodeValidateModule{
nodeExpandModule{
Addr: addrs.Module{"child"},
ModuleCall: &configs.ModuleCall{
Count: hcltest.MockExprLiteral(cty.NumberIntVal(2)),
},
},
}
diags := node.Execute(ctx, walkApply)
if diags.HasErrors() {
t.Fatalf("unexpected error: %v", diags.Err())
}
})
t.Run("invalid count", func(t *testing.T) {
ctx := &MockEvalContext{
InstanceExpanderExpander: instances.NewExpander(),
}
ctx.installSimpleEval()
node := nodeValidateModule{
nodeExpandModule{
Addr: addrs.Module{"child"},
ModuleCall: &configs.ModuleCall{
Count: hcltest.MockExprLiteral(cty.StringVal("invalid")),
},
},
}
err := node.Execute(ctx, walkApply)
if err == nil {
t.Fatal("expected error, got success")
}
})
}