-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
eval_context_mock.go
401 lines (338 loc) · 12.2 KB
/
eval_context_mock.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
package terraform
import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/checks"
"github.com/hashicorp/terraform/internal/configs/configschema"
"github.com/hashicorp/terraform/internal/instances"
"github.com/hashicorp/terraform/internal/lang"
"github.com/hashicorp/terraform/internal/plans"
"github.com/hashicorp/terraform/internal/providers"
"github.com/hashicorp/terraform/internal/provisioners"
"github.com/hashicorp/terraform/internal/refactoring"
"github.com/hashicorp/terraform/internal/states"
"github.com/hashicorp/terraform/internal/tfdiags"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
)
// MockEvalContext is a mock version of EvalContext that can be used
// for tests.
type MockEvalContext struct {
StoppedCalled bool
StoppedValue <-chan struct{}
HookCalled bool
HookHook Hook
HookError error
InputCalled bool
InputInput UIInput
InitProviderCalled bool
InitProviderType string
InitProviderAddr addrs.AbsProviderConfig
InitProviderProvider providers.Interface
InitProviderError error
ProviderCalled bool
ProviderAddr addrs.AbsProviderConfig
ProviderProvider providers.Interface
ProviderSchemaCalled bool
ProviderSchemaAddr addrs.AbsProviderConfig
ProviderSchemaSchema *ProviderSchema
ProviderSchemaError error
CloseProviderCalled bool
CloseProviderAddr addrs.AbsProviderConfig
CloseProviderProvider providers.Interface
ProviderInputCalled bool
ProviderInputAddr addrs.AbsProviderConfig
ProviderInputValues map[string]cty.Value
SetProviderInputCalled bool
SetProviderInputAddr addrs.AbsProviderConfig
SetProviderInputValues map[string]cty.Value
ConfigureProviderFn func(
addr addrs.AbsProviderConfig,
cfg cty.Value) tfdiags.Diagnostics // overrides the other values below, if set
ConfigureProviderCalled bool
ConfigureProviderAddr addrs.AbsProviderConfig
ConfigureProviderConfig cty.Value
ConfigureProviderDiags tfdiags.Diagnostics
ProvisionerCalled bool
ProvisionerName string
ProvisionerProvisioner provisioners.Interface
ProvisionerSchemaCalled bool
ProvisionerSchemaName string
ProvisionerSchemaSchema *configschema.Block
ProvisionerSchemaError error
CloseProvisionersCalled bool
EvaluateBlockCalled bool
EvaluateBlockBody hcl.Body
EvaluateBlockSchema *configschema.Block
EvaluateBlockSelf addrs.Referenceable
EvaluateBlockKeyData InstanceKeyEvalData
EvaluateBlockResultFunc func(
body hcl.Body,
schema *configschema.Block,
self addrs.Referenceable,
keyData InstanceKeyEvalData,
) (cty.Value, hcl.Body, tfdiags.Diagnostics) // overrides the other values below, if set
EvaluateBlockResult cty.Value
EvaluateBlockExpandedBody hcl.Body
EvaluateBlockDiags tfdiags.Diagnostics
EvaluateExprCalled bool
EvaluateExprExpr hcl.Expression
EvaluateExprWantType cty.Type
EvaluateExprSelf addrs.Referenceable
EvaluateExprResultFunc func(
expr hcl.Expression,
wantType cty.Type,
self addrs.Referenceable,
) (cty.Value, tfdiags.Diagnostics) // overrides the other values below, if set
EvaluateExprResult cty.Value
EvaluateExprDiags tfdiags.Diagnostics
EvaluationScopeCalled bool
EvaluationScopeSelf addrs.Referenceable
EvaluationScopeKeyData InstanceKeyEvalData
EvaluationScopeScope *lang.Scope
PathCalled bool
PathPath addrs.ModuleInstance
SetRootModuleArgumentCalled bool
SetRootModuleArgumentAddr addrs.InputVariable
SetRootModuleArgumentValue cty.Value
SetRootModuleArgumentFunc func(addr addrs.InputVariable, v cty.Value)
SetModuleCallArgumentCalled bool
SetModuleCallArgumentModuleCall addrs.ModuleCallInstance
SetModuleCallArgumentVariable addrs.InputVariable
SetModuleCallArgumentValue cty.Value
SetModuleCallArgumentFunc func(callAddr addrs.ModuleCallInstance, varAddr addrs.InputVariable, v cty.Value)
GetVariableValueCalled bool
GetVariableValueAddr addrs.AbsInputVariableInstance
GetVariableValueValue cty.Value
GetVariableValueFunc func(addr addrs.AbsInputVariableInstance) cty.Value // supersedes GetVariableValueValue
ChangesCalled bool
ChangesChanges *plans.ChangesSync
StateCalled bool
StateState *states.SyncState
ChecksCalled bool
ChecksState *checks.State
RefreshStateCalled bool
RefreshStateState *states.SyncState
PrevRunStateCalled bool
PrevRunStateState *states.SyncState
MoveResultsCalled bool
MoveResultsResults refactoring.MoveResults
InstanceExpanderCalled bool
InstanceExpanderExpander *instances.Expander
}
// MockEvalContext implements EvalContext
var _ EvalContext = (*MockEvalContext)(nil)
func (c *MockEvalContext) Stopped() <-chan struct{} {
c.StoppedCalled = true
return c.StoppedValue
}
func (c *MockEvalContext) Hook(fn func(Hook) (HookAction, error)) error {
c.HookCalled = true
if c.HookHook != nil {
if _, err := fn(c.HookHook); err != nil {
return err
}
}
return c.HookError
}
func (c *MockEvalContext) Input() UIInput {
c.InputCalled = true
return c.InputInput
}
func (c *MockEvalContext) InitProvider(addr addrs.AbsProviderConfig) (providers.Interface, error) {
c.InitProviderCalled = true
c.InitProviderType = addr.String()
c.InitProviderAddr = addr
return c.InitProviderProvider, c.InitProviderError
}
func (c *MockEvalContext) Provider(addr addrs.AbsProviderConfig) providers.Interface {
c.ProviderCalled = true
c.ProviderAddr = addr
return c.ProviderProvider
}
func (c *MockEvalContext) ProviderSchema(addr addrs.AbsProviderConfig) (*ProviderSchema, error) {
c.ProviderSchemaCalled = true
c.ProviderSchemaAddr = addr
return c.ProviderSchemaSchema, c.ProviderSchemaError
}
func (c *MockEvalContext) CloseProvider(addr addrs.AbsProviderConfig) error {
c.CloseProviderCalled = true
c.CloseProviderAddr = addr
return nil
}
func (c *MockEvalContext) ConfigureProvider(addr addrs.AbsProviderConfig, cfg cty.Value) tfdiags.Diagnostics {
c.ConfigureProviderCalled = true
c.ConfigureProviderAddr = addr
c.ConfigureProviderConfig = cfg
if c.ConfigureProviderFn != nil {
return c.ConfigureProviderFn(addr, cfg)
}
return c.ConfigureProviderDiags
}
func (c *MockEvalContext) ProviderInput(addr addrs.AbsProviderConfig) map[string]cty.Value {
c.ProviderInputCalled = true
c.ProviderInputAddr = addr
return c.ProviderInputValues
}
func (c *MockEvalContext) SetProviderInput(addr addrs.AbsProviderConfig, vals map[string]cty.Value) {
c.SetProviderInputCalled = true
c.SetProviderInputAddr = addr
c.SetProviderInputValues = vals
}
func (c *MockEvalContext) Provisioner(n string) (provisioners.Interface, error) {
c.ProvisionerCalled = true
c.ProvisionerName = n
return c.ProvisionerProvisioner, nil
}
func (c *MockEvalContext) ProvisionerSchema(n string) (*configschema.Block, error) {
c.ProvisionerSchemaCalled = true
c.ProvisionerSchemaName = n
return c.ProvisionerSchemaSchema, c.ProvisionerSchemaError
}
func (c *MockEvalContext) CloseProvisioners() error {
c.CloseProvisionersCalled = true
return nil
}
func (c *MockEvalContext) EvaluateBlock(body hcl.Body, schema *configschema.Block, self addrs.Referenceable, keyData InstanceKeyEvalData) (cty.Value, hcl.Body, tfdiags.Diagnostics) {
c.EvaluateBlockCalled = true
c.EvaluateBlockBody = body
c.EvaluateBlockSchema = schema
c.EvaluateBlockSelf = self
c.EvaluateBlockKeyData = keyData
if c.EvaluateBlockResultFunc != nil {
return c.EvaluateBlockResultFunc(body, schema, self, keyData)
}
return c.EvaluateBlockResult, c.EvaluateBlockExpandedBody, c.EvaluateBlockDiags
}
func (c *MockEvalContext) EvaluateExpr(expr hcl.Expression, wantType cty.Type, self addrs.Referenceable) (cty.Value, tfdiags.Diagnostics) {
c.EvaluateExprCalled = true
c.EvaluateExprExpr = expr
c.EvaluateExprWantType = wantType
c.EvaluateExprSelf = self
if c.EvaluateExprResultFunc != nil {
return c.EvaluateExprResultFunc(expr, wantType, self)
}
return c.EvaluateExprResult, c.EvaluateExprDiags
}
func (c *MockEvalContext) EvaluateReplaceTriggeredBy(hcl.Expression, instances.RepetitionData) (*addrs.Reference, bool, tfdiags.Diagnostics) {
return nil, false, nil
}
// installSimpleEval is a helper to install a simple mock implementation of
// both EvaluateBlock and EvaluateExpr into the receiver.
//
// These default implementations will either evaluate the given input against
// the scope in field EvaluationScopeScope or, if it is nil, with no eval
// context at all so that only constant values may be used.
//
// This function overwrites any existing functions installed in fields
// EvaluateBlockResultFunc and EvaluateExprResultFunc.
func (c *MockEvalContext) installSimpleEval() {
c.EvaluateBlockResultFunc = func(body hcl.Body, schema *configschema.Block, self addrs.Referenceable, keyData InstanceKeyEvalData) (cty.Value, hcl.Body, tfdiags.Diagnostics) {
if scope := c.EvaluationScopeScope; scope != nil {
// Fully-functional codepath.
var diags tfdiags.Diagnostics
body, diags = scope.ExpandBlock(body, schema)
if diags.HasErrors() {
return cty.DynamicVal, body, diags
}
val, evalDiags := c.EvaluationScopeScope.EvalBlock(body, schema)
diags = diags.Append(evalDiags)
if evalDiags.HasErrors() {
return cty.DynamicVal, body, diags
}
return val, body, diags
}
// Fallback codepath supporting constant values only.
val, hclDiags := hcldec.Decode(body, schema.DecoderSpec(), nil)
return val, body, tfdiags.Diagnostics(nil).Append(hclDiags)
}
c.EvaluateExprResultFunc = func(expr hcl.Expression, wantType cty.Type, self addrs.Referenceable) (cty.Value, tfdiags.Diagnostics) {
if scope := c.EvaluationScopeScope; scope != nil {
// Fully-functional codepath.
return scope.EvalExpr(expr, wantType)
}
// Fallback codepath supporting constant values only.
var diags tfdiags.Diagnostics
val, hclDiags := expr.Value(nil)
diags = diags.Append(hclDiags)
if hclDiags.HasErrors() {
return cty.DynamicVal, diags
}
var err error
val, err = convert.Convert(val, wantType)
if err != nil {
diags = diags.Append(err)
return cty.DynamicVal, diags
}
return val, diags
}
}
func (c *MockEvalContext) EvaluationScope(self addrs.Referenceable, keyData InstanceKeyEvalData) *lang.Scope {
c.EvaluationScopeCalled = true
c.EvaluationScopeSelf = self
c.EvaluationScopeKeyData = keyData
return c.EvaluationScopeScope
}
func (c *MockEvalContext) WithPath(path addrs.ModuleInstance) EvalContext {
newC := *c
newC.PathPath = path
return &newC
}
func (c *MockEvalContext) Path() addrs.ModuleInstance {
c.PathCalled = true
return c.PathPath
}
func (c *MockEvalContext) SetRootModuleArgument(addr addrs.InputVariable, v cty.Value) {
c.SetRootModuleArgumentCalled = true
c.SetRootModuleArgumentAddr = addr
c.SetRootModuleArgumentValue = v
if c.SetRootModuleArgumentFunc != nil {
c.SetRootModuleArgumentFunc(addr, v)
}
}
func (c *MockEvalContext) SetModuleCallArgument(callAddr addrs.ModuleCallInstance, varAddr addrs.InputVariable, v cty.Value) {
c.SetModuleCallArgumentCalled = true
c.SetModuleCallArgumentModuleCall = callAddr
c.SetModuleCallArgumentVariable = varAddr
c.SetModuleCallArgumentValue = v
if c.SetModuleCallArgumentFunc != nil {
c.SetModuleCallArgumentFunc(callAddr, varAddr, v)
}
}
func (c *MockEvalContext) GetVariableValue(addr addrs.AbsInputVariableInstance) cty.Value {
c.GetVariableValueCalled = true
c.GetVariableValueAddr = addr
if c.GetVariableValueFunc != nil {
return c.GetVariableValueFunc(addr)
}
return c.GetVariableValueValue
}
func (c *MockEvalContext) Changes() *plans.ChangesSync {
c.ChangesCalled = true
return c.ChangesChanges
}
func (c *MockEvalContext) State() *states.SyncState {
c.StateCalled = true
return c.StateState
}
func (c *MockEvalContext) Checks() *checks.State {
c.ChecksCalled = true
return c.ChecksState
}
func (c *MockEvalContext) RefreshState() *states.SyncState {
c.RefreshStateCalled = true
return c.RefreshStateState
}
func (c *MockEvalContext) PrevRunState() *states.SyncState {
c.PrevRunStateCalled = true
return c.PrevRunStateState
}
func (c *MockEvalContext) MoveResults() refactoring.MoveResults {
c.MoveResultsCalled = true
return c.MoveResultsResults
}
func (c *MockEvalContext) InstanceExpander() *instances.Expander {
c.InstanceExpanderCalled = true
return c.InstanceExpanderExpander
}