"""Integration tests for lambda function.""" import json from pathlib import Path import boto3 import pytest from botocore.exceptions import ClientError import config from src.aws_utils import split_s3_path THIS_DIR = Path(__file__).parent def invoke_lambda(function_name, payload_bytes: bytes): """Invoke the lambda function.""" client = boto3.client('lambda') return client.invoke( FunctionName=function_name, InvocationType='RequestResponse', LogType='None', Payload=payload_bytes ) def test_invoke_lambda(): """Assert lambda action.""" event_path = THIS_DIR / 'qa-integration-event.json' event = json.loads(event_path.read_text()) s3_location = event['tasks'][0]['destination_url'] assert s3_location.startswith('s3://') bucket, s3_key = split_s3_path(s3_location) s3_client = boto3.client('s3') # cleanup destination key if exists s3_client.delete_object(Bucket=bucket, Key=s3_key) # ensure it does not exist with pytest.raises(ClientError) as exc_info: s3_client.head_object(Bucket=bucket, Key=s3_key) assert exc_info.value.response['Error']['Code'] == '404' result = invoke_lambda( function_name=config.APPLICATION_NAME, payload_bytes=event_path.read_bytes() ) assert result['StatusCode'] == 200 response_payload = json.load(result['Payload']) assert response_payload['return_code'] == 0 assert response_payload['n_tasks'] == 1 # check if s3 key exists response = s3_client.head_object(Bucket=bucket, Key=s3_key) assert response['ContentLength'] > 0