Skip to content

Commit 782844d

Browse files
author
sourabh
committed
adding test case for cloudwatchlogforwarder
1 parent d29a3f9 commit 782844d

File tree

3 files changed

+213
-0
lines changed

3 files changed

+213
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
{
2+
"random_string": {
3+
"lower": true
4+
},
5+
"aws_sqs_queue": {
6+
"name": "SumoCWDeadLetterQueue-{RandomString}"
7+
},
8+
"aws_cloudwatch_log_group": {
9+
"name": "SumoCWLogGroup-{RandomString}",
10+
"retention_in_days": 7
11+
},
12+
"aws_cloudwatch_metric_alarm": {
13+
"actions_enabled": true,
14+
"alarm_actions": [
15+
"arn:aws:sns:{Region}:{AccountId}:SumoCWEmailSNSTopic-{RandomString}"
16+
],
17+
"alarm_description": "Notify via email if number of messages in DeadLetterQueue exceeds threshold",
18+
"alarm_name": "SumoCWSpilloverAlarm-{RandomString}",
19+
"comparison_operator": "GreaterThanThreshold",
20+
"dimensions": {
21+
"QueueName": "SumoCWDeadLetterQueue-{RandomString}"
22+
},
23+
"evaluation_periods": 1,
24+
"metric_name": "ApproximateNumberOfMessagesVisible",
25+
"namespace": "AWS/SQS",
26+
"period": 3600,
27+
"statistic": "Sum",
28+
"threshold": 100000
29+
},
30+
"aws_cw_lambda_function": {
31+
"dead_letter_config": [
32+
{
33+
"target_arn": "arn:aws:sqs:{Region}:{AccountId}:SumoCWDeadLetterQueue-{RandomString}"
34+
}
35+
],
36+
"environment": [
37+
{
38+
"variables": {
39+
"INCLUDE_LOG_INFO": "true",
40+
"LOG_FORMAT": "Others",
41+
"LOG_STREAM_PREFIX": "",
42+
"SUMO_ENDPOINT": "{URL}"
43+
}
44+
}
45+
],
46+
"function_name": "SumoCWLogsLambda-{RandomString}",
47+
"handler": "cloudwatchlogs_lambda.handler",
48+
"s3_bucket": "appdevzipfiles-{Region}",
49+
"s3_key": "cloudwatchlogs-with-dlq.zip"
50+
},
51+
"aws_iam_role": [
52+
{
53+
"name": "SumoCWLambdaExecutionRole-{RandomString}",
54+
"assume_role_policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Action\":\"sts:AssumeRole\"}]}"
55+
}
56+
],
57+
"aws_serverlessapplicationrepository_cloudformation_stack": [
58+
{
59+
"auto_enable_logs_subscription": {
60+
"name": "Auto-Enable-Logs-Subscription-{RandomString}",
61+
"application_id": "arn:aws:serverlessrepo:us-east-1:956882708938:applications/sumologic-loggroup-connector",
62+
"capabilities": [
63+
"CAPABILITY_IAM",
64+
"CAPABILITY_RESOURCE_POLICY"
65+
],
66+
"parameters": {
67+
"DestinationArnValue": "arn:aws:lambda:{Region}:{AccountId}:function:SumoCWLogsLambda-{RandomString}",
68+
"LogGroupPattern": "lambda",
69+
"UseExistingLogs": "true"
70+
},
71+
"semantic_version": "1.0.5"
72+
}
73+
}
74+
],
75+
"aws_sns_topic": {
76+
"name": "SumoCWEmailSNSTopic-{RandomString}",
77+
"policy": "{\"Version\":\"2008-10-17\",\"Id\":\"__default_policy_ID\",\"Statement\":[{\"Sid\":\"__default_statement_ID\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":[\"SNS:GetTopicAttributes\",\"SNS:SetTopicAttributes\",\"SNS:AddPermission\",\"SNS:RemovePermission\",\"SNS:DeleteTopic\",\"SNS:Subscribe\",\"SNS:ListSubscriptionsByTopic\",\"SNS:Publish\",\"SNS:Receive\"],\"Resource\":\"arn:aws:sns:{Region}:{AccountId}:SumoCWEmailSNSTopic-{RandomString}\",\"Condition\":{\"StringEquals\":{\"AWS:SourceOwner\":\"{AccountId}\"}}}]}"
78+
},
79+
"sumologic_collector": [
80+
{
81+
"collector": {
82+
"description": "This collector is created using Sumo Logic terraform AWS CloudWatch Logs forwarder to collect AWS cloudwatch logs.",
83+
"name": "SumoLogic CloudWatch Logs Collector {RandomString}"
84+
}
85+
}
86+
],
87+
"sumologic_source": {
88+
"category": "Labs/aws/cloudwatch",
89+
"content_type": "",
90+
"description": "This source is created using Sumo Logic terraform AWS CloudWatch Logs forwarder to collect AWS cloudwatch logs.",
91+
"name": "CloudWatch Logs Source"
92+
}
93+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package cloudwatchlogsforwarder
2+
3+
import (
4+
"os"
5+
"testing"
6+
"time"
7+
8+
"github.com/SumoLogic/terraform-sumologic-sumo-logic-integrations/tree/master/terratest/common"
9+
"github.com/gruntwork-io/terratest/modules/aws"
10+
"github.com/gruntwork-io/terratest/modules/terraform"
11+
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
12+
)
13+
14+
var COLLECTOR_ID = os.Getenv("COLLECTOR_ID")
15+
var LOOK_UP_MAP = map[string]string{"aws_cw_lambda_function": "aws_lambda_function"}
16+
17+
func SetUpTest(t *testing.T, vars map[string]interface{}, awsregion string) (*terraform.Options, *terraform.ResourceCount) {
18+
envVars := map[string]string{
19+
"AWS_DEFAULT_REGION": awsregion,
20+
"SUMOLOGIC_ACCESSID": common.SumologicAccessID,
21+
"SUMOLOGIC_ACCESSKEY": common.SumologicAccessKey,
22+
"SUMOLOGIC_ENVIRONMENT": common.SumologicEnvironment,
23+
}
24+
25+
terraformOptions, resourceCount := common.ApplyTerraformWithVars(t, vars, envVars)
26+
27+
return terraformOptions, resourceCount
28+
}
29+
30+
func UpdateTerraform(t *testing.T, vars map[string]interface{}, options *terraform.Options) *terraform.ResourceCount {
31+
options.Vars = vars
32+
out := terraform.Apply(t, options)
33+
return terraform.GetResourceCount(t, out)
34+
}
35+
36+
func TestWithDefaultValues(t *testing.T) {
37+
t.Parallel()
38+
aws_region := "us-east-1"
39+
vars := map[string]interface{}{
40+
"create_collector": true,
41+
"auto_enable_logs_subscription": "Both",
42+
}
43+
44+
options, count := SetUpTest(t, vars, aws_region)
45+
46+
// Assert count of Expected resources.
47+
test_structure.RunTestStage(t, "AssertCount", func() {
48+
common.AssertResourceCounts(t, count, 23, 0, 0)
49+
})
50+
51+
assertResource := common.GetAssertResource(t, options.EnvVars)
52+
outputs := common.FetchAllOutputs(t, options)
53+
replacementMap := map[string]interface{}{
54+
"AccountId": aws.GetAccountId(t),
55+
"Region": aws_region,
56+
"SumoAccountId": common.SumoAccountId,
57+
"Deployment": common.SumologicEnvironment,
58+
"OrgId": common.SumologicOrganizationId,
59+
"RandomString": outputs["random_string"].(map[string]interface{})["id"].(string),
60+
"URL": outputs["sumologic_source"].(map[string]interface{})["url"].(string),
61+
}
62+
// Assert if the outputs are actually created in AWS and Sumo Logic.
63+
// This also checks if your expectation are matched with the outputs, so provide an JSON with expected outputs.
64+
expectedOutputs := common.ReadJsonFile("TestWithDefaultValues.json", replacementMap)
65+
test_structure.RunTestStage(t, "AssertOutputs", func() {
66+
common.AssertOutputsWithLookup(t, options, expectedOutputs, LOOK_UP_MAP)
67+
})
68+
69+
// Assert if the logs are sent to Sumo Logic.
70+
assertResource.CheckLogsForPastSixtyMinutes("_sourceid="+outputs["sumologic_source"].(map[string]interface{})["id"].(string), 5, 2*time.Minute)
71+
}

terratest/common/assertaws.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ import (
77
"github.com/aws/aws-sdk-go/aws/session"
88
"github.com/aws/aws-sdk-go/service/cloudformation"
99
"github.com/aws/aws-sdk-go/service/cloudtrail"
10+
"github.com/aws/aws-sdk-go/service/cloudwatch"
1011
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
1112
"github.com/aws/aws-sdk-go/service/firehose"
1213
"github.com/aws/aws-sdk-go/service/iam"
14+
"github.com/aws/aws-sdk-go/service/lambda"
1315
"github.com/aws/aws-sdk-go/service/s3"
1416
"github.com/aws/aws-sdk-go/service/sns"
17+
"github.com/aws/aws-sdk-go/service/sqs"
1518
"github.com/gruntwork-io/terratest/modules/aws"
1619
"github.com/stretchr/testify/assert"
1720
)
@@ -145,3 +148,49 @@ func (a *ResourcesAssert) AWS_KINESIS_FIREHOSE_DELIVERY_STREAM(value interface{}
145148
assert.NoErrorf(a.t, err, "AWS KINESIS FIREHOSE STREAM :- Error Message %v", err)
146149
}
147150
}
151+
152+
func (a *ResourcesAssert) AWS_SQS_QUEUE(value interface{}) {
153+
fmt.Println("******** Asserting AWS SQS QUEUE ********")
154+
queue_names := getKeyValuesFromData(value, "name")
155+
156+
sqs_client := aws.NewSqsClient(a.t, a.AwsRegion)
157+
158+
for _, element := range queue_names {
159+
output, err := sqs_client.ListQueues(&sqs.ListQueuesInput{
160+
QueueNamePrefix: &element,
161+
})
162+
assert.NoErrorf(a.t, err, "AWS SQS :- Error Message %v", err)
163+
assert.Greater(a.t, len(output.QueueUrls), 0, "No SQS found with the name ."+element)
164+
}
165+
}
166+
167+
func (a *ResourcesAssert) AWS_LAMBDA_FUNCTION(value interface{}) {
168+
fmt.Println("******** Asserting AWS LAMBDA FUNCTION ********")
169+
fucntion_names := getKeyValuesFromData(value, "function_name")
170+
171+
mySession := session.Must(session.NewSession())
172+
svc := lambda.New(mySession, aws_sdk.NewConfig().WithRegion(a.AwsRegion))
173+
174+
for _, element := range fucntion_names {
175+
_, err := svc.GetFunction(&lambda.GetFunctionInput{
176+
FunctionName: &element,
177+
})
178+
assert.NoErrorf(a.t, err, "AWS Lambda :- Error Message %v", err)
179+
}
180+
}
181+
182+
func (a *ResourcesAssert) AWS_CLOUDWATCH_METRIC_ALARM(value interface{}) {
183+
fmt.Println("******** Asserting AWS CLOUDWATCH METRIC ALARM ********")
184+
alarm_names := getKeyValuesFromData(value, "alarm_name")
185+
186+
mySession := session.Must(session.NewSession())
187+
svc := cloudwatch.New(mySession, aws_sdk.NewConfig().WithRegion(a.AwsRegion))
188+
189+
for _, element := range alarm_names {
190+
output, err := svc.DescribeAlarms(&cloudwatch.DescribeAlarmsInput{
191+
AlarmNamePrefix: &element,
192+
})
193+
assert.NoErrorf(a.t, err, "AWS CLOUDWATCH ALARM :- Error Message %v", err)
194+
assert.Greater(a.t, len(output.MetricAlarms), 0, "No CloudWatch Alarm found with the name ."+element)
195+
}
196+
}

0 commit comments

Comments
 (0)