github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 testAccCheckStateValue(id, name, value string) resource.TestCheckFunc { 67 return func(s *terraform.State) error { 68 rs, ok := s.RootModule().Resources[id] 69 if !ok { 70 return fmt.Errorf("Not found: %s", id) 71 } 72 if rs.Primary.ID == "" { 73 return fmt.Errorf("No ID is set") 74 } 75 76 v := rs.Primary.Attributes[name] 77 if v != value { 78 return fmt.Errorf( 79 "Value for %s is %s, not %s", name, v, value) 80 } 81 82 return nil 83 } 84 } 85 86 const testAccState_basic = ` 87 data "terraform_remote_state" "foo" { 88 backend = "local" 89 90 config { 91 path = "./test-fixtures/basic.tfstate" 92 } 93 }` 94 95 const testAccState_backend = ` 96 data "terraform_remote_state" "foo" { 97 backend = "_ds_test" 98 99 config { 100 path = "./test-fixtures/basic.tfstate" 101 } 102 }` 103 104 const testAccState_complexOutputs = ` 105 resource "terraform_remote_state" "foo" { 106 backend = "local" 107 108 config { 109 path = "./test-fixtures/complex_outputs.tfstate" 110 } 111 }`