github.com/hooklift/terraform@v0.11.0-beta1.0.20171117000744-6786c1361ffe/terraform/transform_targets_test.go (about) 1 package terraform 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 func TestTargetsTransformer(t *testing.T) { 9 mod := testModule(t, "transform-targets-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 := &ReferenceTransformer{} 28 if err := transform.Transform(&g); err != nil { 29 t.Fatalf("err: %s", err) 30 } 31 } 32 33 { 34 transform := &TargetsTransformer{Targets: []string{"aws_instance.me"}} 35 if err := transform.Transform(&g); err != nil { 36 t.Fatalf("err: %s", err) 37 } 38 } 39 40 actual := strings.TrimSpace(g.String()) 41 expected := strings.TrimSpace(` 42 aws_instance.me 43 aws_subnet.me 44 aws_subnet.me 45 aws_vpc.me 46 aws_vpc.me 47 `) 48 if actual != expected { 49 t.Fatalf("bad:\n\nexpected:\n%s\n\ngot:\n%s\n", expected, actual) 50 } 51 } 52 53 func TestTargetsTransformer_downstream(t *testing.T) { 54 mod := testModule(t, "transform-targets-downstream") 55 56 g := Graph{Path: RootModulePath} 57 { 58 transform := &ConfigTransformer{Module: mod} 59 if err := transform.Transform(&g); err != nil { 60 t.Fatalf("%T failed: %s", transform, err) 61 } 62 } 63 64 { 65 transform := &AttachResourceConfigTransformer{Module: mod} 66 if err := transform.Transform(&g); err != nil { 67 t.Fatalf("%T failed: %s", transform, err) 68 } 69 } 70 71 { 72 transform := &AttachResourceConfigTransformer{Module: mod} 73 if err := transform.Transform(&g); err != nil { 74 t.Fatalf("%T failed: %s", transform, err) 75 } 76 } 77 78 { 79 transform := &OutputTransformer{Module: mod} 80 if err := transform.Transform(&g); err != nil { 81 t.Fatalf("%T failed: %s", transform, err) 82 } 83 } 84 85 { 86 transform := &ReferenceTransformer{} 87 if err := transform.Transform(&g); err != nil { 88 t.Fatalf("err: %s", err) 89 } 90 } 91 92 { 93 transform := &TargetsTransformer{Targets: []string{"module.child.module.grandchild.aws_instance.foo"}} 94 if err := transform.Transform(&g); err != nil { 95 t.Fatalf("%T failed: %s", transform, err) 96 } 97 } 98 99 actual := strings.TrimSpace(g.String()) 100 // Even though we only asked to target the grandchild resource, all of the 101 // outputs that descend from it are also targeted. 102 expected := strings.TrimSpace(` 103 module.child.module.grandchild.aws_instance.foo 104 module.child.module.grandchild.output.id 105 module.child.module.grandchild.aws_instance.foo 106 module.child.output.grandchild_id 107 module.child.module.grandchild.output.id 108 output.grandchild_id 109 module.child.output.grandchild_id 110 `) 111 if actual != expected { 112 t.Fatalf("bad:\n\nexpected:\n%s\n\ngot:\n%s\n", expected, actual) 113 } 114 } 115 116 func TestTargetsTransformer_destroy(t *testing.T) { 117 mod := testModule(t, "transform-targets-destroy") 118 119 g := Graph{Path: RootModulePath} 120 { 121 tf := &ConfigTransformer{Module: mod} 122 if err := tf.Transform(&g); err != nil { 123 t.Fatalf("err: %s", err) 124 } 125 } 126 127 { 128 transform := &AttachResourceConfigTransformer{Module: mod} 129 if err := transform.Transform(&g); err != nil { 130 t.Fatalf("err: %s", err) 131 } 132 } 133 134 { 135 transform := &ReferenceTransformer{} 136 if err := transform.Transform(&g); err != nil { 137 t.Fatalf("err: %s", err) 138 } 139 } 140 141 { 142 transform := &TargetsTransformer{ 143 Targets: []string{"aws_instance.me"}, 144 Destroy: true, 145 } 146 if err := transform.Transform(&g); err != nil { 147 t.Fatalf("err: %s", err) 148 } 149 } 150 151 actual := strings.TrimSpace(g.String()) 152 expected := strings.TrimSpace(` 153 aws_elb.me 154 aws_instance.me 155 aws_instance.me 156 aws_instance.metoo 157 aws_instance.me 158 `) 159 if actual != expected { 160 t.Fatalf("bad:\n\nexpected:\n%s\n\ngot:\n%s\n", expected, actual) 161 } 162 }