github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/resource_provisioner_mock.go (about) 1 package terraform 2 3 import ( 4 "sync" 5 6 "github.com/hashicorp/terraform-plugin-sdk/internal/configs/configschema" 7 ) 8 9 // MockResourceProvisioner implements ResourceProvisioner but mocks out all the 10 // calls for testing purposes. 11 type MockResourceProvisioner struct { 12 sync.Mutex 13 // Anything you want, in case you need to store extra data with the mock. 14 Meta interface{} 15 16 GetConfigSchemaCalled bool 17 GetConfigSchemaReturnSchema *configschema.Block 18 GetConfigSchemaReturnError error 19 20 ApplyCalled bool 21 ApplyOutput UIOutput 22 ApplyState *InstanceState 23 ApplyConfig *ResourceConfig 24 ApplyFn func(*InstanceState, *ResourceConfig) error 25 ApplyReturnError error 26 27 ValidateCalled bool 28 ValidateConfig *ResourceConfig 29 ValidateFn func(c *ResourceConfig) ([]string, []error) 30 ValidateReturnWarns []string 31 ValidateReturnErrors []error 32 33 StopCalled bool 34 StopFn func() error 35 StopReturnError error 36 } 37 38 var _ ResourceProvisioner = (*MockResourceProvisioner)(nil) 39 40 func (p *MockResourceProvisioner) GetConfigSchema() (*configschema.Block, error) { 41 p.GetConfigSchemaCalled = true 42 return p.GetConfigSchemaReturnSchema, p.GetConfigSchemaReturnError 43 } 44 45 func (p *MockResourceProvisioner) Validate(c *ResourceConfig) ([]string, []error) { 46 p.Lock() 47 defer p.Unlock() 48 49 p.ValidateCalled = true 50 p.ValidateConfig = c 51 if p.ValidateFn != nil { 52 return p.ValidateFn(c) 53 } 54 return p.ValidateReturnWarns, p.ValidateReturnErrors 55 } 56 57 func (p *MockResourceProvisioner) Apply( 58 output UIOutput, 59 state *InstanceState, 60 c *ResourceConfig) error { 61 p.Lock() 62 63 p.ApplyCalled = true 64 p.ApplyOutput = output 65 p.ApplyState = state 66 p.ApplyConfig = c 67 if p.ApplyFn != nil { 68 fn := p.ApplyFn 69 p.Unlock() 70 return fn(state, c) 71 } 72 73 defer p.Unlock() 74 return p.ApplyReturnError 75 } 76 77 func (p *MockResourceProvisioner) Stop() error { 78 p.Lock() 79 defer p.Unlock() 80 81 p.StopCalled = true 82 if p.StopFn != nil { 83 return p.StopFn() 84 } 85 86 return p.StopReturnError 87 }