Skip to content

Commit 79e6a43

Browse files
author
bjmb
committed
Merged into master support for testing with remote cluster
2 parents 8a2bf3f + 8252196 commit 79e6a43

18 files changed

Lines changed: 143 additions & 66 deletions

tests/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
try:
16+
import unittest2 as unittest
17+
except ImportError:
18+
import unittest # noqa
1519
import logging
1620
import sys
1721
import socket
22+
import platform
1823

1924
log = logging.getLogger()
2025
log.setLevel('DEBUG')
@@ -41,3 +46,5 @@ def is_gevent_monkey_patched():
4146

4247
def is_monkey_patched():
4348
return is_gevent_monkey_patched() or is_eventlet_monkey_patched()
49+
50+
notwindows = unittest.skipUnless(not "Windows" in platform.system(), "This test is not adequate for windows")

tests/integration/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def _get_cass_version_from_dse(dse_version):
115115

116116
return cass_ver
117117

118+
CASSANDRA_IP = os.getenv('CASSANDRA_IP', '127.0.0.1')
118119
CASSANDRA_DIR = os.getenv('CASSANDRA_DIR', None)
119120
DSE_VERSION = os.getenv('DSE_VERSION', None)
120121
DSE_CRED = os.getenv('DSE_CREDS', None)
@@ -141,6 +142,18 @@ def _get_cass_version_from_dse(dse_version):
141142
CCM_KWARGS['dse_credentials_file'] = DSE_CRED
142143

143144

145+
#This changes the default contact_point parameter in Cluster
146+
def set_default_cass_ip():
147+
if CASSANDRA_IP.startswith("127.0.0."):
148+
return
149+
defaults = list(Cluster.__init__.__defaults__)
150+
defaults = [[CASSANDRA_IP]] + defaults[1:]
151+
try:
152+
Cluster.__init__.__defaults__ = tuple(defaults)
153+
except:
154+
Cluster.__init__.__func__.__defaults__ = tuple(defaults)
155+
156+
144157
def get_default_protocol():
145158

146159
if Version(CASSANDRA_VERSION) >= Version('2.2'):
@@ -208,6 +221,7 @@ def get_unsupported_upper_protocol():
208221

209222
PROTOCOL_VERSION = int(os.getenv('PROTOCOL_VERSION', default_protocol_version))
210223

224+
local = unittest.skipUnless(CASSANDRA_IP.startswith("127.0.0."), 'Tests only runs against local C*')
211225
notprotocolv1 = unittest.skipUnless(PROTOCOL_VERSION > 1, 'Protocol v1 not supported')
212226
lessthenprotocolv4 = unittest.skipUnless(PROTOCOL_VERSION < 4, 'Protocol versions 4 or greater not supported')
213227
greaterthanprotocolv3 = unittest.skipUnless(PROTOCOL_VERSION >= 4, 'Protocol versions less than 4 are not supported')
@@ -299,6 +313,8 @@ def is_current_cluster(cluster_name, node_counts):
299313

300314

301315
def use_cluster(cluster_name, nodes, ipformat=None, start=True, workloads=[]):
316+
set_default_cass_ip()
317+
302318
global CCM_CLUSTER
303319
if USE_CASS_EXTERNAL:
304320
if CCM_CLUSTER:

tests/integration/cqlengine/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from cassandra.cqlengine.management import create_keyspace_simple, CQLENG_ALLOW_SCHEMA_MANAGEMENT
2525
import cassandra
2626

27-
from tests.integration import get_server_versions, use_single_node, PROTOCOL_VERSION
27+
from tests.integration import get_server_versions, use_single_node, PROTOCOL_VERSION, CASSANDRA_IP, set_default_cass_ip
2828
DEFAULT_KEYSPACE = 'cqlengine_test'
2929

3030

@@ -35,6 +35,7 @@ def setup_package():
3535
warnings.simplefilter('always') # for testing warnings, make sure all are let through
3636
os.environ[CQLENG_ALLOW_SCHEMA_MANAGEMENT] = '1'
3737

38+
set_default_cass_ip()
3839
use_single_node()
3940

4041
setup_connection(DEFAULT_KEYSPACE)
@@ -52,7 +53,7 @@ def is_prepend_reversed():
5253

5354

5455
def setup_connection(keyspace_name):
55-
connection.setup(['127.0.0.1'],
56+
connection.setup([CASSANDRA_IP],
5657
consistency=ConsistencyLevel.ONE,
5758
protocol_version=PROTOCOL_VERSION,
5859
default_keyspace=keyspace_name)

tests/integration/cqlengine/columns/test_container_columns.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import traceback
2121
from uuid import uuid4
2222

23-
from cassandra import WriteTimeout
23+
from cassandra import WriteTimeout, OperationTimedOut
2424
import cassandra.cqlengine.columns as columns
2525
from cassandra.cqlengine.functions import get_total_seconds
2626
from cassandra.cqlengine.models import Model, ValidationError
2727
from cassandra.cqlengine.management import sync_table, drop_table
28+
29+
from tests.integration import CASSANDRA_IP
2830
from tests.integration.cqlengine import is_prepend_reversed
2931
from tests.integration.cqlengine.base import BaseCassEngTestCase
3032
from tests.integration import greaterthancass20, CASSANDRA_VERSION
@@ -134,8 +136,11 @@ def test_element_count_validation(self):
134136
break
135137
except WriteTimeout:
136138
ex_type, ex, tb = sys.exc_info()
137-
log.warn("{0}: {1} Backtrace: {2}".format(ex_type.__name__, ex, traceback.extract_tb(tb)))
139+
log.warning("{0}: {1} Backtrace: {2}".format(ex_type.__name__, ex, traceback.extract_tb(tb)))
138140
del tb
141+
except OperationTimedOut:
142+
#This will happen if the host is remote
143+
self.assertFalse(CASSANDRA_IP.startswith("127.0.0."))
139144
self.assertRaises(ValidationError, TestSetModel.create, **{'text_set': set(str(uuid4()) for i in range(65536))})
140145

141146
def test_partial_updates(self):

tests/integration/cqlengine/connections/test_connection.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from cassandra.cluster import Cluster
2525
from cassandra.query import dict_factory
2626

27-
from tests.integration import PROTOCOL_VERSION, execute_with_long_wait_retry
27+
from tests.integration import PROTOCOL_VERSION, execute_with_long_wait_retry, local
2828
from tests.integration.cqlengine.base import BaseCassEngTestCase
2929
from tests.integration.cqlengine import DEFAULT_KEYSPACE, setup_connection
3030
from cassandra.cqlengine import models
@@ -95,10 +95,12 @@ def test_connection_session_switch(self):
9595
self.assertEqual(1, TestConnectModel.objects.count())
9696
self.assertEqual(TestConnectModel.objects.first(), TCM2)
9797

98+
@local
9899
def test_connection_setup_with_setup(self):
99100
connection.setup(hosts=None, default_keyspace=None)
100101
self.assertIsNotNone(connection.get_connection("default").cluster.metadata.get_host("127.0.0.1"))
101102

103+
@local
102104
def test_connection_setup_with_default(self):
103105
connection.default()
104106
self.assertIsNotNone(connection.get_connection("default").cluster.metadata.get_host("127.0.0.1"))

tests/integration/cqlengine/test_connections.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from tests.integration.cqlengine import setup_connection, DEFAULT_KEYSPACE
2424
from tests.integration.cqlengine.base import BaseCassEngTestCase
2525
from tests.integration.cqlengine.query import test_queryset
26+
from tests.integration import local, CASSANDRA_IP
2627

2728

2829
class TestModel(Model):
@@ -44,7 +45,6 @@ class AnotherTestModel(Model):
4445
count = columns.Integer()
4546
text = columns.Text()
4647

47-
4848
class ContextQueryConnectionTests(BaseCassEngTestCase):
4949

5050
@classmethod
@@ -53,8 +53,8 @@ def setUpClass(cls):
5353
create_keyspace_simple('ks1', 1)
5454

5555
conn.unregister_connection('default')
56-
conn.register_connection('fake_cluster', ['127.0.0.100'], lazy_connect=True, retry_connect=True, default=True)
57-
conn.register_connection('cluster', ['127.0.0.1'])
56+
conn.register_connection('fake_cluster', ['1.2.3.4'], lazy_connect=True, retry_connect=True, default=True)
57+
conn.register_connection('cluster', [CASSANDRA_IP])
5858

5959
with ContextQuery(TestModel, connection='cluster') as tm:
6060
sync_table(tm)
@@ -141,7 +141,7 @@ def setUpClass(cls):
141141
super(ManagementConnectionTests, cls).setUpClass()
142142
conn.unregister_connection('default')
143143
conn.register_connection('fake_cluster', ['127.0.0.100'], lazy_connect=True, retry_connect=True, default=True)
144-
conn.register_connection('cluster', ['127.0.0.1'])
144+
conn.register_connection('cluster', [CASSANDRA_IP])
145145

146146
@classmethod
147147
def tearDownClass(cls):
@@ -227,11 +227,11 @@ def test_connection_creation_from_session(self):
227227
228228
@test_category object_mapper
229229
"""
230-
cluster = Cluster(['127.0.0.1'])
230+
cluster = Cluster([CASSANDRA_IP])
231231
session = cluster.connect()
232232
connection_name = 'from_session'
233233
conn.register_connection(connection_name, session=session)
234-
self.assertIsNotNone(conn.get_connection(connection_name).cluster.metadata.get_host("127.0.0.1"))
234+
self.assertIsNotNone(conn.get_connection(connection_name).cluster.metadata.get_host(CASSANDRA_IP))
235235
self.addCleanup(conn.unregister_connection, connection_name)
236236
cluster.shutdown()
237237

@@ -245,8 +245,8 @@ def test_connection_from_hosts(self):
245245
@test_category object_mapper
246246
"""
247247
connection_name = 'from_hosts'
248-
conn.register_connection(connection_name, hosts=['127.0.0.1'])
249-
self.assertIsNotNone(conn.get_connection(connection_name).cluster.metadata.get_host("127.0.0.1"))
248+
conn.register_connection(connection_name, hosts=[CASSANDRA_IP])
249+
self.assertIsNotNone(conn.get_connection(connection_name).cluster.metadata.get_host(CASSANDRA_IP))
250250
self.addCleanup(conn.unregister_connection, connection_name)
251251

252252
def test_connection_param_validation(self):
@@ -258,7 +258,7 @@ def test_connection_param_validation(self):
258258
259259
@test_category object_mapper
260260
"""
261-
cluster = Cluster(['127.0.0.1'])
261+
cluster = Cluster([CASSANDRA_IP])
262262
session = cluster.connect()
263263
with self.assertRaises(CQLEngineException):
264264
conn.register_connection("bad_coonection1", session=session, consistency="not_null")
@@ -275,6 +275,8 @@ def test_connection_param_validation(self):
275275
cluster.shutdown()
276276

277277

278+
cluster.shutdown()
279+
278280
class BatchQueryConnectionTests(BaseCassEngTestCase):
279281

280282
conns = ['cluster']
@@ -289,7 +291,7 @@ def setUpClass(cls):
289291

290292
conn.unregister_connection('default')
291293
conn.register_connection('fake_cluster', ['127.0.0.100'], lazy_connect=True, retry_connect=True, default=True)
292-
conn.register_connection('cluster', ['127.0.0.1'])
294+
conn.register_connection('cluster', [CASSANDRA_IP])
293295

294296
@classmethod
295297
def tearDownClass(cls):
@@ -415,7 +417,6 @@ def test_batch_query_connection_override(self):
415417
with BatchQuery(connection='cluster') as b:
416418
obj1.batch(b).using(connection='test').save()
417419

418-
419420
class UsingDescriptorTests(BaseCassEngTestCase):
420421

421422
conns = ['cluster']
@@ -427,7 +428,7 @@ def setUpClass(cls):
427428

428429
conn.unregister_connection('default')
429430
conn.register_connection('fake_cluster', ['127.0.0.100'], lazy_connect=True, retry_connect=True, default=True)
430-
conn.register_connection('cluster', ['127.0.0.1'])
431+
conn.register_connection('cluster', [CASSANDRA_IP])
431432

432433
@classmethod
433434
def tearDownClass(cls):
@@ -527,13 +528,12 @@ def __init__(self, *args, **kwargs):
527528
super(ModelQuerySetNew, self).__init__(*args, **kwargs)
528529
self._connection = "cluster"
529530

530-
531531
class BaseConnectionTestNoDefault(object):
532532
conns = ['cluster']
533533

534534
@classmethod
535535
def setUpClass(cls):
536-
conn.register_connection('cluster', ['127.0.0.1'])
536+
conn.register_connection('cluster', [CASSANDRA_IP])
537537
test_queryset.TestModel.__queryset__ = ModelQuerySetNew
538538
test_queryset.IndexedTestModel.__queryset__ = ModelQuerySetNew
539539
test_queryset.IndexedCollectionsTestModel.__queryset__ = ModelQuerySetNew

tests/integration/standard/test_authentication.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
from cassandra.cluster import Cluster, NoHostAvailable
1919
from cassandra.auth import PlainTextAuthProvider, SASLClient, SaslAuthProvider
2020

21-
from tests.integration import use_singledc, get_cluster, remove_cluster, PROTOCOL_VERSION
21+
from tests.integration import use_singledc, get_cluster, remove_cluster, PROTOCOL_VERSION, CASSANDRA_IP, \
22+
set_default_cass_ip
2223
from tests.integration.util import assert_quiescent_pool_state
2324

2425
try:
@@ -29,18 +30,25 @@
2930
log = logging.getLogger(__name__)
3031

3132

33+
#This can be tested for remote hosts, but the cluster has to be configured accordingly
34+
#@local
35+
36+
3237
def setup_module():
33-
use_singledc(start=False)
34-
ccm_cluster = get_cluster()
35-
ccm_cluster.stop()
36-
config_options = {'authenticator': 'PasswordAuthenticator',
37-
'authorizer': 'CassandraAuthorizer'}
38-
ccm_cluster.set_configuration_options(config_options)
39-
log.debug("Starting ccm test cluster with %s", config_options)
40-
ccm_cluster.start(wait_for_binary_proto=True, wait_other_notice=True)
41-
# there seems to be some race, with some versions of C* taking longer to
42-
# get the auth (and default user) setup. Sleep here to give it a chance
43-
time.sleep(10)
38+
if CASSANDRA_IP.startswith("127.0.0."):
39+
use_singledc(start=False)
40+
ccm_cluster = get_cluster()
41+
ccm_cluster.stop()
42+
config_options = {'authenticator': 'PasswordAuthenticator',
43+
'authorizer': 'CassandraAuthorizer'}
44+
ccm_cluster.set_configuration_options(config_options)
45+
log.debug("Starting ccm test cluster with %s", config_options)
46+
ccm_cluster.start(wait_for_binary_proto=True, wait_other_notice=True)
47+
# there seems to be some race, with some versions of C* taking longer to
48+
# get the auth (and default user) setup. Sleep here to give it a chance
49+
time.sleep(10)
50+
else:
51+
set_default_cass_ip()
4452

4553

4654
def teardown_module():

tests/integration/standard/test_client_warnings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from cassandra.query import BatchStatement
2222
from cassandra.cluster import Cluster
2323

24-
from tests.integration import use_singledc, PROTOCOL_VERSION
24+
from tests.integration import use_singledc, PROTOCOL_VERSION, local
2525

2626

2727
def setup_module():
@@ -93,6 +93,7 @@ def test_warning_with_trace(self):
9393
self.assertRegexpMatches(future.warnings[0], 'Batch.*exceeding.*')
9494
self.assertIsNotNone(future.get_query_trace())
9595

96+
@local
9697
def test_warning_with_custom_payload(self):
9798
"""
9899
Test to validate client warning with custom payload
@@ -111,6 +112,7 @@ def test_warning_with_custom_payload(self):
111112
self.assertRegexpMatches(future.warnings[0], 'Batch.*exceeding.*')
112113
self.assertDictEqual(future.custom_payload, payload)
113114

115+
@local
114116
def test_warning_with_trace_and_custom_payload(self):
115117
"""
116118
Test to validate client warning with tracing and client warning

0 commit comments

Comments
 (0)