github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/test/resource_test.go (about) 1 package test 2 3 import ( 4 "regexp" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestResource_basic(t *testing.T) { 13 resource.UnitTest(t, resource.TestCase{ 14 Providers: testAccProviders, 15 CheckDestroy: testAccCheckResourceDestroy, 16 Steps: []resource.TestStep{ 17 resource.TestStep{ 18 Config: strings.TrimSpace(` 19 resource "test_resource" "foo" { 20 required = "yep" 21 required_map = { 22 key = "value" 23 } 24 } 25 `), 26 Check: func(s *terraform.State) error { 27 return nil 28 }, 29 }, 30 }, 31 }) 32 } 33 34 // Targeted test in TestContext2Apply_ignoreChangesCreate 35 func TestResource_ignoreChangesRequired(t *testing.T) { 36 resource.UnitTest(t, resource.TestCase{ 37 Providers: testAccProviders, 38 CheckDestroy: testAccCheckResourceDestroy, 39 Steps: []resource.TestStep{ 40 resource.TestStep{ 41 Config: strings.TrimSpace(` 42 resource "test_resource" "foo" { 43 required = "yep" 44 required_map = { 45 key = "value" 46 } 47 lifecycle { 48 ignore_changes = ["required"] 49 } 50 } 51 `), 52 Check: func(s *terraform.State) error { 53 return nil 54 }, 55 }, 56 }, 57 }) 58 } 59 60 func TestResource_ignoreChangesEmpty(t *testing.T) { 61 resource.UnitTest(t, resource.TestCase{ 62 Providers: testAccProviders, 63 CheckDestroy: testAccCheckResourceDestroy, 64 Steps: []resource.TestStep{ 65 resource.TestStep{ 66 Config: strings.TrimSpace(` 67 resource "test_resource" "foo" { 68 required = "yep" 69 required_map = { 70 key = "value" 71 } 72 optional_force_new = "one" 73 lifecycle { 74 ignore_changes = [] 75 } 76 } 77 `), 78 Check: func(s *terraform.State) error { 79 return nil 80 }, 81 }, 82 resource.TestStep{ 83 Config: strings.TrimSpace(` 84 resource "test_resource" "foo" { 85 required = "yep" 86 required_map { 87 key = "value" 88 } 89 optional_force_new = "two" 90 lifecycle { 91 ignore_changes = [] 92 } 93 } 94 `), 95 Check: func(s *terraform.State) error { 96 return nil 97 }, 98 }, 99 }, 100 }) 101 } 102 103 func TestResource_ignoreChangesForceNew(t *testing.T) { 104 resource.UnitTest(t, resource.TestCase{ 105 Providers: testAccProviders, 106 CheckDestroy: testAccCheckResourceDestroy, 107 Steps: []resource.TestStep{ 108 resource.TestStep{ 109 Config: strings.TrimSpace(` 110 resource "test_resource" "foo" { 111 required = "yep" 112 required_map { 113 key = "value" 114 } 115 optional_force_new = "one" 116 lifecycle { 117 ignore_changes = ["optional_force_new"] 118 } 119 } 120 `), 121 Check: func(s *terraform.State) error { 122 return nil 123 }, 124 }, 125 resource.TestStep{ 126 Config: strings.TrimSpace(` 127 resource "test_resource" "foo" { 128 required = "yep" 129 required_map = { 130 key = "value" 131 } 132 optional_force_new = "two" 133 lifecycle { 134 ignore_changes = ["optional_force_new"] 135 } 136 } 137 `), 138 Check: func(s *terraform.State) error { 139 return nil 140 }, 141 }, 142 }, 143 }) 144 } 145 146 // Covers specific scenario in #6005, handled by normalizing boolean strings in 147 // helper/schema 148 func TestResource_ignoreChangesForceNewBoolean(t *testing.T) { 149 resource.UnitTest(t, resource.TestCase{ 150 Providers: testAccProviders, 151 CheckDestroy: testAccCheckResourceDestroy, 152 Steps: []resource.TestStep{ 153 resource.TestStep{ 154 Config: strings.TrimSpace(` 155 resource "test_resource" "foo" { 156 required = "yep" 157 required_map = { 158 key = "value" 159 } 160 optional_force_new = "one" 161 optional_bool = true 162 lifecycle { 163 ignore_changes = ["optional_force_new"] 164 } 165 } 166 `), 167 Check: func(s *terraform.State) error { 168 return nil 169 }, 170 }, 171 resource.TestStep{ 172 Config: strings.TrimSpace(` 173 resource "test_resource" "foo" { 174 required = "yep" 175 required_map = { 176 key = "value" 177 } 178 optional_force_new = "two" 179 optional_bool = true 180 lifecycle { 181 ignore_changes = ["optional_force_new"] 182 } 183 } 184 `), 185 Check: func(s *terraform.State) error { 186 return nil 187 }, 188 }, 189 }, 190 }) 191 } 192 193 // Reproduces plan-time panic described in GH-7170 194 func TestResource_dataSourceListPlanPanic(t *testing.T) { 195 resource.UnitTest(t, resource.TestCase{ 196 Providers: testAccProviders, 197 CheckDestroy: testAccCheckResourceDestroy, 198 Steps: []resource.TestStep{ 199 resource.TestStep{ 200 Config: strings.TrimSpace(` 201 data "test_data_source" "foo" {} 202 resource "test_resource" "foo" { 203 required = "${data.test_data_source.foo.list}" 204 required_map = { 205 key = "value" 206 } 207 } 208 `), 209 ExpectError: regexp.MustCompile(`must be a single value, not a list`), 210 Check: func(s *terraform.State) error { 211 return nil 212 }, 213 }, 214 }, 215 }) 216 } 217 218 // Reproduces apply-time panic described in GH-7170 219 func TestResource_dataSourceListApplyPanic(t *testing.T) { 220 resource.UnitTest(t, resource.TestCase{ 221 Providers: testAccProviders, 222 CheckDestroy: testAccCheckResourceDestroy, 223 Steps: []resource.TestStep{ 224 resource.TestStep{ 225 Config: strings.TrimSpace(` 226 resource "test_resource" "foo" { 227 required = "ok" 228 required_map = { 229 key = "value" 230 } 231 } 232 resource "test_resource" "bar" { 233 required = "${test_resource.foo.computed_list}" 234 required_map = { 235 key = "value" 236 } 237 } 238 `), 239 ExpectError: regexp.MustCompile(`must be a single value, not a list`), 240 Check: func(s *terraform.State) error { 241 return nil 242 }, 243 }, 244 }, 245 }) 246 } 247 248 func TestResource_ignoreChangesMap(t *testing.T) { 249 resource.UnitTest(t, resource.TestCase{ 250 Providers: testAccProviders, 251 CheckDestroy: testAccCheckResourceDestroy, 252 Steps: []resource.TestStep{ 253 resource.TestStep{ 254 Config: strings.TrimSpace(` 255 resource "test_resource" "foo" { 256 required = "yep" 257 required_map = { 258 key = "value" 259 } 260 optional_computed_map { 261 foo = "bar" 262 } 263 lifecycle { 264 ignore_changes = ["optional_computed_map"] 265 } 266 } 267 `), 268 Check: func(s *terraform.State) error { 269 return nil 270 }, 271 }, 272 resource.TestStep{ 273 Config: strings.TrimSpace(` 274 resource "test_resource" "foo" { 275 required = "yep" 276 required_map = { 277 key = "value" 278 } 279 optional_computed_map { 280 foo = "bar" 281 no = "update" 282 } 283 lifecycle { 284 ignore_changes = ["optional_computed_map"] 285 } 286 } 287 `), 288 Check: func(s *terraform.State) error { 289 return nil 290 }, 291 }, 292 }, 293 }) 294 } 295 296 func TestResource_ignoreChangesDependent(t *testing.T) { 297 resource.UnitTest(t, resource.TestCase{ 298 Providers: testAccProviders, 299 CheckDestroy: testAccCheckResourceDestroy, 300 Steps: []resource.TestStep{ 301 resource.TestStep{ 302 Config: strings.TrimSpace(` 303 resource "test_resource" "foo" { 304 count = 2 305 required = "yep" 306 required_map { key = "value" } 307 308 optional_force_new = "one" 309 lifecycle { 310 ignore_changes = ["optional_force_new"] 311 } 312 } 313 resource "test_resource" "bar" { 314 count = 2 315 required = "yep" 316 required_map { key = "value" } 317 optional = "${element(test_resource.foo.*.id, count.index)}" 318 } 319 `), 320 Check: func(s *terraform.State) error { 321 return nil 322 }, 323 }, 324 resource.TestStep{ 325 Config: strings.TrimSpace(` 326 resource "test_resource" "foo" { 327 count = 2 328 required = "yep" 329 required_map { key = "value" } 330 331 optional_force_new = "two" 332 lifecycle { 333 ignore_changes = ["optional_force_new"] 334 } 335 } 336 resource "test_resource" "bar" { 337 count = 2 338 required = "yep" 339 required_map { key = "value" } 340 optional = "${element(test_resource.foo.*.id, count.index)}" 341 } 342 `), 343 Check: func(s *terraform.State) error { 344 return nil 345 }, 346 }, 347 }, 348 }) 349 } 350 351 func TestResource_ignoreChangesStillReplaced(t *testing.T) { 352 resource.UnitTest(t, resource.TestCase{ 353 Providers: testAccProviders, 354 CheckDestroy: testAccCheckResourceDestroy, 355 Steps: []resource.TestStep{ 356 resource.TestStep{ 357 Config: strings.TrimSpace(` 358 resource "test_resource" "foo" { 359 required = "yep" 360 required_map = { 361 key = "value" 362 } 363 optional_force_new = "one" 364 optional_bool = true 365 lifecycle { 366 ignore_changes = ["optional_bool"] 367 } 368 } 369 `), 370 Check: func(s *terraform.State) error { 371 return nil 372 }, 373 }, 374 resource.TestStep{ 375 Config: strings.TrimSpace(` 376 resource "test_resource" "foo" { 377 required = "yep" 378 required_map = { 379 key = "value" 380 } 381 optional_force_new = "two" 382 optional_bool = false 383 lifecycle { 384 ignore_changes = ["optional_bool"] 385 } 386 } 387 `), 388 Check: func(s *terraform.State) error { 389 return nil 390 }, 391 }, 392 }, 393 }) 394 } 395 396 func testAccCheckResourceDestroy(s *terraform.State) error { 397 return nil 398 }