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