"""Test transfer from s3 to storage.""" import json import os from unittest.mock import MagicMock from boto3.s3 import transfer from daemon_asset_copy.connectors import s3 from daemon_asset_copy.constants import job_io_fields from daemon_asset_copy.logic.tasks import transfer_files def test_transfer_from_s3_to_s3(mocker): """Test transfer_from_s3_to_s3.""" mock_s3_source_client = MagicMock() mock_s3_source_client.get_object.return_value = {'Body': 'source-data'} mock_s3_destination_client = MagicMock() mock_get_s3_client = mocker.patch.object( s3, 'get_s3_client', side_effect=[mock_s3_source_client, mock_s3_destination_client], autospec=True, ) mock_transfer_config = mocker.patch.object( transfer, 'TransferConfig', autospec=True, ) env_mock = mocker.patch.object(os, 'getenv', autospec=True) mock_destination_creds = { 'endpoint_url': 'aaa', 'aws_access_key_id': 'bbb', 'aws_secet_access_key': 'ccc', } env_mock.return_value = json.dumps(mock_destination_creds) result = transfer_files.transfer_from_s3_to_s3( { job_io_fields.S3_SOURCE_BUCKET: 'abc', job_io_fields.S3_SOURCE_KEY: 'def', job_io_fields.S3_DESTINATION_BUCKET: 'ghi', job_io_fields.S3_DESTINATION_KEY: 'jkl', job_io_fields.PHYSICAL_LOCATION_ID: 9, } ) env_mock.assert_called_once_with('STORAGE_CREDENTIALS') mock_get_s3_client.assert_has_calls( [ mocker.call(), mocker.call( { 'endpoint_url': 'aaa', 'aws_access_key_id': 'bbb', 'aws_secet_access_key': 'ccc', } ), ] ) mock_s3_source_client.get_object.assert_called_once_with(Bucket='abc', Key='def') mock_s3_destination_client.upload_fileobj.assert_called_once_with( 'source-data', 'ghi', 'jkl', Config=mock_transfer_config() ) mock_s3_destination_client.get_object.assert_called_once_with( Bucket='ghi', Key='jkl' ) assert result == { 'destination_file_name': 'jkl', } def test_transfer_from_s3_to_s3_wav(test_audio_asset_file_location, mocker): """Test transfer_from_s3_to_s3 for wav.""" mock_s3_source_client = MagicMock() mock_s3_source_client.get_object.return_value = {'Body': 'source-data'} mock_s3_destination_client = MagicMock() mock_s3_destination_client.get_object.return_value = { 'Body': open(test_audio_asset_file_location, 'rb'), 'ContentLength': os.path.getsize(test_audio_asset_file_location), } mocker.patch.object( s3, 'get_s3_client', side_effect=[mock_s3_source_client, mock_s3_destination_client], autospec=True, ) env_mock = mocker.patch.object(os, 'getenv', autospec=True) env_mock.return_value = '{}' params = { job_io_fields.S3_SOURCE_BUCKET: 'source1', job_io_fields.S3_SOURCE_KEY: 'file1.wav', job_io_fields.S3_DESTINATION_BUCKET: 'dest1', job_io_fields.S3_DESTINATION_KEY: 'file1.wav', job_io_fields.PHYSICAL_LOCATION_ID: 9, } result = transfer_files.transfer_from_s3_to_s3(params) assert result == { 'destination_file_name': params[job_io_fields.S3_DESTINATION_KEY], 'file_size': 1210892, 'md5_hash': 'a907b1b6164dae3dee9f3027e4db9230', } def test_transfer_from_s3_to_s3_tif(test_tif_asset_file_location, mocker): """Test test_transfer_from_s3_to_s3 for tif.""" mock_s3_source_client = MagicMock() mock_s3_source_client.get_object.return_value = {'Body': 'source-data'} mock_s3_destination_client = MagicMock() mock_s3_destination_client.get_object.return_value = { 'Body': open(test_tif_asset_file_location, 'rb'), 'ContentLength': os.path.getsize(test_tif_asset_file_location), } mocker.patch.object( s3, 'get_s3_client', side_effect=[mock_s3_source_client, mock_s3_destination_client], autospec=True, ) env_mock = mocker.patch.object(os, 'getenv', autospec=True) env_mock.return_value = '{}' params = { job_io_fields.S3_SOURCE_BUCKET: 'source1', job_io_fields.S3_SOURCE_KEY: 'file1.tif', job_io_fields.S3_DESTINATION_BUCKET: 'dest1', job_io_fields.S3_DESTINATION_KEY: 'file1.tif', job_io_fields.PHYSICAL_LOCATION_ID: 9, } result = transfer_files.transfer_from_s3_to_s3(params) assert result == { 'destination_file_name': params[job_io_fields.S3_DESTINATION_KEY], 'file_size': 1401916, 'md5_hash': 'dfa332b4702d025cf6a966ea0195c76e', 'image_height': 800, 'image_width': 584, }