Example #1
0
class TestVerdiDaemon(AiidaTestCase):
    """Tests for `verdi daemon` commands."""
    def setUp(self):
        super().setUp()
        self.daemon_client = DaemonClient(get_config().current_profile)
        self.cli_runner = CliRunner()

    def test_daemon_start(self):
        """Test `verdi daemon start`."""
        try:
            result = self.cli_runner.invoke(cmd_daemon.start, [])
            self.assertClickResultNoException(result)

            daemon_response = self.daemon_client.get_daemon_info()
            worker_response = self.daemon_client.get_worker_info()

            self.assertIn('status', daemon_response, daemon_response)
            self.assertEqual(daemon_response['status'], 'ok', daemon_response)

            self.assertIn('info', worker_response, worker_response)
            self.assertEqual(len(worker_response['info']), 1, worker_response)
        finally:
            self.daemon_client.stop_daemon(wait=True)

    def test_daemon_start_number(self):
        """Test `verdi daemon start` with a specific number of workers."""

        # The `number` argument should be a positive non-zero integer
        for invalid_number in ['string', 0, -1]:
            result = self.cli_runner.invoke(cmd_daemon.start,
                                            [str(invalid_number)])
            self.assertIsNotNone(result.exception)

        try:
            number = 4
            result = self.cli_runner.invoke(cmd_daemon.start, [str(number)])
            self.assertClickResultNoException(result)

            daemon_response = self.daemon_client.get_daemon_info()
            worker_response = self.daemon_client.get_worker_info()

            self.assertIn('status', daemon_response, daemon_response)
            self.assertEqual(daemon_response['status'], 'ok', daemon_response)

            self.assertIn('info', worker_response, worker_response)
            self.assertEqual(len(worker_response['info']), number,
                             worker_response)
        finally:
            self.daemon_client.stop_daemon(wait=True)

    def test_foreground_multiple_workers(self):
        """Test `verdi daemon start` in foreground with more than one worker will fail."""
        try:
            number = 4
            result = self.cli_runner.invoke(
                cmd_daemon.start, ['--foreground', str(number)])
            self.assertIsNotNone(result.exception)
        finally:
            self.daemon_client.stop_daemon(wait=True)
Example #2
0
def with_daemon():
    """Starts the daemon process and then makes sure to kill it once the test is done."""
    import sys
    import signal
    import subprocess

    from aiida.engine.daemon.client import DaemonClient
    from aiida.cmdline.utils.common import get_env_with_venv_bin

    # Add the current python path to the environment that will be used for the daemon sub process.
    # This is necessary to guarantee the daemon can also import all the classes that are defined
    # in this `tests` module.
    env = get_env_with_venv_bin()
    env['PYTHONPATH'] = ':'.join(sys.path)

    profile = get_config().current_profile
    daemon = subprocess.Popen(
        DaemonClient(profile).cmd_string.split(),
        stderr=sys.stderr,
        stdout=sys.stdout,
        env=env,
    )

    yield

    # Note this will always be executed after the yield no matter what happened in the test that used this fixture.
    os.kill(daemon.pid, signal.SIGTERM)
Example #3
0
    def setUp(self):
        super(TestVerdiProcessDaemon, self).setUp()
        from aiida.manage.configuration import get_config
        from aiida.engine.daemon.client import DaemonClient

        profile = get_config().current_profile
        self.daemon_client = DaemonClient(profile)
        self.daemon_pid = subprocess.Popen(
            self.daemon_client.cmd_string.split(),
            stderr=sys.stderr,
            stdout=sys.stdout).pid
        self.runner = get_manager().create_runner(rmq_submit=True)
        self.cli_runner = CliRunner()
Example #4
0
    def get_daemon_client(self):
        """Return the daemon client for the current profile.

        :return: the daemon client
        :rtype: :class:`aiida.daemon.client.DaemonClient`
        :raises aiida.common.MissingConfigurationError: if the configuration file cannot be found
        :raises aiida.common.ProfileConfigurationError: if the given profile does not exist
        """
        from aiida.engine.daemon.client import DaemonClient

        if self._daemon_client is None:
            self._daemon_client = DaemonClient(self.get_profile())

        return self._daemon_client
Example #5
0
    def setUp(self):
        super().setUp()
        from aiida.cmdline.utils.common import get_env_with_venv_bin
        from aiida.engine.daemon.client import DaemonClient
        from aiida.manage.configuration import get_config

        # Add the current python path to the environment that will be used for the daemon sub process. This is necessary
        # to guarantee the daemon can also import all the classes that are defined in this `tests` module.
        env = get_env_with_venv_bin()
        env['PYTHONPATH'] = ':'.join(sys.path)

        profile = get_config().current_profile
        self.daemon_client = DaemonClient(profile)
        self.daemon_pid = subprocess.Popen(
            self.daemon_client.cmd_string.split(), stderr=sys.stderr, stdout=sys.stdout, env=env
        ).pid
        self.runner = get_manager().create_runner(rmq_submit=True)
        self.cli_runner = CliRunner()
Example #6
0
 def setUp(self):
     super().setUp()
     self.config = get_config()
     self.profile = self.config.current_profile
     self.daemon_client = DaemonClient(self.profile)
     self.cli_runner = VerdiRunner(self.config, self.profile)
Example #7
0
class TestVerdiDaemon(AiidaTestCase):
    """Tests for `verdi daemon` commands."""
    def setUp(self):
        super().setUp()
        self.config = get_config()
        self.profile = self.config.current_profile
        self.daemon_client = DaemonClient(self.profile)
        self.cli_runner = VerdiRunner(self.config, self.profile)

    def test_daemon_start(self):
        """Test `verdi daemon start`."""
        try:
            result = self.cli_runner.invoke(cmd_daemon.start, [])
            self.assertClickResultNoException(result)

            daemon_response = self.daemon_client.get_daemon_info()
            worker_response = self.daemon_client.get_worker_info()

            self.assertIn('status', daemon_response, daemon_response)
            self.assertEqual(daemon_response['status'], 'ok', daemon_response)

            self.assertIn('info', worker_response, worker_response)
            self.assertEqual(len(worker_response['info']), 1, worker_response)
        finally:
            self.daemon_client.stop_daemon(wait=True)

    def test_daemon_restart(self):
        """Test `verdi daemon restart` both with and without `--reset` flag."""
        try:
            result = self.cli_runner.invoke(cmd_daemon.start, [])
            self.assertClickResultNoException(result)

            result = self.cli_runner.invoke(cmd_daemon.restart, [])
            self.assertClickResultNoException(result)

            result = self.cli_runner.invoke(cmd_daemon.restart, ['--reset'])
            self.assertClickResultNoException(result)

            daemon_response = self.daemon_client.get_daemon_info()
            worker_response = self.daemon_client.get_worker_info()

            self.assertIn('status', daemon_response, daemon_response)
            self.assertEqual(daemon_response['status'], 'ok', daemon_response)

            self.assertIn('info', worker_response, worker_response)
            self.assertEqual(len(worker_response['info']), 1, worker_response)
        finally:
            self.daemon_client.stop_daemon(wait=True)

    # Tracked in issue #3051
    @pytest.mark.flaky(reruns=2)
    def test_daemon_start_number(self):
        """Test `verdi daemon start` with a specific number of workers."""

        # The `number` argument should be a positive non-zero integer
        for invalid_number in ['string', 0, -1]:
            result = self.cli_runner.invoke(cmd_daemon.start,
                                            [str(invalid_number)])
            self.assertIsNotNone(result.exception)

        try:
            number = 4
            result = self.cli_runner.invoke(cmd_daemon.start, [str(number)])
            self.assertClickResultNoException(result)

            daemon_response = self.daemon_client.get_daemon_info()
            worker_response = self.daemon_client.get_worker_info()

            self.assertIn('status', daemon_response, daemon_response)
            self.assertEqual(daemon_response['status'], 'ok', daemon_response)

            self.assertIn('info', worker_response, worker_response)
            self.assertEqual(len(worker_response['info']), number,
                             worker_response)
        finally:
            self.daemon_client.stop_daemon(wait=True)

    # Tracked in issue #3051
    @pytest.mark.flaky(reruns=2)
    def test_daemon_start_number_config(self):
        """Test `verdi daemon start` with `daemon.default_workers` config option being set."""
        number = 3
        self.config.set_option('daemon.default_workers', number,
                               self.profile.name)

        try:
            result = self.cli_runner.invoke(cmd_daemon.start)
            self.assertClickResultNoException(result)

            daemon_response = self.daemon_client.get_daemon_info()
            worker_response = self.daemon_client.get_worker_info()

            self.assertIn('status', daemon_response, daemon_response)
            self.assertEqual(daemon_response['status'], 'ok', daemon_response)

            self.assertIn('info', worker_response, worker_response)
            self.assertEqual(len(worker_response['info']), number,
                             worker_response)
        finally:
            self.daemon_client.stop_daemon(wait=True)

    def test_foreground_multiple_workers(self):
        """Test `verdi daemon start` in foreground with more than one worker will fail."""
        try:
            number = 4
            result = self.cli_runner.invoke(
                cmd_daemon.start, ['--foreground', str(number)])
            self.assertIsNotNone(result.exception)
        finally:
            self.daemon_client.stop_daemon(wait=True)
Example #8
0
 def setUp(self):
     super().setUp()
     self.daemon_client = DaemonClient(get_config().current_profile)
     self.cli_runner = CliRunner()