Skip to content

Commit 8f7d6fa

Browse files
Fixed old testcases and added new testcases for tag
1 parent 7407152 commit 8f7d6fa

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

loggroup-lambda-connector/test/test-template.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ Parameters:
2323
AllowedValues: [ "true", "false" ]
2424
Description: "Select true for subscribing existing logs"
2525

26+
LogGroupTags:
27+
Type: String
28+
Default: ""
29+
Description: Enter comma separated keyvalue pairs for filtering logGroups using
30+
tags. Ex KeyName1=string,KeyName2=string. This is optional leave it blank if
31+
tag based filtering is not needed.
32+
2633
BucketName:
2734
Type: String
2835
Default: ""
@@ -72,7 +79,7 @@ Resources:
7279
print("success")
7380
Handler: index.lambda_handler
7481
MemorySize: 128
75-
Runtime: python3.7
82+
Runtime: python3.12
7683
Timeout: 60
7784
Role: !GetAtt LambdaRole.Arn
7885

@@ -214,6 +221,7 @@ Resources:
214221
DestinationArnValue: !If [ create_invoke_permission, !GetAtt DummyLambda.Arn, !GetAtt KinesisLogsDeliveryStream.Arn ]
215222
LogGroupPattern: !Ref LogGroupPattern
216223
UseExistingLogs: !Ref UseExistingLogs
224+
LogGroupTags: !Ref LogGroupTags
217225
RoleArn: !If [ create_invoke_permission, "", !GetAtt KinesisLogsRole.Arn ]
218226

219227
Outputs:

loggroup-lambda-connector/test/test_loggroup_lambda_connector.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,38 @@ def test_4_existing_kinesis(self):
7272
#self.invoke_lambda()
7373
self.assert_subscription_filter("SumoLGLBDFilter")
7474

75-
def create_stack_parameters(self, destination, existing, pattern='test'):
75+
def test_5_matching_existing_loggroup_with_pattern_and_tag(self):
76+
self.create_log_group_with_tag()
77+
self.create_stack(self.stack_name, self.template_data,
78+
self.create_stack_parameters("Kinesis","true", loggroup_tag='username=akhil'))
79+
print("Testing Stack Creation")
80+
self.assertTrue(self.stack_exists(self.stack_name))
81+
#self.invoke_lambda()
82+
self.assert_subscription_filter("SumoLGLBDFilter")
83+
84+
def test_6_matching_existing_loggroup_by_tag_only(self):
85+
self.create_log_group_with_tag()
86+
self.create_stack(self.stack_name, self.template_data,
87+
self.create_stack_parameters("Kinesis","true", loggroup_pattern='^$',
88+
loggroup_tag='username=akhil'))
89+
print("Testing Stack Creation")
90+
self.assertTrue(self.stack_exists(self.stack_name))
91+
#self.invoke_lambda()
92+
self.assert_subscription_filter("SumoLGLBDFilter")
93+
94+
def create_stack_parameters(self, destination, existing, loggroup_pattern='test', loggroup_tag=''):
7695
return [
7796
{
7897
'ParameterKey': 'DestinationType',
7998
'ParameterValue': destination
8099
},
81100
{
82101
'ParameterKey': 'LogGroupPattern',
83-
'ParameterValue': pattern
102+
'ParameterValue': loggroup_pattern
103+
},
104+
{
105+
'ParameterKey': 'LogGroupTags',
106+
'ParameterValue': loggroup_tag
84107
},
85108
{
86109
'ParameterKey': 'UseExistingLogs',
@@ -137,6 +160,16 @@ def create_log_group(self):
137160
response = self.log_group_client.create_log_group(logGroupName=self.log_group_name)
138161
print("creating log group", response)
139162

163+
def create_log_group_with_tag(self):
164+
tags = {
165+
'team': 'apps',
166+
'username': 'akhil'
167+
}
168+
self.log_group_name = 'mytag-%s' % (datetime.datetime.now().strftime("%d-%m-%y-%H-%M-%S"))
169+
print("Loggroup Name", self.log_group_name)
170+
response = self.log_group_client.create_log_group(logGroupName=self.log_group_name, tags=tags)
171+
print("creating log group", response)
172+
140173
def assert_subscription_filter(self, filter_name):
141174
sleep(60)
142175
response = self.log_group_client.describe_subscription_filters(
@@ -205,7 +238,8 @@ def create_sam_package_and_upload():
205238

206239
def _run(command, input=None, check=False, **kwargs):
207240
if sys.version_info >= (3, 5):
208-
return subprocess.run(command, capture_output=True)
241+
result = subprocess.run(command, capture_output=True)
242+
return result.returncode, result.stdout, result.stderr
209243
if input is not None:
210244
if 'stdin' in kwargs:
211245
raise ValueError('stdin and input arguments may not both be used.')
@@ -226,11 +260,11 @@ def _run(command, input=None, check=False, **kwargs):
226260

227261

228262
def run_command(cmdargs):
229-
resp = _run(cmdargs)
230-
if len(resp.stderr.decode()) > 0:
263+
retcode, stdout, stderr = _run(cmdargs)
264+
if retcode != 0:
231265
# traceback.print_exc()
232-
raise Exception("Error in run command %s cmd: %s" % (resp, cmdargs))
233-
return resp.stdout
266+
raise Exception("Error in run command %s cmd: %s" % (stderr, cmdargs))
267+
return retcode, stdout, stderr
234268

235269

236270
if __name__ == '__main__':

0 commit comments

Comments
 (0)