-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
67 lines (54 loc) · 2.53 KB
/
test.py
File metadata and controls
67 lines (54 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import json
import unittest
import rule_code
from botocore.stub import Stubber
from rule_util import aws_config
class TestHandlerCase(unittest.TestCase):
def setUp(self):
self.sampleInvokingEvent = {
'configurationItem': {
'configuration': {
'instanceType': 't3.micro',
},
'configurationItemCaptureTime': '2018-01-01T00:00:00.007Z',
'configurationItemStatus': 'ResourceDiscovered',
'resourceType': 'AWS::EC2::Instance',
'resourceId': 'resourceId',
},
'messageType': 'ConfigurationItemChangeNotification'
}
self.sampleEvent = {
'invokingEvent': json.dumps(self.sampleInvokingEvent),
'ruleParameters': '{"desiredInstanceType":"t3.micro"}',
'resultToken': 'result-token',
'eventLeftScope': False,
'executionRoleArn': 'arn:aws:iam::accountId:role/service-role/config-role',
'configRuleArn': 'arn:aws:config:region:accountId:config-rule/config-rule-id',
'configRuleName': 'configRuleName',
'configRuleId': 'configRuleId',
'accountId': 'accountId'
}
def evaluateConfiguration(self, testInvokingEvent, compliance):
testEvent = self.sampleEvent.copy()
testEvent['invokingEvent'] = json.dumps(testInvokingEvent);
expected_response = {
'FailedEvaluations': []
}
with Stubber(aws_config) as stubber:
stubber.add_response('put_evaluations', expected_response)
result = rule_code.lambda_handler(testEvent, {})
self.assertEqual(result, compliance)
def test_verify_noncompliant_resource(self):
testInvokingEvent = self.sampleInvokingEvent.copy()
testInvokingEvent['configurationItem']['configuration']['instanceType'] = 't3.small'
self.evaluateConfiguration(testInvokingEvent, 'NON_COMPLIANT')
def test_verify_compliant_resource(self):
testInvokingEvent = self.sampleInvokingEvent.copy()
testInvokingEvent['configurationItem']['configuration']['instanceType'] = 't3.micro'
self.evaluateConfiguration(testInvokingEvent, 'COMPLIANT')
def test_verify_nonapplicable_resource(self):
testInvokingEvent = self.sampleInvokingEvent.copy()
testInvokingEvent['configurationItem']['resourceType'] = 'AWS::SNS::Topic'
self.evaluateConfiguration(testInvokingEvent, 'NOT_APPLICABLE')
if __name__ == '__main__':
unittest.main()