From 3e85f8aeaee7369a0ec235e27789477317971638 Mon Sep 17 00:00:00 2001 From: Rayhan Hossain Date: Sun, 1 Aug 2021 23:19:47 -0700 Subject: [PATCH] Add validation to stop accepting both of LogStream and LogStreamPrefix Signed-off-by: Rayhan Hossain --- cloudwatch/cloudwatch.go | 4 ++ cloudwatch/cloudwatch_test.go | 93 +++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/cloudwatch/cloudwatch.go b/cloudwatch/cloudwatch.go index 244affa..a35277a 100644 --- a/cloudwatch/cloudwatch.go +++ b/cloudwatch/cloudwatch.go @@ -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 } diff --git a/cloudwatch/cloudwatch_test.go b/cloudwatch/cloudwatch_test.go index d97bc1a..35978eb 100644 --- a/cloudwatch/cloudwatch_test.go +++ b/cloudwatch/cloudwatch_test.go @@ -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) @@ -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) + } + }) + } +}