github.com/rhenning/terraform@v0.8.0-beta2/terraform/transform_orphan_resource_test.go (about) 1 package terraform 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/terraform/dag" 9 ) 10 11 func TestOrphanResourceTransformer(t *testing.T) { 12 mod := testModule(t, "transform-orphan-basic") 13 state := &State{ 14 Modules: []*ModuleState{ 15 &ModuleState{ 16 Path: RootModulePath, 17 Resources: map[string]*ResourceState{ 18 "aws_instance.web": &ResourceState{ 19 Type: "aws_instance", 20 Primary: &InstanceState{ 21 ID: "foo", 22 }, 23 }, 24 25 // The orphan 26 "aws_instance.db": &ResourceState{ 27 Type: "aws_instance", 28 Primary: &InstanceState{ 29 ID: "foo", 30 }, 31 }, 32 }, 33 }, 34 }, 35 } 36 37 g := Graph{Path: RootModulePath} 38 { 39 tf := &ConfigTransformer{Module: mod} 40 if err := tf.Transform(&g); err != nil { 41 t.Fatalf("err: %s", err) 42 } 43 } 44 45 { 46 tf := &OrphanResourceTransformer{ 47 Concrete: testOrphanResourceConcreteFunc, 48 State: state, Module: mod, 49 } 50 if err := tf.Transform(&g); err != nil { 51 t.Fatalf("err: %s", err) 52 } 53 } 54 55 actual := strings.TrimSpace(g.String()) 56 expected := strings.TrimSpace(testTransformOrphanResourceBasicStr) 57 if actual != expected { 58 t.Fatalf("bad:\n\n%s", actual) 59 } 60 } 61 62 func TestOrphanResourceTransformer_countGood(t *testing.T) { 63 mod := testModule(t, "transform-orphan-count") 64 state := &State{ 65 Modules: []*ModuleState{ 66 &ModuleState{ 67 Path: RootModulePath, 68 Resources: map[string]*ResourceState{ 69 "aws_instance.foo.0": &ResourceState{ 70 Type: "aws_instance", 71 Primary: &InstanceState{ 72 ID: "foo", 73 }, 74 }, 75 76 "aws_instance.foo.1": &ResourceState{ 77 Type: "aws_instance", 78 Primary: &InstanceState{ 79 ID: "foo", 80 }, 81 }, 82 }, 83 }, 84 }, 85 } 86 87 g := Graph{Path: RootModulePath} 88 { 89 tf := &ConfigTransformer{Module: mod} 90 if err := tf.Transform(&g); err != nil { 91 t.Fatalf("err: %s", err) 92 } 93 } 94 95 { 96 tf := &OrphanResourceTransformer{ 97 Concrete: testOrphanResourceConcreteFunc, 98 State: state, Module: mod, 99 } 100 if err := tf.Transform(&g); err != nil { 101 t.Fatalf("err: %s", err) 102 } 103 } 104 105 actual := strings.TrimSpace(g.String()) 106 expected := strings.TrimSpace(testTransformOrphanResourceCountStr) 107 if actual != expected { 108 t.Fatalf("bad:\n\n%s", actual) 109 } 110 } 111 112 func TestOrphanResourceTransformer_countBad(t *testing.T) { 113 mod := testModule(t, "transform-orphan-count-empty") 114 state := &State{ 115 Modules: []*ModuleState{ 116 &ModuleState{ 117 Path: RootModulePath, 118 Resources: map[string]*ResourceState{ 119 "aws_instance.foo.0": &ResourceState{ 120 Type: "aws_instance", 121 Primary: &InstanceState{ 122 ID: "foo", 123 }, 124 }, 125 126 "aws_instance.foo.1": &ResourceState{ 127 Type: "aws_instance", 128 Primary: &InstanceState{ 129 ID: "foo", 130 }, 131 }, 132 }, 133 }, 134 }, 135 } 136 137 g := Graph{Path: RootModulePath} 138 { 139 tf := &ConfigTransformer{Module: mod} 140 if err := tf.Transform(&g); err != nil { 141 t.Fatalf("err: %s", err) 142 } 143 } 144 145 { 146 tf := &OrphanResourceTransformer{ 147 Concrete: testOrphanResourceConcreteFunc, 148 State: state, Module: mod, 149 } 150 if err := tf.Transform(&g); err != nil { 151 t.Fatalf("err: %s", err) 152 } 153 } 154 155 actual := strings.TrimSpace(g.String()) 156 expected := strings.TrimSpace(testTransformOrphanResourceCountBadStr) 157 if actual != expected { 158 t.Fatalf("bad:\n\n%s", actual) 159 } 160 } 161 162 func TestOrphanResourceTransformer_modules(t *testing.T) { 163 mod := testModule(t, "transform-orphan-modules") 164 state := &State{ 165 Modules: []*ModuleState{ 166 &ModuleState{ 167 Path: RootModulePath, 168 Resources: map[string]*ResourceState{ 169 "aws_instance.foo": &ResourceState{ 170 Type: "aws_instance", 171 Primary: &InstanceState{ 172 ID: "foo", 173 }, 174 }, 175 }, 176 }, 177 178 &ModuleState{ 179 Path: []string{"root", "child"}, 180 Resources: map[string]*ResourceState{ 181 "aws_instance.web": &ResourceState{ 182 Type: "aws_instance", 183 Primary: &InstanceState{ 184 ID: "foo", 185 }, 186 }, 187 }, 188 }, 189 }, 190 } 191 192 g := Graph{Path: RootModulePath} 193 { 194 tf := &ConfigTransformer{Module: mod} 195 if err := tf.Transform(&g); err != nil { 196 t.Fatalf("err: %s", err) 197 } 198 } 199 200 { 201 tf := &OrphanResourceTransformer{ 202 Concrete: testOrphanResourceConcreteFunc, 203 State: state, Module: mod, 204 } 205 if err := tf.Transform(&g); err != nil { 206 t.Fatalf("err: %s", err) 207 } 208 } 209 210 actual := strings.TrimSpace(g.String()) 211 expected := strings.TrimSpace(testTransformOrphanResourceModulesStr) 212 if actual != expected { 213 t.Fatalf("bad:\n\n%s", actual) 214 } 215 } 216 217 const testTransformOrphanResourceBasicStr = ` 218 aws_instance.db (orphan) 219 aws_instance.web 220 ` 221 222 const testTransformOrphanResourceCountStr = ` 223 aws_instance.foo 224 ` 225 226 const testTransformOrphanResourceCountBadStr = ` 227 aws_instance.foo[0] (orphan) 228 aws_instance.foo[1] (orphan) 229 ` 230 231 const testTransformOrphanResourceModulesStr = ` 232 aws_instance.foo 233 module.child.aws_instance.web (orphan) 234 ` 235 236 func testOrphanResourceConcreteFunc(a *NodeAbstractResource) dag.Vertex { 237 return &testOrphanResourceConcrete{a} 238 } 239 240 type testOrphanResourceConcrete struct { 241 *NodeAbstractResource 242 } 243 244 func (n *testOrphanResourceConcrete) Name() string { 245 return fmt.Sprintf("%s (orphan)", n.NodeAbstractResource.Name()) 246 }