| 1 |
from unittest.mock import Mock, call, patch |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from celery import states |
| 6 |
from celery.backends import azureblockblob |
| 7 |
from celery.backends.azureblockblob import AzureBlockBlobBackend |
| 8 |
from celery.exceptions import ImproperlyConfigured |
| 9 |
|
| 10 |
MODULE_TO_MOCK = "celery.backends.azureblockblob" |
| 11 |
|
| 12 |
pytest.importorskip('azure.storage.blob') |
| 13 |
pytest.importorskip('azure.core.exceptions') |
| 14 |
|
| 15 |
|
| 16 |
class test_AzureBlockBlobBackend: |
| 17 |
def setup_method(self): |
| 18 |
self.url = ( |
| 19 |
"azureblockblob://" |
| 20 |
"DefaultEndpointsProtocol=protocol;" |
| 21 |
"AccountName=name;" |
| 22 |
"AccountKey=key;" |
| 23 |
"EndpointSuffix=suffix") |
| 24 |
|
| 25 |
self.backend = AzureBlockBlobBackend( |
| 26 |
app=self.app, |
| 27 |
url=self.url) |
| 28 |
|
| 29 |
@pytest.fixture(params=['', 'my_folder/']) |
| 30 |
def base_path(self, request): |
| 31 |
return request.param |
| 32 |
|
| 33 |
def test_missing_third_party_sdk(self): |
| 34 |
azurestorage = azureblockblob.azurestorage |
| 35 |
try: |
| 36 |
azureblockblob.azurestorage = None |
| 37 |
with pytest.raises(ImproperlyConfigured): |
| 38 |
AzureBlockBlobBackend(app=self.app, url=self.url) |
| 39 |
finally: |
| 40 |
azureblockblob.azurestorage = azurestorage |
| 41 |
|
| 42 |
def test_bad_connection_url(self): |
| 43 |
with pytest.raises(ImproperlyConfigured): |
| 44 |
AzureBlockBlobBackend._parse_url("azureblockblob://") |
| 45 |
|
| 46 |
with pytest.raises(ImproperlyConfigured): |
| 47 |
AzureBlockBlobBackend._parse_url("") |
| 48 |
|
| 49 |
@patch(MODULE_TO_MOCK + ".BlobServiceClient") |
| 50 |
def test_create_client(self, mock_blob_service_factory): |
| 51 |
mock_blob_service_client_instance = Mock() |
| 52 |
mock_blob_service_factory.from_connection_string.return_value = mock_blob_service_client_instance |
| 53 |
backend = AzureBlockBlobBackend(app=self.app, url=self.url) |
| 54 |
|
| 55 |
# ensure container gets created on client access... |
| 56 |
assert mock_blob_service_client_instance.create_container.call_count == 0 |
| 57 |
assert backend._blob_service_client is not None |
| 58 |
assert mock_blob_service_client_instance.create_container.call_count == 1 |
| 59 |
|
| 60 |
# ...but only once per backend instance |
| 61 |
assert backend._blob_service_client is not None |
| 62 |
assert mock_blob_service_client_instance.create_container.call_count == 1 |
| 63 |
|
| 64 |
@patch(MODULE_TO_MOCK + ".AzureStorageQueuesTransport") |
| 65 |
@patch(MODULE_TO_MOCK + ".BlobServiceClient") |
| 66 |
def test_create_client__default_azure_credentials(self, mock_blob_service_client, mock_kombu_transport): |
| 67 |
credential_mock = Mock() |
| 68 |
mock_blob_service_client.return_value = Mock() |
| 69 |
mock_kombu_transport.parse_uri.return_value = (credential_mock, "dummy_account_url") |
| 70 |
url = "azureblockblob://DefaultAzureCredential@dummy_account_url" |
| 71 |
backend = AzureBlockBlobBackend(app=self.app, url=url) |
| 72 |
assert backend._blob_service_client is not None |
| 73 |
mock_kombu_transport.parse_uri.assert_called_once_with(url.replace("azureblockblob://", "")) |
| 74 |
mock_blob_service_client.assert_called_once_with( |
| 75 |
account_url="dummy_account_url", |
| 76 |
credential=credential_mock, |
| 77 |
connection_timeout=backend._connection_timeout, |
| 78 |
read_timeout=backend._read_timeout, |
| 79 |
) |
| 80 |
|
| 81 |
@patch(MODULE_TO_MOCK + ".AzureStorageQueuesTransport") |
| 82 |
@patch(MODULE_TO_MOCK + ".BlobServiceClient") |
| 83 |
def test_create_client__managed_identity_azure_credentials(self, mock_blob_service_client, mock_kombu_transport): |
| 84 |
credential_mock = Mock() |
| 85 |
mock_blob_service_client.return_value = Mock() |
| 86 |
mock_kombu_transport.parse_uri.return_value = (credential_mock, "dummy_account_url") |
| 87 |
url = "azureblockblob://ManagedIdentityCredential@dummy_account_url" |
| 88 |
backend = AzureBlockBlobBackend(app=self.app, url=url) |
| 89 |
assert backend._blob_service_client is not None |
| 90 |
mock_kombu_transport.parse_uri.assert_called_once_with(url.replace("azureblockblob://", "")) |
| 91 |
mock_blob_service_client.assert_called_once_with( |
| 92 |
account_url="dummy_account_url", |
| 93 |
credential=credential_mock, |
| 94 |
connection_timeout=backend._connection_timeout, |
| 95 |
read_timeout=backend._read_timeout, |
| 96 |
) |
| 97 |
|
| 98 |
@patch(MODULE_TO_MOCK + ".BlobServiceClient") |
| 99 |
def test_configure_client(self, mock_blob_service_factory): |
| 100 |
|
| 101 |
connection_timeout = 3 |
| 102 |
read_timeout = 11 |
| 103 |
self.app.conf.update( |
| 104 |
{ |
| 105 |
'azureblockblob_connection_timeout': connection_timeout, |
| 106 |
'azureblockblob_read_timeout': read_timeout, |
| 107 |
} |
| 108 |
) |
| 109 |
|
| 110 |
mock_blob_service_client_instance = Mock() |
| 111 |
mock_blob_service_factory.from_connection_string.return_value = ( |
| 112 |
mock_blob_service_client_instance |
| 113 |
) |
| 114 |
|
| 115 |
base_url = "azureblockblob://" |
| 116 |
connection_string = "connection_string" |
| 117 |
backend = AzureBlockBlobBackend( |
| 118 |
app=self.app, url=f'{base_url}{connection_string}' |
| 119 |
) |
| 120 |
|
| 121 |
client = backend._blob_service_client |
| 122 |
assert client is mock_blob_service_client_instance |
| 123 |
|
| 124 |
( |
| 125 |
mock_blob_service_factory |
| 126 |
.from_connection_string |
| 127 |
.assert_called_once_with( |
| 128 |
connection_string, |
| 129 |
connection_timeout=connection_timeout, |
| 130 |
read_timeout=read_timeout |
| 131 |
) |
| 132 |
) |
| 133 |
|
| 134 |
@patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._blob_service_client") |
| 135 |
def test_get(self, mock_client, base_path): |
| 136 |
self.backend.base_path = base_path |
| 137 |
self.backend.get(b"mykey") |
| 138 |
|
| 139 |
mock_client.get_blob_client \ |
| 140 |
.assert_called_once_with(blob=base_path + "mykey", container="celery") |
| 141 |
|
| 142 |
mock_client.get_blob_client.return_value \ |
| 143 |
.download_blob.return_value \ |
| 144 |
.readall.return_value \ |
| 145 |
.decode.assert_called_once() |
| 146 |
|
| 147 |
@patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._blob_service_client") |
| 148 |
def test_get_missing(self, mock_client): |
| 149 |
mock_client.get_blob_client.return_value \ |
| 150 |
.download_blob.return_value \ |
| 151 |
.readall.side_effect = azureblockblob.ResourceNotFoundError |
| 152 |
|
| 153 |
assert self.backend.get(b"mykey") is None |
| 154 |
|
| 155 |
@patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._blob_service_client") |
| 156 |
def test_set(self, mock_client, base_path): |
| 157 |
self.backend.base_path = base_path |
| 158 |
self.backend._set_with_state(b"mykey", "myvalue", states.SUCCESS) |
| 159 |
|
| 160 |
mock_client.get_blob_client.assert_called_once_with( |
| 161 |
container="celery", blob=base_path + "mykey") |
| 162 |
|
| 163 |
mock_client.get_blob_client.return_value \ |
| 164 |
.upload_blob.assert_called_once_with("myvalue", overwrite=True) |
| 165 |
|
| 166 |
@patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._blob_service_client") |
| 167 |
def test_mget(self, mock_client, base_path): |
| 168 |
keys = [b"mykey1", b"mykey2"] |
| 169 |
|
| 170 |
self.backend.base_path = base_path |
| 171 |
self.backend.mget(keys) |
| 172 |
|
| 173 |
mock_client.get_blob_client.assert_has_calls( |
| 174 |
[call(blob=base_path + key.decode(), container='celery') for key in keys], |
| 175 |
any_order=True,) |
| 176 |
|
| 177 |
@patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._blob_service_client") |
| 178 |
def test_delete(self, mock_client, base_path): |
| 179 |
self.backend.base_path = base_path |
| 180 |
self.backend.delete(b"mykey") |
| 181 |
|
| 182 |
mock_client.get_blob_client.assert_called_once_with( |
| 183 |
container="celery", blob=base_path + "mykey") |
| 184 |
|
| 185 |
mock_client.get_blob_client.return_value \ |
| 186 |
.delete_blob.assert_called_once() |
| 187 |
|
| 188 |
def test_base_path_conf(self, base_path): |
| 189 |
self.app.conf.azureblockblob_base_path = base_path |
| 190 |
backend = AzureBlockBlobBackend( |
| 191 |
app=self.app, |
| 192 |
url=self.url |
| 193 |
) |
| 194 |
assert backend.base_path == base_path |
| 195 |
|
| 196 |
def test_base_path_conf_default(self): |
| 197 |
backend = AzureBlockBlobBackend( |
| 198 |
app=self.app, |
| 199 |
url=self.url |
| 200 |
) |
| 201 |
assert backend.base_path == '' |
| 202 |
|
| 203 |
|
| 204 |
class test_as_uri: |
| 205 |
def setup_method(self): |
| 206 |
self.url = ( |
| 207 |
"azureblockblob://" |
| 208 |
"DefaultEndpointsProtocol=protocol;" |
| 209 |
"AccountName=name;" |
| 210 |
"AccountKey=account_key;" |
| 211 |
"EndpointSuffix=suffix" |
| 212 |
) |
| 213 |
self.backend = AzureBlockBlobBackend( |
| 214 |
app=self.app, |
| 215 |
url=self.url |
| 216 |
) |
| 217 |
|
| 218 |
def test_as_uri_include_password(self): |
| 219 |
assert self.backend.as_uri(include_password=True) == self.url |
| 220 |
|
| 221 |
def test_as_uri_exclude_password(self): |
| 222 |
assert self.backend.as_uri(include_password=False) == ( |
| 223 |
"azureblockblob://" |
| 224 |
"DefaultEndpointsProtocol=protocol;" |
| 225 |
"AccountName=name;" |
| 226 |
"AccountKey=**;" |
| 227 |
"EndpointSuffix=suffix" |
| 228 |
) |