...

Source file src/github.com/awslabs/amazon-ecr-credential-helper/ecr-login/cache/credentials_test.go

Documentation: github.com/awslabs/amazon-ecr-credential-helper/ecr-login/cache

     1  // Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"). You may
     4  // not use this file except in compliance with the License. A copy of the
     5  // License is located at
     6  //
     7  //	http://aws.amazon.com/apache2.0/
     8  //
     9  // or in the "license" file accompanying this file. This file is distributed
    10  // on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
    11  // express or implied. See the License for the specific language governing
    12  // permissions and limitations under the License.
    13  package cache
    14  
    15  import (
    16  	"testing"
    17  	"time"
    18  
    19  	"github.com/stretchr/testify/assert"
    20  )
    21  
    22  func TestIsValid_NewEntry(t *testing.T) {
    23  	authEntry := &AuthEntry{
    24  		RequestedAt: time.Now(),
    25  		ExpiresAt:   time.Now().Add(12 * time.Hour),
    26  	}
    27  	assert.True(t, authEntry.IsValid(time.Now()))
    28  }
    29  
    30  func TestIsValid_OldEntry(t *testing.T) {
    31  	authEntry := &AuthEntry{
    32  		RequestedAt: time.Now().Add(-12 * time.Hour),
    33  		ExpiresAt:   time.Now(),
    34  	}
    35  	assert.False(t, authEntry.IsValid(time.Now()))
    36  }
    37  
    38  func TestIsValid_BeforeRefreshTime(t *testing.T) {
    39  	now := time.Now()
    40  	authEntry := &AuthEntry{
    41  		RequestedAt: now.Add(-6 * time.Hour),
    42  		ExpiresAt:   now.Add(6 * time.Hour),
    43  	}
    44  	assert.True(t, authEntry.IsValid(now.Add(-1*time.Second)))
    45  }
    46  
    47  func TestIsValid_AtRefreshTime(t *testing.T) {
    48  	now := time.Now()
    49  	authEntry := &AuthEntry{
    50  		RequestedAt: now.Add(-6 * time.Hour),
    51  		ExpiresAt:   now.Add(6 * time.Hour),
    52  	}
    53  	assert.False(t, authEntry.IsValid(now))
    54  }
    55  
    56  func TestIsValid_AfterRefreshTime(t *testing.T) {
    57  	now := time.Now()
    58  	authEntry := &AuthEntry{
    59  		RequestedAt: now.Add(-6 * time.Hour),
    60  		ExpiresAt:   now.Add(6 * time.Hour),
    61  	}
    62  	assert.False(t, authEntry.IsValid(now.Add(time.Second)))
    63  }
    64  

View as plain text