github.com/skyscape-cloud-services/terraform@v0.9.2-0.20170609144644-7ece028a1747/terraform/transform_provider_test.go (about) 1 package terraform 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 func TestProviderTransformer(t *testing.T) { 9 mod := testModule(t, "transform-provider-basic") 10 11 g := Graph{Path: RootModulePath} 12 { 13 tf := &ConfigTransformer{Module: mod} 14 if err := tf.Transform(&g); err != nil { 15 t.Fatalf("err: %s", err) 16 } 17 } 18 19 { 20 transform := &AttachResourceConfigTransformer{Module: mod} 21 if err := transform.Transform(&g); err != nil { 22 t.Fatalf("err: %s", err) 23 } 24 } 25 26 { 27 transform := &MissingProviderTransformer{Providers: []string{"aws"}} 28 if err := transform.Transform(&g); err != nil { 29 t.Fatalf("err: %s", err) 30 } 31 } 32 33 transform := &ProviderTransformer{} 34 if err := transform.Transform(&g); err != nil { 35 t.Fatalf("err: %s", err) 36 } 37 38 actual := strings.TrimSpace(g.String()) 39 expected := strings.TrimSpace(testTransformProviderBasicStr) 40 if actual != expected { 41 t.Fatalf("bad:\n\n%s", actual) 42 } 43 } 44 45 func TestProviderTransformer_moduleChild(t *testing.T) { 46 g := Graph{Path: RootModulePath} 47 48 { 49 tf := &ImportStateTransformer{ 50 Targets: []*ImportTarget{ 51 &ImportTarget{ 52 Addr: "module.moo.foo_instance.qux", 53 ID: "bar", 54 }, 55 }, 56 } 57 if err := tf.Transform(&g); err != nil { 58 t.Fatalf("err: %s", err) 59 } 60 } 61 62 { 63 tf := &MissingProviderTransformer{Providers: []string{"foo", "bar"}} 64 if err := tf.Transform(&g); err != nil { 65 t.Fatalf("err: %s", err) 66 } 67 } 68 69 { 70 tf := &ProviderTransformer{} 71 if err := tf.Transform(&g); err != nil { 72 t.Fatalf("err: %s", err) 73 } 74 } 75 76 actual := strings.TrimSpace(g.String()) 77 expected := strings.TrimSpace(testTransformProviderModuleChildStr) 78 if actual != expected { 79 t.Fatalf("bad:\n\n%s", actual) 80 } 81 } 82 83 func TestCloseProviderTransformer(t *testing.T) { 84 mod := testModule(t, "transform-provider-basic") 85 86 g := Graph{Path: RootModulePath} 87 { 88 tf := &ConfigTransformer{Module: mod} 89 if err := tf.Transform(&g); err != nil { 90 t.Fatalf("err: %s", err) 91 } 92 } 93 94 { 95 transform := &AttachResourceConfigTransformer{Module: mod} 96 if err := transform.Transform(&g); err != nil { 97 t.Fatalf("err: %s", err) 98 } 99 } 100 101 { 102 transform := &MissingProviderTransformer{Providers: []string{"aws"}} 103 if err := transform.Transform(&g); err != nil { 104 t.Fatalf("err: %s", err) 105 } 106 } 107 108 { 109 transform := &ProviderTransformer{} 110 if err := transform.Transform(&g); err != nil { 111 t.Fatalf("err: %s", err) 112 } 113 } 114 115 { 116 transform := &CloseProviderTransformer{} 117 if err := transform.Transform(&g); err != nil { 118 t.Fatalf("err: %s", err) 119 } 120 } 121 122 actual := strings.TrimSpace(g.String()) 123 expected := strings.TrimSpace(testTransformCloseProviderBasicStr) 124 if actual != expected { 125 t.Fatalf("bad:\n\n%s", actual) 126 } 127 } 128 129 func TestCloseProviderTransformer_withTargets(t *testing.T) { 130 mod := testModule(t, "transform-provider-basic") 131 132 g := Graph{Path: RootModulePath} 133 transforms := []GraphTransformer{ 134 &ConfigTransformer{Module: mod}, 135 &MissingProviderTransformer{Providers: []string{"aws"}}, 136 &ProviderTransformer{}, 137 &CloseProviderTransformer{}, 138 &TargetsTransformer{ 139 Targets: []string{"something.else"}, 140 }, 141 } 142 143 for _, tr := range transforms { 144 if err := tr.Transform(&g); err != nil { 145 t.Fatalf("err: %s", err) 146 } 147 } 148 149 actual := strings.TrimSpace(g.String()) 150 expected := strings.TrimSpace(``) 151 if actual != expected { 152 t.Fatalf("expected:%s\n\ngot:\n\n%s", expected, actual) 153 } 154 } 155 156 func TestMissingProviderTransformer(t *testing.T) { 157 mod := testModule(t, "transform-provider-missing") 158 159 g := Graph{Path: RootModulePath} 160 { 161 tf := &ConfigTransformer{Module: mod} 162 if err := tf.Transform(&g); err != nil { 163 t.Fatalf("err: %s", err) 164 } 165 } 166 167 { 168 transform := &AttachResourceConfigTransformer{Module: mod} 169 if err := transform.Transform(&g); err != nil { 170 t.Fatalf("err: %s", err) 171 } 172 } 173 174 { 175 transform := &MissingProviderTransformer{Providers: []string{"aws", "foo", "bar"}} 176 if err := transform.Transform(&g); err != nil { 177 t.Fatalf("err: %s", err) 178 } 179 } 180 181 { 182 transform := &CloseProviderTransformer{} 183 if err := transform.Transform(&g); err != nil { 184 t.Fatalf("err: %s", err) 185 } 186 } 187 188 actual := strings.TrimSpace(g.String()) 189 expected := strings.TrimSpace(testTransformMissingProviderBasicStr) 190 if actual != expected { 191 t.Fatalf("bad:\n\n%s", actual) 192 } 193 } 194 195 func TestMissingProviderTransformer_moduleChild(t *testing.T) { 196 g := Graph{Path: RootModulePath} 197 198 // We use the import state transformer since at the time of writing 199 // this test it is the first and only transformer that will introduce 200 // multiple module-path nodes at a single go. 201 { 202 tf := &ImportStateTransformer{ 203 Targets: []*ImportTarget{ 204 &ImportTarget{ 205 Addr: "module.moo.foo_instance.qux", 206 ID: "bar", 207 }, 208 }, 209 } 210 if err := tf.Transform(&g); err != nil { 211 t.Fatalf("err: %s", err) 212 } 213 } 214 215 { 216 tf := &MissingProviderTransformer{Providers: []string{"foo", "bar"}} 217 if err := tf.Transform(&g); err != nil { 218 t.Fatalf("err: %s", err) 219 } 220 } 221 222 actual := strings.TrimSpace(g.String()) 223 expected := strings.TrimSpace(testTransformMissingProviderModuleChildStr) 224 if actual != expected { 225 t.Fatalf("bad:\n\n%s", actual) 226 } 227 } 228 229 func TestMissingProviderTransformer_moduleGrandchild(t *testing.T) { 230 g := Graph{Path: RootModulePath} 231 232 // We use the import state transformer since at the time of writing 233 // this test it is the first and only transformer that will introduce 234 // multiple module-path nodes at a single go. 235 { 236 tf := &ImportStateTransformer{ 237 Targets: []*ImportTarget{ 238 &ImportTarget{ 239 Addr: "module.a.module.b.foo_instance.qux", 240 ID: "bar", 241 }, 242 }, 243 } 244 if err := tf.Transform(&g); err != nil { 245 t.Fatalf("err: %s", err) 246 } 247 } 248 249 { 250 tf := &MissingProviderTransformer{Providers: []string{"foo", "bar"}} 251 if err := tf.Transform(&g); err != nil { 252 t.Fatalf("err: %s", err) 253 } 254 } 255 256 actual := strings.TrimSpace(g.String()) 257 expected := strings.TrimSpace(testTransformMissingProviderModuleGrandchildStr) 258 if actual != expected { 259 t.Fatalf("bad:\n\n%s", actual) 260 } 261 } 262 263 func TestParentProviderTransformer(t *testing.T) { 264 g := Graph{Path: RootModulePath} 265 266 // Introduce a cihld module 267 { 268 tf := &ImportStateTransformer{ 269 Targets: []*ImportTarget{ 270 &ImportTarget{ 271 Addr: "module.moo.foo_instance.qux", 272 ID: "bar", 273 }, 274 }, 275 } 276 if err := tf.Transform(&g); err != nil { 277 t.Fatalf("err: %s", err) 278 } 279 } 280 281 // Add the missing modules 282 { 283 tf := &MissingProviderTransformer{Providers: []string{"foo", "bar"}} 284 if err := tf.Transform(&g); err != nil { 285 t.Fatalf("err: %s", err) 286 } 287 } 288 289 // Connect parents 290 { 291 tf := &ParentProviderTransformer{} 292 if err := tf.Transform(&g); err != nil { 293 t.Fatalf("err: %s", err) 294 } 295 } 296 297 actual := strings.TrimSpace(g.String()) 298 expected := strings.TrimSpace(testTransformParentProviderStr) 299 if actual != expected { 300 t.Fatalf("bad:\n\n%s", actual) 301 } 302 } 303 304 func TestParentProviderTransformer_moduleGrandchild(t *testing.T) { 305 g := Graph{Path: RootModulePath} 306 307 // We use the import state transformer since at the time of writing 308 // this test it is the first and only transformer that will introduce 309 // multiple module-path nodes at a single go. 310 { 311 tf := &ImportStateTransformer{ 312 Targets: []*ImportTarget{ 313 &ImportTarget{ 314 Addr: "module.a.module.b.foo_instance.qux", 315 ID: "bar", 316 }, 317 }, 318 } 319 if err := tf.Transform(&g); err != nil { 320 t.Fatalf("err: %s", err) 321 } 322 } 323 324 { 325 tf := &MissingProviderTransformer{Providers: []string{"foo", "bar"}} 326 if err := tf.Transform(&g); err != nil { 327 t.Fatalf("err: %s", err) 328 } 329 } 330 331 // Connect parents 332 { 333 tf := &ParentProviderTransformer{} 334 if err := tf.Transform(&g); err != nil { 335 t.Fatalf("err: %s", err) 336 } 337 } 338 339 actual := strings.TrimSpace(g.String()) 340 expected := strings.TrimSpace(testTransformParentProviderModuleGrandchildStr) 341 if actual != expected { 342 t.Fatalf("bad:\n\n%s", actual) 343 } 344 } 345 346 func TestPruneProviderTransformer(t *testing.T) { 347 mod := testModule(t, "transform-provider-prune") 348 349 g := Graph{Path: RootModulePath} 350 { 351 tf := &ConfigTransformer{Module: mod} 352 if err := tf.Transform(&g); err != nil { 353 t.Fatalf("err: %s", err) 354 } 355 } 356 357 { 358 transform := &AttachResourceConfigTransformer{Module: mod} 359 if err := transform.Transform(&g); err != nil { 360 t.Fatalf("err: %s", err) 361 } 362 } 363 364 { 365 transform := &MissingProviderTransformer{Providers: []string{"foo"}} 366 if err := transform.Transform(&g); err != nil { 367 t.Fatalf("err: %s", err) 368 } 369 } 370 371 { 372 transform := &ProviderTransformer{} 373 if err := transform.Transform(&g); err != nil { 374 t.Fatalf("err: %s", err) 375 } 376 } 377 378 { 379 transform := &CloseProviderTransformer{} 380 if err := transform.Transform(&g); err != nil { 381 t.Fatalf("err: %s", err) 382 } 383 } 384 385 { 386 transform := &PruneProviderTransformer{} 387 if err := transform.Transform(&g); err != nil { 388 t.Fatalf("err: %s", err) 389 } 390 } 391 392 actual := strings.TrimSpace(g.String()) 393 expected := strings.TrimSpace(testTransformPruneProviderBasicStr) 394 if actual != expected { 395 t.Fatalf("bad:\n\n%s", actual) 396 } 397 } 398 399 const testTransformProviderBasicStr = ` 400 aws_instance.web 401 provider.aws 402 provider.aws 403 ` 404 405 const testTransformCloseProviderBasicStr = ` 406 aws_instance.web 407 provider.aws 408 provider.aws 409 provider.aws (close) 410 aws_instance.web 411 provider.aws 412 ` 413 414 const testTransformMissingProviderBasicStr = ` 415 aws_instance.web 416 foo_instance.web 417 provider.aws 418 provider.aws (close) 419 aws_instance.web 420 provider.aws 421 provider.foo 422 provider.foo (close) 423 foo_instance.web 424 provider.foo 425 ` 426 427 const testTransformMissingProviderModuleChildStr = ` 428 module.moo.foo_instance.qux (import id: bar) 429 module.moo.provider.foo 430 provider.foo 431 ` 432 433 const testTransformMissingProviderModuleGrandchildStr = ` 434 module.a.module.b.foo_instance.qux (import id: bar) 435 module.a.module.b.provider.foo 436 module.a.provider.foo 437 provider.foo 438 ` 439 440 const testTransformParentProviderStr = ` 441 module.moo.foo_instance.qux (import id: bar) 442 module.moo.provider.foo 443 provider.foo 444 provider.foo 445 ` 446 447 const testTransformParentProviderModuleGrandchildStr = ` 448 module.a.module.b.foo_instance.qux (import id: bar) 449 module.a.module.b.provider.foo 450 module.a.provider.foo 451 module.a.provider.foo 452 provider.foo 453 provider.foo 454 ` 455 456 const testTransformProviderModuleChildStr = ` 457 module.moo.foo_instance.qux (import id: bar) 458 module.moo.provider.foo 459 module.moo.provider.foo 460 provider.foo 461 ` 462 463 const testTransformPruneProviderBasicStr = ` 464 foo_instance.web 465 provider.foo 466 provider.foo 467 provider.foo (close) 468 foo_instance.web 469 provider.foo 470 ` 471 472 const testTransformDisableProviderBasicStr = ` 473 module.child 474 provider.aws (disabled) 475 var.foo 476 provider.aws (close) 477 module.child 478 provider.aws (disabled) 479 provider.aws (disabled) 480 var.foo 481 ` 482 483 const testTransformDisableProviderKeepStr = ` 484 aws_instance.foo 485 provider.aws 486 module.child 487 provider.aws 488 var.foo 489 provider.aws 490 provider.aws (close) 491 aws_instance.foo 492 module.child 493 provider.aws 494 var.foo 495 `