<commit_before>
import logging

from testconfig import config
from tests.integration.core.api_testcase import ApiTestCase
from tests.integration.core.constants import TEST_TIMEOUT
from tests.integration.core.remote_operations import  SimulatorRemoteOperations, RealRemoteOperations


logger = logging.getLogger('test')
logger.setLevel(logging.DEBUG)


class ChromaIntegrationTestCase(ApiTestCase):
    """
    The TestCase class all chroma integration test cases should inherit form.

    This class ties together the common functionality needed in most
    integration test cases. For functionality used in a limited subset
    of tests, please see the *_testcase_mixin modules in this same directory.
    """

    def setUp(self):
        if config.get('simulator', False):
            try:
                from cluster_sim.simulator import ClusterSimulator
            except ImportError:
                raise ImportError("Cannot import simulator, do you need to do a 'setup.py develop' of chroma-agent?")

            state_path = 'simulator_state_%s.%s' % (self.__class__.__name__, self._testMethodName)

            # These are sufficient for tests existing at time of writing, could
            # trivially let tests ask for more by looking for these vars at class scope
            SIMULATOR_SERVER_COUNT = 4
            SIMULATOR_VOLUME_COUNT = 8
            self.simulator = ClusterSimulator(state_path, config['chroma_managers'][0]['server_http_url'])
            self.simulator.setup(SIMULATOR_SERVER_COUNT, SIMULATOR_VOLUME_COUNT)
            self.remote_operations = SimulatorRemoteOperations(self.simulator)
        else:
            self.remote_operations = RealRemoteOperations(self)

        reset = config.get('reset', True)
        if reset:
            self.reset_cluster()
        else:
            # Reset the manager via the API
            self.remote_operations.unmount_clients()
            self.api_force_clear()
            self.remote_operations.clear_ha()

    def tearDown(self):
        if hasattr(self, 'simulator'):
            self.simulator.stop()
            self.simulator.join()

    def reset_cluster(self):
        """
        Will fully wipe a test cluster:
          - dropping and recreating the chroma manager database
          - unmounting any lustre filesystems from the clients
          - unconfiguring any chroma targets in pacemaker
        """
        self.remote_operations.unmount_clients()
        self.reset_chroma_manager_db()
        self.remote_operations.clear_ha()

    def add_hosts(self, addresses):
        """
        Add a list of lustre servers to chroma and ensure lnet is started.
        """<commit_msg>Make simulator state directory unique per-run, and leave it behind only when a test fails.<commit_after>
import logging
import shutil
import sys
import datetime

from testconfig import config
from tests.integration.core.api_testcase import ApiTestCase
from tests.integration.core.constants import TEST_TIMEOUT
from tests.integration.core.remote_operations import  SimulatorRemoteOperations, RealRemoteOperations


logger = logging.getLogger('test')
logger.setLevel(logging.DEBUG)


class ChromaIntegrationTestCase(ApiTestCase):
    """
    The TestCase class all chroma integration test cases should inherit form.

    This class ties together the common functionality needed in most
    integration test cases. For functionality used in a limited subset
    of tests, please see the *_testcase_mixin modules in this same directory.
    """

    def setUp(self):
        if config.get('simulator', False):
            try:
                from cluster_sim.simulator import ClusterSimulator
            except ImportError:
                raise ImportError("Cannot import simulator, do you need to do a 'setup.py develop' of chroma-agent?")

            # The simulator's state directory will be left behind when a test fails,
            # so make sure it has a unique-per-run name
            timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H%M")
            state_path = 'simulator_state_%s.%s_%s' % (self.__class__.__name__, self._testMethodName, timestamp)

            # These are sufficient for tests existing at time of writing, could
            # trivially let tests ask for more by looking for these vars at class scope
            SIMULATOR_SERVER_COUNT = 4
            SIMULATOR_VOLUME_COUNT = 8
            self.simulator = ClusterSimulator(state_path, config['chroma_managers'][0]['server_http_url'])
            self.simulator.setup(SIMULATOR_SERVER_COUNT, SIMULATOR_VOLUME_COUNT)
            self.remote_operations = SimulatorRemoteOperations(self.simulator)
        else:
            self.remote_operations = RealRemoteOperations(self)

        reset = config.get('reset', True)
        if reset:
            self.reset_cluster()
        else:
            # Reset the manager via the API
            self.remote_operations.unmount_clients()
            self.api_force_clear()
            self.remote_operations.clear_ha()

    def tearDown(self):
        if hasattr(self, 'simulator'):
            self.simulator.stop()
            self.simulator.join()

            passed = sys.exc_info() == (None, None, None)
            if passed:
                shutil.rmtree(self.simulator.folder)

    def reset_cluster(self):
        """
        Will fully wipe a test cluster:
          - dropping and recreating the chroma manager database
          - unmounting any lustre filesystems from the clients
          - unconfiguring any chroma targets in pacemaker
        """
        self.remote_operations.unmount_clients()
        self.reset_chroma_manager_db()
        self.remote_operations.clear_ha()

    def add_hosts(self, addresses):
        """
        Add a list of lustre servers to chroma and ensure lnet is started.
        """
{'fraction_non_alphanumeric': 0.07062050051422694, 'fraction_numerical': 0.0010284538909838875, 'mean_word_length': 2.6445971267957527, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2015655', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1488, 'n_words': 463}