| 1 |
from unittest.mock import patch |
| 2 |
|
| 3 |
import boto3 |
| 4 |
import pytest |
| 5 |
from botocore.exceptions import ClientError |
| 6 |
|
| 7 |
try: |
| 8 |
from moto import mock_aws |
| 9 |
except ImportError: |
| 10 |
from moto import mock_s3 as mock_aws |
| 11 |
|
| 12 |
from celery import states |
| 13 |
from celery.backends.s3 import S3Backend |
| 14 |
from celery.exceptions import ImproperlyConfigured |
| 15 |
|
| 16 |
|
| 17 |
class test_S3Backend: |
| 18 |
|
| 19 |
@patch('botocore.credentials.CredentialResolver.load_credentials') |
| 20 |
def test_with_missing_aws_credentials(self, mock_load_credentials): |
| 21 |
self.app.conf.s3_access_key_id = None |
| 22 |
self.app.conf.s3_secret_access_key = None |
| 23 |
self.app.conf.s3_bucket = 'bucket' |
| 24 |
|
| 25 |
mock_load_credentials.return_value = None |
| 26 |
|
| 27 |
with pytest.raises(ImproperlyConfigured, match="Missing aws s3 creds"): |
| 28 |
S3Backend(app=self.app) |
| 29 |
|
| 30 |
@patch('botocore.credentials.CredentialResolver.load_credentials') |
| 31 |
def test_with_no_credentials_in_config_attempts_to_load_credentials(self, mock_load_credentials): |
| 32 |
self.app.conf.s3_access_key_id = None |
| 33 |
self.app.conf.s3_secret_access_key = None |
| 34 |
self.app.conf.s3_bucket = 'bucket' |
| 35 |
|
| 36 |
S3Backend(app=self.app) |
| 37 |
mock_load_credentials.assert_called_once() |
| 38 |
|
| 39 |
@patch('botocore.credentials.CredentialResolver.load_credentials') |
| 40 |
def test_with_credentials_in_config_does_not_search_for_credentials(self, mock_load_credentials): |
| 41 |
self.app.conf.s3_access_key_id = 'somekeyid' |
| 42 |
self.app.conf.s3_secret_access_key = 'somesecret' |
| 43 |
self.app.conf.s3_bucket = 'bucket' |
| 44 |
|
| 45 |
S3Backend(app=self.app) |
| 46 |
mock_load_credentials.assert_not_called() |
| 47 |
|
| 48 |
def test_with_no_given_bucket(self): |
| 49 |
self.app.conf.s3_access_key_id = 'somekeyid' |
| 50 |
self.app.conf.s3_secret_access_key = 'somesecret' |
| 51 |
self.app.conf.s3_bucket = None |
| 52 |
|
| 53 |
with pytest.raises(ImproperlyConfigured, match='Missing bucket name'): |
| 54 |
S3Backend(app=self.app) |
| 55 |
|
| 56 |
@pytest.mark.parametrize('aws_region', |
| 57 |
[None, 'us-east-1'], |
| 58 |
ids=['No given aws region', |
| 59 |
'Specific aws region']) |
| 60 |
@patch('celery.backends.s3.boto3') |
| 61 |
def test_it_creates_an_aws_s3_connection(self, mock_boto3, aws_region): |
| 62 |
self.app.conf.s3_access_key_id = 'somekeyid' |
| 63 |
self.app.conf.s3_secret_access_key = 'somesecret' |
| 64 |
self.app.conf.s3_bucket = 'bucket' |
| 65 |
self.app.conf.s3_region = aws_region |
| 66 |
|
| 67 |
S3Backend(app=self.app) |
| 68 |
mock_boto3.Session.assert_called_once_with( |
| 69 |
aws_access_key_id='somekeyid', |
| 70 |
aws_secret_access_key='somesecret', |
| 71 |
region_name=aws_region) |
| 72 |
|
| 73 |
@pytest.mark.parametrize('endpoint_url', |
| 74 |
[None, 'https://custom.s3'], |
| 75 |
ids=['No given endpoint url', |
| 76 |
'Custom endpoint url']) |
| 77 |
@patch('celery.backends.s3.boto3') |
| 78 |
def test_it_creates_an_aws_s3_resource(self, |
| 79 |
mock_boto3, |
| 80 |
endpoint_url): |
| 81 |
self.app.conf.s3_access_key_id = 'somekeyid' |
| 82 |
self.app.conf.s3_secret_access_key = 'somesecret' |
| 83 |
self.app.conf.s3_bucket = 'bucket' |
| 84 |
self.app.conf.s3_endpoint_url = endpoint_url |
| 85 |
|
| 86 |
S3Backend(app=self.app) |
| 87 |
mock_boto3.Session().resource.assert_called_once_with( |
| 88 |
's3', endpoint_url=endpoint_url) |
| 89 |
|
| 90 |
@pytest.mark.parametrize("key", ['uuid', b'uuid']) |
| 91 |
@mock_aws |
| 92 |
def test_set_and_get_a_key(self, key): |
| 93 |
self._mock_s3_resource() |
| 94 |
|
| 95 |
self.app.conf.s3_access_key_id = 'somekeyid' |
| 96 |
self.app.conf.s3_secret_access_key = 'somesecret' |
| 97 |
self.app.conf.s3_bucket = 'bucket' |
| 98 |
|
| 99 |
s3_backend = S3Backend(app=self.app) |
| 100 |
s3_backend._set_with_state(key, 'another_status', states.SUCCESS) |
| 101 |
|
| 102 |
assert s3_backend.get(key) == 'another_status' |
| 103 |
|
| 104 |
@mock_aws |
| 105 |
def test_set_and_get_a_result(self): |
| 106 |
self._mock_s3_resource() |
| 107 |
|
| 108 |
self.app.conf.result_serializer = 'pickle' |
| 109 |
self.app.conf.s3_access_key_id = 'somekeyid' |
| 110 |
self.app.conf.s3_secret_access_key = 'somesecret' |
| 111 |
self.app.conf.s3_bucket = 'bucket' |
| 112 |
|
| 113 |
s3_backend = S3Backend(app=self.app) |
| 114 |
s3_backend.store_result('foo', 'baar', 'STARTED') |
| 115 |
value = s3_backend.get_result('foo') |
| 116 |
assert value == 'baar' |
| 117 |
|
| 118 |
@mock_aws |
| 119 |
def test_get_a_missing_key(self): |
| 120 |
self._mock_s3_resource() |
| 121 |
|
| 122 |
self.app.conf.s3_access_key_id = 'somekeyid' |
| 123 |
self.app.conf.s3_secret_access_key = 'somesecret' |
| 124 |
self.app.conf.s3_bucket = 'bucket' |
| 125 |
|
| 126 |
s3_backend = S3Backend(app=self.app) |
| 127 |
result = s3_backend.get('uuidddd') |
| 128 |
|
| 129 |
assert result is None |
| 130 |
|
| 131 |
@patch('celery.backends.s3.boto3') |
| 132 |
def test_with_error_while_getting_key(self, mock_boto3): |
| 133 |
error = ClientError({'Error': {'Code': '403', |
| 134 |
'Message': 'Permission denied'}}, |
| 135 |
'error') |
| 136 |
mock_boto3.Session().resource().Object().load.side_effect = error |
| 137 |
|
| 138 |
self.app.conf.s3_access_key_id = 'somekeyid' |
| 139 |
self.app.conf.s3_secret_access_key = 'somesecret' |
| 140 |
self.app.conf.s3_bucket = 'bucket' |
| 141 |
|
| 142 |
s3_backend = S3Backend(app=self.app) |
| 143 |
|
| 144 |
with pytest.raises(ClientError): |
| 145 |
s3_backend.get('uuidddd') |
| 146 |
|
| 147 |
@pytest.mark.parametrize("key", ['uuid', b'uuid']) |
| 148 |
@mock_aws |
| 149 |
def test_delete_a_key(self, key): |
| 150 |
self._mock_s3_resource() |
| 151 |
|
| 152 |
self.app.conf.s3_access_key_id = 'somekeyid' |
| 153 |
self.app.conf.s3_secret_access_key = 'somesecret' |
| 154 |
self.app.conf.s3_bucket = 'bucket' |
| 155 |
|
| 156 |
s3_backend = S3Backend(app=self.app) |
| 157 |
s3_backend._set_with_state(key, 'another_status', states.SUCCESS) |
| 158 |
assert s3_backend.get(key) == 'another_status' |
| 159 |
|
| 160 |
s3_backend.delete(key) |
| 161 |
|
| 162 |
assert s3_backend.get(key) is None |
| 163 |
|
| 164 |
@mock_aws |
| 165 |
def test_with_a_non_existing_bucket(self): |
| 166 |
self._mock_s3_resource() |
| 167 |
|
| 168 |
self.app.conf.s3_access_key_id = 'somekeyid' |
| 169 |
self.app.conf.s3_secret_access_key = 'somesecret' |
| 170 |
self.app.conf.s3_bucket = 'bucket_not_exists' |
| 171 |
|
| 172 |
s3_backend = S3Backend(app=self.app) |
| 173 |
|
| 174 |
with pytest.raises(ClientError, |
| 175 |
match=r'.*The specified bucket does not exist'): |
| 176 |
s3_backend._set_with_state('uuid', 'another_status', states.SUCCESS) |
| 177 |
|
| 178 |
def _mock_s3_resource(self): |
| 179 |
# Create AWS s3 Bucket for moto. |
| 180 |
session = boto3.Session( |
| 181 |
aws_access_key_id='moto_key_id', |
| 182 |
aws_secret_access_key='moto_secret_key', |
| 183 |
region_name='us-east-1' |
| 184 |
) |
| 185 |
s3 = session.resource('s3') |
| 186 |
s3.create_bucket(Bucket='bucket') |