-
Notifications
You must be signed in to change notification settings - Fork 1
/
provider_mock.go
551 lines (442 loc) · 16.1 KB
/
provider_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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
package terraform
import (
"fmt"
"sync"
"github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"
"github.com/zclconf/go-cty/cty/msgpack"
"github.com/khulnasoft/policy-engine/pkg/internal/terraform/configs/configschema"
"github.com/khulnasoft/policy-engine/pkg/internal/terraform/configs/hcl2shim"
"github.com/khulnasoft/policy-engine/pkg/internal/terraform/providers"
)
var _ providers.Interface = (*MockProvider)(nil)
// MockProvider implements providers.Interface but mocks out all the
// calls for testing purposes.
type MockProvider struct {
sync.Mutex
// Anything you want, in case you need to store extra data with the mock.
Meta interface{}
GetProviderSchemaCalled bool
GetProviderSchemaResponse *providers.GetProviderSchemaResponse
ValidateProviderConfigCalled bool
ValidateProviderConfigResponse *providers.ValidateProviderConfigResponse
ValidateProviderConfigRequest providers.ValidateProviderConfigRequest
ValidateProviderConfigFn func(providers.ValidateProviderConfigRequest) providers.ValidateProviderConfigResponse
ValidateResourceConfigCalled bool
ValidateResourceConfigTypeName string
ValidateResourceConfigResponse *providers.ValidateResourceConfigResponse
ValidateResourceConfigRequest providers.ValidateResourceConfigRequest
ValidateResourceConfigFn func(providers.ValidateResourceConfigRequest) providers.ValidateResourceConfigResponse
ValidateDataResourceConfigCalled bool
ValidateDataResourceConfigTypeName string
ValidateDataResourceConfigResponse *providers.ValidateDataResourceConfigResponse
ValidateDataResourceConfigRequest providers.ValidateDataResourceConfigRequest
ValidateDataResourceConfigFn func(providers.ValidateDataResourceConfigRequest) providers.ValidateDataResourceConfigResponse
UpgradeResourceStateCalled bool
UpgradeResourceStateTypeName string
UpgradeResourceStateResponse *providers.UpgradeResourceStateResponse
UpgradeResourceStateRequest providers.UpgradeResourceStateRequest
UpgradeResourceStateFn func(providers.UpgradeResourceStateRequest) providers.UpgradeResourceStateResponse
ConfigureProviderCalled bool
ConfigureProviderResponse *providers.ConfigureProviderResponse
ConfigureProviderRequest providers.ConfigureProviderRequest
ConfigureProviderFn func(providers.ConfigureProviderRequest) providers.ConfigureProviderResponse
StopCalled bool
StopFn func() error
StopResponse error
ReadResourceCalled bool
ReadResourceResponse *providers.ReadResourceResponse
ReadResourceRequest providers.ReadResourceRequest
ReadResourceFn func(providers.ReadResourceRequest) providers.ReadResourceResponse
PlanResourceChangeCalled bool
PlanResourceChangeResponse *providers.PlanResourceChangeResponse
PlanResourceChangeRequest providers.PlanResourceChangeRequest
PlanResourceChangeFn func(providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse
ApplyResourceChangeCalled bool
ApplyResourceChangeResponse *providers.ApplyResourceChangeResponse
ApplyResourceChangeRequest providers.ApplyResourceChangeRequest
ApplyResourceChangeFn func(providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse
ImportResourceStateCalled bool
ImportResourceStateResponse *providers.ImportResourceStateResponse
ImportResourceStateRequest providers.ImportResourceStateRequest
ImportResourceStateFn func(providers.ImportResourceStateRequest) providers.ImportResourceStateResponse
ReadDataSourceCalled bool
ReadDataSourceResponse *providers.ReadDataSourceResponse
ReadDataSourceRequest providers.ReadDataSourceRequest
ReadDataSourceFn func(providers.ReadDataSourceRequest) providers.ReadDataSourceResponse
CloseCalled bool
CloseError error
}
func (p *MockProvider) GetProviderSchema() providers.GetProviderSchemaResponse {
p.Lock()
defer p.Unlock()
p.GetProviderSchemaCalled = true
return p.getProviderSchema()
}
func (p *MockProvider) getProviderSchema() providers.GetProviderSchemaResponse {
// This version of getProviderSchema doesn't do any locking, so it's suitable to
// call from other methods of this mock as long as they are already
// holding the lock.
if p.GetProviderSchemaResponse != nil {
return *p.GetProviderSchemaResponse
}
return providers.GetProviderSchemaResponse{
Provider: providers.Schema{},
DataSources: map[string]providers.Schema{},
ResourceTypes: map[string]providers.Schema{},
}
}
// ProviderSchema is a helper to convert from the internal GetProviderSchemaResponse to
// a ProviderSchema.
func (p *MockProvider) ProviderSchema() *ProviderSchema {
resp := p.getProviderSchema()
schema := &ProviderSchema{
Provider: resp.Provider.Block,
ProviderMeta: resp.ProviderMeta.Block,
ResourceTypes: map[string]*configschema.Block{},
DataSources: map[string]*configschema.Block{},
ResourceTypeSchemaVersions: map[string]uint64{},
}
for resType, s := range resp.ResourceTypes {
schema.ResourceTypes[resType] = s.Block
schema.ResourceTypeSchemaVersions[resType] = uint64(s.Version)
}
for dataSource, s := range resp.DataSources {
schema.DataSources[dataSource] = s.Block
}
return schema
}
func (p *MockProvider) ValidateProviderConfig(r providers.ValidateProviderConfigRequest) (resp providers.ValidateProviderConfigResponse) {
p.Lock()
defer p.Unlock()
p.ValidateProviderConfigCalled = true
p.ValidateProviderConfigRequest = r
if p.ValidateProviderConfigFn != nil {
return p.ValidateProviderConfigFn(r)
}
if p.ValidateProviderConfigResponse != nil {
return *p.ValidateProviderConfigResponse
}
resp.PreparedConfig = r.Config
return resp
}
func (p *MockProvider) ValidateResourceConfig(r providers.ValidateResourceConfigRequest) (resp providers.ValidateResourceConfigResponse) {
p.Lock()
defer p.Unlock()
p.ValidateResourceConfigCalled = true
p.ValidateResourceConfigRequest = r
// Marshall the value to replicate behavior by the GRPC protocol,
// and return any relevant errors
resourceSchema, ok := p.getProviderSchema().ResourceTypes[r.TypeName]
if !ok {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("no schema found for %q", r.TypeName))
return resp
}
_, err := msgpack.Marshal(r.Config, resourceSchema.Block.ImpliedType())
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err)
return resp
}
if p.ValidateResourceConfigFn != nil {
return p.ValidateResourceConfigFn(r)
}
if p.ValidateResourceConfigResponse != nil {
return *p.ValidateResourceConfigResponse
}
return resp
}
func (p *MockProvider) ValidateDataResourceConfig(r providers.ValidateDataResourceConfigRequest) (resp providers.ValidateDataResourceConfigResponse) {
p.Lock()
defer p.Unlock()
p.ValidateDataResourceConfigCalled = true
p.ValidateDataResourceConfigRequest = r
// Marshall the value to replicate behavior by the GRPC protocol
dataSchema, ok := p.getProviderSchema().DataSources[r.TypeName]
if !ok {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("no schema found for %q", r.TypeName))
return resp
}
_, err := msgpack.Marshal(r.Config, dataSchema.Block.ImpliedType())
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err)
return resp
}
if p.ValidateDataResourceConfigFn != nil {
return p.ValidateDataResourceConfigFn(r)
}
if p.ValidateDataResourceConfigResponse != nil {
return *p.ValidateDataResourceConfigResponse
}
return resp
}
func (p *MockProvider) UpgradeResourceState(r providers.UpgradeResourceStateRequest) (resp providers.UpgradeResourceStateResponse) {
p.Lock()
defer p.Unlock()
if !p.ConfigureProviderCalled {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("Configure not called before UpgradeResourceState %q", r.TypeName))
return resp
}
schema, ok := p.getProviderSchema().ResourceTypes[r.TypeName]
if !ok {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("no schema found for %q", r.TypeName))
return resp
}
schemaType := schema.Block.ImpliedType()
p.UpgradeResourceStateCalled = true
p.UpgradeResourceStateRequest = r
if p.UpgradeResourceStateFn != nil {
return p.UpgradeResourceStateFn(r)
}
if p.UpgradeResourceStateResponse != nil {
return *p.UpgradeResourceStateResponse
}
switch {
case r.RawStateFlatmap != nil:
v, err := hcl2shim.HCL2ValueFromFlatmap(r.RawStateFlatmap, schemaType)
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err)
return resp
}
resp.UpgradedState = v
case len(r.RawStateJSON) > 0:
v, err := ctyjson.Unmarshal(r.RawStateJSON, schemaType)
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err)
return resp
}
resp.UpgradedState = v
}
return resp
}
func (p *MockProvider) ConfigureProvider(r providers.ConfigureProviderRequest) (resp providers.ConfigureProviderResponse) {
p.Lock()
defer p.Unlock()
p.ConfigureProviderCalled = true
p.ConfigureProviderRequest = r
if p.ConfigureProviderFn != nil {
return p.ConfigureProviderFn(r)
}
if p.ConfigureProviderResponse != nil {
return *p.ConfigureProviderResponse
}
return resp
}
func (p *MockProvider) Stop() error {
// We intentionally don't lock in this one because the whole point of this
// method is to be called concurrently with another operation that can
// be cancelled. The provider itself is responsible for handling
// any concurrency concerns in this case.
p.StopCalled = true
if p.StopFn != nil {
return p.StopFn()
}
return p.StopResponse
}
func (p *MockProvider) ReadResource(r providers.ReadResourceRequest) (resp providers.ReadResourceResponse) {
p.Lock()
defer p.Unlock()
p.ReadResourceCalled = true
p.ReadResourceRequest = r
if !p.ConfigureProviderCalled {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("Configure not called before ReadResource %q", r.TypeName))
return resp
}
if p.ReadResourceFn != nil {
return p.ReadResourceFn(r)
}
if p.ReadResourceResponse != nil {
resp = *p.ReadResourceResponse
// Make sure the NewState conforms to the schema.
// This isn't always the case for the existing tests.
schema, ok := p.getProviderSchema().ResourceTypes[r.TypeName]
if !ok {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("no schema found for %q", r.TypeName))
return resp
}
newState, err := schema.Block.CoerceValue(resp.NewState)
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err)
}
resp.NewState = newState
return resp
}
// otherwise just return the same state we received
resp.NewState = r.PriorState
resp.Private = r.Private
return resp
}
func (p *MockProvider) PlanResourceChange(r providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
p.Lock()
defer p.Unlock()
if !p.ConfigureProviderCalled {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("Configure not called before PlanResourceChange %q", r.TypeName))
return resp
}
p.PlanResourceChangeCalled = true
p.PlanResourceChangeRequest = r
if p.PlanResourceChangeFn != nil {
return p.PlanResourceChangeFn(r)
}
if p.PlanResourceChangeResponse != nil {
return *p.PlanResourceChangeResponse
}
// this is a destroy plan,
if r.ProposedNewState.IsNull() {
resp.PlannedState = r.ProposedNewState
resp.PlannedPrivate = r.PriorPrivate
return resp
}
schema, ok := p.getProviderSchema().ResourceTypes[r.TypeName]
if !ok {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("no schema found for %q", r.TypeName))
return resp
}
// The default plan behavior is to accept the proposed value, and mark all
// nil computed attributes as unknown.
val, err := cty.Transform(r.ProposedNewState, func(path cty.Path, v cty.Value) (cty.Value, error) {
// We're only concerned with known null values, which can be computed
// by the provider.
if !v.IsKnown() {
return v, nil
}
attrSchema := schema.Block.AttributeByPath(path)
if attrSchema == nil {
// this is an intermediate path which does not represent an attribute
return v, nil
}
// get the current configuration value, to detect when a
// computed+optional attributes has become unset
configVal, err := path.Apply(r.Config)
if err != nil {
return v, err
}
switch {
case attrSchema.Computed && !attrSchema.Optional && v.IsNull():
// this is the easy path, this value is not yet set, and _must_ be computed
return cty.UnknownVal(v.Type()), nil
case attrSchema.Computed && attrSchema.Optional && !v.IsNull() && configVal.IsNull():
// If an optional+computed value has gone from set to unset, it
// becomes computed. (this was not possible to do with legacy
// providers)
return cty.UnknownVal(v.Type()), nil
}
return v, nil
})
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err)
return resp
}
resp.PlannedPrivate = r.PriorPrivate
resp.PlannedState = val
return resp
}
func (p *MockProvider) ApplyResourceChange(r providers.ApplyResourceChangeRequest) (resp providers.ApplyResourceChangeResponse) {
p.Lock()
p.ApplyResourceChangeCalled = true
p.ApplyResourceChangeRequest = r
p.Unlock()
if !p.ConfigureProviderCalled {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("Configure not called before ApplyResourceChange %q", r.TypeName))
return resp
}
if p.ApplyResourceChangeFn != nil {
return p.ApplyResourceChangeFn(r)
}
if p.ApplyResourceChangeResponse != nil {
return *p.ApplyResourceChangeResponse
}
schema, ok := p.getProviderSchema().ResourceTypes[r.TypeName]
if !ok {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("no schema found for %q", r.TypeName))
return resp
}
// if the value is nil, we return that directly to correspond to a delete
if r.PlannedState.IsNull() {
resp.NewState = cty.NullVal(schema.Block.ImpliedType())
return resp
}
val, err := schema.Block.CoerceValue(r.PlannedState)
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err)
return resp
}
// the default behavior will be to create the minimal valid apply value by
// setting unknowns (which correspond to computed attributes) to a zero
// value.
val, _ = cty.Transform(val, func(path cty.Path, v cty.Value) (cty.Value, error) {
if !v.IsKnown() {
ty := v.Type()
switch {
case ty == cty.String:
return cty.StringVal(""), nil
case ty == cty.Number:
return cty.NumberIntVal(0), nil
case ty == cty.Bool:
return cty.False, nil
case ty.IsMapType():
return cty.MapValEmpty(ty.ElementType()), nil
case ty.IsListType():
return cty.ListValEmpty(ty.ElementType()), nil
default:
return cty.NullVal(ty), nil
}
}
return v, nil
})
resp.NewState = val
resp.Private = r.PlannedPrivate
return resp
}
func (p *MockProvider) ImportResourceState(r providers.ImportResourceStateRequest) (resp providers.ImportResourceStateResponse) {
p.Lock()
defer p.Unlock()
if !p.ConfigureProviderCalled {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("Configure not called before ImportResourceState %q", r.TypeName))
return resp
}
p.ImportResourceStateCalled = true
p.ImportResourceStateRequest = r
if p.ImportResourceStateFn != nil {
return p.ImportResourceStateFn(r)
}
if p.ImportResourceStateResponse != nil {
resp = *p.ImportResourceStateResponse
// fixup the cty value to match the schema
for i, res := range resp.ImportedResources {
schema, ok := p.getProviderSchema().ResourceTypes[res.TypeName]
if !ok {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("no schema found for %q", res.TypeName))
return resp
}
var err error
res.State, err = schema.Block.CoerceValue(res.State)
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err)
return resp
}
resp.ImportedResources[i] = res
}
}
return resp
}
func (p *MockProvider) ReadDataSource(r providers.ReadDataSourceRequest) (resp providers.ReadDataSourceResponse) {
p.Lock()
defer p.Unlock()
if !p.ConfigureProviderCalled {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("Configure not called before ReadDataSource %q", r.TypeName))
return resp
}
p.ReadDataSourceCalled = true
p.ReadDataSourceRequest = r
if p.ReadDataSourceFn != nil {
return p.ReadDataSourceFn(r)
}
if p.ReadDataSourceResponse != nil {
resp = *p.ReadDataSourceResponse
}
return resp
}
func (p *MockProvider) Close() error {
p.CloseCalled = true
return p.CloseError
}