github.com/magodo/terraform@v0.11.12-beta1/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" 9 "github.com/hashicorp/terraform/config/module" 10 ) 11 12 func TestConfigTransformer_nilModule(t *testing.T) { 13 g := Graph{Path: RootModulePath} 14 tf := &ConfigTransformer{} 15 if err := tf.Transform(&g); err != nil { 16 t.Fatalf("err: %s", err) 17 } 18 19 if len(g.Vertices()) > 0 { 20 t.Fatalf("graph is not empty: %s", g.String()) 21 } 22 } 23 24 func TestConfigTransformer_unloadedModule(t *testing.T) { 25 mod, err := module.NewTreeModule( 26 "", filepath.Join(fixtureDir, "graph-basic")) 27 if err != nil { 28 t.Fatalf("err: %s", err) 29 } 30 31 g := Graph{Path: RootModulePath} 32 tf := &ConfigTransformer{Module: mod} 33 if err := tf.Transform(&g); err == nil { 34 t.Fatal("should error") 35 } 36 } 37 38 func TestConfigTransformer(t *testing.T) { 39 g := Graph{Path: RootModulePath} 40 tf := &ConfigTransformer{Module: testModule(t, "graph-basic")} 41 if err := tf.Transform(&g); err != nil { 42 t.Fatalf("err: %s", err) 43 } 44 45 actual := strings.TrimSpace(g.String()) 46 expected := strings.TrimSpace(testConfigTransformerGraphBasicStr) 47 if actual != expected { 48 t.Fatalf("bad:\n\n%s", actual) 49 } 50 } 51 52 func TestConfigTransformer_mode(t *testing.T) { 53 g := Graph{Path: RootModulePath} 54 tf := &ConfigTransformer{ 55 Module: testModule(t, "transform-config-mode-data"), 56 ModeFilter: true, 57 Mode: config.DataResourceMode, 58 } 59 if err := tf.Transform(&g); err != nil { 60 t.Fatalf("err: %s", err) 61 } 62 63 actual := strings.TrimSpace(g.String()) 64 expected := strings.TrimSpace(` 65 data.aws_ami.foo 66 `) 67 if actual != expected { 68 t.Fatalf("bad:\n\n%s", actual) 69 } 70 } 71 72 func TestConfigTransformer_nonUnique(t *testing.T) { 73 addr, err := ParseResourceAddress("aws_instance.web") 74 if err != nil { 75 t.Fatalf("bad: %s", err) 76 } 77 78 g := Graph{Path: RootModulePath} 79 g.Add(&NodeAbstractResource{Addr: addr}) 80 tf := &ConfigTransformer{Module: testModule(t, "graph-basic")} 81 if err := tf.Transform(&g); err != nil { 82 t.Fatalf("err: %s", err) 83 } 84 85 actual := strings.TrimSpace(g.String()) 86 expected := strings.TrimSpace(` 87 aws_instance.web 88 aws_instance.web 89 aws_load_balancer.weblb 90 aws_security_group.firewall 91 openstack_floating_ip.random 92 `) 93 if actual != expected { 94 t.Fatalf("bad:\n\n%s", actual) 95 } 96 } 97 98 func TestConfigTransformer_unique(t *testing.T) { 99 addr, err := ParseResourceAddress("aws_instance.web") 100 if err != nil { 101 t.Fatalf("bad: %s", err) 102 } 103 104 g := Graph{Path: RootModulePath} 105 g.Add(&NodeAbstractResource{Addr: addr}) 106 tf := &ConfigTransformer{ 107 Module: testModule(t, "graph-basic"), 108 Unique: true, 109 } 110 if err := tf.Transform(&g); err != nil { 111 t.Fatalf("err: %s", err) 112 } 113 114 actual := strings.TrimSpace(g.String()) 115 expected := strings.TrimSpace(` 116 aws_instance.web 117 aws_load_balancer.weblb 118 aws_security_group.firewall 119 openstack_floating_ip.random 120 `) 121 if actual != expected { 122 t.Fatalf("bad:\n\n%s", actual) 123 } 124 } 125 126 const testConfigTransformerGraphBasicStr = ` 127 aws_instance.web 128 aws_load_balancer.weblb 129 aws_security_group.firewall 130 openstack_floating_ip.random 131 `