<commit_before>
from django.test import TestCase


class MockAgent(object):
    label_counter = 0
    mock_servers = {}

    def __init__(self, host, log = None, console_callback = None, timeout = None):
        self.host = host

    def invoke(self, cmdline):
        print "invoke_agent %s %s" % (self.host, cmdline)
        if cmdline == "get-fqdn":
            return self.mock_servers[self.host.address]['fqdn']
        elif cmdline == "lnet-scan":
            return self.mock_servers[self.host.address]['nids']
        elif cmdline.startswith("format-target"):
            import uuid
            return {'uuid': uuid.uuid1().__str__()}
        elif cmdline.startswith('start-target'):
            # FIXME: this will be nodename when HYD-455 is done
            return {'location': self.host.fqdn}
        elif cmdline.startswith('register-target'):
            self.label_counter += 1
            return {'label': "foofs-TTT%04d" % self.label_counter}


class JobTestCase(TestCase):
    def setUp(self):
        # FIXME: have to do this before every test because otherwise
        # one test will get all the setup of StoragePluginClass records,
        # the in-memory instance of storage_plugin_manager will expect
        # them to still be there but they'll have been cleaned
        # out of the database.  Setting up this stuff should be done
        # as part of the initial DB setup before any test is started
        # so that it's part of the baseline that's rolled back to
        # after each test.
        import configure.lib.storage_plugin.manager
        configure.lib.storage_plugin.manager.storage_plugin_manager = configure.lib.storage_plugin.manager.StoragePluginManager()

        import settings
        if hasattr(settings, 'CELERY_ALWAYS_EAGER'):
            self.old_celery_always_eager = settings.CELERY_ALWAYS_EAGER
        else:
            self.old_celery_always_eager = None
        settings.CELERY_ALWAYS_EAGER = True

        # Intercept attempts to call out to lustre servers
        import configure.lib.agent
        self.old_agent = configure.lib.agent.Agent
        MockAgent.mock_servers = self.mock_servers
        configure.lib.agent.Agent = MockAgent

        from configure.models import ManagedHost
        self.host = ManagedHost.create_from_string('myaddress')
        self.assertEqual(ManagedHost.objects.get(pk = self.host.pk).state, 'lnet_up')
        self.assertEqual(ManagedHost.objects.get(pk = self.host.pk).lnetconfiguration.state, 'nids_known')

    def tearDown(self):
        import configure.lib.agent
        configure.lib.agent.Agent = self.old_agent

        import settings
        if self.old_celery_always_eager:
            settings.CELERY_ALWAYS_EAGER = self.old_celery_always_eager
        else:
            delattr(settings, 'CELERY_ALWAYS_EAGER')


class TestStateManager(JobTestCase):
    mock_servers = {
            'myaddress': {
                'fqdn': 'myaddress.mycompany.com',
                'nids': ["192.168.0.1@tcp"]
            }
    }

    def test_start_target(self):
        from hydraapi.configureapi import create_target
        from configure.models import ManagedMgs
        from configure.lib.state_manager import StateManager
        mgt = create_target(self._test_lun(self.host).id, ManagedMgs, name = "MGS")
        self.assertEqual(ManagedMgs.objects.get(pk = mgt.pk).state, 'unformatted')
        StateManager.set_state(mgt, 'mounted')
        self.assertEqual(ManagedMgs.objects.get(pk = mgt.pk).state, 'mounted')
        StateManager.set_state(mgt, 'unmounted')<commit_msg>Fix settings CELERY_ALWAYS_EAGER -- I previously had it in my local settings so didn't notice this wasn't working<commit_after>
from django.test import TestCase


class MockAgent(object):
    label_counter = 0
    mock_servers = {}

    def __init__(self, host, log = None, console_callback = None, timeout = None):
        self.host = host

    def invoke(self, cmdline):
        print "invoke_agent %s %s" % (self.host, cmdline)
        if cmdline == "get-fqdn":
            return self.mock_servers[self.host.address]['fqdn']
        elif cmdline == "lnet-scan":
            return self.mock_servers[self.host.address]['nids']
        elif cmdline.startswith("format-target"):
            import uuid
            return {'uuid': uuid.uuid1().__str__()}
        elif cmdline.startswith('start-target'):
            # FIXME: this will be nodename when HYD-455 is done
            return {'location': self.host.fqdn}
        elif cmdline.startswith('register-target'):
            self.label_counter += 1
            return {'label': "foofs-TTT%04d" % self.label_counter}


class JobTestCase(TestCase):
    def setUp(self):
        # FIXME: have to do this before every test because otherwise
        # one test will get all the setup of StoragePluginClass records,
        # the in-memory instance of storage_plugin_manager will expect
        # them to still be there but they'll have been cleaned
        # out of the database.  Setting up this stuff should be done
        # as part of the initial DB setup before any test is started
        # so that it's part of the baseline that's rolled back to
        # after each test.
        import configure.lib.storage_plugin.manager
        configure.lib.storage_plugin.manager.storage_plugin_manager = configure.lib.storage_plugin.manager.StoragePluginManager()

        # NB by this stage celery has already read in its settings, so we have to update
        # ALWAYS_EAGER inside celery instead of in settings.*
        from celery.app import app_or_default
        self.old_celery_always_eager = app_or_default().conf.CELERY_ALWAYS_EAGER
        app_or_default().conf.CELERY_ALWAYS_EAGER = True

        # Intercept attempts to call out to lustre servers
        import configure.lib.agent
        self.old_agent = configure.lib.agent.Agent
        MockAgent.mock_servers = self.mock_servers
        configure.lib.agent.Agent = MockAgent

        from configure.models import ManagedHost
        self.host = ManagedHost.create_from_string('myaddress')
        self.assertEqual(ManagedHost.objects.get(pk = self.host.pk).state, 'lnet_up')
        self.assertEqual(ManagedHost.objects.get(pk = self.host.pk).lnetconfiguration.state, 'nids_known')

    def tearDown(self):
        import configure.lib.agent
        configure.lib.agent.Agent = self.old_agent

        from celery.app import app_or_default
        self.old_celery_always_eager = app_or_default().conf.CELERY_ALWAYS_EAGER
        app_or_default().conf.CELERY_ALWAYS_EAGER = self.old_celery_always_eager


class TestStateManager(JobTestCase):
    mock_servers = {
            'myaddress': {
                'fqdn': 'myaddress.mycompany.com',
                'nids': ["192.168.0.1@tcp"]
            }
    }

    def test_start_target(self):
        from hydraapi.configureapi import create_target
        from configure.models import ManagedMgs
        from configure.lib.state_manager import StateManager
        mgt = create_target(self._test_lun(self.host).id, ManagedMgs, name = "MGS")
        self.assertEqual(ManagedMgs.objects.get(pk = mgt.pk).state, 'unformatted')
        StateManager.set_state(mgt, 'mounted')
        self.assertEqual(ManagedMgs.objects.get(pk = mgt.pk).state, 'mounted')
        StateManager.set_state(mgt, 'unmounted')
{'fraction_non_alphanumeric': 0.0919869174161897, 'fraction_numerical': 0.004360861270100845, 'mean_word_length': 2.7197161682716677, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '400787', 'n_tokens_mistral': 2182, 'n_tokens_neox': 2127, 'n_words': 519}