-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathplan_test.go
More file actions
273 lines (228 loc) · 5.64 KB
/
plan_test.go
File metadata and controls
273 lines (228 loc) · 5.64 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright IBM Corp. 2019, 2026
// SPDX-License-Identifier: MPL-2.0
package tfjson
import (
"encoding/json"
"io"
"os"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestPlanValidate(t *testing.T) {
cases := map[string]struct {
planPath string
}{
"basic plan": {
planPath: "testdata/basic/plan.json",
},
"plan with identity": {
planPath: "testdata/identity/plan.json",
},
}
for tn, tc := range cases {
t.Run(tn, func(t *testing.T) {
f, err := os.Open(tc.planPath)
if err != nil {
t.Fatal(err)
}
defer f.Close()
var plan *Plan
if err := json.NewDecoder(f).Decode(&plan); err != nil {
t.Fatal(err)
}
if err := plan.Validate(); err != nil {
t.Fatal(err)
}
})
}
}
func TestPlan_015(t *testing.T) {
f, err := os.Open("testdata/basic/plan-0.15.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()
var plan *Plan
if err := json.NewDecoder(f).Decode(&plan); err != nil {
t.Fatal(err)
}
if err := plan.Validate(); err != nil {
t.Fatal(err)
}
expectedChange := &Change{
Actions: Actions{"create"},
After: map[string]interface{}{"ami": "boop"},
AfterUnknown: map[string]interface{}{"id": true},
BeforeSensitive: false,
AfterSensitive: map[string]interface{}{"ami": true},
}
if diff := cmp.Diff(expectedChange, plan.ResourceChanges[0].Change); diff != "" {
t.Fatalf("unexpected change: %s", diff)
}
expectedVariable := map[string]*ConfigVariable{
"test_var": {
Default: "boop",
Sensitive: true,
},
}
if diff := cmp.Diff(expectedVariable, plan.Config.RootModule.Variables); diff != "" {
t.Fatalf("unexpected variables: %s", diff)
}
}
func TestPlan_withChecks(t *testing.T) {
f, err := os.Open("testdata/has_checks/plan.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()
var plan *Plan
if err := json.NewDecoder(f).Decode(&plan); err != nil {
t.Fatal(err)
}
if err := plan.Validate(); err != nil {
t.Fatal(err)
}
if len(plan.Checks) == 0 {
t.Fatal("expected checks to not be empty")
}
for _, c := range plan.Checks {
for _, instance := range c.Instances {
k := reflect.TypeOf(instance.Address.InstanceKey).Kind()
switch k {
case reflect.Int, reflect.Float32, reflect.Float64, reflect.String:
t.Log("instance key is a valid type")
default:
t.Fatalf("unexpected type %s, expected string or int", k.String())
}
}
}
}
func TestPlan_movedBlock(t *testing.T) {
f, err := os.Open("testdata/moved_block/plan.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()
var plan *Plan
if err := json.NewDecoder(f).Decode(&plan); err != nil {
t.Fatal(err)
}
if err := plan.Validate(); err != nil {
t.Fatal(err)
}
if plan.ResourceChanges[0].PreviousAddress != "random_id.test" {
t.Fatalf("unexpected previous address %s, expected is random_id.test", plan.ResourceChanges[0].PreviousAddress)
}
}
func TestPlan_actionInvocations(t *testing.T) {
f, err := os.Open("testdata/actions/plan.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()
var plan *Plan
if err := json.NewDecoder(f).Decode(&plan); err != nil {
t.Fatal(err)
}
if err := plan.Validate(); err != nil {
t.Fatal(err)
}
if len(plan.ActionInvocations) != 1 {
t.Fatalf("expected exactly 1 action invocation, got %d", len(plan.ActionInvocations))
}
expectedAction := []*ActionInvocation{
{
Address: "action.bufo_print.success",
Type: "bufo_print",
Name: "success",
ConfigValues: map[string]interface{}{
"color": nil,
"name": "bufo-the-builder",
"ratio": nil,
},
ConfigSensitive: map[string]interface{}{},
ConfigUnknown: map[string]interface{}{},
ProviderName: "registry.terraform.io/austinvalle/bufo",
LifecycleActionTrigger: nil,
InvokeActionTrigger: &InvokeActionTrigger{},
},
}
if diff := cmp.Diff(expectedAction, plan.ActionInvocations); diff != "" {
t.Fatalf("unexpected action invocation: %s", diff)
}
}
func TestPlan_UnmarshalJSON(t *testing.T) {
t.Parallel()
b, err := os.ReadFile("testdata/numerics/plan.json")
if err != nil {
t.Fatal(err)
}
numericsTestCases := map[string]struct {
useJSONNumber bool
expected any
}{
"float64": {
expected: 1.23,
},
"json-number": {
useJSONNumber: true,
expected: json.Number("1.23"),
},
}
for name, testCase := range numericsTestCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
plan := &Plan{}
plan.UseJSONNumber(testCase.useJSONNumber)
err = json.Unmarshal(b, &plan)
if err != nil {
t.Fatal(err)
}
after, ok := plan.ResourceChanges[0].Change.After.(map[string]any)
if !ok {
t.Fatal("plan.ResourceChanges[0].Change.After cannot be asserted as map[string]any")
}
attr, ok := after["configurable_attribute"]
if !ok {
t.Fatal("configurable attribute not found")
}
if diff := cmp.Diff(attr, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
jsonValidationTestCases := map[string]struct {
filePath string
expectError bool
}{
"invalid plan JSON": {
filePath: "testdata/invalid/plan.json",
expectError: true,
},
"valid plan JSON": {
filePath: "testdata/basic/plan.json",
},
}
for tn, tc := range jsonValidationTestCases {
t.Run(tn, func(t *testing.T) {
f, err := os.Open(tc.filePath)
if err != nil {
t.Fatal(err)
}
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
t.Fatal(err)
}
var plan Plan
err = json.Unmarshal(b, &plan)
if tc.expectError && err == nil {
t.Fatalf("expected error; got none")
}
if !tc.expectError && err != nil {
t.Errorf("expected no error, got %q", err.Error())
}
})
}
}