Added a test case. Removed setting of defaultTTL in config.

This commit is contained in:
vishalnayak
2015-10-03 15:36:57 -04:00
parent 02f11cb2da
commit e47b2838a0
3 changed files with 19 additions and 11 deletions

View File

@@ -25,7 +25,8 @@ func TestBackend_Config(t *testing.T) {
} }
login_data := map[string]interface{}{ login_data := map[string]interface{}{
"token": "25804d2e8674703adae786201cf5ed60fc6a2905", // This token has to be replaced with a working token for the test to work.
"token": "4fb5f4f0738c7aea5ee06dd595f15236ea78918b",
} }
config_data1 := map[string]interface{}{ config_data1 := map[string]interface{}{
"organization": "hashicorp", "organization": "hashicorp",
@@ -39,25 +40,36 @@ func TestBackend_Config(t *testing.T) {
"max_ttl": "2h", "max_ttl": "2h",
} }
expectedTTL2, _ := time.ParseDuration("1h0m0s") expectedTTL2, _ := time.ParseDuration("1h0m0s")
config_data3 := map[string]interface{}{
"organization": "hashicorp",
"ttl": "50h",
"max_ttl": "50h",
}
logicaltest.Test(t, logicaltest.TestCase{ logicaltest.Test(t, logicaltest.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Backend: b, Backend: b,
Steps: []logicaltest.TestStep{ Steps: []logicaltest.TestStep{
testConfigWrite(t, config_data1), testConfigWrite(t, config_data1),
testLoginWrite(t, login_data, expectedTTL1.Nanoseconds()), testLoginWrite(t, login_data, expectedTTL1.Nanoseconds(), false),
testConfigWrite(t, config_data2), testConfigWrite(t, config_data2),
testLoginWrite(t, login_data, expectedTTL2.Nanoseconds()), testLoginWrite(t, login_data, expectedTTL2.Nanoseconds(), false),
testConfigWrite(t, config_data3),
testLoginWrite(t, login_data, 0, true),
}, },
}) })
} }
func testLoginWrite(t *testing.T, d map[string]interface{}, expectedTTL int64) logicaltest.TestStep { func testLoginWrite(t *testing.T, d map[string]interface{}, expectedTTL int64, expectFail bool) logicaltest.TestStep {
return logicaltest.TestStep{ return logicaltest.TestStep{
Operation: logical.WriteOperation, Operation: logical.WriteOperation,
Path: "login", Path: "login",
ErrorOk: true,
Data: d, Data: d,
Check: func(resp *logical.Response) error { Check: func(resp *logical.Response) error {
if resp.IsError() && expectFail {
return nil
}
var actualTTL int64 var actualTTL int64
actualTTL = resp.Auth.LeaseOptions.TTL.Nanoseconds() actualTTL = resp.Auth.LeaseOptions.TTL.Nanoseconds()
if actualTTL != expectedTTL { if actualTTL != expectedTTL {

View File

@@ -54,9 +54,7 @@ func (b *backend) pathConfigWrite(
var ttl time.Duration var ttl time.Duration
var err error var err error
ttlRaw, ok := data.GetOk("ttl") ttlRaw, ok := data.GetOk("ttl")
if !ok { if !ok || len(ttlRaw.(string)) == 0 {
ttl = b.System().DefaultLeaseTTL()
} else if len(ttlRaw.(string)) == 0 {
ttl = 0 ttl = 0
} else { } else {
ttl, err = time.ParseDuration(ttlRaw.(string)) ttl, err = time.ParseDuration(ttlRaw.(string))
@@ -67,9 +65,7 @@ func (b *backend) pathConfigWrite(
var maxTTL time.Duration var maxTTL time.Duration
maxTTLRaw, ok := data.GetOk("max_ttl") maxTTLRaw, ok := data.GetOk("max_ttl")
if !ok { if !ok || len(maxTTLRaw.(string)) == 0 {
maxTTL = b.System().MaxLeaseTTL()
} else if len(maxTTLRaw.(string)) == 0 {
maxTTL = 0 maxTTL = 0
} else { } else {
maxTTL, err = time.ParseDuration(maxTTLRaw.(string)) maxTTL, err = time.ParseDuration(maxTTLRaw.(string))

View File

@@ -126,7 +126,7 @@ func (b *backend) pathLogin(
ttl, _, err := b.SanitizeTTL(config.TTL.String(), config.MaxTTL.String()) ttl, _, err := b.SanitizeTTL(config.TTL.String(), config.MaxTTL.String())
if err != nil { if err != nil {
return nil, err return logical.ErrorResponse(fmt.Sprintf("[ERR]:%s", err)), nil
} }
return &logical.Response{ return &logical.Response{