github.com/lymingtonprecision/terraform@v0.9.9-0.20170613092852-62acef9611a9/builtin/providers/test/splat_flatten_test.go (about)

     1  package test
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  // This is actually a test of some core functionality in conjunction with
    12  // helper/schema, rather than of the test provider itself.
    13  //
    14  // Here we're just verifying that unknown splats get flattened when assigned
    15  // to list and set attributes. A variety of other situations are tested in
    16  // an apply context test in the core package, but for this part we lean on
    17  // helper/schema and thus need to exercise it at a higher level.
    18  
    19  func TestSplatFlatten(t *testing.T) {
    20  	resource.UnitTest(t, resource.TestCase{
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckResourceDestroy,
    23  		Steps: []resource.TestStep{
    24  			resource.TestStep{
    25  				Config: `
    26  resource "test_resource" "source" {
    27  	required = "foo ${count.index}"
    28  	required_map = {
    29  	    key = "value"
    30  	}
    31  	count = 3
    32  }
    33  
    34  resource "test_resource" "splatted" {
    35  	# This legacy form of splatting into a list is still supported for
    36  	# backward-compatibility but no longer suggested.
    37  	set = ["${test_resource.source.*.computed_from_required}"]
    38  	list = ["${test_resource.source.*.computed_from_required}"]
    39  
    40  	required = "yep"
    41  	required_map = {
    42  	    key = "value"
    43  	}
    44  }
    45  				`,
    46  				Check: func(s *terraform.State) error {
    47  					gotAttrs := s.RootModule().Resources["test_resource.splatted"].Primary.Attributes
    48  					t.Logf("attrs %#v", gotAttrs)
    49  					wantAttrs := map[string]string{
    50  						"list.#": "3",
    51  						"list.0": "foo 0",
    52  						"list.1": "foo 1",
    53  						"list.2": "foo 2",
    54  
    55  						// This depends on the default set hash implementation.
    56  						// If that changes, these keys will need to be updated.
    57  						"set.#":          "3",
    58  						"set.1136855734": "foo 0",
    59  						"set.885275168":  "foo 1",
    60  						"set.2915920794": "foo 2",
    61  					}
    62  					errored := false
    63  					for k, want := range wantAttrs {
    64  						got := gotAttrs[k]
    65  						if got != want {
    66  							t.Errorf("Wrong %s value %q; want %q", k, got, want)
    67  							errored = true
    68  						}
    69  					}
    70  					if errored {
    71  						return errors.New("incorrect attribute values")
    72  					}
    73  					return nil
    74  				},
    75  			},
    76  		},
    77  	})
    78  
    79  }