github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/terraform/data_source_state_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestState_basic(t *testing.T) {
    12  	resource.UnitTest(t, resource.TestCase{
    13  		PreCheck:  func() { testAccPreCheck(t) },
    14  		Providers: testAccProviders,
    15  		Steps: []resource.TestStep{
    16  			{
    17  				Config: testAccState_basic,
    18  				Check: resource.ComposeTestCheckFunc(
    19  					testAccCheckStateValue(
    20  						"data.terraform_remote_state.foo", "foo", "bar"),
    21  				),
    22  			},
    23  		},
    24  	})
    25  }
    26  
    27  func TestState_complexOutputs(t *testing.T) {
    28  	resource.UnitTest(t, resource.TestCase{
    29  		PreCheck:  func() { testAccPreCheck(t) },
    30  		Providers: testAccProviders,
    31  		Steps: []resource.TestStep{
    32  			{
    33  				Config: testAccState_complexOutputs,
    34  				Check: resource.ComposeTestCheckFunc(
    35  					testAccCheckStateValue("terraform_remote_state.foo", "backend", "local"),
    36  					testAccCheckStateValue("terraform_remote_state.foo", "config.path", "./test-fixtures/complex_outputs.tfstate"),
    37  					testAccCheckStateValue("terraform_remote_state.foo", "computed_set.#", "2"),
    38  					testAccCheckStateValue("terraform_remote_state.foo", `map.%`, "2"),
    39  					testAccCheckStateValue("terraform_remote_state.foo", `map.key`, "test"),
    40  				),
    41  			},
    42  		},
    43  	})
    44  }
    45  
    46  func testAccCheckStateValue(id, name, value string) resource.TestCheckFunc {
    47  	return func(s *terraform.State) error {
    48  		rs, ok := s.RootModule().Resources[id]
    49  		if !ok {
    50  			return fmt.Errorf("Not found: %s", id)
    51  		}
    52  		if rs.Primary.ID == "" {
    53  			return fmt.Errorf("No ID is set")
    54  		}
    55  
    56  		v := rs.Primary.Attributes[name]
    57  		if v != value {
    58  			return fmt.Errorf(
    59  				"Value for %s is %s, not %s", name, v, value)
    60  		}
    61  
    62  		return nil
    63  	}
    64  }
    65  
    66  const testAccState_basic = `
    67  data "terraform_remote_state" "foo" {
    68  	backend = "local"
    69  
    70  	config {
    71  		path = "./test-fixtures/basic.tfstate"
    72  	}
    73  }`
    74  
    75  const testAccState_complexOutputs = `
    76  resource "terraform_remote_state" "foo" {
    77  	backend = "local"
    78  
    79  	config {
    80  		path = "./test-fixtures/complex_outputs.tfstate"
    81  	}
    82  }`