"""Tests for S3 util functions.""" from unittest.mock import MagicMock from unittest.mock import patch from freezegun import freeze_time from garcon.activity import Activity from feed_sender.garcon_contrib.aws.utils import garcon_s3 as s3utils from feed_sender.util.aws import s3 @patch('feed_sender.util.aws.s3.S3Connection') @patch('feed_sender.util.aws.s3.Bucket') def test_check_s3_key_exist(mock_bucket, mock_s3_connection, monkeypatch): """Test s3.check_s3_key_exist function.""" mock_bucket_object = MagicMock() mock_key = MagicMock() mock_key.size = 111 mock_key.name = 'name' mock_key.md5 = '27fh83' mock_bucket_object.get_key = lambda s3_path: mock_key mock_bucket_object.list = lambda path: [mock_key] mock_bucket.return_value = mock_bucket_object monkeypatch.setattr( s3utils, 'extract_bucket_path', MagicMock(return_value=('bucket', 'bucket_path'))) resp = s3.check_s3_key_exist('s3://bucket/bucket_path') assert resp is True mock_bucket_object.list = lambda path: [] mock_bucket.return_value = mock_bucket_object resp = s3.check_s3_key_exist('s3://bucket/bucket_path') assert resp is False @freeze_time('2015-12-01 12:23:00') def test_wait_for_s3_objects_existence(monkeypatch): """Test s3.wait_for_s3_objects_existence task.""" monkeypatch.setattr( s3, 'check_s3_key_exist', MagicMock(return_value=True)) expected = s3.wait_for_s3_objects_existence(Activity(), 's3_path', 10, 1) assert expected.get('exists') @patch('feed_sender.util.aws.s3.s3utils.extract_bucket_path') @patch('feed_sender.util.aws.s3.boto3') @patch('feed_sender.util.aws.s3.gzip.open') def test_is_gzip_file_on_s3_corrupted( mock_gzip, mock_boto3, mock_s3utils): """Test is_gzip_file_on_s3_corrupted.""" mock_s3utils.return_value = ('test_bucket', 'test_key') mock_boto3.return_value = MagicMock() mock_pointer = MagicMock() mock_pointer.read.side_effect = Exception('error occurs.') mock_gzip.return_value.__enter__.return_value = mock_pointer actual = s3.is_gzip_file_on_s3_corrupted('s3://test_bucket/test_key') assert actual @patch('feed_sender.util.aws.s3.s3utils.extract_bucket_path') @patch('feed_sender.util.aws.s3.boto3') @patch('feed_sender.util.aws.s3.gzip.open') def test_gzip_file_on_s3_is_not_corrupted( mock_gzip, mock_boto3, mock_s3utils): """Test is_gzip_file_on_s3_corrupted file is not corrupted.""" mock_s3utils.return_value = ('test_bucket', 'test_key') mock_boto3.return_value = MagicMock() mock_pointer = MagicMock() mock_pointer.read.return_value = None mock_gzip.return_value.__enter__.return_value = mock_pointer actual = s3.is_gzip_file_on_s3_corrupted('s3://test_bucket/test_key') assert not actual