github.com/subuk/terraform@v0.6.14-0.20160317140351-de1567c2e732/rpc/resource_provider_test.go (about) 1 package rpc 2 3 import ( 4 "errors" 5 "reflect" 6 "testing" 7 8 "github.com/hashicorp/terraform/terraform" 9 ) 10 11 func TestResourceProvider_impl(t *testing.T) { 12 var _ terraform.ResourceProvider = new(ResourceProvider) 13 } 14 15 func TestResourceProvider_input(t *testing.T) { 16 client, server := testNewClientServer(t) 17 defer client.Close() 18 19 p := server.ProviderFunc().(*terraform.MockResourceProvider) 20 21 provider, err := client.ResourceProvider() 22 if err != nil { 23 t.Fatalf("err: %s", err) 24 } 25 26 input := new(terraform.MockUIInput) 27 28 expected := &terraform.ResourceConfig{ 29 Raw: map[string]interface{}{"bar": "baz"}, 30 } 31 p.InputReturnConfig = expected 32 33 // Input 34 config := &terraform.ResourceConfig{ 35 Raw: map[string]interface{}{"foo": "bar"}, 36 } 37 actual, err := provider.Input(input, config) 38 if !p.InputCalled { 39 t.Fatal("input should be called") 40 } 41 if !reflect.DeepEqual(p.InputConfig, config) { 42 t.Fatalf("bad: %#v", p.InputConfig) 43 } 44 if err != nil { 45 t.Fatalf("bad: %#v", err) 46 } 47 48 if !reflect.DeepEqual(actual, expected) { 49 t.Fatalf("bad: %#v", actual) 50 } 51 } 52 53 func TestResourceProvider_configure(t *testing.T) { 54 client, server := testNewClientServer(t) 55 defer client.Close() 56 57 p := server.ProviderFunc().(*terraform.MockResourceProvider) 58 59 provider, err := client.ResourceProvider() 60 if err != nil { 61 t.Fatalf("err: %s", err) 62 } 63 64 // Configure 65 config := &terraform.ResourceConfig{ 66 Raw: map[string]interface{}{"foo": "bar"}, 67 } 68 e := provider.Configure(config) 69 if !p.ConfigureCalled { 70 t.Fatal("configure should be called") 71 } 72 if !reflect.DeepEqual(p.ConfigureConfig, config) { 73 t.Fatalf("bad: %#v", p.ConfigureConfig) 74 } 75 if e != nil { 76 t.Fatalf("bad: %#v", e) 77 } 78 } 79 80 func TestResourceProvider_configure_errors(t *testing.T) { 81 p := new(terraform.MockResourceProvider) 82 client, server := testClientServer(t) 83 name, err := Register(server, p) 84 if err != nil { 85 t.Fatalf("err: %s", err) 86 } 87 provider := &ResourceProvider{Client: client, Name: name} 88 89 p.ConfigureReturnError = errors.New("foo") 90 91 // Configure 92 config := &terraform.ResourceConfig{ 93 Raw: map[string]interface{}{"foo": "bar"}, 94 } 95 e := provider.Configure(config) 96 if !p.ConfigureCalled { 97 t.Fatal("configure should be called") 98 } 99 if !reflect.DeepEqual(p.ConfigureConfig, config) { 100 t.Fatalf("bad: %#v", p.ConfigureConfig) 101 } 102 if e == nil { 103 t.Fatal("should have error") 104 } 105 if e.Error() != "foo" { 106 t.Fatalf("bad: %s", e) 107 } 108 } 109 110 func TestResourceProvider_configure_warnings(t *testing.T) { 111 p := new(terraform.MockResourceProvider) 112 client, server := testClientServer(t) 113 name, err := Register(server, p) 114 if err != nil { 115 t.Fatalf("err: %s", err) 116 } 117 provider := &ResourceProvider{Client: client, Name: name} 118 119 // Configure 120 config := &terraform.ResourceConfig{ 121 Raw: map[string]interface{}{"foo": "bar"}, 122 } 123 e := provider.Configure(config) 124 if !p.ConfigureCalled { 125 t.Fatal("configure should be called") 126 } 127 if !reflect.DeepEqual(p.ConfigureConfig, config) { 128 t.Fatalf("bad: %#v", p.ConfigureConfig) 129 } 130 if e != nil { 131 t.Fatalf("bad: %#v", e) 132 } 133 } 134 135 func TestResourceProvider_apply(t *testing.T) { 136 p := new(terraform.MockResourceProvider) 137 client, server := testClientServer(t) 138 name, err := Register(server, p) 139 if err != nil { 140 t.Fatalf("err: %s", err) 141 } 142 provider := &ResourceProvider{Client: client, Name: name} 143 144 p.ApplyReturn = &terraform.InstanceState{ 145 ID: "bob", 146 } 147 148 // Apply 149 info := &terraform.InstanceInfo{} 150 state := &terraform.InstanceState{} 151 diff := &terraform.InstanceDiff{} 152 newState, err := provider.Apply(info, state, diff) 153 if !p.ApplyCalled { 154 t.Fatal("apply should be called") 155 } 156 if !reflect.DeepEqual(p.ApplyDiff, diff) { 157 t.Fatalf("bad: %#v", p.ApplyDiff) 158 } 159 if err != nil { 160 t.Fatalf("bad: %#v", err) 161 } 162 if !reflect.DeepEqual(p.ApplyReturn, newState) { 163 t.Fatalf("bad: %#v", newState) 164 } 165 } 166 167 func TestResourceProvider_diff(t *testing.T) { 168 p := new(terraform.MockResourceProvider) 169 client, server := testClientServer(t) 170 name, err := Register(server, p) 171 if err != nil { 172 t.Fatalf("err: %s", err) 173 } 174 provider := &ResourceProvider{Client: client, Name: name} 175 176 p.DiffReturn = &terraform.InstanceDiff{ 177 Attributes: map[string]*terraform.ResourceAttrDiff{ 178 "foo": &terraform.ResourceAttrDiff{ 179 Old: "", 180 New: "bar", 181 }, 182 }, 183 } 184 185 // Diff 186 info := &terraform.InstanceInfo{} 187 state := &terraform.InstanceState{} 188 config := &terraform.ResourceConfig{ 189 Raw: map[string]interface{}{"foo": "bar"}, 190 } 191 diff, err := provider.Diff(info, state, config) 192 if !p.DiffCalled { 193 t.Fatal("diff should be called") 194 } 195 if !reflect.DeepEqual(p.DiffDesired, config) { 196 t.Fatalf("bad: %#v", p.DiffDesired) 197 } 198 if err != nil { 199 t.Fatalf("bad: %#v", err) 200 } 201 if !reflect.DeepEqual(p.DiffReturn, diff) { 202 t.Fatalf("bad: %#v", diff) 203 } 204 } 205 206 func TestResourceProvider_diff_error(t *testing.T) { 207 p := new(terraform.MockResourceProvider) 208 client, server := testClientServer(t) 209 name, err := Register(server, p) 210 if err != nil { 211 t.Fatalf("err: %s", err) 212 } 213 provider := &ResourceProvider{Client: client, Name: name} 214 215 p.DiffReturnError = errors.New("foo") 216 217 // Diff 218 info := &terraform.InstanceInfo{} 219 state := &terraform.InstanceState{} 220 config := &terraform.ResourceConfig{ 221 Raw: map[string]interface{}{"foo": "bar"}, 222 } 223 diff, err := provider.Diff(info, state, config) 224 if !p.DiffCalled { 225 t.Fatal("diff should be called") 226 } 227 if !reflect.DeepEqual(p.DiffDesired, config) { 228 t.Fatalf("bad: %#v", p.DiffDesired) 229 } 230 if err == nil { 231 t.Fatal("should have error") 232 } 233 if diff != nil { 234 t.Fatal("should not have diff") 235 } 236 } 237 238 func TestResourceProvider_refresh(t *testing.T) { 239 p := new(terraform.MockResourceProvider) 240 client, server := testClientServer(t) 241 name, err := Register(server, p) 242 if err != nil { 243 t.Fatalf("err: %s", err) 244 } 245 provider := &ResourceProvider{Client: client, Name: name} 246 247 p.RefreshReturn = &terraform.InstanceState{ 248 ID: "bob", 249 } 250 251 // Refresh 252 info := &terraform.InstanceInfo{} 253 state := &terraform.InstanceState{} 254 newState, err := provider.Refresh(info, state) 255 if !p.RefreshCalled { 256 t.Fatal("refresh should be called") 257 } 258 if !reflect.DeepEqual(p.RefreshState, state) { 259 t.Fatalf("bad: %#v", p.RefreshState) 260 } 261 if err != nil { 262 t.Fatalf("bad: %#v", err) 263 } 264 if !reflect.DeepEqual(p.RefreshReturn, newState) { 265 t.Fatalf("bad: %#v", newState) 266 } 267 } 268 269 func TestResourceProvider_resources(t *testing.T) { 270 p := new(terraform.MockResourceProvider) 271 client, server := testClientServer(t) 272 name, err := Register(server, p) 273 if err != nil { 274 t.Fatalf("err: %s", err) 275 } 276 provider := &ResourceProvider{Client: client, Name: name} 277 278 expected := []terraform.ResourceType{ 279 {"foo"}, 280 {"bar"}, 281 } 282 283 p.ResourcesReturn = expected 284 285 // Resources 286 result := provider.Resources() 287 if !p.ResourcesCalled { 288 t.Fatal("resources should be called") 289 } 290 if !reflect.DeepEqual(result, expected) { 291 t.Fatalf("bad: %#v", result) 292 } 293 } 294 295 func TestResourceProvider_validate(t *testing.T) { 296 p := new(terraform.MockResourceProvider) 297 client, server := testClientServer(t) 298 name, err := Register(server, p) 299 if err != nil { 300 t.Fatalf("err: %s", err) 301 } 302 provider := &ResourceProvider{Client: client, Name: name} 303 304 // Configure 305 config := &terraform.ResourceConfig{ 306 Raw: map[string]interface{}{"foo": "bar"}, 307 } 308 w, e := provider.Validate(config) 309 if !p.ValidateCalled { 310 t.Fatal("configure should be called") 311 } 312 if !reflect.DeepEqual(p.ValidateConfig, config) { 313 t.Fatalf("bad: %#v", p.ValidateConfig) 314 } 315 if w != nil { 316 t.Fatalf("bad: %#v", w) 317 } 318 if e != nil { 319 t.Fatalf("bad: %#v", e) 320 } 321 } 322 323 func TestResourceProvider_validate_errors(t *testing.T) { 324 p := new(terraform.MockResourceProvider) 325 p.ValidateReturnErrors = []error{errors.New("foo")} 326 327 client, server := testClientServer(t) 328 name, err := Register(server, p) 329 if err != nil { 330 t.Fatalf("err: %s", err) 331 } 332 provider := &ResourceProvider{Client: client, Name: name} 333 334 // Configure 335 config := &terraform.ResourceConfig{ 336 Raw: map[string]interface{}{"foo": "bar"}, 337 } 338 w, e := provider.Validate(config) 339 if !p.ValidateCalled { 340 t.Fatal("configure should be called") 341 } 342 if !reflect.DeepEqual(p.ValidateConfig, config) { 343 t.Fatalf("bad: %#v", p.ValidateConfig) 344 } 345 if w != nil { 346 t.Fatalf("bad: %#v", w) 347 } 348 349 if len(e) != 1 { 350 t.Fatalf("bad: %#v", e) 351 } 352 if e[0].Error() != "foo" { 353 t.Fatalf("bad: %#v", e) 354 } 355 } 356 357 func TestResourceProvider_validate_warns(t *testing.T) { 358 p := new(terraform.MockResourceProvider) 359 p.ValidateReturnWarns = []string{"foo"} 360 361 client, server := testClientServer(t) 362 name, err := Register(server, p) 363 if err != nil { 364 t.Fatalf("err: %s", err) 365 } 366 provider := &ResourceProvider{Client: client, Name: name} 367 368 // Configure 369 config := &terraform.ResourceConfig{ 370 Raw: map[string]interface{}{"foo": "bar"}, 371 } 372 w, e := provider.Validate(config) 373 if !p.ValidateCalled { 374 t.Fatal("configure should be called") 375 } 376 if !reflect.DeepEqual(p.ValidateConfig, config) { 377 t.Fatalf("bad: %#v", p.ValidateConfig) 378 } 379 if e != nil { 380 t.Fatalf("bad: %#v", e) 381 } 382 383 expected := []string{"foo"} 384 if !reflect.DeepEqual(w, expected) { 385 t.Fatalf("bad: %#v", w) 386 } 387 } 388 389 func TestResourceProvider_validateResource(t *testing.T) { 390 p := new(terraform.MockResourceProvider) 391 client, server := testClientServer(t) 392 name, err := Register(server, p) 393 if err != nil { 394 t.Fatalf("err: %s", err) 395 } 396 provider := &ResourceProvider{Client: client, Name: name} 397 398 // Configure 399 config := &terraform.ResourceConfig{ 400 Raw: map[string]interface{}{"foo": "bar"}, 401 } 402 w, e := provider.ValidateResource("foo", config) 403 if !p.ValidateResourceCalled { 404 t.Fatal("configure should be called") 405 } 406 if p.ValidateResourceType != "foo" { 407 t.Fatalf("bad: %#v", p.ValidateResourceType) 408 } 409 if !reflect.DeepEqual(p.ValidateResourceConfig, config) { 410 t.Fatalf("bad: %#v", p.ValidateResourceConfig) 411 } 412 if w != nil { 413 t.Fatalf("bad: %#v", w) 414 } 415 if e != nil { 416 t.Fatalf("bad: %#v", e) 417 } 418 } 419 420 func TestResourceProvider_validateResource_errors(t *testing.T) { 421 p := new(terraform.MockResourceProvider) 422 p.ValidateResourceReturnErrors = []error{errors.New("foo")} 423 424 client, server := testClientServer(t) 425 name, err := Register(server, p) 426 if err != nil { 427 t.Fatalf("err: %s", err) 428 } 429 provider := &ResourceProvider{Client: client, Name: name} 430 431 // Configure 432 config := &terraform.ResourceConfig{ 433 Raw: map[string]interface{}{"foo": "bar"}, 434 } 435 w, e := provider.ValidateResource("foo", config) 436 if !p.ValidateResourceCalled { 437 t.Fatal("configure should be called") 438 } 439 if p.ValidateResourceType != "foo" { 440 t.Fatalf("bad: %#v", p.ValidateResourceType) 441 } 442 if !reflect.DeepEqual(p.ValidateResourceConfig, config) { 443 t.Fatalf("bad: %#v", p.ValidateResourceConfig) 444 } 445 if w != nil { 446 t.Fatalf("bad: %#v", w) 447 } 448 449 if len(e) != 1 { 450 t.Fatalf("bad: %#v", e) 451 } 452 if e[0].Error() != "foo" { 453 t.Fatalf("bad: %#v", e) 454 } 455 } 456 457 func TestResourceProvider_validateResource_warns(t *testing.T) { 458 p := new(terraform.MockResourceProvider) 459 p.ValidateResourceReturnWarns = []string{"foo"} 460 461 client, server := testClientServer(t) 462 name, err := Register(server, p) 463 if err != nil { 464 t.Fatalf("err: %s", err) 465 } 466 provider := &ResourceProvider{Client: client, Name: name} 467 468 // Configure 469 config := &terraform.ResourceConfig{ 470 Raw: map[string]interface{}{"foo": "bar"}, 471 } 472 w, e := provider.ValidateResource("foo", config) 473 if !p.ValidateResourceCalled { 474 t.Fatal("configure should be called") 475 } 476 if p.ValidateResourceType != "foo" { 477 t.Fatalf("bad: %#v", p.ValidateResourceType) 478 } 479 if !reflect.DeepEqual(p.ValidateResourceConfig, config) { 480 t.Fatalf("bad: %#v", p.ValidateResourceConfig) 481 } 482 if e != nil { 483 t.Fatalf("bad: %#v", e) 484 } 485 486 expected := []string{"foo"} 487 if !reflect.DeepEqual(w, expected) { 488 t.Fatalf("bad: %#v", w) 489 } 490 } 491 492 func TestResourceProvider_close(t *testing.T) { 493 client, _ := testNewClientServer(t) 494 defer client.Close() 495 496 provider, err := client.ResourceProvider() 497 if err != nil { 498 t.Fatalf("err: %s", err) 499 } 500 501 var p interface{} 502 p = provider 503 pCloser, ok := p.(terraform.ResourceProviderCloser) 504 if !ok { 505 t.Fatal("should be a ResourceProviderCloser") 506 } 507 508 if err := pCloser.Close(); err != nil { 509 t.Fatalf("failed to close provider: %s", err) 510 } 511 512 // The connection should be closed now, so if we to make a 513 // new call we should get an error. 514 err = provider.Configure(&terraform.ResourceConfig{}) 515 if err == nil { 516 t.Fatal("should have error") 517 } 518 }