[cloud] bugfix for lambda module with empty environment (#22196)

* lambda module - failing test case: shows lambda create mishandles empty environment

* lambda module - fix lambda create with empty environment

* lambda module - fix lambda create with empty environment - style fixes
This commit is contained in:
mikedlr 2017-03-02 14:25:39 +00:00 committed by Ryan Brown
parent 7d397e7d3e
commit 710b6c7209
2 changed files with 48 additions and 1 deletions

View file

@ -454,9 +454,11 @@ def main():
'Code': code, 'Code': code,
'Timeout': timeout, 'Timeout': timeout,
'MemorySize': memory_size, 'MemorySize': memory_size,
'Environment':{'Variables': environment_variables}
} }
if environment_variables:
func_kwargs.update({'Environment': {'Variables': environment_variables}})
if dead_letter_arn: if dead_letter_arn:
func_kwargs.update({'DeadLetterConfig': {'TargetARN': dead_letter_arn}}) func_kwargs.update({'DeadLetterConfig': {'TargetARN': dead_letter_arn}})

View file

@ -68,6 +68,20 @@ base_module_args={
} }
def make_mock_no_connection_connection(config):
"""return a mock of ansible's boto3_conn ready to return a mock AWS API client"""
lambda_client_double = MagicMock()
lambda_client_double.get_function.configure_mock(
return_value=False
)
lambda_client_double.update_function_configuration.configure_mock(
return_value={
'Version' : 1
}
)
fake_boto3_conn=Mock(return_value=lambda_client_double)
return (fake_boto3_conn, lambda_client_double)
def make_mock_connection(config): def make_mock_connection(config):
"""return a mock of ansible's boto3_conn ready to return a mock AWS API client""" """return a mock of ansible's boto3_conn ready to return a mock AWS API client"""
lambda_client_double = MagicMock() lambda_client_double = MagicMock()
@ -98,6 +112,37 @@ def fail_json_double(*args, **kwargs):
#TODO: def test_handle_different_types_in_config_params(): #TODO: def test_handle_different_types_in_config_params():
def test_create_lambda_if_not_exist():
set_module_args(base_module_args)
(boto3_conn_double, lambda_client_double)=make_mock_no_connection_connection(code_change_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), \
"unexpectedly updated lambda configuration when should have only created"
assert(len(lambda_client_double.update_function_code.mock_calls) == 0), \
"update lambda function code when function should have been created only"
assert(len(lambda_client_double.create_function.mock_calls) > 0), \
"failed to call create_function "
(create_args, create_kwargs)=lambda_client_double.create_function.call_args
assert (len(create_kwargs) > 0), "expected create called with keyword args, none found"
try:
# For now I assume that we should NOT send an empty environment. It might
# be okay / better to explicitly send an empty environment. However `None'
# is not acceptable - mikedlr
create_kwargs["Environment"]
raise(Exception("Environment sent to boto when none expected"))
except KeyError:
pass #We are happy, no environment is fine
def test_update_lambda_if_code_changed(): def test_update_lambda_if_code_changed():
set_module_args(base_module_args) set_module_args(base_module_args)