github.com/peterbale/terraform@v0.9.0-beta2.0.20170315142748-5723acd55547/builtin/providers/test/data_source_test.go (about)

     1  package test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestDataSource_dataSourceCount(t *testing.T) {
    14  	resource.UnitTest(t, resource.TestCase{
    15  		Providers: testAccProviders,
    16  		CheckDestroy: func(s *terraform.State) error {
    17  			return nil
    18  		},
    19  		Steps: []resource.TestStep{
    20  			{
    21  				Config: strings.TrimSpace(`
    22  data "test_data_source" "test" {
    23    count = 3
    24    input = "count-${count.index}"
    25  }
    26  
    27  resource "test_resource" "foo" {
    28    required     = "yep"
    29    required_map = {
    30      key = "value"
    31    }
    32  
    33    list = ["${data.test_data_source.test.*.output}"]
    34  }
    35  				`),
    36  				Check: func(s *terraform.State) error {
    37  					res, hasRes := s.RootModule().Resources["test_resource.foo"]
    38  					if !hasRes {
    39  						return errors.New("No test_resource.foo in state")
    40  					}
    41  					if res.Primary.Attributes["list.#"] != "3" {
    42  						return errors.New("Wrong list.#, expected 3")
    43  					}
    44  					if res.Primary.Attributes["list.0"] != "count-0" {
    45  						return errors.New("Wrong list.0, expected count-0")
    46  					}
    47  					if res.Primary.Attributes["list.1"] != "count-1" {
    48  						return errors.New("Wrong list.0, expected count-1")
    49  					}
    50  					if res.Primary.Attributes["list.2"] != "count-2" {
    51  						return errors.New("Wrong list.0, expected count-2")
    52  					}
    53  					return nil
    54  				},
    55  			},
    56  		},
    57  	})
    58  }
    59  
    60  // Test that the output of a data source can be used as the value for
    61  // a "count" in a real resource. This would fail with "count cannot be computed"
    62  // at some point.
    63  func TestDataSource_valueAsResourceCount(t *testing.T) {
    64  	resource.UnitTest(t, resource.TestCase{
    65  		Providers: testAccProviders,
    66  		CheckDestroy: func(s *terraform.State) error {
    67  			return nil
    68  		},
    69  		Steps: []resource.TestStep{
    70  			{
    71  				Config: strings.TrimSpace(`
    72  data "test_data_source" "test" {
    73    input = "4"
    74  }
    75  
    76  resource "test_resource" "foo" {
    77    count = "${data.test_data_source.test.output}"
    78  
    79    required     = "yep"
    80    required_map = {
    81      key = "value"
    82    }
    83  }
    84  				`),
    85  				Check: func(s *terraform.State) error {
    86  					count := 0
    87  					for k, _ := range s.RootModule().Resources {
    88  						if strings.HasPrefix(k, "test_resource.foo.") {
    89  							count++
    90  						}
    91  					}
    92  
    93  					if count != 4 {
    94  						return fmt.Errorf("bad count: %d", count)
    95  					}
    96  					return nil
    97  				},
    98  			},
    99  		},
   100  	})
   101  }