<commit_before>import re
import time

from testconfig import config

from tests.utils.http_requests import AuthorizedHttpRequests
from tests.integration.core.constants import TEST_TIMEOUT
from tests.integration.core.testcases import ChromaIntegrationTestCase


class TestFilesystemDetection(ChromaIntegrationTestCase):
    def setUp(self):
        user = config['chroma_managers'][0]['users'][0]
        self.chroma_manager = AuthorizedHttpRequests(user['username'], user['password'],
                server_http_url = config['chroma_managers'][0]['server_http_url'])
        self.reset_cluster(self.chroma_manager)

        # Attempt to ensure all the targets are mounted for the filesystem.
        for host in config['lustre_servers']:
            self.remote_command(
                host['address'],
                "mount -a",
                expected_return_code = None
            )

    def test_filesystem_detection(self):
        # HACKAROUND FOR HYD-1112. Replace with call in this comment when fixed.
        # hosts = self.add_hosts([l['address'] for l in config['lustre_servers']])
        addresses = [l['address'] for l in config['lustre_servers']]
        host_create_command_ids = []
        for host_address in addresses:
            response = self.chroma_manager.post(
                '/api/host/',
                body = {'address': host_address}
            )
            self.assertEqual(response.successful, True, response.text)
            host_id = response.json['host']['id']
            host_create_command_ids.append(response.json['command']['id'])
            self.assertTrue(host_id)

            response = self.chroma_manager.get(
                '/api/host/%s/' % host_id,
            )
            self.assertEqual(response.successful, True, response.text)
            host = response.json
            self.assertEqual(host['address'], host_address)

        # Wait for the host setup and device discovery to complete
        self.wait_for_commands(self.chroma_manager, host_create_command_ids, verify_successful = False)

        # Verify there are now n hosts in the database.
        response = self.chroma_manager.get(
            '/api/host/',
        )
        self.assertEqual(response.successful, True, response.text)
        hosts = response.json['objects']
        self.assertEqual(len(addresses), len(hosts))

        # Wait for all hosts to migrate to lnet_up status. (Lnet is really up before
        # this test even starts, but this is part of the hackaround.)
        lnet_up = False
        running_time = 0
        while not lnet_up and running_time < TEST_TIMEOUT:
            lnet_up = True
            for host in hosts:
                try:
                    self.set_state(
                        '/api/host/%s/' % host['id'],
                        'lnet_up'
                    )
                except AssertionError:
                    lnet_up = False
            running_time += 1
            time.sleep(1)
        # END Hackaround

        # Verify hosts are immutable
        response = self.chroma_manager.get(
            '/api/host/',
        )
        self.assertEqual(response.successful, True, response.text)
        hosts = response.json['objects']
        self.assertEqual(len(config['lustre_servers']), len(hosts))
        for host in hosts:
            self.assertTrue(host['immutable_state'])
            available_job_classes = [j['class_name'] for j in host['available_jobs']]
            self.assertIn('ForceRemoveHostJob', available_job_classes)
            available_transition_states = [t['state'] for t in host['available_transitions']]
            self.assertListEqual(['removed'], available_transition_states)

        # Issue command to detect existing filesystem
        response = self.chroma_manager.post(
            '/api/command/',
            body = {
                'message': 'Detecting filesystems',
                'jobs': [{
                    'class_name': 'DetectTargetsJob',
                    'args': {},
                }],<commit_msg>Removing test hack around HYD-1143 since fixed.<commit_after>import re
import time

from testconfig import config

from tests.utils.http_requests import AuthorizedHttpRequests
from tests.integration.core.constants import TEST_TIMEOUT
from tests.integration.core.testcases import ChromaIntegrationTestCase


class TestFilesystemDetection(ChromaIntegrationTestCase):
    def setUp(self):
        user = config['chroma_managers'][0]['users'][0]
        self.chroma_manager = AuthorizedHttpRequests(user['username'], user['password'],
                server_http_url = config['chroma_managers'][0]['server_http_url'])
        self.reset_cluster(self.chroma_manager)

        # Attempt to ensure all the targets are mounted for the filesystem.
        for host in config['lustre_servers']:
            self.remote_command(
                host['address'],
                "mount -a",
                expected_return_code = None
            )

    def test_filesystem_detection(self):
        hosts = self.add_hosts([l['address'] for l in config['lustre_servers']])

        # Verify hosts are immutable
        response = self.chroma_manager.get(
            '/api/host/',
        )
        self.assertEqual(response.successful, True, response.text)
        hosts = response.json['objects']
        self.assertEqual(len(config['lustre_servers']), len(hosts))
        for host in hosts:
            self.assertTrue(host['immutable_state'])
            available_job_classes = [j['class_name'] for j in host['available_jobs']]
            self.assertIn('ForceRemoveHostJob', available_job_classes)
            available_transition_states = [t['state'] for t in host['available_transitions']]
            self.assertListEqual(['removed'], available_transition_states)

        # Issue command to detect existing filesystem
        response = self.chroma_manager.post(
            '/api/command/',
            body = {
                'message': 'Detecting filesystems',
                'jobs': [{
                    'class_name': 'DetectTargetsJob',
                    'args': {},
                }],
{'fraction_non_alphanumeric': 0.10029306414848584, 'fraction_numerical': 0.0027678280690328883, 'mean_word_length': 2.243400211193242, 'pattern_counts': {'":': 0, '<': 4, '<?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': '379501', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1605, 'n_words': 366}