github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/config/raw_config_test.go (about) 1 package config 2 3 import ( 4 "encoding/gob" 5 "reflect" 6 "testing" 7 8 "github.com/hashicorp/terraform/config/lang/ast" 9 ) 10 11 func TestNewRawConfig(t *testing.T) { 12 raw := map[string]interface{}{ 13 "foo": "${var.bar}", 14 "bar": `${file("boom.txt")}`, 15 } 16 17 rc, err := NewRawConfig(raw) 18 if err != nil { 19 t.Fatalf("err: %s", err) 20 } 21 22 if len(rc.Interpolations) != 2 { 23 t.Fatalf("bad: %#v", rc.Interpolations) 24 } 25 if len(rc.Variables) != 1 { 26 t.Fatalf("bad: %#v", rc.Variables) 27 } 28 } 29 30 func TestRawConfig(t *testing.T) { 31 raw := map[string]interface{}{ 32 "foo": "${var.bar}", 33 } 34 35 rc, err := NewRawConfig(raw) 36 if err != nil { 37 t.Fatalf("err: %s", err) 38 } 39 40 // Before interpolate, Config() should be the raw 41 if !reflect.DeepEqual(rc.Config(), raw) { 42 t.Fatalf("bad: %#v", rc.Config()) 43 } 44 45 vars := map[string]ast.Variable{ 46 "var.bar": ast.Variable{ 47 Value: "baz", 48 Type: ast.TypeString, 49 }, 50 } 51 if err := rc.Interpolate(vars); err != nil { 52 t.Fatalf("err: %s", err) 53 } 54 55 actual := rc.Config() 56 expected := map[string]interface{}{ 57 "foo": "baz", 58 } 59 60 if !reflect.DeepEqual(actual, expected) { 61 t.Fatalf("bad: %#v", actual) 62 } 63 if len(rc.UnknownKeys()) != 0 { 64 t.Fatalf("bad: %#v", rc.UnknownKeys()) 65 } 66 } 67 68 func TestRawConfig_double(t *testing.T) { 69 raw := map[string]interface{}{ 70 "foo": "${var.bar}", 71 } 72 73 rc, err := NewRawConfig(raw) 74 if err != nil { 75 t.Fatalf("err: %s", err) 76 } 77 78 vars := map[string]ast.Variable{ 79 "var.bar": ast.Variable{ 80 Value: "baz", 81 Type: ast.TypeString, 82 }, 83 } 84 if err := rc.Interpolate(vars); err != nil { 85 t.Fatalf("err: %s", err) 86 } 87 88 actual := rc.Config() 89 expected := map[string]interface{}{ 90 "foo": "baz", 91 } 92 93 if !reflect.DeepEqual(actual, expected) { 94 t.Fatalf("bad: %#v", actual) 95 } 96 97 vars = map[string]ast.Variable{ 98 "var.bar": ast.Variable{ 99 Value: "what", 100 Type: ast.TypeString, 101 }, 102 } 103 if err := rc.Interpolate(vars); err != nil { 104 t.Fatalf("err: %s", err) 105 } 106 107 actual = rc.Config() 108 expected = map[string]interface{}{ 109 "foo": "what", 110 } 111 112 if !reflect.DeepEqual(actual, expected) { 113 t.Fatalf("bad: %#v", actual) 114 } 115 } 116 117 func TestRawConfig_merge(t *testing.T) { 118 raw1 := map[string]interface{}{ 119 "foo": "${var.foo}", 120 "bar": "${var.bar}", 121 } 122 123 rc1, err := NewRawConfig(raw1) 124 if err != nil { 125 t.Fatalf("err: %s", err) 126 } 127 128 { 129 vars := map[string]ast.Variable{ 130 "var.foo": ast.Variable{ 131 Value: "foovalue", 132 Type: ast.TypeString, 133 }, 134 "var.bar": ast.Variable{ 135 Value: "nope", 136 Type: ast.TypeString, 137 }, 138 } 139 if err := rc1.Interpolate(vars); err != nil { 140 t.Fatalf("err: %s", err) 141 } 142 } 143 144 raw2 := map[string]interface{}{ 145 "bar": "${var.bar}", 146 "baz": "${var.baz}", 147 } 148 149 rc2, err := NewRawConfig(raw2) 150 if err != nil { 151 t.Fatalf("err: %s", err) 152 } 153 154 { 155 vars := map[string]ast.Variable{ 156 "var.bar": ast.Variable{ 157 Value: "barvalue", 158 Type: ast.TypeString, 159 }, 160 "var.baz": ast.Variable{ 161 Value: UnknownVariableValue, 162 Type: ast.TypeString, 163 }, 164 } 165 if err := rc2.Interpolate(vars); err != nil { 166 t.Fatalf("err: %s", err) 167 } 168 } 169 170 // Merge the two 171 rc3 := rc1.Merge(rc2) 172 173 // Raw should be merged 174 raw3 := map[string]interface{}{ 175 "foo": "${var.foo}", 176 "bar": "${var.bar}", 177 "baz": "${var.baz}", 178 } 179 if !reflect.DeepEqual(rc3.Raw, raw3) { 180 t.Fatalf("bad: %#v", rc3.Raw) 181 } 182 183 actual := rc3.Config() 184 expected := map[string]interface{}{ 185 "foo": "foovalue", 186 "bar": "barvalue", 187 } 188 if !reflect.DeepEqual(actual, expected) { 189 t.Fatalf("bad: %#v", actual) 190 } 191 192 expectedKeys := []string{"baz"} 193 if !reflect.DeepEqual(rc3.UnknownKeys(), expectedKeys) { 194 t.Fatalf("bad: %#v", rc3.UnknownKeys()) 195 } 196 } 197 198 func TestRawConfig_syntax(t *testing.T) { 199 raw := map[string]interface{}{ 200 "foo": "${var", 201 } 202 203 if _, err := NewRawConfig(raw); err == nil { 204 t.Fatal("should error") 205 } 206 } 207 208 func TestRawConfig_unknown(t *testing.T) { 209 raw := map[string]interface{}{ 210 "foo": "${var.bar}", 211 } 212 213 rc, err := NewRawConfig(raw) 214 if err != nil { 215 t.Fatalf("err: %s", err) 216 } 217 218 vars := map[string]ast.Variable{ 219 "var.bar": ast.Variable{ 220 Value: UnknownVariableValue, 221 Type: ast.TypeString, 222 }, 223 } 224 if err := rc.Interpolate(vars); err != nil { 225 t.Fatalf("err: %s", err) 226 } 227 228 actual := rc.Config() 229 expected := map[string]interface{}{} 230 231 if !reflect.DeepEqual(actual, expected) { 232 t.Fatalf("bad: %#v", actual) 233 } 234 235 expectedKeys := []string{"foo"} 236 if !reflect.DeepEqual(rc.UnknownKeys(), expectedKeys) { 237 t.Fatalf("bad: %#v", rc.UnknownKeys()) 238 } 239 } 240 241 func TestRawConfig_unknownPartial(t *testing.T) { 242 raw := map[string]interface{}{ 243 "foo": "${var.bar}/32", 244 } 245 246 rc, err := NewRawConfig(raw) 247 if err != nil { 248 t.Fatalf("err: %s", err) 249 } 250 251 vars := map[string]ast.Variable{ 252 "var.bar": ast.Variable{ 253 Value: UnknownVariableValue, 254 Type: ast.TypeString, 255 }, 256 } 257 if err := rc.Interpolate(vars); err != nil { 258 t.Fatalf("err: %s", err) 259 } 260 261 actual := rc.Config() 262 expected := map[string]interface{}{} 263 264 if !reflect.DeepEqual(actual, expected) { 265 t.Fatalf("bad: %#v", actual) 266 } 267 268 expectedKeys := []string{"foo"} 269 if !reflect.DeepEqual(rc.UnknownKeys(), expectedKeys) { 270 t.Fatalf("bad: %#v", rc.UnknownKeys()) 271 } 272 } 273 274 func TestRawConfigValue(t *testing.T) { 275 raw := map[string]interface{}{ 276 "foo": "${var.bar}", 277 } 278 279 rc, err := NewRawConfig(raw) 280 if err != nil { 281 t.Fatalf("err: %s", err) 282 } 283 284 rc.Key = "" 285 if rc.Value() != nil { 286 t.Fatalf("bad: %#v", rc.Value()) 287 } 288 289 rc.Key = "foo" 290 if rc.Value() != "${var.bar}" { 291 t.Fatalf("err: %#v", rc.Value()) 292 } 293 294 vars := map[string]ast.Variable{ 295 "var.bar": ast.Variable{ 296 Value: "baz", 297 Type: ast.TypeString, 298 }, 299 } 300 if err := rc.Interpolate(vars); err != nil { 301 t.Fatalf("err: %s", err) 302 } 303 304 if rc.Value() != "baz" { 305 t.Fatalf("bad: %#v", rc.Value()) 306 } 307 } 308 309 func TestRawConfig_implGob(t *testing.T) { 310 var _ gob.GobDecoder = new(RawConfig) 311 var _ gob.GobEncoder = new(RawConfig) 312 }