github.com/handlerbot/terraform@v0.10.0-beta1.0.20180726153736-26b68d98f9cb/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  // outputs should never have a null value, but don't crash if we ever encounter
    67  // them.
    68  func TestState_nullOutputs(t *testing.T) {
    69  	resource.UnitTest(t, resource.TestCase{
    70  		PreCheck:  func() { testAccPreCheck(t) },
    71  		Providers: testAccProviders,
    72  		Steps: []resource.TestStep{
    73  			{
    74  				Config: testAccState_nullOutputs,
    75  			},
    76  		},
    77  	})
    78  }
    79  
    80  func TestEmptyState_defaults(t *testing.T) {
    81  	resource.UnitTest(t, resource.TestCase{
    82  		PreCheck:  func() { testAccPreCheck(t) },
    83  		Providers: testAccProviders,
    84  		Steps: []resource.TestStep{
    85  			{
    86  				Config: testAccEmptyState_defaults,
    87  				Check: resource.ComposeTestCheckFunc(
    88  					testAccCheckStateValue(
    89  						"data.terraform_remote_state.foo", "foo", "bar"),
    90  				),
    91  			},
    92  		},
    93  	})
    94  }
    95  
    96  func TestState_defaults(t *testing.T) {
    97  	resource.UnitTest(t, resource.TestCase{
    98  		PreCheck:  func() { testAccPreCheck(t) },
    99  		Providers: testAccProviders,
   100  		Steps: []resource.TestStep{
   101  			{
   102  				Config: testAccEmptyState_defaults,
   103  				Check: resource.ComposeTestCheckFunc(
   104  					testAccCheckStateValue(
   105  						"data.terraform_remote_state.foo", "foo", "bar"),
   106  				),
   107  			},
   108  		},
   109  	})
   110  }
   111  
   112  func testAccCheckStateValue(id, name, value string) resource.TestCheckFunc {
   113  	return func(s *terraform.State) error {
   114  		rs, ok := s.RootModule().Resources[id]
   115  		if !ok {
   116  			return fmt.Errorf("Not found: %s", id)
   117  		}
   118  		if rs.Primary.ID == "" {
   119  			return fmt.Errorf("No ID is set")
   120  		}
   121  
   122  		v := rs.Primary.Attributes[name]
   123  		if v != value {
   124  			return fmt.Errorf(
   125  				"Value for %s is %s, not %s", name, v, value)
   126  		}
   127  
   128  		return nil
   129  	}
   130  }
   131  
   132  // make sure that the deprecated environment field isn't overridden by the
   133  // default value for workspace.
   134  func TestState_deprecatedEnvironment(t *testing.T) {
   135  	resource.UnitTest(t, resource.TestCase{
   136  		PreCheck:  func() { testAccPreCheck(t) },
   137  		Providers: testAccProviders,
   138  		Steps: []resource.TestStep{
   139  			{
   140  				Config: testAccState_deprecatedEnvironment,
   141  				Check: resource.ComposeTestCheckFunc(
   142  					testAccCheckStateValue(
   143  						// if the workspace default value overrides the
   144  						// environment, this will get the foo value from the
   145  						// default state.
   146  						"data.terraform_remote_state.foo", "foo", ""),
   147  				),
   148  			},
   149  		},
   150  	})
   151  }
   152  
   153  const testAccState_basic = `
   154  data "terraform_remote_state" "foo" {
   155  	backend = "local"
   156  
   157  	config {
   158  		path = "./test-fixtures/basic.tfstate"
   159  	}
   160  }`
   161  
   162  const testAccState_backend = `
   163  data "terraform_remote_state" "foo" {
   164  	backend = "_ds_test"
   165  
   166  	config {
   167  		path = "./test-fixtures/basic.tfstate"
   168  	}
   169  }`
   170  
   171  const testAccState_complexOutputs = `
   172  resource "terraform_remote_state" "foo" {
   173  	backend = "local"
   174  
   175  	config {
   176  		path = "./test-fixtures/complex_outputs.tfstate"
   177  	}
   178  }`
   179  
   180  const testAccState_nullOutputs = `
   181  resource "terraform_remote_state" "foo" {
   182  	backend = "local"
   183  
   184  	config {
   185  		path = "./test-fixtures/null_outputs.tfstate"
   186  	}
   187  }`
   188  
   189  const testAccEmptyState_defaults = `
   190  data "terraform_remote_state" "foo" {
   191  	backend = "local"
   192  
   193  	config {
   194  		path = "./test-fixtures/empty.tfstate"
   195  	}
   196  
   197  	defaults {
   198  		foo = "bar"
   199  	}
   200  }`
   201  
   202  const testAccState_defaults = `
   203  data "terraform_remote_state" "foo" {
   204  	backend = "local"
   205  
   206  	config {
   207  		path = "./test-fixtures/basic.tfstate"
   208  	}
   209  
   210  	defaults {
   211  		foo = "not bar"
   212  	}
   213  }`
   214  
   215  const testAccState_deprecatedEnvironment = `
   216  data "terraform_remote_state" "foo" {
   217  	backend = "local"
   218  	environment = "deprecated"
   219  
   220  	config {
   221  		path = "./test-fixtures/basic.tfstate"
   222  	}
   223  }`