...

Source file src/github.com/awslabs/amazon-ecr-credential-helper/ecr-login/cache/file_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  
    14  package cache
    15  
    16  import (
    17  	"os"
    18  	"path/filepath"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  const (
    26  	testRegistryName   = "testRegistry"
    27  	testCachePrefixKey = "prefix-"
    28  	testPublicCacheKey = "public-"
    29  	testFilename       = "test.json"
    30  )
    31  
    32  var (
    33  	testAuthEntry = AuthEntry{
    34  		AuthorizationToken: "testToken",
    35  		RequestedAt:        time.Now().Add(-5 * time.Hour),
    36  		ExpiresAt:          time.Now().Add(7 * time.Hour),
    37  		ProxyEndpoint:      "testEndpoint",
    38  		Service:            ServiceECR,
    39  	}
    40  	testPublicAuthEntry = AuthEntry{
    41  		AuthorizationToken: "testToken",
    42  		RequestedAt:        time.Now().Add(-5 * time.Hour),
    43  		ExpiresAt:          time.Now().Add(7 * time.Hour),
    44  		ProxyEndpoint:      "testEndpoint",
    45  		Service:            ServiceECRPublic,
    46  	}
    47  	testPath          = os.TempDir() + "/ecr"
    48  	testFullFillename = filepath.Join(testPath, testFilename)
    49  )
    50  
    51  func TestAuthEntryValid(t *testing.T) {
    52  	assert.True(t, testAuthEntry.IsValid(time.Now()))
    53  }
    54  
    55  func TestAuthEntryInValid(t *testing.T) {
    56  	assert.True(t, testAuthEntry.IsValid(time.Now().Add(time.Second)))
    57  }
    58  
    59  func TestCredentials(t *testing.T) {
    60  	credentialCache := NewFileCredentialsCache(testPath, testFilename, testCachePrefixKey, testPublicCacheKey)
    61  
    62  	credentialCache.Set(testRegistryName, &testAuthEntry)
    63  
    64  	entry := credentialCache.Get(testRegistryName)
    65  	assert.Equal(t, testAuthEntry.AuthorizationToken, entry.AuthorizationToken)
    66  	assert.Equal(t, testAuthEntry.ProxyEndpoint, entry.ProxyEndpoint)
    67  	assert.WithinDuration(t, testAuthEntry.RequestedAt, entry.RequestedAt, 1*time.Second)
    68  	assert.WithinDuration(t, testAuthEntry.ExpiresAt, entry.ExpiresAt, 1*time.Second)
    69  	assert.Equal(t, testAuthEntry.Service, entry.Service)
    70  
    71  	entries := credentialCache.List()
    72  	assert.NotEmpty(t, entries)
    73  	assert.Len(t, entries, 1)
    74  	assert.Equal(t, entry, entries[0])
    75  
    76  	credentialCache.Clear()
    77  
    78  	entry = credentialCache.Get(testRegistryName)
    79  	assert.Nil(t, entry)
    80  }
    81  
    82  func TestCredentialsPublic(t *testing.T) {
    83  	credentialCache := NewFileCredentialsCache(testPath, testFilename, testCachePrefixKey, testPublicCacheKey)
    84  
    85  	credentialCache.Set(testRegistryName, &testPublicAuthEntry)
    86  
    87  	entry := credentialCache.GetPublic()
    88  	assert.Equal(t, testPublicAuthEntry.AuthorizationToken, entry.AuthorizationToken)
    89  	assert.Equal(t, testPublicAuthEntry.ProxyEndpoint, entry.ProxyEndpoint)
    90  	assert.WithinDuration(t, testPublicAuthEntry.RequestedAt, entry.RequestedAt, 1*time.Second)
    91  	assert.WithinDuration(t, testPublicAuthEntry.ExpiresAt, entry.ExpiresAt, 1*time.Second)
    92  	assert.Equal(t, testPublicAuthEntry.Service, entry.Service)
    93  
    94  	entries := credentialCache.List()
    95  	assert.NotEmpty(t, entries)
    96  	assert.Len(t, entries, 1)
    97  	assert.Equal(t, entry, entries[0])
    98  
    99  	credentialCache.Clear()
   100  
   101  	entry = credentialCache.GetPublic()
   102  	assert.Nil(t, entry)
   103  }
   104  
   105  func TestPreviousVersionCache(t *testing.T) {
   106  	credentialCache := NewFileCredentialsCache(testPath, testFilename, testCachePrefixKey, testPublicCacheKey)
   107  
   108  	registryCache := newRegistryCache()
   109  	registryCache.Version = "0.1"
   110  	registryCache.Registries[testRegistryName] = &testAuthEntry
   111  	credentialCache.(*fileCredentialCache).save(registryCache)
   112  
   113  	entry := credentialCache.Get(testRegistryName)
   114  	assert.Nil(t, entry)
   115  
   116  	credentialCache.Clear()
   117  }
   118  
   119  const testBadJson = "{nope not good json at all."
   120  
   121  func TestInvalidCache(t *testing.T) {
   122  	credentialCache := NewFileCredentialsCache(testPath, testFilename, testCachePrefixKey, testPublicCacheKey)
   123  
   124  	file, err := os.Create(testFullFillename)
   125  	assert.NoError(t, err)
   126  
   127  	file.WriteString(testBadJson)
   128  	err = file.Close()
   129  	assert.NoError(t, err)
   130  
   131  	entry := credentialCache.Get(testRegistryName)
   132  	assert.Nil(t, entry)
   133  
   134  	credentialCache.Clear()
   135  }
   136  

View as plain text