예제 #1
0
def logs_client(aws_region):
    with mock_logs():
        yield boto3.client("logs", region_name=aws_region)
예제 #2
0
def cw_client():
    with mock_logs():
        yield boto3.client('logs')
예제 #3
0
def cloudwatch_client(region):
    with mock_logs():
        yield boto3.client("logs", region_name=region)
예제 #4
0
def test_lambda_execute_ecs_task(mock=False):
    """
    Test for lambda function that creates ecs tasks
    :param mock: True: tests using moto lib, False: tests using real cloud env (make sure you have your default creds
    set)
    :return:
    """
    ms3 = mock_s3()
    ml = mock_lambda()
    mecs = mock_ecs()
    mlogs = mock_logs()
    if mock:
        ms3.start()
        ml.start()
        mecs.start()
        mlogs.start()

    bucket_name = 'testamolbucket'
    key_name = 'taskarn.list'  # path to file that contains task ARNs
    script_dir = os.getcwd()
    lambda_function_filepath = r'..\..\scripts'
    lambda_function_file = 'LambdaExecuteEcsTask.py'
    assert os.path.exists(
        os.path.join(lambda_function_filepath,
                     lambda_function_file)), 'File does not exist!'

    lambda_function_name = 'testfunction'
    lambda_execution_role = 'arn:aws:iam::123456789:role/test-amol-role-lambda'
    lambda_zip_file = '.'.join([lambda_function_file.split('.')[0], 'zip'])

    os.chdir(lambda_function_filepath)
    lambda_zip_filepath = r'..\test\outputs\{0}'.format(lambda_zip_file)
    with zipfile.ZipFile(lambda_zip_filepath, 'w') as myzip:
        myzip.write(lambda_function_file)
    _upload_file_to_bucket(lambda_zip_filepath, bucket_name, lambda_zip_file)
    _create_lambda_function(lambda_function_name,
                            'LambdaExecuteEcsTask.lambda_handler',
                            lambda_execution_role, lambda_zip_file,
                            bucket_name)

    s3_event_payload_data = {
        'Records': [{
            's3': {
                'object': {
                    'key': 'testkey'
                },
                'bucket': {
                    'name': bucket_name
                }
            }
        }]
    }

    os.chdir(script_dir)
    _upload_file_to_bucket(key_name, bucket_name, key_name)

    if not mock:
        # lambda function which uses boto lib cannot be mocked, but invoking the lambda can be mocked in any case
        # Note: This lambda function invocation has already been validated in real AWS environment.
        _invoke_lambda_function('testfunction',
                                json.dumps(s3_event_payload_data))