github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/vault/resource_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  	"github.com/hashicorp/vault/api"
    11  )
    12  
    13  func TestResourceGenericSecret(t *testing.T) {
    14  	r.Test(t, r.TestCase{
    15  		Providers: testProviders,
    16  		PreCheck:  func() { testAccPreCheck(t) },
    17  		Steps: []r.TestStep{
    18  			r.TestStep{
    19  				Config: testResourceGenericSecret_initialConfig,
    20  				Check:  testResourceGenericSecret_initialCheck,
    21  			},
    22  			r.TestStep{
    23  				Config: testResourceGenericSecret_updateConfig,
    24  				Check:  testResourceGenericSecret_updateCheck,
    25  			},
    26  		},
    27  	})
    28  }
    29  
    30  var testResourceGenericSecret_initialConfig = `
    31  
    32  resource "vault_generic_secret" "test" {
    33      path = "secret/foo"
    34      allow_read = true
    35      data_json = <<EOT
    36  {
    37      "zip": "zap"
    38  }
    39  EOT
    40  }
    41  
    42  `
    43  
    44  func testResourceGenericSecret_initialCheck(s *terraform.State) error {
    45  	resourceState := s.Modules[0].Resources["vault_generic_secret.test"]
    46  	if resourceState == nil {
    47  		return fmt.Errorf("resource not found in state")
    48  	}
    49  
    50  	instanceState := resourceState.Primary
    51  	if instanceState == nil {
    52  		return fmt.Errorf("resource has no primary instance")
    53  	}
    54  
    55  	path := instanceState.ID
    56  
    57  	if path != instanceState.Attributes["path"] {
    58  		return fmt.Errorf("id doesn't match path")
    59  	}
    60  	if path != "secret/foo" {
    61  		return fmt.Errorf("unexpected secret path")
    62  	}
    63  
    64  	client := testProvider.Meta().(*api.Client)
    65  	secret, err := client.Logical().Read(path)
    66  	if err != nil {
    67  		return fmt.Errorf("error reading back secret: %s", err)
    68  	}
    69  
    70  	if got, want := secret.Data["zip"], "zap"; got != want {
    71  		return fmt.Errorf("'zip' data is %q; want %q", got, want)
    72  	}
    73  
    74  	return nil
    75  }
    76  
    77  var testResourceGenericSecret_updateConfig = `
    78  
    79  resource "vault_generic_secret" "test" {
    80      path = "secret/foo"
    81      allow_read = true
    82      data_json = <<EOT
    83  {
    84      "zip": "zoop"
    85  }
    86  EOT
    87  }
    88  
    89  `
    90  
    91  func testResourceGenericSecret_updateCheck(s *terraform.State) error {
    92  	resourceState := s.Modules[0].Resources["vault_generic_secret.test"]
    93  	instanceState := resourceState.Primary
    94  
    95  	path := instanceState.ID
    96  
    97  	client := testProvider.Meta().(*api.Client)
    98  	secret, err := client.Logical().Read(path)
    99  	if err != nil {
   100  		return fmt.Errorf("error reading back secret: %s", err)
   101  	}
   102  
   103  	if got, want := secret.Data["zip"], "zoop"; got != want {
   104  		return fmt.Errorf("'zip' data is %q; want %q", got, want)
   105  	}
   106  
   107  	return nil
   108  }