github.com/articulate/terraform@v0.6.13-0.20160303003731-8d31c93862de/terraform/resource_test.go (about) 1 package terraform 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/hashicorp/hil/ast" 8 "github.com/hashicorp/terraform/config" 9 ) 10 11 func TestInstanceInfo(t *testing.T) { 12 cases := []struct { 13 Info *InstanceInfo 14 Result string 15 }{ 16 { 17 &InstanceInfo{ 18 Id: "foo", 19 }, 20 "foo", 21 }, 22 { 23 &InstanceInfo{ 24 Id: "foo", 25 ModulePath: rootModulePath, 26 }, 27 "foo", 28 }, 29 { 30 &InstanceInfo{ 31 Id: "foo", 32 ModulePath: []string{"root", "consul"}, 33 }, 34 "module.consul.foo", 35 }, 36 } 37 38 for i, tc := range cases { 39 actual := tc.Info.HumanId() 40 if actual != tc.Result { 41 t.Fatalf("%d: %s", i, actual) 42 } 43 } 44 } 45 46 func TestResourceConfigGet(t *testing.T) { 47 cases := []struct { 48 Config map[string]interface{} 49 Vars map[string]string 50 Key string 51 Value interface{} 52 }{ 53 { 54 Config: nil, 55 Key: "foo", 56 Value: nil, 57 }, 58 59 { 60 Config: map[string]interface{}{ 61 "foo": "${var.foo}", 62 }, 63 Key: "foo", 64 Value: "${var.foo}", 65 }, 66 67 { 68 Config: map[string]interface{}{ 69 "foo": "${var.foo}", 70 }, 71 Vars: map[string]string{"foo": "bar"}, 72 Key: "foo", 73 Value: "bar", 74 }, 75 76 { 77 Config: map[string]interface{}{ 78 "foo": []interface{}{1, 2, 5}, 79 }, 80 Key: "foo.0", 81 Value: 1, 82 }, 83 84 { 85 Config: map[string]interface{}{ 86 "foo": []interface{}{1, 2, 5}, 87 }, 88 Key: "foo.5", 89 Value: nil, 90 }, 91 } 92 93 for i, tc := range cases { 94 var rawC *config.RawConfig 95 if tc.Config != nil { 96 var err error 97 rawC, err = config.NewRawConfig(tc.Config) 98 if err != nil { 99 t.Fatalf("err: %s", err) 100 } 101 } 102 103 if tc.Vars != nil { 104 vs := make(map[string]ast.Variable) 105 for k, v := range tc.Vars { 106 vs["var."+k] = ast.Variable{Value: v, Type: ast.TypeString} 107 } 108 109 if err := rawC.Interpolate(vs); err != nil { 110 t.Fatalf("err: %s", err) 111 } 112 } 113 114 rc := NewResourceConfig(rawC) 115 rc.interpolateForce() 116 117 v, _ := rc.Get(tc.Key) 118 if !reflect.DeepEqual(v, tc.Value) { 119 t.Fatalf("%d bad: %#v", i, v) 120 } 121 } 122 } 123 124 func testResourceConfig( 125 t *testing.T, c map[string]interface{}) *ResourceConfig { 126 raw, err := config.NewRawConfig(c) 127 if err != nil { 128 t.Fatalf("err: %s", err) 129 } 130 131 return NewResourceConfig(raw) 132 }