github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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      data_json = <<EOT
    35  {
    36      "zip": "zap"
    37  }
    38  EOT
    39  }
    40  
    41  `
    42  
    43  func testResourceGenericSecret_initialCheck(s *terraform.State) error {
    44  	resourceState := s.Modules[0].Resources["vault_generic_secret.test"]
    45  	if resourceState == nil {
    46  		return fmt.Errorf("resource not found in state")
    47  	}
    48  
    49  	instanceState := resourceState.Primary
    50  	if instanceState == nil {
    51  		return fmt.Errorf("resource has no primary instance")
    52  	}
    53  
    54  	path := instanceState.ID
    55  
    56  	if path != instanceState.Attributes["path"] {
    57  		return fmt.Errorf("id doesn't match path")
    58  	}
    59  	if path != "secret/foo" {
    60  		return fmt.Errorf("unexpected secret path")
    61  	}
    62  
    63  	client := testProvider.Meta().(*api.Client)
    64  	secret, err := client.Logical().Read(path)
    65  	if err != nil {
    66  		return fmt.Errorf("error reading back secret: %s", err)
    67  	}
    68  
    69  	if got, want := secret.Data["zip"], "zap"; got != want {
    70  		return fmt.Errorf("'zip' data is %q; want %q", got, want)
    71  	}
    72  
    73  	return nil
    74  }
    75  
    76  var testResourceGenericSecret_updateConfig = `
    77  
    78  resource "vault_generic_secret" "test" {
    79      path = "secret/foo"
    80      data_json = <<EOT
    81  {
    82      "zip": "zoop"
    83  }
    84  EOT
    85  }
    86  
    87  `
    88  
    89  func testResourceGenericSecret_updateCheck(s *terraform.State) error {
    90  	resourceState := s.Modules[0].Resources["vault_generic_secret.test"]
    91  	instanceState := resourceState.Primary
    92  
    93  	path := instanceState.ID
    94  
    95  	client := testProvider.Meta().(*api.Client)
    96  	secret, err := client.Logical().Read(path)
    97  	if err != nil {
    98  		return fmt.Errorf("error reading back secret: %s", err)
    99  	}
   100  
   101  	if got, want := secret.Data["zip"], "zoop"; got != want {
   102  		return fmt.Errorf("'zip' data is %q; want %q", got, want)
   103  	}
   104  
   105  	return nil
   106  }