From fb150ef72f52749a80c380c7f2158c6d436ae97d Mon Sep 17 00:00:00 2001 From: James Nugent Date: Sat, 3 Sep 2016 13:54:34 -0700 Subject: [PATCH] provider/test: Add test of data source count.index This adds a unit test to the test provider that verifies count.index behaves correctly. Although not ideal this is hard to implement as a context test without changing around the (non helper/schema) implementation of the x_data_source. --- builtin/providers/test/data_source.go | 18 ++++++- builtin/providers/test/data_source_test.go | 57 ++++++++++++++++++++++ builtin/providers/test/resource.go | 7 +++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 builtin/providers/test/data_source_test.go diff --git a/builtin/providers/test/data_source.go b/builtin/providers/test/data_source.go index 206b8fb849..0ad5b5ad49 100644 --- a/builtin/providers/test/data_source.go +++ b/builtin/providers/test/data_source.go @@ -11,11 +11,21 @@ func testDataSource() *schema.Resource { Read: testDataSourceRead, Schema: map[string]*schema.Schema{ - "list": &schema.Schema{ + "list": { Type: schema.TypeList, Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, }, + + "input": { + Type: schema.TypeString, + Optional: true, + }, + + "output": { + Type: schema.TypeString, + Computed: true, + }, }, } } @@ -24,5 +34,11 @@ func testDataSourceRead(d *schema.ResourceData, meta interface{}) error { d.SetId(time.Now().UTC().String()) d.Set("list", []interface{}{"one", "two", "three"}) + if input, hasInput := d.GetOk("input"); hasInput { + d.Set("output", input) + } else { + d.Set("output", "some output") + } + return nil } diff --git a/builtin/providers/test/data_source_test.go b/builtin/providers/test/data_source_test.go new file mode 100644 index 0000000000..7a14b58af3 --- /dev/null +++ b/builtin/providers/test/data_source_test.go @@ -0,0 +1,57 @@ +package test + +import ( + "errors" + "strings" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestDataSource_dataSourceCount(t *testing.T) { + resource.UnitTest(t, resource.TestCase{ + Providers: testAccProviders, + CheckDestroy: func(s *terraform.State) error { + return nil + }, + Steps: []resource.TestStep{ + { + Config: strings.TrimSpace(` +data "test_data_source" "test" { + count = 3 + input = "count-${count.index}" +} + +resource "test_resource" "foo" { + required = "yep" + required_map = { + key = "value" + } + + list = ["${data.test_data_source.test.*.output}"] +} + `), + Check: func(s *terraform.State) error { + res, hasRes := s.RootModule().Resources["test_resource.foo"] + if !hasRes { + return errors.New("No test_resource.foo in state") + } + if res.Primary.Attributes["list.#"] != "3" { + return errors.New("Wrong list.#, expected 3") + } + if res.Primary.Attributes["list.0"] != "count-0" { + return errors.New("Wrong list.0, expected count-0") + } + if res.Primary.Attributes["list.1"] != "count-1" { + return errors.New("Wrong list.0, expected count-1") + } + if res.Primary.Attributes["list.2"] != "count-2" { + return errors.New("Wrong list.0, expected count-2") + } + return nil + }, + }, + }, + }) +} diff --git a/builtin/providers/test/resource.go b/builtin/providers/test/resource.go index 2017d7639e..7981497c3c 100644 --- a/builtin/providers/test/resource.go +++ b/builtin/providers/test/resource.go @@ -97,6 +97,13 @@ func testResource() *schema.Resource { Type: schema.TypeMap, Computed: true, }, + "list": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, "list_of_map": { Type: schema.TypeList, Optional: true,