github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/vault/resource_mount_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 TestResourceMount(t *testing.T) {
    13  	r.Test(t, r.TestCase{
    14  		Providers: testProviders,
    15  		PreCheck:  func() { testAccPreCheck(t) },
    16  		Steps: []r.TestStep{
    17  			{
    18  				Config: testResourceMount_initialConfig,
    19  				Check:  testResourceMount_initialCheck,
    20  			},
    21  			{
    22  				Config: testResourceMount_updateConfig,
    23  				Check:  testResourceMount_updateCheck,
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  var testResourceMount_initialConfig = `
    30  
    31  resource "vault_mount" "test" {
    32  	path = "example"
    33  	type = "generic"
    34  	description = "Example mount for testing"
    35  	default_lease_ttl_seconds = 3600
    36  	max_lease_ttl_seconds = 36000
    37  }
    38  
    39  `
    40  
    41  func testResourceMount_initialCheck(s *terraform.State) error {
    42  	resourceState := s.Modules[0].Resources["vault_mount.test"]
    43  	if resourceState == nil {
    44  		return fmt.Errorf("resource not found in state")
    45  	}
    46  
    47  	instanceState := resourceState.Primary
    48  	if instanceState == nil {
    49  		return fmt.Errorf("resource has no primary instance")
    50  	}
    51  
    52  	path := instanceState.ID
    53  
    54  	if path != instanceState.Attributes["path"] {
    55  		return fmt.Errorf("id doesn't match path")
    56  	}
    57  
    58  	if path != "example" {
    59  		return fmt.Errorf("unexpected path value")
    60  	}
    61  
    62  	mount, err := findMount(path)
    63  	if err != nil {
    64  		return fmt.Errorf("error reading back mount: %s", err)
    65  	}
    66  
    67  	if wanted := "Example mount for testing"; mount.Description != wanted {
    68  		return fmt.Errorf("description is %v; wanted %v", mount.Description, wanted)
    69  	}
    70  
    71  	if wanted := "generic"; mount.Type != wanted {
    72  		return fmt.Errorf("type is %v; wanted %v", mount.Description, wanted)
    73  	}
    74  
    75  	if wanted := 3600; mount.Config.DefaultLeaseTTL != wanted {
    76  		return fmt.Errorf("default lease ttl is %v; wanted %v", mount.Description, wanted)
    77  	}
    78  
    79  	if wanted := 36000; mount.Config.MaxLeaseTTL != wanted {
    80  		return fmt.Errorf("max lease ttl is %v; wanted %v", mount.Description, wanted)
    81  	}
    82  
    83  	return nil
    84  }
    85  
    86  var testResourceMount_updateConfig = `
    87  
    88  resource "vault_mount" "test" {
    89  	path = "remountingExample"
    90  	type = "generic"
    91  	description = "Example mount for testing"
    92  	default_lease_ttl_seconds = 7200
    93  	max_lease_ttl_seconds = 72000
    94  }
    95  
    96  `
    97  
    98  func testResourceMount_updateCheck(s *terraform.State) error {
    99  	resourceState := s.Modules[0].Resources["vault_mount.test"]
   100  	instanceState := resourceState.Primary
   101  
   102  	path := instanceState.ID
   103  
   104  	if path != instanceState.Attributes["path"] {
   105  		return fmt.Errorf("id doesn't match path")
   106  	}
   107  
   108  	if path != "remountingExample" {
   109  		return fmt.Errorf("unexpected path value")
   110  	}
   111  
   112  	mount, err := findMount(path)
   113  	if err != nil {
   114  		return fmt.Errorf("error reading back mount: %s", err)
   115  	}
   116  
   117  	if wanted := "Example mount for testing"; mount.Description != wanted {
   118  		return fmt.Errorf("description is %v; wanted %v", mount.Description, wanted)
   119  	}
   120  
   121  	if wanted := "generic"; mount.Type != wanted {
   122  		return fmt.Errorf("type is %v; wanted %v", mount.Description, wanted)
   123  	}
   124  
   125  	if wanted := 7200; mount.Config.DefaultLeaseTTL != wanted {
   126  		return fmt.Errorf("default lease ttl is %v; wanted %v", mount.Description, wanted)
   127  	}
   128  
   129  	if wanted := 72000; mount.Config.MaxLeaseTTL != wanted {
   130  		return fmt.Errorf("max lease ttl is %v; wanted %v", mount.Description, wanted)
   131  	}
   132  
   133  	return nil
   134  }
   135  
   136  func findMount(path string) (*api.MountOutput, error) {
   137  	client := testProvider.Meta().(*api.Client)
   138  
   139  	path = path + "/"
   140  
   141  	mounts, err := client.Sys().ListMounts()
   142  	if err != nil {
   143  		return nil, err
   144  	}
   145  
   146  	if mounts[path] != nil {
   147  		return mounts[path], nil
   148  	}
   149  
   150  	return nil, fmt.Errorf("Unable to find mount %s in Vault; current list: %v", path, mounts)
   151  }