Skip to content

Commit

Permalink
Add validation to stop accepting both of LogStream and LogStreamPrefix
Browse files Browse the repository at this point in the history
Signed-off-by: Rayhan Hossain <[email protected]>
  • Loading branch information
hossain-rayhan committed Aug 2, 2021
1 parent 25707bd commit 3e85f8a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ func (config OutputPluginConfig) Validate() error {
return fmt.Errorf("log_stream_name or log_stream_prefix is required")
}

if config.LogStreamName != "" && config.LogStreamPrefix != "" {
return fmt.Errorf("either log_stream_name or log_stream_prefix can be configured. They cannot be provided together")
}

return nil
}

Expand Down
93 changes: 93 additions & 0 deletions cloudwatch/cloudwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,84 @@ const (
testSequenceToken = "sequence-token"
)

type configTest struct {
name string
config OutputPluginConfig
isValidConfig bool
expectedError string
}

var (
configValidationTestCases = []configTest{
{
name: "ValidConfiguration",
config: OutputPluginConfig{
Region: testRegion,
LogGroupName: testLogGroup,
LogStreamPrefix: testLogStreamPrefix,
},
isValidConfig: true,
expectedError: "",
},
{
name: "MissingRegion",
config: OutputPluginConfig{
LogGroupName: testLogGroup,
LogStreamPrefix: testLogStreamPrefix,
},
isValidConfig: false,
expectedError: "region is a required parameter",
},
{
name: "MissingLogGroup",
config: OutputPluginConfig{
Region: testRegion,
LogStreamPrefix: testLogStreamPrefix,
},
isValidConfig: false,
expectedError: "log_group_name is a required parameter",
},
{
name: "OnlyLogStreamNameProvided",
config: OutputPluginConfig{
Region: testRegion,
LogGroupName: testLogGroup,
LogStreamName: "testLogStream",
},
isValidConfig: true,
},
{
name: "OnlyLogStreamPrefixProvided",
config: OutputPluginConfig{
Region: testRegion,
LogGroupName: testLogGroup,
LogStreamPrefix: testLogStreamPrefix,
},
isValidConfig: true,
},
{
name: "LogStreamAndPrefixBothProvided",
config: OutputPluginConfig{
Region: testRegion,
LogGroupName: testLogGroup,
LogStreamName: "testLogStream",
LogStreamPrefix: testLogStreamPrefix,
},
isValidConfig: false,
expectedError: "either log_stream_name or log_stream_prefix can be configured. They cannot be provided together",
},
{
name: "LogStreamAndPrefixBothMissing",
config: OutputPluginConfig{
Region: testRegion,
LogGroupName: testLogGroup,
},
isValidConfig: false,
expectedError: "log_stream_name or log_stream_prefix is required",
},
}
)

// helper function to make a log stream/log group name template from a string.
func testTemplate(template string) *fastTemplate {
t, _ := newTemplate(template)
Expand Down Expand Up @@ -661,3 +739,18 @@ func setupTimeout() *plugins.Timeout {
})
return timer
}

func TestValidate(t *testing.T) {
for _, test := range configValidationTestCases {
t.Run(test.name, func(t *testing.T) {
err := test.config.Validate()

if test.isValidConfig {
assert.Nil(t, err)
} else {
assert.NotNil(t, err)
assert.Equal(t, err.Error(), test.expectedError)
}
})
}
}

0 comments on commit 3e85f8a

Please sign in to comment.