-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
transform_diff_test.go
167 lines (149 loc) · 4.36 KB
/
transform_diff_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package terraform
import (
"strings"
"testing"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/plans"
)
func TestDiffTransformer_nilDiff(t *testing.T) {
g := Graph{Path: addrs.RootModuleInstance}
tf := &DiffTransformer{}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
if len(g.Vertices()) > 0 {
t.Fatal("graph should be empty")
}
}
func TestDiffTransformer(t *testing.T) {
g := Graph{Path: addrs.RootModuleInstance}
beforeVal, err := plans.NewDynamicValue(cty.StringVal(""), cty.String)
if err != nil {
t.Fatal(err)
}
afterVal, err := plans.NewDynamicValue(cty.StringVal(""), cty.String)
if err != nil {
t.Fatal(err)
}
tf := &DiffTransformer{
Changes: &plans.Changes{
Resources: []*plans.ResourceInstanceChangeSrc{
{
Addr: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
ProviderAddr: addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("aws"),
Module: addrs.RootModule,
},
ChangeSrc: plans.ChangeSrc{
Action: plans.Update,
Before: beforeVal,
After: afterVal,
},
},
},
},
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testTransformDiffBasicStr)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
}
}
func TestDiffTransformer_noOpChange(t *testing.T) {
// "No-op" changes are how we record explicitly in a plan that we did
// indeed visit a particular resource instance during the planning phase
// and concluded that no changes were needed, as opposed to the resource
// instance not existing at all or having been excluded from planning
// entirely.
//
// We must include nodes for resource instances with no-op changes in the
// apply graph, even though they won't take any external actions, because
// there are some secondary effects such as precondition/postcondition
// checks that can refer to objects elsewhere and so might have their
// results changed even if the resource instance they are attached to
// didn't actually change directly itself.
// aws_instance.foo has a precondition, so should be included in the final
// graph. aws_instance.bar has no conditions, so there is nothing to
// execute during apply and it should not be included in the graph.
m := testModuleInline(t, map[string]string{
"main.tf": `
resource "aws_instance" "bar" {
}
resource "aws_instance" "foo" {
test_string = "ok"
lifecycle {
precondition {
condition = self.test_string != ""
error_message = "resource error"
}
}
}
`})
g := Graph{Path: addrs.RootModuleInstance}
beforeVal, err := plans.NewDynamicValue(cty.StringVal(""), cty.String)
if err != nil {
t.Fatal(err)
}
tf := &DiffTransformer{
Config: m,
Changes: &plans.Changes{
Resources: []*plans.ResourceInstanceChangeSrc{
{
Addr: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
ProviderAddr: addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("aws"),
Module: addrs.RootModule,
},
ChangeSrc: plans.ChangeSrc{
// A "no-op" change has the no-op action and has the
// same object as both Before and After.
Action: plans.NoOp,
Before: beforeVal,
After: beforeVal,
},
},
{
Addr: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "bar",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
ProviderAddr: addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("aws"),
Module: addrs.RootModule,
},
ChangeSrc: plans.ChangeSrc{
// A "no-op" change has the no-op action and has the
// same object as both Before and After.
Action: plans.NoOp,
Before: beforeVal,
After: beforeVal,
},
},
},
},
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testTransformDiffBasicStr)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
}
}
const testTransformDiffBasicStr = `
aws_instance.foo
`