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