Skip to content

Commit

Permalink
CLI: Fail early in verdi presto when profile name already exists (#…
Browse files Browse the repository at this point in the history
…6488)

If an explicit profile name is specified with `-p/--profile-name` it
should be validated as soon as possible and error if the profile already
exists, before anything else is done. This prevents, for example, that a
PostgreSQL user and database are created that are then not cleaned up.
  • Loading branch information
sphuber authored Jun 28, 2024
1 parent a6cf7fc commit 45a8b46
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/aiida/cmdline/commands/cmd_presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ def verdi_presto(
from aiida.manage.configuration import create_profile, load_profile
from aiida.orm import Computer

if profile_name in ctx.obj.config.profile_names:
raise click.BadParameter(f'The profile `{profile_name}` already exists.', param_hint='--profile-name')

postgres_config_kwargs = {
'profile_name': profile_name,
'postgres_hostname': postgres_hostname,
Expand Down
22 changes: 21 additions & 1 deletion tests/cmdline/commands/test_presto.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Tests for ``verdi presto``."""

import textwrap

import pytest
from aiida.cmdline.commands.cmd_presto import get_default_presto_profile_name, verdi_presto
from aiida.manage.configuration import profile_context
Expand Down Expand Up @@ -50,7 +52,7 @@ def detect_rabbitmq_config(**kwargs):

@pytest.mark.requires_rmq
@pytest.mark.usefixtures('empty_config')
def test_presto_with_rmq(pytestconfig, run_cli_command, monkeypatch):
def test_presto_with_rmq(pytestconfig, run_cli_command):
"""Test the ``verdi presto``."""
result = run_cli_command(verdi_presto, ['--non-interactive'])
assert 'Created new profile `presto`.' in result.output
Expand Down Expand Up @@ -91,3 +93,21 @@ def test_presto_overdose(run_cli_command, config_with_profile_factory):
config_with_profile_factory(name='presto-10')
result = run_cli_command(verdi_presto)
assert 'Created new profile `presto-11`.' in result.output


@pytest.mark.requires_psql
@pytest.mark.usefixtures('empty_config')
def test_presto_profile_name_exists(run_cli_command, config_with_profile_factory):
"""Test ``verdi presto`` fails early if the specified profile name already exists."""
profile_name = 'custom-presto'
config_with_profile_factory(name=profile_name)
options = ['--non-interactive', '--use-postgres', '--profile-name', profile_name]
result = run_cli_command(verdi_presto, options, raises=True)
# Matching for the complete literal output as a way to test that nothing else of the command was run, such as
# configuring the broker or creating a database for PostgreSQL
assert result.output == textwrap.dedent("""\
Usage: presto [OPTIONS]
Try 'presto --help' for help.
Error: Invalid value for --profile-name: The profile `custom-presto` already exists.
""")

0 comments on commit 45a8b46

Please sign in to comment.