...
1
2
3
4
5
6
7
8
9
10
11
12
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
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