| 1 |
from datetime import datetime |
| 2 |
from pickle import dumps, loads |
| 3 |
from unittest.mock import Mock |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from celery import states |
| 8 |
from celery.exceptions import ImproperlyConfigured |
| 9 |
from celery.utils.objects import Bunch |
| 10 |
|
| 11 |
CASSANDRA_MODULES = [ |
| 12 |
'cassandra', |
| 13 |
'cassandra.auth', |
| 14 |
'cassandra.cluster', |
| 15 |
'cassandra.query', |
| 16 |
] |
| 17 |
|
| 18 |
|
| 19 |
class test_CassandraBackend: |
| 20 |
|
| 21 |
def setup_method(self): |
| 22 |
self.app.conf.update( |
| 23 |
cassandra_servers=['example.com'], |
| 24 |
cassandra_keyspace='celery', |
| 25 |
cassandra_table='task_results', |
| 26 |
) |
| 27 |
|
| 28 |
@pytest.mark.patched_module(*CASSANDRA_MODULES) |
| 29 |
def test_init_no_cassandra(self, module): |
| 30 |
# should raise ImproperlyConfigured when no python-driver |
| 31 |
# installed. |
| 32 |
from celery.backends import cassandra as mod |
| 33 |
prev, mod.cassandra = mod.cassandra, None |
| 34 |
try: |
| 35 |
with pytest.raises(ImproperlyConfigured): |
| 36 |
mod.CassandraBackend(app=self.app) |
| 37 |
finally: |
| 38 |
mod.cassandra = prev |
| 39 |
|
| 40 |
@pytest.mark.patched_module(*CASSANDRA_MODULES) |
| 41 |
def test_init_with_and_without_LOCAL_QUROM(self, module): |
| 42 |
from celery.backends import cassandra as mod |
| 43 |
mod.cassandra = Mock() |
| 44 |
|
| 45 |
cons = mod.cassandra.ConsistencyLevel = Bunch( |
| 46 |
LOCAL_QUORUM='foo', |
| 47 |
) |
| 48 |
|
| 49 |
self.app.conf.cassandra_read_consistency = 'LOCAL_FOO' |
| 50 |
self.app.conf.cassandra_write_consistency = 'LOCAL_FOO' |
| 51 |
|
| 52 |
mod.CassandraBackend(app=self.app) |
| 53 |
cons.LOCAL_FOO = 'bar' |
| 54 |
mod.CassandraBackend(app=self.app) |
| 55 |
|
| 56 |
# no servers and no bundle_path raises ImproperlyConfigured |
| 57 |
with pytest.raises(ImproperlyConfigured): |
| 58 |
self.app.conf.cassandra_servers = None |
| 59 |
self.app.conf.cassandra_secure_bundle_path = None |
| 60 |
mod.CassandraBackend( |
| 61 |
app=self.app, keyspace='b', column_family='c', |
| 62 |
) |
| 63 |
|
| 64 |
# both servers no bundle_path raises ImproperlyConfigured |
| 65 |
with pytest.raises(ImproperlyConfigured): |
| 66 |
self.app.conf.cassandra_servers = ['localhost'] |
| 67 |
self.app.conf.cassandra_secure_bundle_path = ( |
| 68 |
'/home/user/secure-connect-bundle.zip') |
| 69 |
mod.CassandraBackend( |
| 70 |
app=self.app, keyspace='b', column_family='c', |
| 71 |
) |
| 72 |
|
| 73 |
def test_init_with_cloud(self): |
| 74 |
# Tests behavior when Cluster.connect works properly |
| 75 |
# and cluster is created with 'cloud' param instead of 'contact_points' |
| 76 |
from celery.backends import cassandra as mod |
| 77 |
|
| 78 |
class DummyClusterWithBundle: |
| 79 |
|
| 80 |
def __init__(self, *args, **kwargs): |
| 81 |
if args != (): |
| 82 |
# this cluster is supposed to be created with 'cloud=...' |
| 83 |
raise ValueError('I should be created with kwargs only') |
| 84 |
pass |
| 85 |
|
| 86 |
def connect(self, *args, **kwargs): |
| 87 |
return Mock() |
| 88 |
|
| 89 |
mod.cassandra = Mock() |
| 90 |
mod.cassandra.cluster = Mock() |
| 91 |
mod.cassandra.cluster.Cluster = DummyClusterWithBundle |
| 92 |
|
| 93 |
self.app.conf.cassandra_secure_bundle_path = '/path/to/bundle.zip' |
| 94 |
self.app.conf.cassandra_servers = None |
| 95 |
|
| 96 |
x = mod.CassandraBackend(app=self.app) |
| 97 |
x._get_connection() |
| 98 |
assert isinstance(x._cluster, DummyClusterWithBundle) |
| 99 |
|
| 100 |
@pytest.mark.patched_module(*CASSANDRA_MODULES) |
| 101 |
@pytest.mark.usefixtures('depends_on_current_app') |
| 102 |
def test_reduce(self, module): |
| 103 |
from celery.backends.cassandra import CassandraBackend |
| 104 |
assert loads(dumps(CassandraBackend(app=self.app))) |
| 105 |
|
| 106 |
@pytest.mark.patched_module(*CASSANDRA_MODULES) |
| 107 |
def test_get_task_meta_for(self, module): |
| 108 |
from celery.backends import cassandra as mod |
| 109 |
mod.cassandra = Mock() |
| 110 |
|
| 111 |
x = mod.CassandraBackend(app=self.app) |
| 112 |
session = x._session = Mock() |
| 113 |
execute = session.execute = Mock() |
| 114 |
result_set = Mock() |
| 115 |
result_set.one.return_value = [ |
| 116 |
states.SUCCESS, '1', datetime.now(), b'', b'' |
| 117 |
] |
| 118 |
execute.return_value = result_set |
| 119 |
x.decode = Mock() |
| 120 |
meta = x._get_task_meta_for('task_id') |
| 121 |
assert meta['status'] == states.SUCCESS |
| 122 |
|
| 123 |
result_set.one.return_value = [] |
| 124 |
x._session.execute.return_value = result_set |
| 125 |
meta = x._get_task_meta_for('task_id') |
| 126 |
assert meta['status'] == states.PENDING |
| 127 |
|
| 128 |
def test_as_uri(self): |
| 129 |
# Just ensure as_uri works properly |
| 130 |
from celery.backends import cassandra as mod |
| 131 |
mod.cassandra = Mock() |
| 132 |
|
| 133 |
x = mod.CassandraBackend(app=self.app) |
| 134 |
x.as_uri() |
| 135 |
x.as_uri(include_password=False) |
| 136 |
|
| 137 |
@pytest.mark.patched_module(*CASSANDRA_MODULES) |
| 138 |
def test_store_result(self, module): |
| 139 |
from celery.backends import cassandra as mod |
| 140 |
mod.cassandra = Mock() |
| 141 |
|
| 142 |
x = mod.CassandraBackend(app=self.app) |
| 143 |
session = x._session = Mock() |
| 144 |
session.execute = Mock() |
| 145 |
x._store_result('task_id', 'result', states.SUCCESS) |
| 146 |
|
| 147 |
def test_timeouting_cluster(self): |
| 148 |
# Tests behavior when Cluster.connect raises |
| 149 |
# cassandra.OperationTimedOut. |
| 150 |
from celery.backends import cassandra as mod |
| 151 |
|
| 152 |
class OTOExc(Exception): |
| 153 |
pass |
| 154 |
|
| 155 |
class VeryFaultyCluster: |
| 156 |
def __init__(self, *args, **kwargs): |
| 157 |
pass |
| 158 |
|
| 159 |
def connect(self, *args, **kwargs): |
| 160 |
raise OTOExc() |
| 161 |
|
| 162 |
def shutdown(self): |
| 163 |
pass |
| 164 |
|
| 165 |
mod.cassandra = Mock() |
| 166 |
mod.cassandra.OperationTimedOut = OTOExc |
| 167 |
mod.cassandra.cluster = Mock() |
| 168 |
mod.cassandra.cluster.Cluster = VeryFaultyCluster |
| 169 |
|
| 170 |
x = mod.CassandraBackend(app=self.app) |
| 171 |
|
| 172 |
with pytest.raises(OTOExc): |
| 173 |
x._store_result('task_id', 'result', states.SUCCESS) |
| 174 |
assert x._cluster is None |
| 175 |
assert x._session is None |
| 176 |
|
| 177 |
def test_create_result_table(self): |
| 178 |
# Tests behavior when session.execute raises |
| 179 |
# cassandra.AlreadyExists. |
| 180 |
from celery.backends import cassandra as mod |
| 181 |
|
| 182 |
class OTOExc(Exception): |
| 183 |
pass |
| 184 |
|
| 185 |
class FaultySession: |
| 186 |
def __init__(self, *args, **kwargs): |
| 187 |
pass |
| 188 |
|
| 189 |
def execute(self, *args, **kwargs): |
| 190 |
raise OTOExc() |
| 191 |
|
| 192 |
class DummyCluster: |
| 193 |
|
| 194 |
def __init__(self, *args, **kwargs): |
| 195 |
pass |
| 196 |
|
| 197 |
def connect(self, *args, **kwargs): |
| 198 |
return FaultySession() |
| 199 |
|
| 200 |
mod.cassandra = Mock() |
| 201 |
mod.cassandra.cluster = Mock() |
| 202 |
mod.cassandra.cluster.Cluster = DummyCluster |
| 203 |
mod.cassandra.AlreadyExists = OTOExc |
| 204 |
|
| 205 |
x = mod.CassandraBackend(app=self.app) |
| 206 |
x._get_connection(write=True) |
| 207 |
assert x._session is not None |
| 208 |
|
| 209 |
def test_init_session(self): |
| 210 |
# Tests behavior when Cluster.connect works properly |
| 211 |
from celery.backends import cassandra as mod |
| 212 |
|
| 213 |
class DummyCluster: |
| 214 |
|
| 215 |
def __init__(self, *args, **kwargs): |
| 216 |
pass |
| 217 |
|
| 218 |
def connect(self, *args, **kwargs): |
| 219 |
return Mock() |
| 220 |
|
| 221 |
mod.cassandra = Mock() |
| 222 |
mod.cassandra.cluster = Mock() |
| 223 |
mod.cassandra.cluster.Cluster = DummyCluster |
| 224 |
|
| 225 |
x = mod.CassandraBackend(app=self.app) |
| 226 |
assert x._session is None |
| 227 |
x._get_connection(write=True) |
| 228 |
assert x._session is not None |
| 229 |
|
| 230 |
s = x._session |
| 231 |
x._get_connection() |
| 232 |
assert s is x._session |
| 233 |
|
| 234 |
def test_auth_provider(self): |
| 235 |
# Ensure valid auth_provider works properly, and invalid one raises |
| 236 |
# ImproperlyConfigured exception. |
| 237 |
from celery.backends import cassandra as mod |
| 238 |
|
| 239 |
class DummyAuth: |
| 240 |
ValidAuthProvider = Mock() |
| 241 |
|
| 242 |
mod.cassandra = Mock() |
| 243 |
mod.cassandra.auth = DummyAuth |
| 244 |
|
| 245 |
# Valid auth_provider |
| 246 |
self.app.conf.cassandra_auth_provider = 'ValidAuthProvider' |
| 247 |
self.app.conf.cassandra_auth_kwargs = { |
| 248 |
'username': 'stuff' |
| 249 |
} |
| 250 |
mod.CassandraBackend(app=self.app) |
| 251 |
|
| 252 |
# Invalid auth_provider |
| 253 |
self.app.conf.cassandra_auth_provider = 'SpiderManAuth' |
| 254 |
self.app.conf.cassandra_auth_kwargs = { |
| 255 |
'username': 'Jack' |
| 256 |
} |
| 257 |
with pytest.raises(ImproperlyConfigured): |
| 258 |
mod.CassandraBackend(app=self.app) |
| 259 |
|
| 260 |
def test_options(self): |
| 261 |
# Ensure valid options works properly |
| 262 |
from celery.backends import cassandra as mod |
| 263 |
|
| 264 |
mod.cassandra = Mock() |
| 265 |
# Valid options |
| 266 |
self.app.conf.cassandra_options = { |
| 267 |
'cql_version': '3.2.1', |
| 268 |
'protocol_version': 3 |
| 269 |
} |
| 270 |
self.app.conf.cassandra_port = None |
| 271 |
x = mod.CassandraBackend(app=self.app) |
| 272 |
# Default port is 9042 |
| 273 |
assert x.port == 9042 |
| 274 |
|
| 275 |
# Valid options with port specified |
| 276 |
self.app.conf.cassandra_port = 1234 |
| 277 |
x = mod.CassandraBackend(app=self.app) |
| 278 |
assert x.port == 1234 |