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) >
            0), "boto connections never used"
    assert (len(boto3_conn_double.mock_calls) <
            2), "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_update_lambda_if_code_changed():

    set_module_args(base_module_args)
    (boto3_conn_double,
     lambda_client_double) = make_mock_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) >
            0), "boto connections never used"
    assert (len(boto3_conn_double.mock_calls) <
            2), "multiple boto connections used unexpectedly"
    assert(len(lambda_client_double.update_function_configuration.mock_calls) == 0), \
        "unexpectedly updatede lambda configuration when only code 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) > 1), \
        "failed to update lambda function when code changed"
    # 3 because after uploading we call into the return from mock to try to find what function version
    # was returned so the MagicMock actually sees two calls for one update.
    assert(len(lambda_client_double.update_function_code.mock_calls) < 3), \
        "lambda function code update called multiple times when only one time should be needed"
def test_warn_region_not_specified():

    set_module_args({
        "name": "lambda_name",
        "state": "present",
        # Module is called without a region causing error
        # "region": "us-east-1",
        "zip_file": "test/units/modules/cloud/amazon/fixtures/thezip.zip",
        "runtime": 'python2.7',
        "role": 'arn:aws:iam::987654321012:role/lambda_basic_execution',
        "handler": 'lambda_python.my_handler'
    })

    get_aws_connection_info_double = Mock(return_value=(None, None, None))

    with patch.object(lda, 'get_aws_connection_info',
                      get_aws_connection_info_double):
        with patch.object(basic.AnsibleModule, 'fail_json', fail_json_double):
            try:
                lda.main()
            except AnsibleFailJson as e:
                result = e.args[0]
                assert ("region must be specified" in result['msg'])
def test_dont_update_lambda_if_nothing_changed():
    set_module_args(base_module_args)
    (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) >
            0), "boto connections never used"
    assert (len(boto3_conn_double.mock_calls) <
            2), "multiple boto connections used unexpectedly"
    assert(len(lambda_client_double.update_function_configuration.mock_calls) == 0), \
        "updated lambda function when no configuration changed"
    assert(len(lambda_client_double.update_function_code.mock_calls) == 0), \
        "updated lambda code when no change should have happened"
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) >
            0), "boto connections never used"
    assert (len(boto3_conn_double.mock_calls) <
            2), "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