-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
component_test.go
405 lines (375 loc) · 14.9 KB
/
component_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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package stackeval
import (
"context"
"strings"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/instances"
"github.com/hashicorp/terraform/internal/stacks/stackaddrs"
"github.com/hashicorp/terraform/internal/tfdiags"
"github.com/zclconf/go-cty-debug/ctydebug"
"github.com/zclconf/go-cty/cty"
)
// TestComponentInstances is a test of the [Component.CheckInstances] function.
//
// In particular, note that it's _not_ a test of the [ComponentInstance] type
// as a whole, although [Component.CheckInstances] does return a collection of
// those so there is _some_ coverage of that in here.
func TestComponentCheckInstances(t *testing.T) {
getComponent := func(ctx context.Context, main *Main) *Component {
mainStack := main.MainStack(ctx)
component := mainStack.Component(ctx, stackaddrs.Component{Name: "foo"})
if component == nil {
t.Fatal("component.foo does not exist, but it should exist")
}
return component
}
subtestInPromisingTask(t, "single instance", func(ctx context.Context, t *testing.T) {
cfg := testStackConfig(t, "component", "single_instance")
main := testEvaluator(t, testEvaluatorOpts{
Config: cfg,
TestOnlyGlobals: map[string]cty.Value{
"component_inputs": cty.EmptyObjectVal,
},
})
component := getComponent(ctx, main)
forEachVal, diags := component.CheckForEachValue(ctx, InspectPhase)
assertNoDiags(t, diags)
if forEachVal != cty.NilVal {
t.Fatalf("unexpected for_each value\ngot: %#v\nwant: cty.NilVal", forEachVal)
}
insts, diags := component.CheckInstances(ctx, InspectPhase)
assertNoDiags(t, diags)
if got, want := len(insts), 1; got != want {
t.Fatalf("wrong number of instances %d; want %d\n%#v", got, want, insts)
}
inst, ok := insts[addrs.NoKey]
if !ok {
t.Fatalf("missing expected addrs.NoKey instance\n%s", spew.Sdump(insts))
}
if diff := cmp.Diff(instances.RepetitionData{}, inst.RepetitionData(), ctydebug.CmpOptions); diff != "" {
t.Errorf("wrong repetition data\n%s", diff)
}
})
t.Run("for_each", func(t *testing.T) {
cfg := testStackConfig(t, "component", "for_each")
subtestInPromisingTask(t, "no instances", func(ctx context.Context, t *testing.T) {
main := testEvaluator(t, testEvaluatorOpts{
Config: cfg,
TestOnlyGlobals: map[string]cty.Value{
"component_instances": cty.MapValEmpty(cty.EmptyObject),
},
})
component := getComponent(ctx, main)
forEachVal, diags := component.CheckForEachValue(ctx, InspectPhase)
assertNoDiags(t, diags)
if got, want := forEachVal, cty.MapValEmpty(cty.EmptyObject); !want.RawEquals(got) {
t.Fatalf("unexpected for_each value\ngot: %#v\nwant: %#v", got, want)
}
insts, diags := component.CheckInstances(ctx, InspectPhase)
assertNoDiags(t, diags)
if got, want := len(insts), 0; got != want {
t.Fatalf("wrong number of instances %d; want %d\n%#v", got, want, insts)
}
// For this particular function we take the unusual approach of
// distinguishing between a nil map and a non-nil empty map so
// we can distinguish between "definitely no instances" (this case)
// and "we don't know how many instances there are" (tested in other
// subtests of this test, below.)
if insts == nil {
t.Error("CheckInstances result is nil; should be non-nil empty map")
}
})
subtestInPromisingTask(t, "two instances", func(ctx context.Context, t *testing.T) {
wantForEachVal := cty.MapVal(map[string]cty.Value{
"a": cty.ObjectVal(map[string]cty.Value{
"test_string": cty.StringVal("in a"),
}),
"b": cty.ObjectVal(map[string]cty.Value{
"test_string": cty.StringVal("in b"),
}),
})
main := testEvaluator(t, testEvaluatorOpts{
Config: cfg,
TestOnlyGlobals: map[string]cty.Value{
"component_instances": wantForEachVal,
},
})
component := getComponent(ctx, main)
gotForEachVal, diags := component.CheckForEachValue(ctx, InspectPhase)
assertNoDiags(t, diags)
if !wantForEachVal.RawEquals(gotForEachVal) {
t.Fatalf("unexpected for_each value\ngot: %#v\nwant: %#v", gotForEachVal, wantForEachVal)
}
insts, diags := component.CheckInstances(ctx, InspectPhase)
assertNoDiags(t, diags)
if got, want := len(insts), 2; got != want {
t.Fatalf("wrong number of instances %d; want %d\n%#v", got, want, insts)
}
t.Run("instance a", func(t *testing.T) {
inst, ok := insts[addrs.StringKey("a")]
if !ok {
t.Fatalf("missing expected addrs.StringKey(\"a\") instance\n%s", spew.Sdump(insts))
}
wantRepData := instances.RepetitionData{
EachKey: cty.StringVal("a"),
EachValue: cty.ObjectVal(map[string]cty.Value{
"test_string": cty.StringVal("in a"),
}),
}
if diff := cmp.Diff(wantRepData, inst.RepetitionData(), ctydebug.CmpOptions); diff != "" {
t.Errorf("wrong repetition data\n%s", diff)
}
})
t.Run("instance b", func(t *testing.T) {
inst, ok := insts[addrs.StringKey("b")]
if !ok {
t.Fatalf("missing expected addrs.StringKey(\"b\") instance\n%s", spew.Sdump(insts))
}
wantRepData := instances.RepetitionData{
EachKey: cty.StringVal("b"),
EachValue: cty.ObjectVal(map[string]cty.Value{
"test_string": cty.StringVal("in b"),
}),
}
if diff := cmp.Diff(wantRepData, inst.RepetitionData(), ctydebug.CmpOptions); diff != "" {
t.Errorf("wrong repetition data\n%s", diff)
}
})
})
subtestInPromisingTask(t, "null", func(ctx context.Context, t *testing.T) {
main := testEvaluator(t, testEvaluatorOpts{
Config: cfg,
TestOnlyGlobals: map[string]cty.Value{
"component_instances": cty.NullVal(cty.Map(cty.EmptyObject)),
},
})
component := getComponent(ctx, main)
gotVal, diags := component.CheckForEachValue(ctx, InspectPhase)
assertMatchingDiag(t, diags, func(diag tfdiags.Diagnostic) bool {
return diag.Severity() == tfdiags.Error && strings.Contains(diag.Description().Detail, "The for_each expression produced a null value")
})
wantVal := cty.DynamicVal // placeholder for invalid result
if !wantVal.RawEquals(gotVal) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", gotVal, wantVal)
}
})
subtestInPromisingTask(t, "string", func(ctx context.Context, t *testing.T) {
main := testEvaluator(t, testEvaluatorOpts{
Config: cfg,
TestOnlyGlobals: map[string]cty.Value{
"component_instances": cty.StringVal("nope"),
},
})
component := getComponent(ctx, main)
gotVal, diags := component.CheckForEachValue(ctx, InspectPhase)
assertMatchingDiag(t, diags, func(diag tfdiags.Diagnostic) bool {
return (diag.Severity() == tfdiags.Error &&
diag.Description().Detail == "The for_each expression must produce either a map of any type or a set of strings. The keys of the map or the set elements will serve as unique identifiers for multiple instances of this component.")
})
wantVal := cty.DynamicVal // placeholder for invalid result
if !wantVal.RawEquals(gotVal) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", gotVal, wantVal)
}
// When the for_each expression is invalid, CheckInstances should
// return a single instance with dynamic values in the repetition data.
// We don't distinguish between invalid and unknown for_each values.
gotInsts, diags := component.CheckInstances(ctx, InspectPhase)
assertNoDiags(t, diags)
if got, want := len(gotInsts), 1; got != want {
t.Fatalf("wrong number of instances %d; want %d\n%#v", got, want, gotInsts)
}
})
subtestInPromisingTask(t, "unknown", func(ctx context.Context, t *testing.T) {
main := testEvaluator(t, testEvaluatorOpts{
Config: cfg,
TestOnlyGlobals: map[string]cty.Value{
"component_instances": cty.UnknownVal(cty.Map(cty.EmptyObject)),
},
})
component := getComponent(ctx, main)
gotVal, diags := component.CheckForEachValue(ctx, InspectPhase)
assertNoDiags(t, diags)
wantVal := cty.UnknownVal(cty.Map(cty.EmptyObject))
if !wantVal.RawEquals(gotVal) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", gotVal, wantVal)
}
// When the for_each expression is unknown, CheckInstances should
// return a single instance with dynamic values in the repetition data.
gotInsts, diags := component.CheckInstances(ctx, InspectPhase)
assertNoDiags(t, diags)
if got, want := len(gotInsts), 1; got != want {
t.Fatalf("wrong number of instances %d; want %d\n%#v", got, want, gotInsts)
}
if gotInsts[addrs.WildcardKey] == nil {
t.Fatalf("missing expected addrs.WildcardKey instance\n%#v", gotInsts)
}
if gotInsts[addrs.WildcardKey].repetition.EachKey.IsKnown() {
t.Errorf("EachKey should be unknown, but is known")
}
if gotInsts[addrs.WildcardKey].repetition.EachValue.IsKnown() {
t.Errorf("EachValue should be unknown, but is known")
}
})
})
}
func TestComponentResultValue(t *testing.T) {
getComponent := func(ctx context.Context, t *testing.T, main *Main) *Component {
mainStack := main.MainStack(ctx)
component := mainStack.Component(ctx, stackaddrs.Component{Name: "foo"})
if component == nil {
t.Fatal("component.foo does not exist, but it should exist")
}
return component
}
subtestInPromisingTask(t, "single instance", func(ctx context.Context, t *testing.T) {
cfg := testStackConfig(t, "component", "single_instance")
main := testEvaluator(t, testEvaluatorOpts{
Config: cfg,
TestOnlyGlobals: map[string]cty.Value{
"child_stack_inputs": cty.ObjectVal(map[string]cty.Value{
"test": cty.StringVal("hello"),
}),
},
})
component := getComponent(ctx, t, main)
got := component.ResultValue(ctx, InspectPhase)
want := cty.ObjectVal(map[string]cty.Value{
// FIXME: This currently returns empty object because we
// aren't tracking component output values in prior state.
// Once we fix that, we should see an output value called "test"
// here.
//"test": cty.StringVal("hello"),
})
if diff := cmp.Diff(want, got, ctydebug.CmpOptions); diff != "" {
t.Fatalf("wrong result\n%s", diff)
}
})
t.Run("for_each", func(t *testing.T) {
cfg := testStackConfig(t, "component", "for_each")
subtestInPromisingTask(t, "no instances", func(ctx context.Context, t *testing.T) {
main := testEvaluator(t, testEvaluatorOpts{
Config: cfg,
TestOnlyGlobals: map[string]cty.Value{
"component_instances": cty.MapValEmpty(cty.EmptyObject),
},
})
component := getComponent(ctx, t, main)
got := component.ResultValue(ctx, InspectPhase)
want := cty.EmptyObjectVal
if diff := cmp.Diff(want, got, ctydebug.CmpOptions); diff != "" {
t.Fatalf("wrong result\n%s", diff)
}
})
subtestInPromisingTask(t, "two instances", func(ctx context.Context, t *testing.T) {
forEachVal := cty.MapVal(map[string]cty.Value{
"a": cty.ObjectVal(map[string]cty.Value{
"test": cty.StringVal("in a"),
}),
"b": cty.ObjectVal(map[string]cty.Value{
"test": cty.StringVal("in b"),
}),
})
main := testEvaluator(t, testEvaluatorOpts{
Config: cfg,
TestOnlyGlobals: map[string]cty.Value{
"component_instances": forEachVal,
},
})
component := getComponent(ctx, t, main)
got := component.ResultValue(ctx, InspectPhase)
want := cty.ObjectVal(map[string]cty.Value{
"a": cty.ObjectVal(map[string]cty.Value{
// FIXME: This currently returns empty object because we
// aren't tracking component output values in prior state.
// Once we fix that, we should see an output value called "test"
// here.
}),
"b": cty.ObjectVal(map[string]cty.Value{
// FIXME: This currently returns empty object because we
// aren't tracking component output values in prior state.
// Once we fix that, we should see an output value called "test"
// here.
}),
})
// FIXME: the cmp transformer ctydebug.CmpOptions seems to find
// this particular pair of values troubling, causing it to get
// into an infinite recursion. For now we'll just use RawEquals,
// at the expense of a less helpful failure message. This seems
// to be a bug in upstream ctydebug.
if !want.RawEquals(got) {
t.Fatalf("wrong result\ngot: %#v\nwant: %#v", got, want)
}
})
subtestInPromisingTask(t, "null", func(ctx context.Context, t *testing.T) {
main := testEvaluator(t, testEvaluatorOpts{
Config: cfg,
TestOnlyGlobals: map[string]cty.Value{
"component_instances": cty.NullVal(cty.Map(cty.EmptyObject)),
},
})
component := getComponent(ctx, t, main)
got := component.ResultValue(ctx, InspectPhase)
// When the for_each expression is invalid, the result value
// is unknown so we can use it as a placeholder for partial
// downstream checking.
want := cty.DynamicVal
// FIXME: the cmp transformer ctydebug.CmpOptions seems to find
// this particular pair of values troubling, causing it to get
// into an infinite recursion. For now we'll just use RawEquals,
// at the expense of a less helpful failure message. This seems
// to be a bug in upstream ctydebug.
if !want.RawEquals(got) {
t.Fatalf("wrong result\ngot: %#v\nwant: %#v", got, want)
}
})
subtestInPromisingTask(t, "string", func(ctx context.Context, t *testing.T) {
main := testEvaluator(t, testEvaluatorOpts{
Config: cfg,
TestOnlyGlobals: map[string]cty.Value{
"component_instances": cty.StringVal("nope"),
},
})
component := getComponent(ctx, t, main)
got := component.ResultValue(ctx, InspectPhase)
// When the for_each expression is invalid, the result value
// is unknown so we can use it as a placeholder for partial
// downstream checking.
want := cty.DynamicVal
// FIXME: the cmp transformer ctydebug.CmpOptions seems to find
// this particular pair of values troubling, causing it to get
// into an infinite recursion. For now we'll just use RawEquals,
// at the expense of a less helpful failure message. This seems
// to be a bug in upstream ctydebug.
if !want.RawEquals(got) {
t.Fatalf("wrong result\ngot: %#v\nwant: %#v", got, want)
}
})
subtestInPromisingTask(t, "unknown", func(ctx context.Context, t *testing.T) {
main := testEvaluator(t, testEvaluatorOpts{
Config: cfg,
TestOnlyGlobals: map[string]cty.Value{
"component_instances": cty.UnknownVal(cty.Map(cty.EmptyObject)),
},
})
component := getComponent(ctx, t, main)
got := component.ResultValue(ctx, InspectPhase)
// When the for_each expression is unknown, the result value
// is unknown too so we can use it as a placeholder for partial
// downstream checking.
want := cty.DynamicVal
// FIXME: the cmp transformer ctydebug.CmpOptions seems to find
// this particular pair of values troubling, causing it to get
// into an infinite recursion. For now we'll just use RawEquals,
// at the expense of a less helpful failure message. This seems
// to be a bug in upstream ctydebug.
if !want.RawEquals(got) {
t.Fatalf("wrong result\ngot: %#v\nwant: %#v", got, want)
}
})
})
}