11"""Tests for ``verdi presto``."""
22
33import textwrap
4+ from unittest .mock import MagicMock
45
56import pytest
67
1011from aiida .orm import Computer
1112
1213
14+ @pytest .fixture ()
15+ def mock_daemon_client (monkeypatch ):
16+ """Mock the daemon client so the daemon is not actually started during tests."""
17+ from aiida .engine .daemon import client as daemon_client_mod
18+
19+ mock_client = MagicMock ()
20+ monkeypatch .setattr (daemon_client_mod , 'get_daemon_client' , lambda * args , ** kwargs : mock_client )
21+ return mock_client
22+
23+
1324@pytest .mark .parametrize (
1425 'profile_names, expected' ,
1526 (
@@ -30,8 +41,8 @@ def get_profile_names(self):
3041 assert get_default_presto_profile_name () == expected
3142
3243
33- @pytest .mark .usefixtures ('empty_config' )
34- def test_presto_without_rmq (pytestconfig , run_cli_command , monkeypatch ):
44+ @pytest .mark .usefixtures ('empty_config' , 'mock_daemon_client' )
45+ def test_presto_without_rmq (run_cli_command , monkeypatch ):
3546 """Test the ``verdi presto`` without RabbitMQ falls back to ZMQ."""
3647 from aiida .brokers .rabbitmq import defaults
3748
@@ -52,7 +63,7 @@ def detect_rabbitmq_config(**kwargs):
5263 assert profile .process_control_backend == 'core.zmq'
5364
5465
55- @pytest .mark .usefixtures ('empty_config' )
66+ @pytest .mark .usefixtures ('empty_config' , 'mock_daemon_client' )
5667def test_presto (run_cli_command ):
5768 """Test that ``verdi presto`` configures a broker (RabbitMQ if available, otherwise ZMQ)."""
5869 result = run_cli_command (verdi_presto , ['--non-interactive' ])
@@ -67,7 +78,7 @@ def test_presto(run_cli_command):
6778
6879
6980@pytest .mark .requires_psql
70- @pytest .mark .usefixtures ('empty_config' )
81+ @pytest .mark .usefixtures ('empty_config' , 'mock_daemon_client' )
7182def test_presto_use_postgres (run_cli_command , manager ):
7283 """Test the ``verdi presto`` with the ``--use-postgres`` flag."""
7384 result = run_cli_command (verdi_presto , ['--non-interactive' , '--use-postgres' ])
@@ -89,14 +100,70 @@ def test_presto_use_postgres_fail(run_cli_command):
89100 assert 'Failed to connect to the PostgreSQL server' in result .output
90101
91102
92- @pytest .mark .usefixtures ('empty_config' )
103+ @pytest .mark .usefixtures ('empty_config' , 'mock_daemon_client' )
93104def test_presto_overdose (run_cli_command , config_with_profile_factory ):
94105 """Test that ``verdi presto`` still works for users that have over 10 presto profiles."""
95106 config_with_profile_factory (name = 'presto-10' )
96107 result = run_cli_command (verdi_presto )
97108 assert 'Created new profile `presto-11`.' in result .output
98109
99110
111+ @pytest .mark .usefixtures ('empty_config' )
112+ def test_presto_starts_daemon (run_cli_command , mock_daemon_client ):
113+ """Test that ``verdi presto`` auto-starts the daemon."""
114+ result = run_cli_command (verdi_presto , ['--non-interactive' ])
115+ assert 'Starting the daemon' in result .output
116+ mock_daemon_client .start_daemon .assert_called_once_with ()
117+
118+
119+ @pytest .mark .usefixtures ('empty_config' )
120+ def test_presto_no_daemon_autostart_env (run_cli_command , mock_daemon_client , monkeypatch ):
121+ """Test that ``AIIDA_NO_DAEMON_AUTOSTART=1`` skips the daemon auto-start."""
122+ monkeypatch .setenv ('AIIDA_NO_DAEMON_AUTOSTART' , '1' )
123+ result = run_cli_command (verdi_presto , ['--non-interactive' ])
124+ assert 'Created new profile' in result .output
125+ assert 'Starting the daemon' not in result .output
126+ mock_daemon_client .start_daemon .assert_not_called ()
127+
128+
129+ @pytest .mark .parametrize ('value' , ('0' , 'true' , 'false' , '' , 'banana' ))
130+ @pytest .mark .usefixtures ('empty_config' )
131+ def test_presto_daemon_autostart_env_non_one (run_cli_command , mock_daemon_client , monkeypatch , value ):
132+ """Test that anything other than the literal string ``1`` does not skip the daemon.
133+
134+ Only ``AIIDA_NO_DAEMON_AUTOSTART=1`` is recognized; any other value (including ``0``, ``true``,
135+ typos, or empty string) falls through to "start the daemon" so a misspelled or unrecognized
136+ value doesn't silently disable the daemon.
137+ """
138+ monkeypatch .setenv ('AIIDA_NO_DAEMON_AUTOSTART' , value )
139+ result = run_cli_command (verdi_presto , ['--non-interactive' ])
140+ assert 'Starting the daemon' in result .output
141+ mock_daemon_client .start_daemon .assert_called_once_with ()
142+
143+
144+ @pytest .mark .usefixtures ('empty_config' )
145+ def test_presto_no_broker_skips_daemon (run_cli_command , mock_daemon_client ):
146+ """Test that ``verdi presto --no-broker`` skips the daemon auto-start (no broker means no daemon)."""
147+ result = run_cli_command (verdi_presto , ['--non-interactive' , '--no-broker' ])
148+ assert 'Created new profile' in result .output
149+ assert 'Starting the daemon' not in result .output
150+ mock_daemon_client .start_daemon .assert_not_called ()
151+
152+
153+ @pytest .mark .usefixtures ('empty_config' )
154+ def test_presto_daemon_start_failure (run_cli_command , mock_daemon_client ):
155+ """Test that ``verdi presto`` handles daemon start failure gracefully."""
156+ from aiida .engine .daemon .client import DaemonException
157+
158+ mock_daemon_client .start_daemon .side_effect = DaemonException ('Could not start daemon' )
159+
160+ result = run_cli_command (verdi_presto , ['--non-interactive' ])
161+ assert 'Created new profile' in result .output
162+ assert 'FAILED' in result .output
163+ assert 'Could not start daemon' in result .output
164+ assert 'verdi daemon start' in result .output
165+
166+
100167@pytest .mark .requires_psql
101168@pytest .mark .usefixtures ('empty_config' )
102169def test_presto_profile_name_exists (run_cli_command , config_with_profile_factory ):
0 commit comments