github.com/hooklift/terraform@v0.11.0-beta1.0.20171117000744-6786c1361ffe/builtin/providers/terraform/data_source_state_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	backendinit "github.com/hashicorp/terraform/backend/init"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestState_basic(t *testing.T) {
    13  	resource.UnitTest(t, resource.TestCase{
    14  		PreCheck:  func() { testAccPreCheck(t) },
    15  		Providers: testAccProviders,
    16  		Steps: []resource.TestStep{
    17  			{
    18  				Config: testAccState_basic,
    19  				Check: resource.ComposeTestCheckFunc(
    20  					testAccCheckStateValue(
    21  						"data.terraform_remote_state.foo", "foo", "bar"),
    22  				),
    23  			},
    24  		},
    25  	})
    26  }
    27  
    28  func TestState_backends(t *testing.T) {
    29  	backendinit.Set("_ds_test", backendinit.Backend("local"))
    30  	defer backendinit.Set("_ds_test", nil)
    31  
    32  	resource.UnitTest(t, resource.TestCase{
    33  		PreCheck:  func() { testAccPreCheck(t) },
    34  		Providers: testAccProviders,
    35  		Steps: []resource.TestStep{
    36  			{
    37  				Config: testAccState_backend,
    38  				Check: resource.ComposeTestCheckFunc(
    39  					testAccCheckStateValue(
    40  						"data.terraform_remote_state.foo", "foo", "bar"),
    41  				),
    42  			},
    43  		},
    44  	})
    45  }
    46  
    47  func TestState_complexOutputs(t *testing.T) {
    48  	resource.UnitTest(t, resource.TestCase{
    49  		PreCheck:  func() { testAccPreCheck(t) },
    50  		Providers: testAccProviders,
    51  		Steps: []resource.TestStep{
    52  			{
    53  				Config: testAccState_complexOutputs,
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testAccCheckStateValue("terraform_remote_state.foo", "backend", "local"),
    56  					testAccCheckStateValue("terraform_remote_state.foo", "config.path", "./test-fixtures/complex_outputs.tfstate"),
    57  					testAccCheckStateValue("terraform_remote_state.foo", "computed_set.#", "2"),
    58  					testAccCheckStateValue("terraform_remote_state.foo", `map.%`, "2"),
    59  					testAccCheckStateValue("terraform_remote_state.foo", `map.key`, "test"),
    60  				),
    61  			},
    62  		},
    63  	})
    64  }
    65  
    66  func TestEmptyState_defaults(t *testing.T) {
    67  	resource.UnitTest(t, resource.TestCase{
    68  		PreCheck:  func() { testAccPreCheck(t) },
    69  		Providers: testAccProviders,
    70  		Steps: []resource.TestStep{
    71  			{
    72  				Config: testAccEmptyState_defaults,
    73  				Check: resource.ComposeTestCheckFunc(
    74  					testAccCheckStateValue(
    75  						"data.terraform_remote_state.foo", "foo", "bar"),
    76  				),
    77  			},
    78  		},
    79  	})
    80  }
    81  
    82  func TestState_defaults(t *testing.T) {
    83  	resource.UnitTest(t, resource.TestCase{
    84  		PreCheck:  func() { testAccPreCheck(t) },
    85  		Providers: testAccProviders,
    86  		Steps: []resource.TestStep{
    87  			{
    88  				Config: testAccEmptyState_defaults,
    89  				Check: resource.ComposeTestCheckFunc(
    90  					testAccCheckStateValue(
    91  						"data.terraform_remote_state.foo", "foo", "bar"),
    92  				),
    93  			},
    94  		},
    95  	})
    96  }
    97  
    98  func testAccCheckStateValue(id, name, value string) resource.TestCheckFunc {
    99  	return func(s *terraform.State) error {
   100  		rs, ok := s.RootModule().Resources[id]
   101  		if !ok {
   102  			return fmt.Errorf("Not found: %s", id)
   103  		}
   104  		if rs.Primary.ID == "" {
   105  			return fmt.Errorf("No ID is set")
   106  		}
   107  
   108  		v := rs.Primary.Attributes[name]
   109  		if v != value {
   110  			return fmt.Errorf(
   111  				"Value for %s is %s, not %s", name, v, value)
   112  		}
   113  
   114  		return nil
   115  	}
   116  }
   117  
   118  const testAccState_basic = `
   119  data "terraform_remote_state" "foo" {
   120  	backend = "local"
   121  
   122  	config {
   123  		path = "./test-fixtures/basic.tfstate"
   124  	}
   125  }`
   126  
   127  const testAccState_backend = `
   128  data "terraform_remote_state" "foo" {
   129  	backend = "_ds_test"
   130  
   131  	config {
   132  		path = "./test-fixtures/basic.tfstate"
   133  	}
   134  }`
   135  
   136  const testAccState_complexOutputs = `
   137  resource "terraform_remote_state" "foo" {
   138  	backend = "local"
   139  
   140  	config {
   141  		path = "./test-fixtures/complex_outputs.tfstate"
   142  	}
   143  }`
   144  
   145  const testAccEmptyState_defaults = `
   146  data "terraform_remote_state" "foo" {
   147  	backend = "local"
   148  
   149  	config {
   150  		path = "./test-fixtures/empty.tfstate"
   151  	}
   152  
   153  	defaults {
   154  		foo = "bar"
   155  	}
   156  }`
   157  
   158  const testAccState_defaults = `
   159  data "terraform_remote_state" "foo" {
   160  	backend = "local"
   161  
   162  	config {
   163  		path = "./test-fixtures/basic.tfstate"
   164  	}
   165  
   166  	defaults {
   167  		foo = "not bar"
   168  	}
   169  }`