|
1 | 1 | import logging |
2 | 2 | log = logging.getLogger(__name__) |
3 | 3 | import unittest |
| 4 | +import os |
| 5 | +from threading import Event |
4 | 6 |
|
5 | | -from cassandra.cluster import Cluster |
6 | | -from cassandra.policies import HostDistance |
| 7 | +logging.getLogger().addHandler(logging.StreamHandler()) |
7 | 8 |
|
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') |
9 | 14 |
|
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 |
19 | 25 |
|
| 26 | + |
| 27 | +def setup_package(): |
20 | 28 | 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: |
25 | 29 | 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) |
30 | 39 |
|
| 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 |
31 | 45 |
|
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 |
41 | 48 |
|
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,)) |
48 | 49 |
|
49 | | - finally: |
| 50 | +def teardown_package(): |
| 51 | + if CCM_CLUSTER: |
50 | 52 | 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() |
0 commit comments