github.com/tarrant/terraform@v0.3.8-0.20150402012457-f68c9eee638e/terraform/transform_config_test.go (about) 1 package terraform 2 3 import ( 4 "path/filepath" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/terraform/config/module" 9 ) 10 11 func TestConfigTransformer_nilModule(t *testing.T) { 12 g := Graph{Path: RootModulePath} 13 tf := &ConfigTransformer{} 14 if err := tf.Transform(&g); err == nil { 15 t.Fatal("should error") 16 } 17 } 18 19 func TestConfigTransformer_unloadedModule(t *testing.T) { 20 mod, err := module.NewTreeModule( 21 "", filepath.Join(fixtureDir, "graph-basic")) 22 if err != nil { 23 t.Fatalf("err: %s", err) 24 } 25 26 g := Graph{Path: RootModulePath} 27 tf := &ConfigTransformer{Module: mod} 28 if err := tf.Transform(&g); err == nil { 29 t.Fatal("should error") 30 } 31 } 32 33 func TestConfigTransformer(t *testing.T) { 34 g := Graph{Path: RootModulePath} 35 tf := &ConfigTransformer{Module: testModule(t, "graph-basic")} 36 if err := tf.Transform(&g); err != nil { 37 t.Fatalf("err: %s", err) 38 } 39 40 actual := strings.TrimSpace(g.String()) 41 expected := strings.TrimSpace(testGraphBasicStr) 42 if actual != expected { 43 t.Fatalf("bad:\n\n%s", actual) 44 } 45 } 46 47 func TestConfigTransformer_dependsOn(t *testing.T) { 48 g := Graph{Path: RootModulePath} 49 tf := &ConfigTransformer{Module: testModule(t, "graph-depends-on")} 50 if err := tf.Transform(&g); err != nil { 51 t.Fatalf("err: %s", err) 52 } 53 54 actual := strings.TrimSpace(g.String()) 55 expected := strings.TrimSpace(testGraphDependsOnStr) 56 if actual != expected { 57 t.Fatalf("bad:\n\n%s", actual) 58 } 59 } 60 61 func TestConfigTransformer_modules(t *testing.T) { 62 g := Graph{Path: RootModulePath} 63 tf := &ConfigTransformer{Module: testModule(t, "graph-modules")} 64 if err := tf.Transform(&g); err != nil { 65 t.Fatalf("err: %s", err) 66 } 67 68 actual := strings.TrimSpace(g.String()) 69 expected := strings.TrimSpace(testGraphModulesStr) 70 if actual != expected { 71 t.Fatalf("bad:\n\n%s", actual) 72 } 73 } 74 75 func TestConfigTransformer_outputs(t *testing.T) { 76 g := Graph{Path: RootModulePath} 77 tf := &ConfigTransformer{Module: testModule(t, "graph-outputs")} 78 if err := tf.Transform(&g); err != nil { 79 t.Fatalf("err: %s", err) 80 } 81 82 actual := strings.TrimSpace(g.String()) 83 expected := strings.TrimSpace(testGraphOutputsStr) 84 if actual != expected { 85 t.Fatalf("bad:\n\n%s", actual) 86 } 87 } 88 89 func TestConfigTransformer_errMissingDeps(t *testing.T) { 90 g := Graph{Path: RootModulePath} 91 tf := &ConfigTransformer{Module: testModule(t, "graph-missing-deps")} 92 if err := tf.Transform(&g); err == nil { 93 t.Fatalf("err: %s", err) 94 } 95 } 96 97 const testGraphBasicStr = ` 98 aws_instance.web 99 aws_security_group.firewall 100 aws_load_balancer.weblb 101 aws_instance.web 102 aws_security_group.firewall 103 openstack_floating_ip.random 104 provider.aws 105 openstack_floating_ip.random 106 ` 107 108 const testGraphDependsOnStr = ` 109 aws_instance.db 110 aws_instance.web 111 aws_instance.web 112 ` 113 114 const testGraphModulesStr = ` 115 aws_instance.web 116 aws_security_group.firewall 117 module.consul 118 aws_security_group.firewall 119 module.consul 120 aws_security_group.firewall 121 provider.aws 122 ` 123 124 const testGraphOutputsStr = ` 125 aws_instance.foo 126 output.foo 127 aws_instance.foo 128 `