From 7139ad427e24aa46fc0d83f6d8344ebc96a6e98c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 10 Apr 2015 21:29:03 -0700 Subject: [PATCH] logical: lease tests --- logical/lease.go | 12 ++++-- logical/lease_test.go | 98 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 logical/lease_test.go diff --git a/logical/lease.go b/logical/lease.go index e25d74e72..a98755097 100644 --- a/logical/lease.go +++ b/logical/lease.go @@ -32,18 +32,22 @@ func (l *LeaseOptions) LeaseEnabled() bool { // LeaseTotal is the total lease time including the grace period func (l *LeaseOptions) LeaseTotal() time.Duration { - if l.Lease == 0 { + if l.Lease <= 0 { return 0 } + + if l.LeaseGracePeriod < 0 { + return l.Lease + } + return l.Lease + l.LeaseGracePeriod } // ExpirationTime computes the time until expiration including the grace period func (l *LeaseOptions) ExpirationTime() time.Time { var expireTime time.Time - if l.Lease > 0 { - leaseTotal := l.Lease + l.LeaseGracePeriod - expireTime = l.LeaseIssue.UTC().Add(leaseTotal) + if !l.LeaseIssue.IsZero() && l.Lease > 0 { + expireTime = l.LeaseIssue.UTC().Add(l.LeaseTotal()) } return expireTime diff --git a/logical/lease_test.go b/logical/lease_test.go new file mode 100644 index 000000000..d4e908e13 --- /dev/null +++ b/logical/lease_test.go @@ -0,0 +1,98 @@ +package logical + +import ( + "testing" + "time" +) + +func TestLeaseOptionsLeaseTotal(t *testing.T) { + var l LeaseOptions + l.Lease = 1 * time.Hour + + actual := l.LeaseTotal() + expected := l.Lease + if actual != expected { + t.Fatalf("bad: %s", actual) + } +} + +func TestLeaseOptionsLeaseTotal_grace(t *testing.T) { + var l LeaseOptions + l.Lease = 1 * time.Hour + l.LeaseGracePeriod = 30 * time.Minute + + actual := l.LeaseTotal() + expected := l.Lease + l.LeaseGracePeriod + if actual != expected { + t.Fatalf("bad: %s", actual) + } +} + +func TestLeaseOptionsLeaseTotal_negLease(t *testing.T) { + var l LeaseOptions + l.Lease = -1 * 1 * time.Hour + l.LeaseGracePeriod = 30 * time.Minute + + actual := l.LeaseTotal() + expected := time.Duration(0) + if actual != expected { + t.Fatalf("bad: %s", actual) + } +} + +func TestLeaseOptionsLeaseTotal_negGrace(t *testing.T) { + var l LeaseOptions + l.Lease = 1 * time.Hour + l.LeaseGracePeriod = -1 * 30 * time.Minute + + actual := l.LeaseTotal() + expected := l.Lease + if actual != expected { + t.Fatalf("bad: %s", actual) + } +} + +func TestLeaseOptionsExpirationTime(t *testing.T) { + var l LeaseOptions + l.Lease = 1 * time.Hour + l.LeaseIssue = time.Now().UTC() + + actual := l.ExpirationTime() + expected := l.LeaseIssue.Add(l.Lease) + if !actual.Equal(expected) { + t.Fatalf("bad: %s", actual) + } +} + +func TestLeaseOptionsExpirationTime_grace(t *testing.T) { + var l LeaseOptions + l.Lease = 1 * time.Hour + l.LeaseGracePeriod = 30 * time.Minute + l.LeaseIssue = time.Now().UTC() + + actual := l.ExpirationTime() + expected := l.LeaseIssue.Add(l.Lease + l.LeaseGracePeriod) + if !actual.Equal(expected) { + t.Fatalf("bad: %s", actual) + } +} + +func TestLeaseOptionsExpirationTime_graceNegative(t *testing.T) { + var l LeaseOptions + l.Lease = 1 * time.Hour + l.LeaseGracePeriod = -1 * 30 * time.Minute + l.LeaseIssue = time.Now().UTC() + + actual := l.ExpirationTime() + expected := l.LeaseIssue.Add(l.Lease) + if !actual.Equal(expected) { + t.Fatalf("bad: %s", actual) + } +} + +func TestLeaseOptionsExpirationTime_noLease(t *testing.T) { + var l LeaseOptions + if !l.ExpirationTime().IsZero() { + t.Fatal("should be zero") + } +}