github.com/tomaszheflik/terraform@v0.7.3-0.20160827060421-32f990b41594/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 // get from map 93 { 94 Config: map[string]interface{}{ 95 "mapname": []map[string]interface{}{ 96 map[string]interface{}{"key": 1}, 97 }, 98 }, 99 Key: "mapname.0.key", 100 Value: 1, 101 }, 102 103 // get from map with dot in key 104 { 105 Config: map[string]interface{}{ 106 "mapname": []map[string]interface{}{ 107 map[string]interface{}{"key.name": 1}, 108 }, 109 }, 110 Key: "mapname.0.key.name", 111 Value: 1, 112 }, 113 114 // get from map with overlapping key names 115 { 116 Config: map[string]interface{}{ 117 "mapname": []map[string]interface{}{ 118 map[string]interface{}{ 119 "key.name": 1, 120 "key.name.2": 2, 121 }, 122 }, 123 }, 124 Key: "mapname.0.key.name.2", 125 Value: 2, 126 }, 127 { 128 Config: map[string]interface{}{ 129 "mapname": []map[string]interface{}{ 130 map[string]interface{}{ 131 "key.name": 1, 132 "key.name.foo": 2, 133 }, 134 }, 135 }, 136 Key: "mapname.0.key.name", 137 Value: 1, 138 }, 139 { 140 Config: map[string]interface{}{ 141 "mapname": []map[string]interface{}{ 142 map[string]interface{}{ 143 "listkey": []map[string]interface{}{ 144 {"key": 3}, 145 }, 146 }, 147 }, 148 }, 149 Key: "mapname.0.listkey.0.key", 150 Value: 3, 151 }, 152 // FIXME: this is ambiguous, and matches the nested map 153 // leaving here to catch this behaviour if it changes. 154 { 155 Config: map[string]interface{}{ 156 "mapname": []map[string]interface{}{ 157 map[string]interface{}{ 158 "key.name": 1, 159 "key.name.0": 2, 160 "key": map[string]interface{}{"name": 3}, 161 }, 162 }, 163 }, 164 Key: "mapname.0.key.name", 165 Value: 3, 166 }, 167 /* 168 // TODO: can't access this nested list at all. 169 // FIXME: key with name matching substring of nested list can panic 170 { 171 Config: map[string]interface{}{ 172 "mapname": []map[string]interface{}{ 173 map[string]interface{}{ 174 "key.name": []map[string]interface{}{ 175 {"subkey": 1}, 176 }, 177 "key": 3, 178 }, 179 }, 180 }, 181 Key: "mapname.0.key.name.0.subkey", 182 Value: 3, 183 }, 184 */ 185 } 186 187 for i, tc := range cases { 188 var rawC *config.RawConfig 189 if tc.Config != nil { 190 var err error 191 rawC, err = config.NewRawConfig(tc.Config) 192 if err != nil { 193 t.Fatalf("err: %s", err) 194 } 195 } 196 197 if tc.Vars != nil { 198 vs := make(map[string]ast.Variable) 199 for k, v := range tc.Vars { 200 vs["var."+k] = ast.Variable{Value: v, Type: ast.TypeString} 201 } 202 203 if err := rawC.Interpolate(vs); err != nil { 204 t.Fatalf("err: %s", err) 205 } 206 } 207 208 rc := NewResourceConfig(rawC) 209 rc.interpolateForce() 210 211 v, _ := rc.Get(tc.Key) 212 if !reflect.DeepEqual(v, tc.Value) { 213 t.Fatalf("%d bad: %#v", i, v) 214 } 215 } 216 } 217 218 func testResourceConfig( 219 t *testing.T, c map[string]interface{}) *ResourceConfig { 220 raw, err := config.NewRawConfig(c) 221 if err != nil { 222 t.Fatalf("err: %s", err) 223 } 224 225 return NewResourceConfig(raw) 226 }