github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/vault/resource_auth_backend_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  	"github.com/hashicorp/vault/api"
    10  )
    11  
    12  func TestResourceAuth(t *testing.T) {
    13  	r.Test(t, r.TestCase{
    14  		Providers: testProviders,
    15  		PreCheck:  func() { testAccPreCheck(t) },
    16  		Steps: []r.TestStep{
    17  			r.TestStep{
    18  				Config: testResourceAuth_initialConfig,
    19  				Check:  testResourceAuth_initialCheck,
    20  			},
    21  			r.TestStep{
    22  				Config: testResourceAuth_updateConfig,
    23  				Check:  testResourceAuth_updateCheck,
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  var testResourceAuth_initialConfig = `
    30  
    31  resource "vault_auth_backend" "test" {
    32  	type = "github"
    33  }
    34  
    35  `
    36  
    37  func testResourceAuth_initialCheck(s *terraform.State) error {
    38  	resourceState := s.Modules[0].Resources["vault_auth_backend.test"]
    39  	if resourceState == nil {
    40  		return fmt.Errorf("resource not found in state")
    41  	}
    42  
    43  	instanceState := resourceState.Primary
    44  	if instanceState == nil {
    45  		return fmt.Errorf("resource has no primary instance")
    46  	}
    47  
    48  	name := instanceState.ID
    49  
    50  	if name != instanceState.Attributes["type"] {
    51  		return fmt.Errorf("id doesn't match name")
    52  	}
    53  
    54  	if name != "github" {
    55  		return fmt.Errorf("unexpected auth name %s", name)
    56  	}
    57  
    58  	client := testProvider.Meta().(*api.Client)
    59  	auths, err := client.Sys().ListAuth()
    60  
    61  	if err != nil {
    62  		return fmt.Errorf("error reading back auth: %s", err)
    63  	}
    64  
    65  	found := false
    66  	for _, auth := range auths {
    67  		if auth.Type == name {
    68  			found = true
    69  			break
    70  		}
    71  	}
    72  
    73  	if !found {
    74  		return fmt.Errorf("could not find auth backend %s in %+v", name, auths)
    75  	}
    76  
    77  	return nil
    78  }
    79  
    80  var testResourceAuth_updateConfig = `
    81  
    82  resource "vault_auth_backend" "test" {
    83  	type = "ldap"
    84  }
    85  
    86  `
    87  
    88  func testResourceAuth_updateCheck(s *terraform.State) error {
    89  	resourceState := s.Modules[0].Resources["vault_auth_backend.test"]
    90  	if resourceState == nil {
    91  		return fmt.Errorf("resource not found in state")
    92  	}
    93  
    94  	instanceState := resourceState.Primary
    95  	if instanceState == nil {
    96  		return fmt.Errorf("resource has no primary instance")
    97  	}
    98  
    99  	name := instanceState.ID
   100  
   101  	if name != instanceState.Attributes["type"] {
   102  		return fmt.Errorf("id doesn't match name")
   103  	}
   104  
   105  	if name != "ldap" {
   106  		return fmt.Errorf("unexpected auth name")
   107  	}
   108  
   109  	client := testProvider.Meta().(*api.Client)
   110  	auths, err := client.Sys().ListAuth()
   111  
   112  	if err != nil {
   113  		return fmt.Errorf("error reading back auth: %s", err)
   114  	}
   115  
   116  	found := false
   117  	for _, auth := range auths {
   118  		if auth.Type == name {
   119  			found = true
   120  			break
   121  		}
   122  	}
   123  
   124  	if !found {
   125  		return fmt.Errorf("could not find auth backend %s in %+v", name, auths)
   126  	}
   127  
   128  	return nil
   129  }