|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
import json, unittest |
|
3
|
|
|
from mock import MagicMock |
|
4
|
|
|
from awslambdahelper import AWSConfigRule |
|
5
|
|
|
from awslambdahelper import CompliantEvaluation |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class TestScheduledRuleTsts(unittest.TestCase): |
|
9
|
|
|
def setUp(self): |
|
10
|
|
|
self.parameters = [ |
|
11
|
|
|
( |
|
12
|
|
|
{ |
|
13
|
|
|
"invokingEvent": json.dumps({ |
|
14
|
|
|
"awsAccountId": "123456789012", |
|
15
|
|
|
"notificationCreationTime": "2016-07-13T21:50:00.373Z", |
|
16
|
|
|
"messageType": "ScheduledNotification", |
|
17
|
|
|
"recordVersion": "1.0" |
|
18
|
|
|
}), |
|
19
|
|
|
"ruleParameters": json.dumps({ |
|
20
|
|
|
"myParameterKey": "myParameterValue" |
|
21
|
|
|
}), |
|
22
|
|
|
"resultToken": "myResultToken", |
|
23
|
|
|
"eventLeftScope": False, |
|
24
|
|
|
"executionRoleArn": "arn:aws:iam::123456789012:role/config-role", |
|
25
|
|
|
"configRuleArn": "arn:aws:config:us-east-1:123456789012:config-rule/config-rule-0123456", |
|
26
|
|
|
"configRuleName": "periodic-config-rule", |
|
27
|
|
|
"configRuleId": "config-rule-6543210", |
|
28
|
|
|
"accountId": "123456789012", |
|
29
|
|
|
"version": "1.0" |
|
30
|
|
|
}, |
|
31
|
|
|
# Response |
|
32
|
|
|
{ |
|
33
|
|
|
"Evaluations": [{ |
|
34
|
|
|
"OrderingTimestamp": "2016-07-13T21:50:00.373Z", |
|
35
|
|
|
"ComplianceResourceId": "i-00000000", |
|
36
|
|
|
"ComplianceResourceType": "AWS::EC2::Instance", |
|
37
|
|
|
"Annotation": "This resource is compliant with the rule.", |
|
38
|
|
|
"ComplianceType": "COMPLIANT" |
|
39
|
|
|
}], |
|
40
|
|
|
"ResultToken": "myResultToken" |
|
41
|
|
|
} |
|
42
|
|
|
), |
|
43
|
|
|
( |
|
44
|
|
|
{ |
|
45
|
|
|
"invokingEvent": json.dumps({ |
|
46
|
|
|
"awsAccountId": "123456789012", |
|
47
|
|
|
"notificationCreationTime": "2016-07-13T21:50:00.373Z", |
|
48
|
|
|
"messageType": "ScheduledNotification", |
|
49
|
|
|
"recordVersion": "1.0" |
|
50
|
|
|
}), |
|
51
|
|
|
"resultToken": "myResultToken", |
|
52
|
|
|
"eventLeftScope": False, |
|
53
|
|
|
"executionRoleArn": "arn:aws:iam::123456789012:role/config-role", |
|
54
|
|
|
"configRuleArn": "arn:aws:config:us-east-1:123456789012:config-rule/config-rule-0123456", |
|
55
|
|
|
"configRuleName": "periodic-config-rule", |
|
56
|
|
|
"configRuleId": "config-rule-6543210", |
|
57
|
|
|
"accountId": "123456789012", |
|
58
|
|
|
"version": "1.0" |
|
59
|
|
|
}, |
|
60
|
|
|
# Response |
|
61
|
|
|
{ |
|
62
|
|
|
"Evaluations": [{ |
|
63
|
|
|
"OrderingTimestamp": "2016-07-13T21:50:00.373Z", |
|
64
|
|
|
"ComplianceResourceId": "i-00000000", |
|
65
|
|
|
"ComplianceResourceType": "AWS::EC2::Instance", |
|
66
|
|
|
"Annotation": "This resource is compliant with the rule.", |
|
67
|
|
|
"ComplianceType": "COMPLIANT" |
|
68
|
|
|
}], |
|
69
|
|
|
"ResultToken": "myResultToken" |
|
70
|
|
|
} |
|
71
|
|
|
) |
|
72
|
|
|
] |
|
73
|
|
|
|
|
74
|
|
|
def test_schedule_event(self): |
|
75
|
|
|
class MockScheduleRule(AWSConfigRule): |
|
76
|
|
|
def find_violation_scheduled(self, rule_parameters, accountid): |
|
77
|
|
|
return [CompliantEvaluation( |
|
78
|
|
|
ResourceType="AWS::EC2::Instance", |
|
79
|
|
|
ResourceId="i-00000000" |
|
80
|
|
|
)] |
|
81
|
|
|
|
|
82
|
|
|
mock_rule = MockScheduleRule() |
|
83
|
|
|
|
|
84
|
|
|
for lambda_event, put_evaluations_response in self.parameters: |
|
85
|
|
|
mock_rule.put_evaluations = MagicMock() |
|
86
|
|
|
|
|
87
|
|
|
mock_rule.lambda_handler( |
|
88
|
|
|
event=lambda_event, |
|
89
|
|
|
context=None |
|
90
|
|
|
) |
|
91
|
|
|
|
|
92
|
|
|
mock_rule.put_evaluations.assert_called_once_with(**put_evaluations_response) |
|
93
|
|
|
|