github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/vault/data_source_generic_secret_test.go (about)

     1  package vault
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	r "github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestDataSourceGenericSecret(t *testing.T) {
    12  	r.Test(t, r.TestCase{
    13  		Providers: testProviders,
    14  		PreCheck:  func() { testAccPreCheck(t) },
    15  		Steps: []r.TestStep{
    16  			r.TestStep{
    17  				Config: testDataSourceGenericSecret_config,
    18  				Check:  testDataSourceGenericSecret_check,
    19  			},
    20  		},
    21  	})
    22  }
    23  
    24  var testDataSourceGenericSecret_config = `
    25  
    26  resource "vault_generic_secret" "test" {
    27      path = "secret/foo"
    28      data_json = <<EOT
    29  {
    30      "zip": "zap"
    31  }
    32  EOT
    33  }
    34  
    35  data "vault_generic_secret" "test" {
    36      path = "${vault_generic_secret.test.path}"
    37  }
    38  
    39  `
    40  
    41  func testDataSourceGenericSecret_check(s *terraform.State) error {
    42  	resourceState := s.Modules[0].Resources["data.vault_generic_secret.test"]
    43  	if resourceState == nil {
    44  		return fmt.Errorf("resource not found in state %v", s.Modules[0].Resources)
    45  	}
    46  
    47  	iState := resourceState.Primary
    48  	if iState == nil {
    49  		return fmt.Errorf("resource has no primary instance")
    50  	}
    51  
    52  	wantJson := `{"zip":"zap"}`
    53  	if got, want := iState.Attributes["data_json"], wantJson; got != want {
    54  		return fmt.Errorf("data_json contains %s; want %s", got, want)
    55  	}
    56  
    57  	if got, want := iState.Attributes["data.zip"], "zap"; got != want {
    58  		return fmt.Errorf("data[\"zip\"] contains %s; want %s", got, want)
    59  	}
    60  
    61  	return nil
    62  }