@@ -3,6 +3,7 @@ package elb
33import (
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+
1519func 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+
2838func 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+ }
0 commit comments