Skip to content

Commit

Permalink
Add S3 and SecretsManager validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Duchesne committed Jul 29, 2019
1 parent c8cfa6c commit 6a27cff
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
41 changes: 41 additions & 0 deletions credentials/source_s3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package credentials

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestS3SourceValidate(t *testing.T) {
t.Parallel()

cases := []struct {
name string
source *AWSS3Source
expectedError error
}{
{
name: "No bucket",
source: &AWSS3Source{Key: "test"},
expectedError: fmt.Errorf("S3 sources must define a bucket"),
},
{
name: "No key",
source: &AWSS3Source{Bucket: "bucket"},
expectedError: fmt.Errorf("S3 sources must define a key"),
},
{
name: "Valid",
source: &AWSS3Source{Bucket: "bucket", Key: "test"},
expectedError: nil,
},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, "Amazon S3", tt.source.Type())
assert.Equal(t, tt.expectedError, tt.source.ValidateConfiguration())
})
}
}
2 changes: 1 addition & 1 deletion credentials/source_secretsmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (source *AWSSecretsManagerSource) Type() string {

// ValidateConfiguration verifies that the source's attributes are valid
func (source *AWSSecretsManagerSource) ValidateConfiguration() error {
if source.SecretID == "" && source.SecretPrefix == "" {
if (source.SecretID == "" && source.SecretPrefix == "") || (source.SecretID != "" && source.SecretPrefix != "") {
return fmt.Errorf("Either `secret_id` or `secret_prefix` must be defined on a secretsmanager source")
}
return nil
Expand Down
46 changes: 46 additions & 0 deletions credentials/source_secretsmanager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package credentials

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSecretsManagerSourceValidate(t *testing.T) {
t.Parallel()

cases := []struct {
name string
source *AWSSecretsManagerSource
expectedError error
}{
{
name: "No config",
source: &AWSSecretsManagerSource{},
expectedError: fmt.Errorf("Either `secret_id` or `secret_prefix` must be defined on a secretsmanager source"),
},
{
name: "Both secret ID and prefix",
source: &AWSSecretsManagerSource{SecretID: "test", SecretPrefix: "test2"},
expectedError: fmt.Errorf("Either `secret_id` or `secret_prefix` must be defined on a secretsmanager source"),
},
{
name: "Valid with only ID",
source: &AWSSecretsManagerSource{SecretID: "test"},
expectedError: nil,
},
{
name: "Valid with only prefix",
source: &AWSSecretsManagerSource{SecretPrefix: "test"},
expectedError: nil,
},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, "Amazon SecretsManager", tt.source.Type())
assert.Equal(t, tt.expectedError, tt.source.ValidateConfiguration())
})
}
}

0 comments on commit 6a27cff

Please sign in to comment.