2015-04-11 21:46:09 +00:00
|
|
|
package framework
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
2018-04-03 16:20:20 +00:00
|
|
|
func TestCalculateTTL(t *testing.T) {
|
2016-01-29 20:27:01 +00:00
|
|
|
testSysView := logical.StaticSystemView{
|
|
|
|
DefaultLeaseTTLVal: 5 * time.Hour,
|
|
|
|
MaxLeaseTTLVal: 30 * time.Hour,
|
|
|
|
}
|
|
|
|
|
2015-04-11 21:46:09 +00:00
|
|
|
cases := map[string]struct {
|
2018-04-03 16:20:20 +00:00
|
|
|
Increment time.Duration
|
2016-01-29 20:27:01 +00:00
|
|
|
BackendDefault time.Duration
|
|
|
|
BackendMax time.Duration
|
2018-04-03 16:20:20 +00:00
|
|
|
Period time.Duration
|
|
|
|
ExplicitMaxTTL time.Duration
|
2016-01-29 20:27:01 +00:00
|
|
|
Result time.Duration
|
2018-04-03 16:20:20 +00:00
|
|
|
Warnings int
|
2016-01-29 20:27:01 +00:00
|
|
|
Error bool
|
2015-04-11 21:46:09 +00:00
|
|
|
}{
|
2016-01-30 01:02:15 +00:00
|
|
|
"valid request, good bounds, increment is preferred": {
|
2016-01-29 20:27:01 +00:00
|
|
|
BackendDefault: 30 * time.Hour,
|
|
|
|
Increment: 1 * time.Hour,
|
2016-01-30 01:02:15 +00:00
|
|
|
Result: 1 * time.Hour,
|
2016-01-29 20:27:01 +00:00
|
|
|
},
|
|
|
|
|
2016-01-30 01:02:15 +00:00
|
|
|
"valid request, zero backend default, uses increment": {
|
2016-01-29 20:27:01 +00:00
|
|
|
BackendDefault: 0,
|
|
|
|
Increment: 1 * time.Hour,
|
|
|
|
Result: 1 * time.Hour,
|
|
|
|
},
|
|
|
|
|
2016-01-30 01:02:15 +00:00
|
|
|
"lease increment is zero, uses backend default": {
|
2016-01-29 20:27:01 +00:00
|
|
|
BackendDefault: 30 * time.Hour,
|
|
|
|
Increment: 0,
|
|
|
|
Result: 30 * time.Hour,
|
2015-04-11 21:46:09 +00:00
|
|
|
},
|
2015-04-11 23:28:16 +00:00
|
|
|
|
2016-01-30 01:02:15 +00:00
|
|
|
"lease increment and default are zero, uses systemview": {
|
2016-01-29 20:27:01 +00:00
|
|
|
BackendDefault: 0,
|
|
|
|
Increment: 0,
|
|
|
|
Result: 5 * time.Hour,
|
2015-04-11 23:28:16 +00:00
|
|
|
},
|
2015-04-11 21:46:09 +00:00
|
|
|
|
2016-01-29 20:27:01 +00:00
|
|
|
"backend max and associated request are too long": {
|
|
|
|
BackendDefault: 40 * time.Hour,
|
|
|
|
BackendMax: 45 * time.Hour,
|
|
|
|
Result: 30 * time.Hour,
|
2018-04-03 16:20:20 +00:00
|
|
|
Warnings: 1,
|
2015-04-11 21:51:00 +00:00
|
|
|
},
|
|
|
|
|
2016-01-30 01:02:15 +00:00
|
|
|
"all request values are larger than the system view, so the system view limits": {
|
2016-01-29 20:27:01 +00:00
|
|
|
BackendDefault: 40 * time.Hour,
|
|
|
|
BackendMax: 50 * time.Hour,
|
|
|
|
Increment: 40 * time.Hour,
|
|
|
|
Result: 30 * time.Hour,
|
2018-04-03 16:20:20 +00:00
|
|
|
Warnings: 1,
|
2015-04-11 21:46:09 +00:00
|
|
|
},
|
2015-04-11 21:51:00 +00:00
|
|
|
|
2016-01-29 20:27:01 +00:00
|
|
|
"request within backend max": {
|
|
|
|
BackendDefault: 9 * time.Hour,
|
|
|
|
BackendMax: 5 * time.Hour,
|
|
|
|
Increment: 4 * time.Hour,
|
2016-01-30 01:02:15 +00:00
|
|
|
Result: 4 * time.Hour,
|
2015-04-11 23:41:08 +00:00
|
|
|
},
|
|
|
|
|
2016-01-29 20:27:01 +00:00
|
|
|
"request outside backend max": {
|
|
|
|
BackendDefault: 9 * time.Hour,
|
|
|
|
BackendMax: 4 * time.Hour,
|
|
|
|
Increment: 5 * time.Hour,
|
|
|
|
Result: 4 * time.Hour,
|
2018-04-03 16:20:20 +00:00
|
|
|
Warnings: 1,
|
2015-04-11 23:41:08 +00:00
|
|
|
},
|
|
|
|
|
2016-01-29 20:27:01 +00:00
|
|
|
"request is negative, no backend default, use sysview": {
|
|
|
|
Increment: -7 * time.Hour,
|
|
|
|
Result: 5 * time.Hour,
|
2015-04-11 21:51:00 +00:00
|
|
|
},
|
2015-06-17 21:24:12 +00:00
|
|
|
|
2016-01-29 20:27:01 +00:00
|
|
|
"lease increment too large": {
|
|
|
|
Increment: 40 * time.Hour,
|
|
|
|
Result: 30 * time.Hour,
|
2018-04-03 16:20:20 +00:00
|
|
|
Warnings: 1,
|
|
|
|
},
|
|
|
|
|
|
|
|
"periodic, good request, period is preferred": {
|
|
|
|
Increment: 3 * time.Hour,
|
|
|
|
BackendDefault: 4 * time.Hour,
|
|
|
|
BackendMax: 2 * time.Hour,
|
|
|
|
Period: 1 * time.Hour,
|
|
|
|
Result: 1 * time.Hour,
|
|
|
|
},
|
|
|
|
|
|
|
|
"period too large, explicit max ttl is preferred": {
|
|
|
|
Period: 2 * time.Hour,
|
|
|
|
ExplicitMaxTTL: 1 * time.Hour,
|
|
|
|
Result: 1 * time.Hour,
|
|
|
|
Warnings: 1,
|
|
|
|
},
|
|
|
|
|
|
|
|
"period too large, capped by backend max": {
|
|
|
|
Period: 2 * time.Hour,
|
|
|
|
BackendMax: 1 * time.Hour,
|
|
|
|
Result: 1 * time.Hour,
|
|
|
|
Warnings: 1,
|
2015-06-17 21:24:12 +00:00
|
|
|
},
|
2015-04-11 21:46:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
2018-04-03 16:20:20 +00:00
|
|
|
ttl, warnings, err := CalculateTTL(testSysView, tc.Increment, tc.BackendDefault, tc.Period, tc.BackendMax, tc.ExplicitMaxTTL, time.Time{})
|
2015-04-11 21:51:00 +00:00
|
|
|
if (err != nil) != tc.Error {
|
2015-04-11 21:46:09 +00:00
|
|
|
t.Fatalf("bad: %s\nerr: %s", name, err)
|
|
|
|
}
|
2015-04-11 21:51:00 +00:00
|
|
|
if tc.Error {
|
|
|
|
continue
|
|
|
|
}
|
2015-04-11 21:46:09 +00:00
|
|
|
|
|
|
|
// Round it to the nearest hour
|
2018-04-03 16:20:20 +00:00
|
|
|
now := time.Now().Round(time.Hour)
|
|
|
|
lease := now.Add(ttl).Round(time.Hour).Sub(now)
|
2015-04-11 21:46:09 +00:00
|
|
|
if lease != tc.Result {
|
|
|
|
t.Fatalf("bad: %s\nlease: %s", name, lease)
|
|
|
|
}
|
2018-04-03 16:20:20 +00:00
|
|
|
|
|
|
|
if tc.Warnings != len(warnings) {
|
|
|
|
t.Fatalf("bad: %s\nwarning count mismatch, expect %d, got %d: %#v", name, tc.Warnings, len(warnings), warnings)
|
|
|
|
}
|
2015-04-11 21:46:09 +00:00
|
|
|
}
|
|
|
|
}
|