...

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

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

     1  // Copyright 2017 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  	"fmt"
    18  	"os"
    19  	"testing"
    20  
    21  	"github.com/aws/aws-sdk-go-v2/aws"
    22  	"github.com/aws/aws-sdk-go-v2/credentials"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  const (
    27  	testRegion        = "test-region"
    28  	testCacheFilename = "cache.json"
    29  	testAccessKey     = "accessKey"
    30  	testSecretKey     = "secretKey"
    31  	testToken         = "token"
    32  	// base64 MD5 sum of Credentials struct
    33  	testCredentialHash = "YWNjZXNzS2V51B2M2Y8AsgTpgAmY7PhCfg=="
    34  )
    35  
    36  func TestFactoryBuildFileCache(t *testing.T) {
    37  	config := aws.Config{
    38  		Region:      testRegion,
    39  		Credentials: credentials.NewStaticCredentialsProvider(testAccessKey, testSecretKey, testToken),
    40  	}
    41  
    42  	cache := BuildCredentialsCache(config, "")
    43  	assert.NotNil(t, cache)
    44  
    45  	fileCache, ok := cache.(*fileCredentialCache)
    46  
    47  	assert.True(t, ok, "built cache is not a fileCredentialsCache")
    48  	assert.Equal(t, fileCache.cachePrefixKey, fmt.Sprintf("%s-%s-", testRegion, testCredentialHash))
    49  	assert.Equal(t, fileCache.filename, testCacheFilename)
    50  }
    51  
    52  func TestFactoryBuildNullCacheWithoutCredentials(t *testing.T) {
    53  	config := aws.Config{
    54  		Region:      testRegion,
    55  		Credentials: aws.AnonymousCredentials{},
    56  	}
    57  
    58  	cache := BuildCredentialsCache(config, "")
    59  	assert.NotNil(t, cache)
    60  
    61  	_, ok := cache.(*nullCredentialsCache)
    62  	assert.True(t, ok, "built cache is a nullCredentialsCache")
    63  }
    64  
    65  func TestFactoryBuildNullCache(t *testing.T) {
    66  	os.Setenv("AWS_ECR_DISABLE_CACHE", "1")
    67  	defer os.Setenv("AWS_ECR_DISABLE_CACHE", "1")
    68  
    69  	config := aws.Config{Region: testRegion}
    70  
    71  	cache := BuildCredentialsCache(config, "")
    72  	assert.NotNil(t, cache)
    73  	_, ok := cache.(*nullCredentialsCache)
    74  	assert.True(t, ok, "built cache is a nullCredentialsCache")
    75  }
    76  

View as plain text