Skip to content

Commit bb3d3ba

Browse files
committed
Start using CCM for integration tests
1 parent 4058b65 commit bb3d3ba

4 files changed

Lines changed: 64 additions & 41 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ dist
77
.coverage
88
cover/
99
docs/_build/
10+
tests/integration/ccm

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def run(self):
6262
packages=["cassandra", "cassandra.io"],
6363
ext_modules=[murmur3],
6464
install_requires=['futures'],
65-
test_requires=['nose', 'mock'],
65+
tests_require=['nose', 'mock', 'ccm'],
6666
cmdclass={"doc": doc},
6767
classifiers=[
6868
'Development Status :: 3 - Alpha',

tests/integration/__init__.py

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,75 @@
11
import logging
22
log = logging.getLogger(__name__)
33
import unittest
4+
import os
5+
from threading import Event
46

5-
from cassandra.cluster import Cluster
6-
from cassandra.policies import HostDistance
7+
logging.getLogger().addHandler(logging.StreamHandler())
78

8-
existing_keyspaces = None
9+
try:
10+
from ccmlib.cluster import Cluster as CCMCluster
11+
from ccmlib import common
12+
except ImportError:
13+
raise unittest.SkipTest('ccm is a dependency for integration tests')
914

10-
def setup_package():
11-
try:
12-
cluster = Cluster()
13-
cluster.set_core_connections_per_host(HostDistance.LOCAL, 1)
14-
cluster.set_max_connections_per_host(HostDistance.LOCAL, 1)
15-
session = cluster.connect()
16-
except Exception, exc:
17-
log.exception('Failed to connect to cluster:')
18-
raise unittest.SkipTest('Failed to connect to cluster: %r' % exc)
15+
CLUSTER_NAME = 'test_cluster'
16+
CCM_CLUSTER = None
17+
18+
path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'ccm')
19+
if not os.path.exists(path):
20+
os.mkdir(path)
21+
22+
23+
def get_cluster():
24+
return CCM_CLUSTER
1925

26+
27+
def setup_package():
2028
try:
21-
global existing_keyspaces
22-
results = session.execute("SELECT keyspace_name FROM system.schema_keyspaces")
23-
existing_keyspaces = set([row[0] for row in results])
24-
finally:
2529
try:
26-
cluster.shutdown()
27-
except Exception, exc:
28-
log.exception('Failed to connect to cluster:')
29-
raise unittest.SkipTest('Failed to connect to cluster: %r' % exc)
30+
cluster = CCMCluster.load(path, CLUSTER_NAME)
31+
log.debug("Found existing ccm test cluster, clearing")
32+
cluster.clear()
33+
except:
34+
log.debug("Creating new ccm test cluster")
35+
cluster = CCMCluster(path, CLUSTER_NAME, cassandra_version='1.2.6')
36+
cluster.set_configuration_options({'start_native_transport': True})
37+
common.switch_cluster(path, CLUSTER_NAME)
38+
cluster.populate(3)
3039

40+
log.debug("Starting ccm test cluster")
41+
cluster.start(wait_for_binary_proto=True)
42+
except:
43+
log.exception("Failed to start ccm cluster:")
44+
raise
3145

32-
def teardown_package():
33-
try:
34-
cluster = Cluster()
35-
cluster.set_core_connections_per_host(HostDistance.LOCAL, 1)
36-
cluster.set_max_connections_per_host(HostDistance.LOCAL, 1)
37-
session = cluster.connect()
38-
except Exception:
39-
log.exception('Failed to connect to cluster:')
40-
return
46+
global CCM_CLUSTER
47+
CCM_CLUSTER = cluster
4148

42-
try:
43-
if existing_keyspaces:
44-
results = session.execute("SELECT keyspace_name FROM system.schema_keyspaces")
45-
current_keyspaces = set([row[0] for row in results])
46-
for keyspace in current_keyspaces - existing_keyspaces:
47-
session.execute("DROP KEYSPACE %s" % (keyspace,))
4849

49-
finally:
50+
def teardown_package():
51+
if CCM_CLUSTER:
5052
try:
51-
cluster.shutdown()
52-
except:
53-
log.exception('Failed to connect to cluster:')
53+
CCM_CLUSTER.clear()
54+
except Exception:
55+
log.exception("Failed to clear cluster")
56+
57+
58+
class UpDownWaiter(object):
59+
60+
def __init__(self, host):
61+
self.down_event = Event()
62+
self.up_event = Event()
63+
host.monitor.register(self)
64+
65+
def on_up(self, host):
66+
self.up_event.set()
67+
68+
def on_down(self, host):
69+
self.down_event.set()
70+
71+
def wait_for_down(self):
72+
self.down_event.wait()
73+
74+
def wait_for_up(self):
75+
self.up_event.wait()

tests/integration/test_metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def test_token(self):
278278
cluster = Cluster()
279279
cluster.connect()
280280
tmap = cluster.metadata.token_map
281-
self.assertTrue(issubclass(tmap.token_cls, Token))
281+
self.assertTrue(issubclass(tmap.token_class, Token))
282282
self.assertEqual(1, len(tmap.ring))
283283
self.assertEqual(1, len(tmap.tokens_to_hosts))
284284
cluster.shutdown()

0 commit comments

Comments
 (0)