github.com/skyscape-cloud-services/terraform@v0.9.2-0.20170609144644-7ece028a1747/terraform/resource_provider_mock.go (about) 1 package terraform 2 3 import "sync" 4 5 // MockResourceProvider implements ResourceProvider but mocks out all the 6 // calls for testing purposes. 7 type MockResourceProvider struct { 8 sync.Mutex 9 10 // Anything you want, in case you need to store extra data with the mock. 11 Meta interface{} 12 13 CloseCalled bool 14 CloseError error 15 InputCalled bool 16 InputInput UIInput 17 InputConfig *ResourceConfig 18 InputReturnConfig *ResourceConfig 19 InputReturnError error 20 InputFn func(UIInput, *ResourceConfig) (*ResourceConfig, error) 21 ApplyCalled bool 22 ApplyInfo *InstanceInfo 23 ApplyState *InstanceState 24 ApplyDiff *InstanceDiff 25 ApplyFn func(*InstanceInfo, *InstanceState, *InstanceDiff) (*InstanceState, error) 26 ApplyReturn *InstanceState 27 ApplyReturnError error 28 ConfigureCalled bool 29 ConfigureConfig *ResourceConfig 30 ConfigureFn func(*ResourceConfig) error 31 ConfigureReturnError error 32 DiffCalled bool 33 DiffInfo *InstanceInfo 34 DiffState *InstanceState 35 DiffDesired *ResourceConfig 36 DiffFn func(*InstanceInfo, *InstanceState, *ResourceConfig) (*InstanceDiff, error) 37 DiffReturn *InstanceDiff 38 DiffReturnError error 39 RefreshCalled bool 40 RefreshInfo *InstanceInfo 41 RefreshState *InstanceState 42 RefreshFn func(*InstanceInfo, *InstanceState) (*InstanceState, error) 43 RefreshReturn *InstanceState 44 RefreshReturnError error 45 ResourcesCalled bool 46 ResourcesReturn []ResourceType 47 ReadDataApplyCalled bool 48 ReadDataApplyInfo *InstanceInfo 49 ReadDataApplyDiff *InstanceDiff 50 ReadDataApplyFn func(*InstanceInfo, *InstanceDiff) (*InstanceState, error) 51 ReadDataApplyReturn *InstanceState 52 ReadDataApplyReturnError error 53 ReadDataDiffCalled bool 54 ReadDataDiffInfo *InstanceInfo 55 ReadDataDiffDesired *ResourceConfig 56 ReadDataDiffFn func(*InstanceInfo, *ResourceConfig) (*InstanceDiff, error) 57 ReadDataDiffReturn *InstanceDiff 58 ReadDataDiffReturnError error 59 StopCalled bool 60 StopFn func() error 61 StopReturnError error 62 DataSourcesCalled bool 63 DataSourcesReturn []DataSource 64 ValidateCalled bool 65 ValidateConfig *ResourceConfig 66 ValidateFn func(*ResourceConfig) ([]string, []error) 67 ValidateReturnWarns []string 68 ValidateReturnErrors []error 69 ValidateResourceFn func(string, *ResourceConfig) ([]string, []error) 70 ValidateResourceCalled bool 71 ValidateResourceType string 72 ValidateResourceConfig *ResourceConfig 73 ValidateResourceReturnWarns []string 74 ValidateResourceReturnErrors []error 75 ValidateDataSourceFn func(string, *ResourceConfig) ([]string, []error) 76 ValidateDataSourceCalled bool 77 ValidateDataSourceType string 78 ValidateDataSourceConfig *ResourceConfig 79 ValidateDataSourceReturnWarns []string 80 ValidateDataSourceReturnErrors []error 81 82 ImportStateCalled bool 83 ImportStateInfo *InstanceInfo 84 ImportStateID string 85 ImportStateReturn []*InstanceState 86 ImportStateReturnError error 87 ImportStateFn func(*InstanceInfo, string) ([]*InstanceState, error) 88 } 89 90 func (p *MockResourceProvider) Close() error { 91 p.CloseCalled = true 92 return p.CloseError 93 } 94 95 func (p *MockResourceProvider) Input( 96 input UIInput, c *ResourceConfig) (*ResourceConfig, error) { 97 p.InputCalled = true 98 p.InputInput = input 99 p.InputConfig = c 100 if p.InputFn != nil { 101 return p.InputFn(input, c) 102 } 103 return p.InputReturnConfig, p.InputReturnError 104 } 105 106 func (p *MockResourceProvider) Validate(c *ResourceConfig) ([]string, []error) { 107 p.Lock() 108 defer p.Unlock() 109 110 p.ValidateCalled = true 111 p.ValidateConfig = c 112 if p.ValidateFn != nil { 113 return p.ValidateFn(c) 114 } 115 return p.ValidateReturnWarns, p.ValidateReturnErrors 116 } 117 118 func (p *MockResourceProvider) ValidateResource(t string, c *ResourceConfig) ([]string, []error) { 119 p.Lock() 120 defer p.Unlock() 121 122 p.ValidateResourceCalled = true 123 p.ValidateResourceType = t 124 p.ValidateResourceConfig = c 125 126 if p.ValidateResourceFn != nil { 127 return p.ValidateResourceFn(t, c) 128 } 129 130 return p.ValidateResourceReturnWarns, p.ValidateResourceReturnErrors 131 } 132 133 func (p *MockResourceProvider) Configure(c *ResourceConfig) error { 134 p.Lock() 135 defer p.Unlock() 136 137 p.ConfigureCalled = true 138 p.ConfigureConfig = c 139 140 if p.ConfigureFn != nil { 141 return p.ConfigureFn(c) 142 } 143 144 return p.ConfigureReturnError 145 } 146 147 func (p *MockResourceProvider) Stop() error { 148 p.Lock() 149 defer p.Unlock() 150 151 p.StopCalled = true 152 if p.StopFn != nil { 153 return p.StopFn() 154 } 155 156 return p.StopReturnError 157 } 158 159 func (p *MockResourceProvider) Apply( 160 info *InstanceInfo, 161 state *InstanceState, 162 diff *InstanceDiff) (*InstanceState, error) { 163 // We only lock while writing data. Reading is fine 164 p.Lock() 165 p.ApplyCalled = true 166 p.ApplyInfo = info 167 p.ApplyState = state 168 p.ApplyDiff = diff 169 p.Unlock() 170 171 if p.ApplyFn != nil { 172 return p.ApplyFn(info, state, diff) 173 } 174 175 return p.ApplyReturn.DeepCopy(), p.ApplyReturnError 176 } 177 178 func (p *MockResourceProvider) Diff( 179 info *InstanceInfo, 180 state *InstanceState, 181 desired *ResourceConfig) (*InstanceDiff, error) { 182 p.Lock() 183 defer p.Unlock() 184 185 p.DiffCalled = true 186 p.DiffInfo = info 187 p.DiffState = state 188 p.DiffDesired = desired 189 if p.DiffFn != nil { 190 return p.DiffFn(info, state, desired) 191 } 192 193 return p.DiffReturn.DeepCopy(), p.DiffReturnError 194 } 195 196 func (p *MockResourceProvider) Refresh( 197 info *InstanceInfo, 198 s *InstanceState) (*InstanceState, error) { 199 p.Lock() 200 defer p.Unlock() 201 202 p.RefreshCalled = true 203 p.RefreshInfo = info 204 p.RefreshState = s 205 206 if p.RefreshFn != nil { 207 return p.RefreshFn(info, s) 208 } 209 210 return p.RefreshReturn.DeepCopy(), p.RefreshReturnError 211 } 212 213 func (p *MockResourceProvider) Resources() []ResourceType { 214 p.Lock() 215 defer p.Unlock() 216 217 p.ResourcesCalled = true 218 return p.ResourcesReturn 219 } 220 221 func (p *MockResourceProvider) ImportState(info *InstanceInfo, id string) ([]*InstanceState, error) { 222 p.Lock() 223 defer p.Unlock() 224 225 p.ImportStateCalled = true 226 p.ImportStateInfo = info 227 p.ImportStateID = id 228 if p.ImportStateFn != nil { 229 return p.ImportStateFn(info, id) 230 } 231 232 var result []*InstanceState 233 if p.ImportStateReturn != nil { 234 result = make([]*InstanceState, len(p.ImportStateReturn)) 235 for i, v := range p.ImportStateReturn { 236 result[i] = v.DeepCopy() 237 } 238 } 239 240 return result, p.ImportStateReturnError 241 } 242 243 func (p *MockResourceProvider) ValidateDataSource(t string, c *ResourceConfig) ([]string, []error) { 244 p.Lock() 245 defer p.Unlock() 246 247 p.ValidateDataSourceCalled = true 248 p.ValidateDataSourceType = t 249 p.ValidateDataSourceConfig = c 250 251 if p.ValidateDataSourceFn != nil { 252 return p.ValidateDataSourceFn(t, c) 253 } 254 255 return p.ValidateDataSourceReturnWarns, p.ValidateDataSourceReturnErrors 256 } 257 258 func (p *MockResourceProvider) ReadDataDiff( 259 info *InstanceInfo, 260 desired *ResourceConfig) (*InstanceDiff, error) { 261 p.Lock() 262 defer p.Unlock() 263 264 p.ReadDataDiffCalled = true 265 p.ReadDataDiffInfo = info 266 p.ReadDataDiffDesired = desired 267 if p.ReadDataDiffFn != nil { 268 return p.ReadDataDiffFn(info, desired) 269 } 270 271 return p.ReadDataDiffReturn.DeepCopy(), p.ReadDataDiffReturnError 272 } 273 274 func (p *MockResourceProvider) ReadDataApply( 275 info *InstanceInfo, 276 d *InstanceDiff) (*InstanceState, error) { 277 p.Lock() 278 defer p.Unlock() 279 280 p.ReadDataApplyCalled = true 281 p.ReadDataApplyInfo = info 282 p.ReadDataApplyDiff = d 283 284 if p.ReadDataApplyFn != nil { 285 return p.ReadDataApplyFn(info, d) 286 } 287 288 return p.ReadDataApplyReturn.DeepCopy(), p.ReadDataApplyReturnError 289 } 290 291 func (p *MockResourceProvider) DataSources() []DataSource { 292 p.Lock() 293 defer p.Unlock() 294 295 p.DataSourcesCalled = true 296 return p.DataSourcesReturn 297 }