Skip to content

Commit

Permalink
Added tests for and fixed bug in err caching delay
Browse files Browse the repository at this point in the history
  • Loading branch information
parimaldeshmukh committed Apr 15, 2019
1 parent b02413a commit ff0800b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
28 changes: 26 additions & 2 deletions scintegtests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scintegtests

import (
"bytes"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/secretsmanager"
"github.com/aws/aws-sdk-go/service/secretsmanager/secretsmanageriface"
Expand All @@ -13,14 +14,15 @@ import (
)

var (
randStringSet = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
randStringSet = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
secretNamePrefix = "scIntegTest_"
subTests = []func(t *testing.T, api secretsmanageriface.SecretsManagerAPI) string{
subTests = []func(t *testing.T, api secretsmanageriface.SecretsManagerAPI) string{
integTest_getSecretBinary,
integTest_getSecretBinaryWithStage,
integTest_getSecretString,
integTest_getSecretStringWithStage,
integTest_getSecretStringWithTTL,
integTest_getSecretStringNoSecret,
}
)

Expand Down Expand Up @@ -108,6 +110,11 @@ func getPrevRunSecrets(secretsManagerClient *secretsmanager.SecretsManager) []st

func performDelete(secretNames *[]string, secretsManagerClient *secretsmanager.SecretsManager, forceDelete bool) {
for _, secretName := range *secretNames {

if secretName == "" {
continue
}

time.Sleep(time.Second / 2)
_, _ = secretsManagerClient.DeleteSecret(&secretsmanager.DeleteSecretInput{
SecretId: &secretName,
Expand Down Expand Up @@ -355,3 +362,20 @@ func integTest_getSecretStringWithTTL(t *testing.T, api secretsmanageriface.Secr

return *createResult.ARN
}

func integTest_getSecretStringNoSecret(t *testing.T, api secretsmanageriface.SecretsManagerAPI) string {
cache, _ := secretcache.New(
func(c *secretcache.Cache) { c.Client = api },
)

secretName := "NoSuchSecret"
_, err := cache.GetSecretString(secretName)

if err == nil {
t.Errorf("Expected to not find a secret called %s", secretName)
} else if awsErr, _ := err.(awserr.Error); awsErr.Code() != secretsmanager.ErrCodeResourceNotFoundException {
t.Errorf("Expected %s err but got %s", secretsmanager.ErrCodeResourceNotFoundException, awsErr.Code())
}

return ""
}
2 changes: 1 addition & 1 deletion secretcache/cacheItem.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (ci *secretCacheItem) refresh() {
ci.err = err
delay := exceptionRetryDelayBase * math.Pow(exceptionRetryGrowthFactor, float64(ci.errorCount))
delay = math.Min(delay, exceptionRetryDelayMax)
delayDuration := time.Nanosecond * time.Duration(delay)
delayDuration := time.Millisecond * time.Duration(delay)
ci.nextRetryTime = time.Now().Add(delayDuration).UnixNano()
return
}
Expand Down
12 changes: 10 additions & 2 deletions secretcache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,13 @@ func TestGetSecretStringMultipleNotFound(t *testing.T) {
_, err := secretCache.GetSecretStringWithStage("test", "versionStage-42")

if err == nil {
t.Fatalf("Expected error for a missing secret")
t.Fatalf("Expected error: secretNotFound for a missing secret")
}
}

if mockClient.DescribeSecretCallCount != 1 {
t.Fatalf("Expected a single call to DescribeSecret API, got %d", mockClient.DescribeSecretCallCount)
}
}

func TestGetSecretBinaryMultipleNotFound(t *testing.T) {
Expand All @@ -343,9 +347,13 @@ func TestGetSecretBinaryMultipleNotFound(t *testing.T) {
_, err := secretCache.GetSecretBinaryWithStage("test", "versionStage-42")

if err == nil {
t.Fatalf("Expected error for a missing secret")
t.Fatalf("Expected error: secretNotFound for a missing secret")
}
}

if mockClient.DescribeSecretCallCount != 1 {
t.Fatalf("Expected a single call to DescribeSecret API, got %d", mockClient.DescribeSecretCallCount)
}
}

func TestGetSecretVersionStageEmpty(t *testing.T) {
Expand Down

0 comments on commit ff0800b

Please sign in to comment.