From 1614bf09f10aaa1995647a631ba0f28536d8e417 Mon Sep 17 00:00:00 2001 From: Samuel Karp Date: Fri, 19 Aug 2016 23:00:04 -0700 Subject: [PATCH] cache: Fix expiration logic and make readable --- ecr-login/api/client_test.go | 1 + ecr-login/cache/credentials.go | 5 ++- ecr-login/cache/credentials_test.go | 63 +++++++++++++++++++++++++++++ ecr-login/cache/file_test.go | 4 +- 4 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 ecr-login/cache/credentials_test.go diff --git a/ecr-login/api/client_test.go b/ecr-login/api/client_test.go index f5061fc9..0260029a 100644 --- a/ecr-login/api/client_test.go +++ b/ecr-login/api/client_test.go @@ -142,6 +142,7 @@ func TestGetAuthConfigGetCacheSuccess(t *testing.T) { authEntry := &cache.AuthEntry{ ProxyEndpoint: testProxyEndpoint, ExpiresAt: expiresAt, + RequestedAt: time.Now(), AuthorizationToken: authorizationToken, } diff --git a/ecr-login/cache/credentials.go b/ecr-login/cache/credentials.go index a81b9343..4870bacc 100644 --- a/ecr-login/cache/credentials.go +++ b/ecr-login/cache/credentials.go @@ -31,6 +31,7 @@ type AuthEntry struct { // Checks if AuthEntry is still valid at testTime. AuthEntries expire at 1/2 of their original // requested window. func (authEntry *AuthEntry) IsValid(testTime time.Time) bool { - window := authEntry.ExpiresAt.Sub(authEntry.RequestedAt) - return authEntry.ExpiresAt.After(testTime.Add(-1 * (window / time.Duration(2)))) + validWindow := authEntry.ExpiresAt.Sub(authEntry.RequestedAt) + refreshTime := authEntry.ExpiresAt.Add(-1 * validWindow / time.Duration(2)) + return testTime.Before(refreshTime) } diff --git a/ecr-login/cache/credentials_test.go b/ecr-login/cache/credentials_test.go new file mode 100644 index 00000000..02336cbb --- /dev/null +++ b/ecr-login/cache/credentials_test.go @@ -0,0 +1,63 @@ +// Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. +package cache + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestIsValid_NewEntry(t *testing.T) { + authEntry := &AuthEntry{ + RequestedAt: time.Now(), + ExpiresAt: time.Now().Add(12 * time.Hour), + } + assert.True(t, authEntry.IsValid(time.Now())) +} + +func TestIsValid_OldEntry(t *testing.T) { + authEntry := &AuthEntry{ + RequestedAt: time.Now().Add(-12 * time.Hour), + ExpiresAt: time.Now(), + } + assert.False(t, authEntry.IsValid(time.Now())) +} + +func TestIsValid_BeforeRefreshTime(t *testing.T) { + now := time.Now() + authEntry := &AuthEntry{ + RequestedAt: now.Add(-6 * time.Hour), + ExpiresAt: now.Add(6 * time.Hour), + } + assert.True(t, authEntry.IsValid(now.Add(-1*time.Second))) +} + +func TestIsValid_AtRefreshTime(t *testing.T) { + now := time.Now() + authEntry := &AuthEntry{ + RequestedAt: now.Add(-6 * time.Hour), + ExpiresAt: now.Add(6 * time.Hour), + } + assert.False(t, authEntry.IsValid(now)) +} + +func TestIsValid_AfterRefreshTime(t *testing.T) { + now := time.Now() + authEntry := &AuthEntry{ + RequestedAt: now.Add(-6 * time.Hour), + ExpiresAt: now.Add(6 * time.Hour), + } + assert.False(t, authEntry.IsValid(now.Add(time.Second))) +} diff --git a/ecr-login/cache/file_test.go b/ecr-login/cache/file_test.go index 1ca4192d..049bba30 100644 --- a/ecr-login/cache/file_test.go +++ b/ecr-login/cache/file_test.go @@ -24,8 +24,8 @@ import ( var testAuthEntry = AuthEntry{ AuthorizationToken: "testToken", - RequestedAt: time.Now().Add(-6 * time.Hour), - ExpiresAt: time.Now().Add(6 * time.Hour), + RequestedAt: time.Now().Add(-5 * time.Hour), + ExpiresAt: time.Now().Add(7 * time.Hour), ProxyEndpoint: "testEndpoint", }