Skip to content

Commit fe96cd5

Browse files
author
sourabh
committed
adding more test cases for ELB
1 parent ffccc65 commit fe96cd5

File tree

4 files changed

+284
-1
lines changed

4 files changed

+284
-1
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"aws_serverlessapplicationrepository_cloudformation_stack": [
3+
{
4+
"auto_enable_access_logs": {
5+
"name": "Auto-Enable-Access-Logs-Elb",
6+
"application_id": "arn:aws:serverlessrepo:us-east-1:956882708938:applications/sumologic-s3-logging-auto-enable",
7+
"capabilities": [
8+
"CAPABILITY_IAM",
9+
"CAPABILITY_RESOURCE_POLICY"
10+
],
11+
"parameters": {
12+
"AutoEnableLogging": "ALB",
13+
"BucketName": "cf-templates-1qpf3unpuo1hw-{Region}",
14+
"BucketPrefix": "elasticloadbalancing"
15+
},
16+
"semantic_version": "1.0.2"
17+
}
18+
}
19+
],
20+
"aws_iam_role": {},
21+
"aws_s3_bucket": {},
22+
"aws_sns_subscription": {
23+
"delivery_policy": "{\"healthyRetryPolicy\":{\"minDelayTarget\":10,\"maxDelayTarget\":300,\"numRetries\":40,\"numMaxDelayRetries\":5,\"numNoDelayRetries\":0,\"numMinDelayRetries\":3,\"backoffFunction\":\"exponential\"},\"sicklyRetryPolicy\":null,\"throttlePolicy\":null,\"guaranteed\":false}",
24+
"endpoint_auto_confirms": true,
25+
"topic_arn": "arn:aws:sns:{Region}:{AccountId}:SumoLogic-Terraform-Elb-Module-{AccountId}"
26+
},
27+
"aws_sns_topic": [
28+
{
29+
"sns_topic": {
30+
"name": "SumoLogic-Terraform-Elb-Module-{AccountId}",
31+
"policy": "{\"Version\":\"2008-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"s3.amazonaws.com\"},\"Action\":\"sns:Publish\",\"Resource\":\"arn:aws:sns:{Region}:{AccountId}:SumoLogic-Terraform-Elb-Module-{AccountId}\",\"Condition\":{\"StringEquals\":{\"aws:SourceAccount\":\"{AccountId}\"},\"ArnLike\":{\"aws:SourceArn\":\"arn:aws:s3:::cf-templates-1qpf3unpuo1hw-{Region}\"}}}]}"
32+
}
33+
}
34+
],
35+
"aws_s3_bucket_notification": {},
36+
"sumologic_collector": {},
37+
"sumologic_source": {
38+
"category": "Labs/test/elb",
39+
"content_type": "AwsElbBucket",
40+
"description": "This source is created.",
41+
"path": [
42+
{
43+
"bucket_name": "{BucketName}",
44+
"path_expression": "{PathExpression}"
45+
}
46+
],
47+
"paused": false,
48+
"scan_interval": 60000,
49+
"name": "My ELB Source Exixting IAM"
50+
}
51+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"aws_serverlessapplicationrepository_cloudformation_stack": [
3+
{
4+
"auto_enable_access_logs": {
5+
"name": "Auto-Enable-Access-Logs-Elb",
6+
"application_id": "arn:aws:serverlessrepo:us-east-1:956882708938:applications/sumologic-s3-logging-auto-enable",
7+
"capabilities": [
8+
"CAPABILITY_IAM",
9+
"CAPABILITY_RESOURCE_POLICY"
10+
],
11+
"parameters": {
12+
"AutoEnableLogging": "ALB",
13+
"BucketName": "cf-templates-1qpf3unpuo1hw-{Region}",
14+
"BucketPrefix": "elasticloadbalancing"
15+
},
16+
"semantic_version": "1.0.2"
17+
}
18+
}
19+
],
20+
"aws_iam_role": {},
21+
"aws_s3_bucket": {},
22+
"aws_sns_subscription": {
23+
"delivery_policy": "{\"healthyRetryPolicy\":{\"minDelayTarget\":10,\"maxDelayTarget\":300,\"numRetries\":40,\"numMaxDelayRetries\":5,\"numNoDelayRetries\":0,\"numMinDelayRetries\":3,\"backoffFunction\":\"exponential\"},\"sicklyRetryPolicy\":null,\"throttlePolicy\":null,\"guaranteed\":false}",
24+
"endpoint_auto_confirms": true,
25+
"topic_arn": "arn:aws:sns:{Region}:{AccountId}:dynamodb"
26+
},
27+
"aws_sns_topic": {},
28+
"aws_s3_bucket_notification": {},
29+
"sumologic_collector": {},
30+
"sumologic_source": {
31+
"category": "Labs/test/elb",
32+
"content_type": "AwsElbBucket",
33+
"description": "This source is created.",
34+
"path": [
35+
{
36+
"bucket_name": "{BucketName}",
37+
"path_expression": "{PathExpression}"
38+
}
39+
],
40+
"paused": false,
41+
"scan_interval": 60000,
42+
"name": "My ELB Source Existing Resources"
43+
}
44+
}

terratest/aws/elb/elb_test.go

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package elb
33
import (
44
"fmt"
55
"net/http"
6+
"os"
67
"testing"
78
"time"
89

@@ -12,6 +13,9 @@ import (
1213
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
1314
)
1415

16+
var IAM_ROLE = os.Getenv("IAM_ROLE")
17+
var COLLECTOR_ID = os.Getenv("COLLECTOR_ID")
18+
1519
func SetUpTest(t *testing.T, vars map[string]interface{}, awsregion string) (*terraform.Options, *terraform.ResourceCount) {
1620
envVars := map[string]string{
1721
"AWS_DEFAULT_REGION": awsregion,
@@ -25,6 +29,12 @@ func SetUpTest(t *testing.T, vars map[string]interface{}, awsregion string) (*te
2529
return terraformOptions, resourceCount
2630
}
2731

32+
func UpdateTerraform(t *testing.T, vars map[string]interface{}, options *terraform.Options) *terraform.ResourceCount {
33+
options.Vars = vars
34+
out := terraform.Apply(t, options)
35+
return terraform.GetResourceCount(t, out)
36+
}
37+
2838
func TestWithDefaultValues(t *testing.T) {
2939
t.Parallel()
3040
aws_region := "us-east-2"
@@ -63,6 +73,184 @@ func TestWithDefaultValues(t *testing.T) {
6373

6474
// Assert if the logs are sent to Sumo Logic.
6575
outputs := common.FetchAllOutputs(t, options)
76+
assertResource.CheckLogsForPastSixtyMinutes("_sourceid="+outputs["sumologic_source"].(map[string]interface{})["id"].(string), 5, 2*time.Minute)
6677
assertResource.ValidateLoadBalancerAccessLogs(lb_id, outputs["aws_s3_bucket"].(map[string]interface{})["s3_bucket"].(map[string]interface{})["bucket"].(string))
78+
}
79+
80+
func TestWithExistingResourcesValues(t *testing.T) {
81+
t.Parallel()
82+
BUCKET_NAME := os.Getenv("BUCKET_NAME_US_WEST_1")
83+
PATH_EXPRESSION := os.Getenv("PATH_EXPRESSION_US_WEST_1")
84+
SNS_TOPIC := os.Getenv("TOPIC_ARN_US_WEST_1")
85+
86+
aws_region := "us-west-1"
87+
assertResource := common.GetAssertResource(t, map[string]string{"AWS_DEFAULT_REGION": aws_region})
88+
lb_id, dns := assertResource.CreateELB("TestWithDefaultValuesLB", "TestWithDefaultValuesTG")
89+
replacementMap := map[string]interface{}{
90+
"AccountId": aws.GetAccountId(t),
91+
"Region": aws_region,
92+
"SumoAccountId": common.SumoAccountId,
93+
"Deployment": common.SumologicEnvironment,
94+
"OrgId": common.SumologicOrganizationId,
95+
"BucketName": BUCKET_NAME,
96+
"PathExpression": PATH_EXPRESSION,
97+
}
98+
vars := map[string]interface{}{
99+
"create_collector": false,
100+
"sumologic_organization_id": common.SumologicOrganizationId,
101+
"auto_enable_access_logs": "Existing",
102+
"source_details": map[string]interface{}{
103+
"source_name": "My ELB Source Existing Resources",
104+
"source_category": "Labs/test/elb",
105+
"description": "This source is created.",
106+
"bucket_details": map[string]interface{}{
107+
"create_bucket": false,
108+
"bucket_name": BUCKET_NAME,
109+
"path_expression": PATH_EXPRESSION,
110+
"force_destroy_bucket": true,
111+
},
112+
"paused": false,
113+
"scan_interval": 60000,
114+
"cutoff_relative_time": "-1d",
115+
"fields": map[string]string{
116+
"TestCollector": "MyValue",
117+
},
118+
"sumo_account_id": "926226587429",
119+
"collector_id": COLLECTOR_ID,
120+
"iam_role_arn": IAM_ROLE,
121+
"sns_topic_arn": SNS_TOPIC,
122+
},
123+
}
124+
125+
options, count := SetUpTest(t, vars, aws_region)
126+
127+
// Assert count of Expected resources.
128+
test_structure.RunTestStage(t, "AssertCount", func() {
129+
common.AssertResourceCounts(t, count, 4, 0, 0)
130+
})
131+
132+
// Assert if the outputs are actually created in AWS and Sumo Logic.
133+
// This also checks if your expectation are matched with the outputs, so provide an JSON with expected outputs.
134+
expectedOutputs := common.ReadJsonFile("TestWithExistingResourcesValues.json", replacementMap)
135+
test_structure.RunTestStage(t, "AssertOutputs", func() {
136+
common.AssertOutputs(t, options, expectedOutputs)
137+
})
138+
139+
// Before checking logs, create a load balancer, check if access logs has been enabled and then hit it to generate logs
140+
http.Get(fmt.Sprintf("http://%s", *dns))
141+
142+
// Assert if the logs are sent to Sumo Logic.
143+
outputs := common.FetchAllOutputs(t, options)
144+
assertResource.ValidateLoadBalancerAccessLogs(lb_id, BUCKET_NAME)
145+
assertResource.CheckLogsForPastSixtyMinutes("_sourceid="+outputs["sumologic_source"].(map[string]interface{})["id"].(string), 5, 2*time.Minute)
146+
}
147+
148+
func TestWithExistingCollectorIAMNewSNSResources(t *testing.T) {
149+
t.Parallel()
150+
BUCKET_NAME := os.Getenv("BUCKET_NAME_AP_SOUTH_1")
151+
PATH_EXPRESSION := os.Getenv("PATH_EXPRESSION_AP_SOUTH_1")
152+
153+
aws_region := "ap-south-1"
154+
assertResource := common.GetAssertResource(t, map[string]string{"AWS_DEFAULT_REGION": aws_region})
155+
lb_id, dns := assertResource.CreateELB("TestWithDefaultValuesLB", "TestWithDefaultValuesTG")
156+
replacementMap := map[string]interface{}{
157+
"AccountId": aws.GetAccountId(t),
158+
"Region": aws_region,
159+
"SumoAccountId": common.SumoAccountId,
160+
"Deployment": common.SumologicEnvironment,
161+
"OrgId": common.SumologicOrganizationId,
162+
"BucketName": BUCKET_NAME,
163+
"PathExpression": PATH_EXPRESSION,
164+
}
165+
vars := map[string]interface{}{
166+
"create_collector": false,
167+
"sumologic_organization_id": common.SumologicOrganizationId,
168+
"auto_enable_access_logs": "Existing",
169+
"source_details": map[string]interface{}{
170+
"source_name": "My ELB Source Exixting IAM",
171+
"source_category": "Labs/test/elb",
172+
"description": "This source is created.",
173+
"bucket_details": map[string]interface{}{
174+
"create_bucket": false,
175+
"bucket_name": BUCKET_NAME,
176+
"path_expression": PATH_EXPRESSION,
177+
"force_destroy_bucket": true,
178+
},
179+
"paused": false,
180+
"scan_interval": 60000,
181+
"cutoff_relative_time": "-1d",
182+
"fields": map[string]string{
183+
"TestCollector": "MyValue",
184+
},
185+
"sumo_account_id": "926226587429",
186+
"collector_id": COLLECTOR_ID,
187+
"iam_role_arn": IAM_ROLE,
188+
"sns_topic_arn": "",
189+
},
190+
}
191+
192+
options, count := SetUpTest(t, vars, aws_region)
193+
194+
// Assert count of Expected resources.
195+
test_structure.RunTestStage(t, "AssertCount", func() {
196+
common.AssertResourceCounts(t, count, 5, 0, 0)
197+
})
198+
199+
// Assert if the outputs are actually created in AWS and Sumo Logic.
200+
// This also checks if your expectation are matched with the outputs, so provide an JSON with expected outputs.
201+
expectedOutputs := common.ReadJsonFile("TestWithExistingCollectorIAMNewSNSResources.json", replacementMap)
202+
test_structure.RunTestStage(t, "AssertOutputs", func() {
203+
common.AssertOutputs(t, options, expectedOutputs)
204+
})
205+
206+
// Before checking logs, create a load balancer, check if access logs has been enabled and then hit it to generate logs
207+
http.Get(fmt.Sprintf("http://%s", *dns))
208+
209+
// Assert if the logs are sent to Sumo Logic.
210+
outputs := common.FetchAllOutputs(t, options)
211+
assertResource.ValidateLoadBalancerAccessLogs(lb_id, BUCKET_NAME)
67212
assertResource.CheckLogsForPastSixtyMinutes("_sourceid="+outputs["sumologic_source"].(map[string]interface{})["id"].(string), 5, 2*time.Minute)
68213
}
214+
215+
func TestUpdates(t *testing.T) {
216+
t.Parallel()
217+
aws_region := "us-east-1"
218+
vars := map[string]interface{}{
219+
"create_collector": true,
220+
"collector_details": map[string]interface{}{
221+
"collector_name": "Test Updates",
222+
"description": "This collector is created for testing elb terraform module.",
223+
"fields": map[string]interface{}{
224+
"MyCollector": "TestTerraform",
225+
},
226+
},
227+
"sumologic_organization_id": common.SumologicOrganizationId,
228+
"auto_enable_access_logs": "Existing",
229+
}
230+
231+
options, count := SetUpTest(t, vars, aws_region)
232+
233+
// Assert count of Expected resources.
234+
test_structure.RunTestStage(t, "AssertCount", func() {
235+
common.AssertResourceCounts(t, count, 10, 0, 0)
236+
})
237+
238+
vars = map[string]interface{}{
239+
"create_collector": true,
240+
"collector_details": map[string]interface{}{
241+
"collector_name": "Test Updates Again",
242+
"description": "This collector is created for testing elb terraform module.",
243+
"fields": map[string]interface{}{
244+
"MyCollector": "TestTerraform",
245+
},
246+
},
247+
"sumologic_organization_id": common.SumologicOrganizationId,
248+
"auto_enable_access_logs": "None",
249+
}
250+
251+
count = UpdateTerraform(t, vars, options)
252+
253+
test_structure.RunTestStage(t, "UpdateFirst", func() {
254+
common.AssertResourceCounts(t, count, 0, 2, 1)
255+
})
256+
}

terratest/common/awshelper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (a *ResourcesAssert) ValidateLoadBalancerAccessLogs(lb_id *string, bucketNa
157157
if assert.NoError(a.t, error, "Error while fetching load balancer details") {
158158
for _, element := range output.Attributes {
159159
if strings.Compare("access_logs.s3.enabled", *element.Key) == 0 {
160-
assert.Equal(a.t, "true", element.Value, "Access Logs is not enabled")
160+
assert.Equal(a.t, aws_sdk.String("true"), element.Value, "Access Logs is not enabled")
161161
}
162162
if strings.Compare("access_logs.s3.bucket", *element.Key) == 0 {
163163
assert.Equal(a.t, &bucketName, element.Value, "Access Logs bucket does not match.")

0 commit comments

Comments
 (0)