import atexit
import ccmlib.cluster
import os
import sys
import tempfile
import logging
from distutils.util import strtobool



class TestConfig:
    pass


test_config = TestConfig()
test_config.n_nodes = int(os.environ.get('TEST_CASSANDRA_N_NODES', '2'))
TEST_DEBUG = strtobool(os.environ.get("TEST_DEBUG", "False").lower())
if TEST_DEBUG:
    logging.warning(("You are using TEST_DEBUG=True, a Cassandra cluster must be already running. "
                     "Keep in mind that the results of the test might be altered by data already existing."))


def set_ccm_cluster():
    global test_config
    test_config.ccm_cluster = ccmlib.cluster.Cluster(
        tempfile.mkdtemp("tmp_data"),
        'hecuba_test',
        cassandra_version=os.environ.get('TEST_CASSANDRA_VERSION', '3.11.4'))


def set_up_default_cassandra():
    global test_config
    if hasattr(test_config, "ccm_cluster") and any(map(lambda node: node.is_live(), test_config.ccm_cluster.nodes.values())):
        return

    set_ccm_cluster()
    try:
        test_config.ccm_cluster.populate(test_config.n_nodes).start(allow_root=True)
    except Exception as a:
        if TEST_DEBUG:
            logging.warning("TEST_DEBUG: ignoring exception")
        else:
            raise a

    if 'hecuba' in sys.modules:
        import importlib
        import hecuba
        importlib.reload(hecuba)


@atexit.register
def turning_down_cassandra():
    global test_config
    if not TEST_DEBUG:
        print("Turning down Cassandra")
        if test_config is not None and test_config.ccm_cluster is not None:
            from hfetch import disconnectCassandra
            disconnectCassandra()
            test_config.ccm_cluster.stop()
            test_config.ccm_cluster.clear()
@@ -120,17 +120,16 @@
 obool%0A%0A%0A
-%0A
 class Te
@@ -893,16 +893,29 @@
 and any(
+%0A            
 map(lamb
@@ -1471,48 +1471,8 @@
 UG:%0A
-        print(%22Turning down Cassandra%22)%0A
     
@@ -1506,16 +1506,24 @@
 one and 
+hasattr(
 test_con
@@ -1517,33 +1517,35 @@
 attr(test_config
-.
+, %22
 ccm_cluster is n
@@ -1543,21 +1543,55 @@
 ster
- is not None:
+%22):%0A            print(%22Turning down Cassandra%22)
 %0A   
@@ -1715,16 +1715,16 @@
 .stop()%0A
-
         
@@ -1758,8 +1758,9 @@
 .clear()
+%0A

Fix tests without ccm