github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/test/data_source_test.go (about)

     1  package test
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestDataSource_dataSourceCount(t *testing.T) {
    13  	resource.UnitTest(t, resource.TestCase{
    14  		Providers: testAccProviders,
    15  		CheckDestroy: func(s *terraform.State) error {
    16  			return nil
    17  		},
    18  		Steps: []resource.TestStep{
    19  			{
    20  				Config: strings.TrimSpace(`
    21  data "test_data_source" "test" {
    22    count = 3
    23    input = "count-${count.index}"
    24  }
    25  
    26  resource "test_resource" "foo" {
    27    required     = "yep"
    28    required_map = {
    29      key = "value"
    30    }
    31  
    32    list = ["${data.test_data_source.test.*.output}"]
    33  }
    34  				`),
    35  				Check: func(s *terraform.State) error {
    36  					res, hasRes := s.RootModule().Resources["test_resource.foo"]
    37  					if !hasRes {
    38  						return errors.New("No test_resource.foo in state")
    39  					}
    40  					if res.Primary.Attributes["list.#"] != "3" {
    41  						return errors.New("Wrong list.#, expected 3")
    42  					}
    43  					if res.Primary.Attributes["list.0"] != "count-0" {
    44  						return errors.New("Wrong list.0, expected count-0")
    45  					}
    46  					if res.Primary.Attributes["list.1"] != "count-1" {
    47  						return errors.New("Wrong list.0, expected count-1")
    48  					}
    49  					if res.Primary.Attributes["list.2"] != "count-2" {
    50  						return errors.New("Wrong list.0, expected count-2")
    51  					}
    52  					return nil
    53  				},
    54  			},
    55  		},
    56  	})
    57  }