Skip to content

Commit

Permalink
verdi status: do not except when no profile is configured
Browse files Browse the repository at this point in the history
Instead, print that no profile could be found and suggest that one is
setup with `verdi quicksetup` or `verdi setup`.

To test this, a new pytest fixture is created that creates a completely
new and independent configuration folder, along with fixtures to create
profiles to add to the config and caching configuration files. Similar
code already exists for normal unittests, but this can be removed once
those have been refactored to pytests.
  • Loading branch information
sphuber committed Jul 14, 2020
1 parent dd01f68 commit a56291d
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
6 changes: 6 additions & 0 deletions aiida/cmdline/commands/cmd_status.py
Expand Up @@ -14,6 +14,7 @@
import click

from aiida.cmdline.commands.cmd_verdi import verdi
from aiida.cmdline.utils import echo
from aiida.common.log import override_log_level
from ..utils.echo import ExitCode

Expand Down Expand Up @@ -64,6 +65,11 @@ def verdi_status(no_rmq):
manager = get_manager()
profile = manager.get_profile()

if profile is None:
print_status(ServiceStatus.WARNING, 'profile', 'no profile configured yet')
echo.echo_info('Configure a profile by running `verdi quicksetup` or `verdi setup`.')
return

try:
profile = manager.get_profile()
print_status(ServiceStatus.UP, 'profile', 'On profile {}'.format(profile.name))
Expand Down
11 changes: 9 additions & 2 deletions tests/cmdline/commands/test_status.py
Expand Up @@ -13,7 +13,7 @@


def test_status(run_cli_command):
"""Test running verdi status."""
"""Test `verdi status`."""
options = []
result = run_cli_command(cmd_status.verdi_status, options)

Expand All @@ -25,8 +25,15 @@ def test_status(run_cli_command):
assert string in result.output


def test_status_no_profile(run_cli_command, create_empty_config_instance):
"""Test `verdi status` when there is no profile."""
options = []
result = run_cli_command(cmd_status.verdi_status, options)
assert 'no profile configured yet' in result.output


def test_status_no_rmq(run_cli_command):
"""Test running verdi status, with no rmq check"""
"""Test `verdi status` without a check for RabbitMQ."""
options = ['--no-rmq']
result = run_cli_command(cmd_status.verdi_status, options)

Expand Down
100 changes: 99 additions & 1 deletion tests/conftest.py
Expand Up @@ -8,7 +8,11 @@
# For further information please visit http://www.aiida.net #
###########################################################################
"""Configuration file for pytest tests."""
import pytest # pylint: disable=unused-import
import os

import pytest

from aiida.manage.configuration import Config, Profile, get_config

pytest_plugins = ['aiida.manage.tests.pytest_fixtures'] # pylint: disable=invalid-name

Expand Down Expand Up @@ -144,3 +148,97 @@ def _generate_calculation_node(process_state=ProcessState.FINISHED, exit_status=
return node

return _generate_calculation_node


@pytest.fixture
def create_empty_config_instance(tmp_path) -> Config:
"""Create a temporary configuration instance.
This creates a temporary directory with a clean `.aiida` folder and basic configuration file. The currently loaded
configuration and profile are stored in memory and are automatically restored at the end of this context manager.
:return: a new empty config instance.
"""
from aiida.common.utils import Capturing
from aiida.manage import configuration
from aiida.manage.configuration import settings, load_profile, reset_profile

# Store the current configuration instance and config directory path
current_config = configuration.CONFIG
current_config_path = current_config.dirpath
current_profile_name = configuration.PROFILE.name

reset_profile()
configuration.CONFIG = None

# Create a temporary folder, set it as the current config directory path and reset the loaded configuration
settings.AIIDA_CONFIG_FOLDER = str(tmp_path)

# Create the instance base directory structure, the config file and a dummy profile
settings.create_instance_directories()

# The constructor of `Config` called by `load_config` will print warning messages about migrating it
with Capturing():
configuration.CONFIG = configuration.load_config(create=True)

yield get_config()

# Reset the config folder path and the config instance. Note this will always be executed after the yield no
# matter what happened in the test that used this fixture.
reset_profile()
settings.AIIDA_CONFIG_FOLDER = current_config_path
configuration.CONFIG = current_config
load_profile(current_profile_name)


@pytest.fixture
def create_profile() -> Profile:
"""Create a new profile instance.
:return: the profile instance.
"""

def _create_profile(name, **kwargs):

repository_dirpath = kwargs.pop('repository_dirpath', get_config().dirpath)

profile_dictionary = {
'default_user': kwargs.pop('default_user', 'dummy@localhost'),
'database_engine': kwargs.pop('database_engine', 'postgresql_psycopg2'),
'database_backend': kwargs.pop('database_backend', 'django'),
'database_hostname': kwargs.pop('database_hostname', 'localhost'),
'database_port': kwargs.pop('database_port', 5432),
'database_name': kwargs.pop('database_name', name),
'database_username': kwargs.pop('database_username', 'user'),
'database_password': kwargs.pop('database_password', 'pass'),
'repository_uri': 'file:///' + os.path.join(repository_dirpath, 'repository_' + name),
}

return Profile(name, profile_dictionary)

return _create_profile


@pytest.fixture
def create_caching_config(create_config_instance) -> Config:
"""Create a caching configuration file in the current configuration folder.
:return: the config instance that was passed or created which can be used to determine the absolute filepath.
"""

def _create_caching_config(caching_config, config=None):
import os
import yaml
from aiida.manage.caching import FILENAME_CONFIG

if config is None:
config = create_config_instance

filepath = os.path.join(create_config_instance.dirpath, FILENAME_CONFIG)

with open(filepath, 'w') as handle:
yaml.dump(caching_config, handle)

return create_config_instance

return _create_caching_config

0 comments on commit a56291d

Please sign in to comment.