forked from nficano/python-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_create_function.py
More file actions
144 lines (120 loc) · 4.9 KB
/
test_create_function.py
File metadata and controls
144 lines (120 loc) · 4.9 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import pytest
import unittest
from unittest.mock import MagicMock
from aws_lambda.aws_lambda import create_function
class TestFunctionCreation(object):
@pytest.fixture
def setEnv(self, mocker):
mocker.patch.dict('os.environ', {
'S3_BUCKET_NAME': 'testBucket',
'LAMBDA_FUNCTION_NAME': 'testFunc'
})
@pytest.fixture
def cfgDict(self):
return {
'profile': 'testAWSProfile',
'aws_access_key_id': 'testKey',
'aws_secret_access_key': 'testSecret',
'region': 'testRegion',
'handler': 'test.py',
'runtime': 'testPython',
}
@pytest.fixture
def kwargDict(self):
return {
'FunctionName': 'testFunc',
'Runtime': 'testPython',
'Role': 'testRole',
'Handler': 'test.py',
'Code': {'ZipFile': 'bytes'},
'Description': '',
'Timeout': 15,
'MemorySize': 512,
'VpcConfig': {'SubnetIds': [], 'SecurityGroupIds': []},
'Publish': True
}
@pytest.fixture
def commonMocks(self, mocker):
mocker.patch('aws_lambda.aws_lambda.read', return_value='bytes')
mocker.patch('aws_lambda.aws_lambda.get_account_id')
mocker.patch(
'aws_lambda.aws_lambda.get_role_name',
return_value='testRole'
)
@pytest.fixture
def mockClient(self, mocker):
mockClient = MagicMock()
mocker.patch(
'aws_lambda.aws_lambda.get_client',
return_value=mockClient
)
return mockClient
def test_basicCreation(self, mocker, setEnv, cfgDict, kwargDict,
commonMocks, mockClient):
mocker.patch('aws_lambda.aws_lambda.get_concurrency', return_value=0)
create_function(cfgDict, 'testPath')
mockClient.create_function.assert_called_with(**kwargDict)
assert not mockClient.put_function_concurrency.called
def test_creationWithSubnetSecurity(self, mocker, setEnv, cfgDict,
kwargDict, commonMocks, mockClient):
mocker.patch('aws_lambda.aws_lambda.get_concurrency', return_value=0)
cfgDict['subnet_ids'] = 'sub1, sub2'
cfgDict['security_group_ids'] = 'group1, group2'
kwargDict['VpcConfig']['SubnetIds'] = ['sub1', 'sub2']
kwargDict['VpcConfig']['SecurityGroupIds'] = ['group1', 'group2']
create_function(cfgDict, 'testPath')
mockClient.create_function.assert_called_with(**kwargDict)
assert not mockClient.put_function_concurrency.called
def test_s3Creation(self, mocker, setEnv, cfgDict, kwargDict, commonMocks,
mockClient):
mocker.patch('aws_lambda.aws_lambda.get_concurrency', return_value=0)
kwargDict['Code'] = {
'S3Bucket': 'testBucket',
'S3Key': 'testFile'
}
create_function(cfgDict, 'testPath', use_s3=True, s3_file='testFile')
mockClient.create_function.assert_called_with(**kwargDict)
assert not mockClient.put_function_concurrency.called
def test_createWithConcurrency(self, mocker, setEnv, cfgDict, kwargDict,
commonMocks, mockClient):
mocker.patch('aws_lambda.aws_lambda.get_concurrency', return_value=1)
create_function(cfgDict, 'testPath')
mockClient.create_function.assert_called_with(**kwargDict)
mockClient.put_function_concurrency.assert_called_with(**{
'FunctionName': 'testFunc',
'ReservedConcurrentExecutions': 1
})
def test_createWithTags(self, mocker, setEnv, cfgDict, kwargDict,
commonMocks, mockClient):
mocker.patch('aws_lambda.aws_lambda.get_concurrency', return_value=0)
cfgDict['tags'] = {
'test': 'test'
}
kwargDict['Tags'] = {
'test': 'test'
}
create_function(cfgDict, 'testPath')
mockClient.create_function.assert_called_with(**kwargDict)
assert not mockClient.put_function_concurrency.called
def test_createWithEnvVars(self, mocker, setEnv, cfgDict, kwargDict,
commonMocks, mockClient):
mocker.patch('aws_lambda.aws_lambda.get_concurrency', return_value=0)
mocker.patch(
'aws_lambda.aws_lambda.get_environment_variable_value',
side_effect=[1, 'testing']
)
cfgDict['environment_variables'] = {
'numVar': '1',
'txtVar': 'testing'
}
kwargDict['Environment'] = {
'Variables': {
'numVar': 1,
'txtVar': 'testing'
}
}
create_function(cfgDict, 'testPath')
mockClient.create_function.assert_called_with(**kwargDict)
assert not mockClient.put_function_concurrency.called
if __name__ == '__main__':
unittest.main()