github.com/tomaszheflik/terraform@v0.7.3-0.20160827060421-32f990b41594/terraform/transform_orphan_test.go (about) 1 package terraform 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/hashicorp/terraform/dag" 8 ) 9 10 func TestOrphanTransformer(t *testing.T) { 11 mod := testModule(t, "transform-orphan-basic") 12 state := &State{ 13 Modules: []*ModuleState{ 14 &ModuleState{ 15 Path: RootModulePath, 16 Resources: map[string]*ResourceState{ 17 "aws_instance.web": &ResourceState{ 18 Type: "aws_instance", 19 Primary: &InstanceState{ 20 ID: "foo", 21 }, 22 }, 23 24 // The orphan 25 "aws_instance.db": &ResourceState{ 26 Type: "aws_instance", 27 Primary: &InstanceState{ 28 ID: "foo", 29 }, 30 }, 31 }, 32 }, 33 }, 34 } 35 36 g := Graph{Path: RootModulePath} 37 { 38 tf := &ConfigTransformer{Module: mod} 39 if err := tf.Transform(&g); err != nil { 40 t.Fatalf("err: %s", err) 41 } 42 } 43 44 transform := &OrphanTransformer{State: state, Module: mod} 45 if err := transform.Transform(&g); err != nil { 46 t.Fatalf("err: %s", err) 47 } 48 49 actual := strings.TrimSpace(g.String()) 50 expected := strings.TrimSpace(testTransformOrphanBasicStr) 51 if actual != expected { 52 t.Fatalf("bad:\n\n%s", actual) 53 } 54 } 55 56 func TestOrphanTransformer_modules(t *testing.T) { 57 mod := testModule(t, "transform-orphan-modules") 58 state := &State{ 59 Modules: []*ModuleState{ 60 &ModuleState{ 61 Path: RootModulePath, 62 Resources: map[string]*ResourceState{ 63 "aws_instance.foo": &ResourceState{ 64 Type: "aws_instance", 65 Primary: &InstanceState{ 66 ID: "foo", 67 }, 68 }, 69 }, 70 }, 71 72 // Orphan module 73 &ModuleState{ 74 Path: []string{RootModuleName, "foo"}, 75 Resources: map[string]*ResourceState{ 76 "aws_instance.web": &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 transform := &OrphanTransformer{State: state, Module: mod} 96 if err := transform.Transform(&g); err != nil { 97 t.Fatalf("err: %s", err) 98 } 99 100 actual := strings.TrimSpace(g.String()) 101 expected := strings.TrimSpace(testTransformOrphanModulesStr) 102 if actual != expected { 103 t.Fatalf("bad:\n\n%s", actual) 104 } 105 } 106 107 func TestOrphanTransformer_modulesDeps(t *testing.T) { 108 mod := testModule(t, "transform-orphan-modules") 109 state := &State{ 110 Modules: []*ModuleState{ 111 &ModuleState{ 112 Path: RootModulePath, 113 Resources: map[string]*ResourceState{ 114 "aws_instance.foo": &ResourceState{ 115 Type: "aws_instance", 116 Primary: &InstanceState{ 117 ID: "foo", 118 }, 119 }, 120 }, 121 }, 122 123 // Orphan module 124 &ModuleState{ 125 Path: []string{RootModuleName, "foo"}, 126 Resources: map[string]*ResourceState{ 127 "aws_instance.web": &ResourceState{ 128 Type: "aws_instance", 129 Primary: &InstanceState{ 130 ID: "foo", 131 }, 132 }, 133 }, 134 Dependencies: []string{ 135 "aws_instance.foo", 136 }, 137 }, 138 }, 139 } 140 141 g := Graph{Path: RootModulePath} 142 { 143 tf := &ConfigTransformer{Module: mod} 144 if err := tf.Transform(&g); err != nil { 145 t.Fatalf("err: %s", err) 146 } 147 } 148 149 transform := &OrphanTransformer{State: state, Module: mod} 150 if err := transform.Transform(&g); err != nil { 151 t.Fatalf("err: %s", err) 152 } 153 154 actual := strings.TrimSpace(g.String()) 155 expected := strings.TrimSpace(testTransformOrphanModulesDepsStr) 156 if actual != expected { 157 t.Fatalf("bad:\n\n%s", actual) 158 } 159 } 160 161 func TestOrphanTransformer_modulesDepsOrphan(t *testing.T) { 162 mod := testModule(t, "transform-orphan-modules") 163 state := &State{ 164 Modules: []*ModuleState{ 165 &ModuleState{ 166 Path: RootModulePath, 167 Resources: map[string]*ResourceState{ 168 "aws_instance.web": &ResourceState{ 169 Type: "aws_instance", 170 Primary: &InstanceState{ 171 ID: "foo", 172 }, 173 }, 174 }, 175 }, 176 177 // Orphan module 178 &ModuleState{ 179 Path: []string{RootModuleName, "foo"}, 180 Resources: map[string]*ResourceState{ 181 "aws_instance.web": &ResourceState{ 182 Type: "aws_instance", 183 Primary: &InstanceState{ 184 ID: "foo", 185 }, 186 }, 187 }, 188 Dependencies: []string{ 189 "aws_instance.web", 190 }, 191 }, 192 }, 193 } 194 195 g := Graph{Path: RootModulePath} 196 { 197 tf := &ConfigTransformer{Module: mod} 198 if err := tf.Transform(&g); err != nil { 199 t.Fatalf("err: %s", err) 200 } 201 } 202 203 transform := &OrphanTransformer{State: state, Module: mod} 204 if err := transform.Transform(&g); err != nil { 205 t.Fatalf("err: %s", err) 206 } 207 208 actual := strings.TrimSpace(g.String()) 209 expected := strings.TrimSpace(testTransformOrphanModulesDepsOrphanStr) 210 if actual != expected { 211 t.Fatalf("bad:\n\n%s", actual) 212 } 213 } 214 215 func TestOrphanTransformer_modulesNoRoot(t *testing.T) { 216 mod := testModule(t, "transform-orphan-modules") 217 state := &State{ 218 Modules: []*ModuleState{ 219 // Orphan module 220 &ModuleState{ 221 Path: []string{RootModuleName, "foo"}, 222 Resources: map[string]*ResourceState{ 223 "aws_instance.web": &ResourceState{ 224 Type: "aws_instance", 225 Primary: &InstanceState{ 226 ID: "foo", 227 }, 228 }, 229 }, 230 }, 231 }, 232 } 233 234 g := Graph{Path: RootModulePath} 235 { 236 tf := &ConfigTransformer{Module: mod} 237 if err := tf.Transform(&g); err != nil { 238 t.Fatalf("err: %s", err) 239 } 240 } 241 242 transform := &OrphanTransformer{State: state, Module: mod} 243 if err := transform.Transform(&g); err != nil { 244 t.Fatalf("err: %s", err) 245 } 246 247 actual := strings.TrimSpace(g.String()) 248 expected := strings.TrimSpace(testTransformOrphanModulesNoRootStr) 249 if actual != expected { 250 t.Fatalf("bad:\n\n%s", actual) 251 } 252 } 253 254 func TestOrphanTransformer_resourceDepends(t *testing.T) { 255 mod := testModule(t, "transform-orphan-basic") 256 state := &State{ 257 Modules: []*ModuleState{ 258 &ModuleState{ 259 Path: RootModulePath, 260 Resources: map[string]*ResourceState{ 261 "aws_instance.web": &ResourceState{ 262 Type: "aws_instance", 263 Primary: &InstanceState{ 264 ID: "foo", 265 }, 266 }, 267 268 // The orphan 269 "aws_instance.db": &ResourceState{ 270 Type: "aws_instance", 271 Primary: &InstanceState{ 272 ID: "foo", 273 }, 274 Dependencies: []string{ 275 "aws_instance.web", 276 }, 277 }, 278 }, 279 }, 280 }, 281 } 282 283 g := Graph{Path: RootModulePath} 284 { 285 tf := &ConfigTransformer{Module: mod} 286 if err := tf.Transform(&g); err != nil { 287 t.Fatalf("err: %s", err) 288 } 289 } 290 291 transform := &OrphanTransformer{State: state, Module: mod} 292 if err := transform.Transform(&g); err != nil { 293 t.Fatalf("err: %s", err) 294 } 295 296 actual := strings.TrimSpace(g.String()) 297 expected := strings.TrimSpace(testTransformOrphanResourceDependsStr) 298 if actual != expected { 299 t.Fatalf("bad:\n\n%s", actual) 300 } 301 } 302 303 func TestOrphanTransformer_nilState(t *testing.T) { 304 mod := testModule(t, "transform-orphan-basic") 305 306 g := Graph{Path: RootModulePath} 307 { 308 tf := &ConfigTransformer{Module: mod} 309 if err := tf.Transform(&g); err != nil { 310 t.Fatalf("err: %s", err) 311 } 312 } 313 314 transform := &OrphanTransformer{State: nil, Module: mod} 315 if err := transform.Transform(&g); err != nil { 316 t.Fatalf("err: %s", err) 317 } 318 319 actual := strings.TrimSpace(g.String()) 320 expected := strings.TrimSpace(testTransformOrphanNilStateStr) 321 if actual != expected { 322 t.Fatalf("bad:\n\n%s", actual) 323 } 324 } 325 326 func TestGraphNodeOrphanModule_impl(t *testing.T) { 327 var _ dag.Vertex = new(graphNodeOrphanModule) 328 var _ dag.NamedVertex = new(graphNodeOrphanModule) 329 var _ GraphNodeExpandable = new(graphNodeOrphanModule) 330 } 331 332 func TestGraphNodeOrphanResource_impl(t *testing.T) { 333 var _ dag.Vertex = new(graphNodeOrphanResource) 334 var _ dag.NamedVertex = new(graphNodeOrphanResource) 335 var _ GraphNodeProviderConsumer = new(graphNodeOrphanResource) 336 var _ GraphNodeAddressable = new(graphNodeOrphanResource) 337 } 338 339 func TestGraphNodeOrphanResource_ProvidedBy(t *testing.T) { 340 n := &graphNodeOrphanResource{ResourceKey: &ResourceStateKey{Type: "aws_instance"}} 341 if v := n.ProvidedBy(); v[0] != "aws" { 342 t.Fatalf("bad: %#v", v) 343 } 344 } 345 346 func TestGraphNodeOrphanResource_ProvidedBy_alias(t *testing.T) { 347 n := &graphNodeOrphanResource{ResourceKey: &ResourceStateKey{Type: "aws_instance"}, Provider: "aws.bar"} 348 if v := n.ProvidedBy(); v[0] != "aws.bar" { 349 t.Fatalf("bad: %#v", v) 350 } 351 } 352 353 const testTransformOrphanBasicStr = ` 354 aws_instance.db (orphan) 355 aws_instance.web 356 ` 357 358 const testTransformOrphanModulesStr = ` 359 aws_instance.foo 360 module.foo (orphan) 361 ` 362 363 const testTransformOrphanModulesDepsStr = ` 364 aws_instance.foo 365 module.foo (orphan) 366 aws_instance.foo 367 ` 368 369 const testTransformOrphanModulesDepsOrphanStr = ` 370 aws_instance.foo 371 aws_instance.web (orphan) 372 module.foo (orphan) 373 aws_instance.web (orphan) 374 ` 375 376 const testTransformOrphanNilStateStr = ` 377 aws_instance.web 378 ` 379 380 const testTransformOrphanResourceDependsStr = ` 381 aws_instance.db (orphan) 382 aws_instance.web 383 aws_instance.web 384 ` 385 386 const testTransformOrphanModulesNoRootStr = ` 387 aws_instance.foo 388 module.foo (orphan) 389 `