github.com/spirius/terraform@v0.10.0-beta2.0.20170714185654-87b2c0cf8fea/helper/diff/resource_builder_test.go (about) 1 package diff 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/terraform/config" 7 "github.com/hashicorp/terraform/terraform" 8 ) 9 10 func TestResourceBuilder_attrSetComputed(t *testing.T) { 11 rb := &ResourceBuilder{ 12 Attrs: map[string]AttrType{ 13 "foo": AttrTypeCreate, 14 }, 15 ComputedAttrs: []string{ 16 "foo", 17 }, 18 } 19 20 state := &terraform.InstanceState{} 21 c := testConfig(t, map[string]interface{}{ 22 "foo": "bar", 23 }, nil) 24 25 diff, err := rb.Diff(state, c) 26 if err != nil { 27 t.Fatalf("err: %s", err) 28 } 29 if diff == nil { 30 t.Fatal("diff shold not be nil") 31 } 32 33 actual := testResourceDiffStr(diff) 34 expected := testRBAttrSetComputedDiff 35 if actual != expected { 36 t.Fatalf("bad: %s", actual) 37 } 38 } 39 40 func TestResourceBuilder_attrSetComputedComplex(t *testing.T) { 41 rb := &ResourceBuilder{ 42 Attrs: map[string]AttrType{ 43 "foo": AttrTypeCreate, 44 }, 45 ComputedAttrs: []string{ 46 "foo", 47 }, 48 } 49 50 state := &terraform.InstanceState{ 51 ID: "foo", 52 Attributes: map[string]string{ 53 "foo.#": "0", 54 }, 55 } 56 57 c := testConfig(t, map[string]interface{}{}, nil) 58 59 diff, err := rb.Diff(state, c) 60 if err != nil { 61 t.Fatalf("err: %s", err) 62 } 63 if diff != nil { 64 t.Fatalf("diff shold be nil: %#v", diff) 65 } 66 } 67 68 func TestResourceBuilder_replaceComputed(t *testing.T) { 69 rb := &ResourceBuilder{ 70 Attrs: map[string]AttrType{ 71 "foo": AttrTypeCreate, 72 }, 73 ComputedAttrs: []string{ 74 "foo", 75 }, 76 } 77 78 state := &terraform.InstanceState{ 79 ID: "foo", 80 Attributes: map[string]string{ 81 "foo": "bar", 82 }, 83 } 84 c := testConfig(t, nil, nil) 85 86 diff, err := rb.Diff(state, c) 87 if err != nil { 88 t.Fatalf("err: %s", err) 89 } 90 if diff != nil { 91 t.Fatalf("should be nil: %#v", diff) 92 } 93 } 94 95 func TestResourceBuilder_complex(t *testing.T) { 96 rb := &ResourceBuilder{ 97 Attrs: map[string]AttrType{ 98 "listener": AttrTypeUpdate, 99 }, 100 } 101 102 state := &terraform.InstanceState{ 103 ID: "foo", 104 Attributes: map[string]string{ 105 "ignore": "1", 106 "listener.#": "1", 107 "listener.0.port": "80", 108 }, 109 } 110 111 c := testConfig(t, map[string]interface{}{ 112 "listener": []interface{}{ 113 map[interface{}]interface{}{ 114 "port": 3000, 115 }, 116 }, 117 }, nil) 118 119 diff, err := rb.Diff(state, c) 120 if err != nil { 121 t.Fatalf("err: %s", err) 122 } 123 if diff == nil { 124 t.Fatal("should not be nil") 125 } 126 127 actual := testResourceDiffStr(diff) 128 expected := testRBComplexDiff 129 if actual != expected { 130 t.Fatalf("bad: %s", actual) 131 } 132 } 133 134 func TestResourceBuilder_complexReplace(t *testing.T) { 135 rb := &ResourceBuilder{ 136 Attrs: map[string]AttrType{ 137 "listener": AttrTypeUpdate, 138 }, 139 } 140 141 state := &terraform.InstanceState{ 142 ID: "foo", 143 Attributes: map[string]string{ 144 "ignore": "1", 145 "listener.#": "1", 146 "listener.0.port": "80", 147 }, 148 } 149 150 c := testConfig(t, map[string]interface{}{ 151 "listener": []interface{}{ 152 map[interface{}]interface{}{ 153 "value": "50", 154 }, 155 }, 156 }, nil) 157 158 diff, err := rb.Diff(state, c) 159 if err != nil { 160 t.Fatalf("err: %s", err) 161 } 162 if diff == nil { 163 t.Fatal("should not be nil") 164 } 165 166 actual := testResourceDiffStr(diff) 167 expected := testRBComplexReplaceDiff 168 if actual != expected { 169 t.Fatalf("bad: %s", actual) 170 } 171 } 172 173 func TestResourceBuilder_computedAttrsUpdate(t *testing.T) { 174 rb := &ResourceBuilder{ 175 Attrs: map[string]AttrType{ 176 "foo": AttrTypeUpdate, 177 }, 178 ComputedAttrsUpdate: []string{ 179 "bar", 180 }, 181 } 182 183 state := &terraform.InstanceState{ 184 Attributes: map[string]string{"foo": "foo"}, 185 } 186 c := testConfig(t, map[string]interface{}{ 187 "foo": "bar", 188 }, nil) 189 190 diff, err := rb.Diff(state, c) 191 if err != nil { 192 t.Fatalf("err: %s", err) 193 } 194 if diff == nil { 195 t.Fatal("diff shold not be nil") 196 } 197 198 actual := testResourceDiffStr(diff) 199 expected := testRBComputedAttrUpdate 200 if actual != expected { 201 t.Fatalf("bad: %s", actual) 202 } 203 } 204 205 func TestResourceBuilder_new(t *testing.T) { 206 rb := &ResourceBuilder{ 207 Attrs: map[string]AttrType{ 208 "foo": AttrTypeUpdate, 209 }, 210 ComputedAttrs: []string{"private_ip"}, 211 } 212 213 state := &terraform.InstanceState{} 214 215 c := testConfig(t, map[string]interface{}{ 216 "foo": "bar", 217 }, nil) 218 219 diff, err := rb.Diff(state, c) 220 if err != nil { 221 t.Fatalf("err: %s", err) 222 } 223 if diff == nil { 224 t.Fatal("should not be nil") 225 } 226 227 actual := testResourceDiffStr(diff) 228 expected := testRBNewDiff 229 if actual != expected { 230 t.Fatalf("bad: %s", actual) 231 } 232 } 233 234 func TestResourceBuilder_preProcess(t *testing.T) { 235 rb := &ResourceBuilder{ 236 Attrs: map[string]AttrType{ 237 "foo": AttrTypeCreate, 238 }, 239 240 PreProcess: map[string]PreProcessFunc{ 241 "foo": func(v string) string { 242 return "bar" + v 243 }, 244 }, 245 } 246 247 state := &terraform.InstanceState{} 248 c := testConfig(t, map[string]interface{}{ 249 "foo": "foo", 250 }, nil) 251 252 diff, err := rb.Diff(state, c) 253 if err != nil { 254 t.Fatalf("err: %s", err) 255 } 256 if diff == nil { 257 t.Fatal("diff shold not be nil") 258 } 259 260 actual := testResourceDiffStr(diff) 261 expected := testRBPreProcessDiff 262 if actual != expected { 263 t.Fatalf("bad: %s", actual) 264 } 265 266 actual = diff.Attributes["foo"].NewExtra.(string) 267 expected = "foo" 268 if actual != expected { 269 t.Fatalf("bad: %#v", actual) 270 } 271 } 272 273 func TestResourceBuilder_preProcessUnknown(t *testing.T) { 274 rb := &ResourceBuilder{ 275 Attrs: map[string]AttrType{ 276 "foo": AttrTypeCreate, 277 }, 278 279 PreProcess: map[string]PreProcessFunc{ 280 "foo": func(string) string { 281 return "bar" 282 }, 283 }, 284 } 285 286 state := &terraform.InstanceState{} 287 c := testConfig(t, map[string]interface{}{ 288 "foo": "${var.unknown}", 289 }, map[string]string{ 290 "var.unknown": config.UnknownVariableValue, 291 }) 292 293 diff, err := rb.Diff(state, c) 294 if err != nil { 295 t.Fatalf("err: %s", err) 296 } 297 if diff == nil { 298 t.Fatal("diff shold not be nil") 299 } 300 301 actual := testResourceDiffStr(diff) 302 expected := testRBPreProcessUnknownDiff 303 if actual != expected { 304 t.Fatalf("bad: %s", actual) 305 } 306 } 307 308 func TestResourceBuilder_requiresNew(t *testing.T) { 309 rb := &ResourceBuilder{ 310 ComputedAttrs: []string{"private_ip"}, 311 Attrs: map[string]AttrType{ 312 "ami": AttrTypeCreate, 313 }, 314 } 315 316 state := &terraform.InstanceState{ 317 ID: "1", 318 Attributes: map[string]string{ 319 "ami": "foo", 320 "private_ip": "127.0.0.1", 321 }, 322 } 323 324 c := testConfig(t, map[string]interface{}{ 325 "ami": "bar", 326 }, nil) 327 328 diff, err := rb.Diff(state, c) 329 if err != nil { 330 t.Fatalf("err: %s", err) 331 } 332 if diff == nil { 333 t.Fatal("should not be nil") 334 } 335 336 actual := testResourceDiffStr(diff) 337 expected := testRBRequiresNewDiff 338 if actual != expected { 339 t.Fatalf("bad: %s", actual) 340 } 341 } 342 343 func TestResourceBuilder_same(t *testing.T) { 344 rb := &ResourceBuilder{ 345 ComputedAttrs: []string{"private_ip"}, 346 } 347 348 state := &terraform.InstanceState{ 349 ID: "1", 350 Attributes: map[string]string{ 351 "foo": "bar", 352 }, 353 } 354 355 c := testConfig(t, map[string]interface{}{ 356 "foo": "bar", 357 }, nil) 358 359 diff, err := rb.Diff(state, c) 360 if err != nil { 361 t.Fatalf("err: %s", err) 362 } 363 if diff != nil { 364 t.Fatalf("should not diff: %#v", diff) 365 } 366 } 367 368 func TestResourceBuilder_unknown(t *testing.T) { 369 rb := &ResourceBuilder{ 370 Attrs: map[string]AttrType{ 371 "foo": AttrTypeUpdate, 372 }, 373 } 374 375 state := &terraform.InstanceState{} 376 377 c := testConfig(t, map[string]interface{}{ 378 "foo": "${var.unknown}", 379 }, map[string]string{ 380 "var.foo": "bar", 381 "var.unknown": config.UnknownVariableValue, 382 }) 383 384 diff, err := rb.Diff(state, c) 385 if err != nil { 386 t.Fatalf("err: %s", err) 387 } 388 if diff == nil { 389 t.Fatal("should not be nil") 390 } 391 392 actual := testResourceDiffStr(diff) 393 expected := testRBUnknownDiff 394 if actual != expected { 395 t.Fatalf("bad: %s", actual) 396 } 397 } 398 399 func TestResourceBuilder_vars(t *testing.T) { 400 rb := &ResourceBuilder{ 401 Attrs: map[string]AttrType{ 402 "foo": AttrTypeUpdate, 403 }, 404 } 405 406 state := &terraform.InstanceState{} 407 408 c := testConfig(t, map[string]interface{}{ 409 "foo": "${var.foo}", 410 }, map[string]string{ 411 "var.foo": "bar", 412 }) 413 414 diff, err := rb.Diff(state, c) 415 if err != nil { 416 t.Fatalf("err: %s", err) 417 } 418 if diff == nil { 419 t.Fatal("should not be nil") 420 } 421 422 actual := testResourceDiffStr(diff) 423 expected := testRBVarsDiff 424 if actual != expected { 425 t.Fatalf("bad: %s", actual) 426 } 427 } 428 429 const testRBAttrSetComputedDiff = `CREATE 430 IN foo: "" => "bar" (forces new resource) 431 ` 432 433 const testRBComplexDiff = `UPDATE 434 IN listener.0.port: "80" => "3000" 435 ` 436 437 const testRBComplexReplaceDiff = `UPDATE 438 IN listener.0.port: "80" => "<removed>" 439 IN listener.0.value: "" => "50" 440 ` 441 442 const testRBComputedAttrUpdate = `UPDATE 443 OUT bar: "" => "<computed>" 444 IN foo: "foo" => "bar" 445 ` 446 447 const testRBNewDiff = `UPDATE 448 IN foo: "" => "bar" 449 OUT private_ip: "" => "<computed>" 450 ` 451 452 const testRBPreProcessDiff = `CREATE 453 IN foo: "" => "barfoo" (forces new resource) 454 ` 455 456 const testRBPreProcessUnknownDiff = `CREATE 457 IN foo: "" => "${var.unknown}" (forces new resource) 458 ` 459 460 const testRBRequiresNewDiff = `CREATE 461 IN ami: "foo" => "bar" (forces new resource) 462 OUT private_ip: "127.0.0.1" => "<computed>" 463 ` 464 465 const testRBUnknownDiff = `UPDATE 466 IN foo: "" => "${var.unknown}" 467 ` 468 469 const testRBVarsDiff = `UPDATE 470 IN foo: "" => "bar" 471 `