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