Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sphuber committed Apr 7, 2024
1 parent de17117 commit c0d281f
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 22 deletions.
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ classifiers = [
'Topic :: Scientific/Engineering'
]
dependencies = [
'aiida-core~=2.5.0',
'aiida-core@git+https://github.com/sphuber/aiida-core@feature/improved-profile-fixtures',
'azure-storage-blob',
'boto3'
]
Expand All @@ -44,7 +44,6 @@ pre-commit = [
]
tests = [
'moto[s3]==4.2.8',
'pgtest~=1.3,>=1.3.1',
'pytest~=7.2'
]

Expand Down
2 changes: 1 addition & 1 deletion src/aiida_s3/storage/psql_aws_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PsqlAwsS3Storage(PsqlDosBackend):

migrator = PsqlAwsS3StorageMigrator

class Configuration(PsqlDosBackend.Configuration):
class Model(PsqlDosBackend.Model):
"""Model describing required information to configure an instance of the storage."""

aws_access_key_id: str = Field(
Expand Down
2 changes: 1 addition & 1 deletion src/aiida_s3/storage/psql_azure_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class PsqlAzureBlobStorage(PsqlDosBackend):

migrator = PsqlAzureBlobStorageMigrator

class Configuration(PsqlDosBackend.Configuration):
class Model(PsqlDosBackend.Model):
"""Model describing required information to configure an instance of the storage."""

container_name: str = Field(
Expand Down
2 changes: 1 addition & 1 deletion src/aiida_s3/storage/psql_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class PsqlS3Storage(PsqlDosBackend):

migrator = PsqlS3StorageMigrator

class Configuration(PsqlDosBackend.Configuration):
class Model(PsqlDosBackend.Model):
"""Model describing required information to configure an instance of the storage."""

endpoint_url: str = Field(
Expand Down
9 changes: 5 additions & 4 deletions tests/cli/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ def filepath_config():


@pytest.mark.parametrize('filepath_config', filepath_config())
def test_setup(aiida_manager, aiida_instance, monkeypatch, filepath_config):
def test_setup(aiida_tmp_config, monkeypatch, filepath_config):
"""Test the ``verdi profile setup`` command for all storage backends.
This will just verify that the command accepts the ``--config`` option with a valid YAML file containing the options
for the command and that it creates a new profile. The command normally also initialises the storage backend but
that usually requires credentials which are faked here and so the initialisation would fail. That is why the
:meth:`aiida.orm.implementation.storage_backend.StorageBackend.initialise` method is monkeypatched to be a no-op.
"""
from aiida.manage import configuration

with filepath_config.open() as handle:
profile_config = yaml.safe_load(handle)

Expand All @@ -32,9 +34,8 @@ def test_setup(aiida_manager, aiida_instance, monkeypatch, filepath_config):
profile_name = profile_config['profile']

monkeypatch.setattr(cls, 'initialise', lambda *args: True)
monkeypatch.setattr(cls, 'read_only', True)
monkeypatch.setattr(aiida_manager, 'get_profile_storage', lambda *args: cls)
monkeypatch.setattr(configuration, 'create_default_user', lambda *args: None)

result = CliRunner().invoke(profile_setup, [entry_point, '-n', '--config', str(filepath_config)])
assert f'Success: Created new profile `{profile_name}`.' in result.output
assert profile_name in aiida_instance.profile_names
assert profile_name in aiida_tmp_config.profile_names
64 changes: 51 additions & 13 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import pytest
from aiida.manage.configuration.profile import Profile

pytest_plugins = ['aiida.manage.tests.pytest_fixtures']
pytest_plugins = 'aiida.tools.pytest_fixtures'


def recursive_merge(left: dict[t.Any, t.Any], right: dict[t.Any, t.Any]) -> None:
Expand Down Expand Up @@ -143,6 +143,32 @@ def s3_client(s3) -> botocore.client.BaseClient:
client.delete_bucket(Bucket=bucket_name)


@pytest.fixture(scope='session')
def config_psql_dos(
tmp_path_factory: pytest.tmpdir.TempPathFactory,
postgres_cluster: dict[str, str],
) -> t.Callable[[dict[str, t.Any] | None], dict[str, t.Any]]:
"""Return a profile configuration for the :class:`~aiida.storage.psql_dos.backend.PsqlDosBackend`."""

def factory(
database_name: str | None = None, database_username: str | None = None, database_password: str | None = None
) -> dict[str, t.Any]:
"""Return a storage configuration for the :class:`~aiida.storage.psql_dos.backend.PsqlDosBackend`.
:returns: The storage configuration.
"""
storage_config = postgres_cluster.create_database(
database_name=database_name,
database_username=database_username,
database_password=database_password,
)
storage_config['repository_uri'] = f'file://{tmp_path_factory.mktemp("repository")}'

return storage_config

return factory


@pytest.fixture(scope='session')
def config_psql_s3(
config_psql_dos: t.Callable[[dict[str, t.Any] | None], dict[str, t.Any]],
Expand All @@ -156,18 +182,21 @@ def factory(custom_configuration: dict[str, t.Any] | None = None) -> dict[str, t
:param custom_configuration: Custom configuration to override default profile configuration.
:returns: The profile configuration.
"""
configuration = config_psql_dos({})
recursive_merge(configuration, {'storage': {'backend': 's3.psql_s3', 'config': {**s3}}})
configuration = config_psql_dos()
recursive_merge(configuration, s3)
recursive_merge(configuration, custom_configuration or {})
return configuration

return factory


@pytest.fixture(scope='session')
def psql_s3_profile(aiida_profile_factory, config_psql_s3) -> t.Generator[Profile, None, None]:
def psql_s3_profile(aiida_config, config_psql_s3) -> t.Generator[Profile, None, None]:
"""Return a test profile configured for the :class:`aiida_s3.storage.psql_s3.PsqlS3Storage`."""
yield aiida_profile_factory(config_psql_s3())
from aiida.tools.pytest_fixtures.configuration import tmp_aiida_profile

with tmp_aiida_profile(aiida_config, storage_backend='s3.psql_s3', storage_config=config_psql_s3()) as profile:
yield profile


@pytest.fixture(scope='session')
Expand Down Expand Up @@ -296,18 +325,23 @@ def factory(custom_configuration: dict[str, t.Any] | None = None) -> dict[str, t
:param custom_configuration: Custom configuration to override default profile configuration.
:returns: The profile configuration.
"""
configuration = config_psql_dos({})
recursive_merge(configuration, {'storage': {'backend': 's3.psql_aws_s3', 'config': {**aws_s3}}})
configuration = config_psql_dos()
recursive_merge(configuration, aws_s3)
recursive_merge(configuration, custom_configuration or {})
return configuration

return factory


@pytest.fixture(scope='session')
def psql_aws_s3_profile(aiida_profile_factory, config_psql_aws_s3) -> t.Generator[Profile, None, None]:
def psql_aws_s3_profile(aiida_config, config_psql_aws_s3) -> t.Generator[Profile, None, None]:
"""Return a test profile configured for the :class:`aiida_s3.storage.psql_aws_s3.PsqlAwsS3Storage`."""
yield aiida_profile_factory(config_psql_aws_s3())
from aiida.tools.pytest_fixtures.configuration import tmp_aiida_profile

with tmp_aiida_profile(
aiida_config, storage_backend='s3.psql_aws_s3', storage_config=config_psql_aws_s3()
) as profile:
yield profile


@pytest.fixture(scope='session')
Expand Down Expand Up @@ -408,8 +442,8 @@ def factory(custom_configuration: dict[str, t.Any] | None = None) -> dict[str, t
:param custom_configuration: Custom configuration to override default profile configuration.
:returns: The profile configuration.
"""
configuration = config_psql_dos({})
recursive_merge(configuration, {'storage': {'backend': 's3.psql_azure_blob', 'config': {**azure_blob_storage}}})
configuration = config_psql_dos()
recursive_merge(configuration, azure_blob_storage)
recursive_merge(configuration, custom_configuration or {})
return configuration

Expand All @@ -418,20 +452,24 @@ def factory(custom_configuration: dict[str, t.Any] | None = None) -> dict[str, t

@pytest.fixture(scope='session')
def psql_azure_blob_profile(
aiida_config,
should_mock_azure_blob,
aiida_profile_factory,
config_psql_azure_blob,
azure_blob_storage,
) -> t.Generator[Profile, None, None] | None:
"""Return a test profile configured for the :class:`aiida_s3.storage.psql_azure_blob.PsqlAzureBlobStorage`."""
from aiida.tools.pytest_fixtures.configuration import tmp_aiida_profile
from aiida_s3.repository.azure_blob import AzureBlobStorageRepositoryBackend

if should_mock_azure_blob:
# Azure cannot yet be successfully mocked, so if we are mocking, skip the test.
yield None
else:
try:
yield aiida_profile_factory(config_psql_azure_blob())
with tmp_aiida_profile(
aiida_config, storage_backend='s3.psql_azure_blob', storage_config=config_psql_azure_blob()
) as profile:
yield profile
finally:
repository = AzureBlobStorageRepositoryBackend(**azure_blob_storage)
repository.erase()
Expand Down

0 comments on commit c0d281f

Please sign in to comment.