lambda: Fix adding environment variables to functions previously not having any (#22313)

This commit is contained in:
Michał Masłowski 2017-03-06 17:10:40 +01:00 committed by Ryan Brown
parent 2d10ad4a3e
commit 228131d1cd
2 changed files with 28 additions and 1 deletions

View file

@ -330,7 +330,7 @@ def main():
func_kwargs.update({'Timeout': timeout}) func_kwargs.update({'Timeout': timeout})
if memory_size and current_config['MemorySize'] != memory_size: if memory_size and current_config['MemorySize'] != memory_size:
func_kwargs.update({'MemorySize': memory_size}) func_kwargs.update({'MemorySize': memory_size})
if (environment_variables is not None) and (current_config['Environment']['Variables'] != environment_variables): if (environment_variables is not None) and (current_config.get('Environment', {}).get('Variables', {}) != environment_variables):
func_kwargs.update({'Environment':{'Variables': environment_variables}}) func_kwargs.update({'Environment':{'Variables': environment_variables}})
if dead_letter_arn is not None: if dead_letter_arn is not None:
if current_config.get('DeadLetterConfig'): if current_config.get('DeadLetterConfig'):

View file

@ -65,6 +65,9 @@ base_module_args={
"timeout" : 3, "timeout" : 3,
"handler": 'lambda_python.my_handler' "handler": 'lambda_python.my_handler'
} }
module_args_with_environment=dict(base_module_args, environment_variables={
"variable_name": "variable_value"
})
def make_mock_no_connection_connection(config): def make_mock_no_connection_connection(config):
@ -206,6 +209,30 @@ def test_update_lambda_if_only_one_config_item_changed():
assert(len(lambda_client_double.update_function_code.mock_calls) == 0), \ assert(len(lambda_client_double.update_function_code.mock_calls) == 0), \
"updated lambda code when no change should have happened" "updated lambda code when no change should have happened"
def test_update_lambda_if_added_environment_variable():
set_module_args(module_args_with_environment)
(boto3_conn_double,lambda_client_double)=make_mock_connection(base_lambda_config)
with patch.object(lda, 'boto3_conn', boto3_conn_double):
try:
lda.main()
except SystemExit:
pass
# guard against calling other than for a lambda connection (e.g. IAM)
assert(len(boto3_conn_double.mock_calls) == 1), "multiple boto connections used unexpectedly"
assert(len(lambda_client_double.update_function_configuration.mock_calls) > 0), \
"failed to update lambda function when configuration changed"
assert(len(lambda_client_double.update_function_configuration.mock_calls) < 2), \
"lambda function update called multiple times when only one time should be needed"
assert(len(lambda_client_double.update_function_code.mock_calls) == 0), \
"updated lambda code when no change should have happened"
(update_args, update_kwargs)=lambda_client_double.update_function_configuration.call_args
assert (len(update_kwargs) > 0), "expected update configuration called with keyword args, none found"
assert update_kwargs['Environment']['Variables'] == module_args_with_environment['environment_variables']
def test_dont_update_lambda_if_nothing_changed(): def test_dont_update_lambda_if_nothing_changed():
set_module_args(base_module_args) set_module_args(base_module_args)