"""Integration tests for Fargate task.""" import os import boto3 from botocore.exceptions import ClientError from mypy_boto3_s3.client import S3Client from src.config import ( APPLICATION_NAME, ENVIRONMENT, EXECUTION_NAME, EXPORT_ID, S3_INPUT_BUCKET, S3_OUTPUT_BUCKET, ) ASSETS_DIR = os.path.join( os.path.dirname(os.path.realpath(__file__)), "assets", ) s3_client: S3Client = boto3.client("s3") ecs_client = boto3.client("ecs", region_name="us-east-1") def upload_asset_to_s3() -> bool | None: bucket = "qa-orcd-mezzanine-assets" for root, _, files in os.walk(ASSETS_DIR): files.remove("asset_locations_1.json") for file_name in files: local_file_path = os.path.join(root, file_name) s3_key = os.path.relpath(local_file_path, ASSETS_DIR) try: s3_client.upload_file(local_file_path, bucket, s3_key) print( f"Successfully uploaded {local_file_path} to s3://{bucket}/{s3_key}" ) except Exception as e: print(f"Error uploading {local_file_path}: {e}") return False return True def upload_json_to_s3() -> bool: local_file_path = f"{ASSETS_DIR}/asset_locations_1.json" s3_key = f"{EXPORT_ID}/{EXECUTION_NAME}/asset_locations_1.json" try: s3_client.upload_file(local_file_path, S3_INPUT_BUCKET, s3_key) print( f"Successfully uploaded {local_file_path} to s3://{S3_INPUT_BUCKET}/{s3_key}" ) return True except Exception as e: print(f"Error uploading {local_file_path}: {e}") return False def load_assets_from_s3() -> bool | None: destination_file = f"{EXPORT_ID}/197187827862/197187827862_1_1.wav" try: s3_client.head_object(Bucket=S3_OUTPUT_BUCKET, Key=destination_file) return True except ClientError as e: # If a 404 error is returned, the object does not exist. if e.response["Error"]["Code"] == "404": return False return None def test_asset_copy_worker_execution(get_network_config: dict[str, str]) -> None: cluster_name = f"{ENVIRONMENT}-{APPLICATION_NAME}" # task_definition_family = "ACTIVE" upload_asset_to_s3() upload_json_to_s3() # Run the Fargate task response = ecs_client.run_task( cluster=cluster_name, taskDefinition=cluster_name, count=1, launchType="FARGATE", networkConfiguration=get_network_config, overrides={ "containerOverrides": [ { "name": APPLICATION_NAME, "environment": [ {"name": "execution_name", "value": "test-execution-1"}, {"name": "export_id", "value": "test-export-456"}, {"name": "part_index", "value": "1"}, { "name": "S3_INPUT_BUCKET", "value": "qa-bulk-asset-download-input", }, { "name": "S3_OUTPUT_BUCKET", "value": "qa-bulk-asset-download-output", }, ], } ] }, ) # Assert task started successfully print(f"============={response['tasks'][0]['lastStatus']} =================") assert response["tasks"][0]["lastStatus"] in ["PROVISIONING", "PENDING", "RUNNING"] task_arn = response["tasks"][0]["taskArn"] ecs_client.get_waiter("tasks_stopped").wait(cluster=cluster_name, tasks=[task_arn]) stopped_task = ecs_client.describe_tasks(cluster=cluster_name, tasks=[task_arn])[ "tasks" ][0] assert stopped_task["lastStatus"] == "STOPPED" assert stopped_task["stoppedReason"] == "Essential container in task exited" # Check asset copied to destination bucket by asset_copy_worker task. output_bucket_result = load_assets_from_s3() assert output_bucket_result is True # delete json file json_file = f"{EXPORT_ID}/{EXECUTION_NAME}/asset_locations_1.json" s3_client.delete_object(Bucket=S3_INPUT_BUCKET, Key=json_file) # delete assets s3_client.delete_objects( Bucket="qa-orcd-mezzanine-assets", Delete={ "Objects": [ {"Key": "test_tif_image.tif"}, {"Key": "test1.wav"}, {"Key": "test2.wav"}, ] }, )