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