From 54f8fe31189c1ed534ee247af2b5a2b556f38c1c Mon Sep 17 00:00:00 2001 From: Alexander Goscinski Date: Sun, 2 Feb 2025 09:21:13 +0100 Subject: [PATCH] Handle error message during storage backend init better --- src/aiida/manage/configuration/config.py | 9 ++++----- tests/manage/configuration/test_config.py | 6 +++--- tests/storage/sqlite/test_container.py | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 tests/storage/sqlite/test_container.py diff --git a/src/aiida/manage/configuration/config.py b/src/aiida/manage/configuration/config.py index 02d6b56a10..08d84f7ffe 100644 --- a/src/aiida/manage/configuration/config.py +++ b/src/aiida/manage/configuration/config.py @@ -526,15 +526,14 @@ def create_profile( ) LOGGER.report('Initialising the storage backend.') - profile.storage_cls.initialise(profile) try: + # not sure if scope is good, at least put it in log #with contextlib.redirect_stdout(io.StringIO()): - #profile.storage_cls.initialise(profile) - pass + profile.storage_cls.initialise(profile) except Exception as exception: raise StorageMigrationError( - f'Storage backend initialisation failed, probably because the configuration is incorrect:\n{exception}' - ) from exception + f'During initialisation of storage backend. Please check above traceback for cause.' + ) LOGGER.report('Storage initialisation completed.') self.add_profile(profile) diff --git a/tests/manage/configuration/test_config.py b/tests/manage/configuration/test_config.py index 77adc4bf6d..7868b85398 100644 --- a/tests/manage/configuration/test_config.py +++ b/tests/manage/configuration/test_config.py @@ -445,8 +445,8 @@ def test_create_profile_raises(config_with_profile, monkeypatch, entry_points): config = config_with_profile profile_name = uuid.uuid4().hex - def raise_storage_migration_error(*args, **kwargs): - raise exceptions.StorageMigrationError() + def raise_storage_migration_error(*_, **__): + raise exceptions.StorageMigrationError("Monkey patchted error") monkeypatch.setattr(SqliteTempBackend, 'initialise', raise_storage_migration_error) entry_points.add(InvalidBaseStorage, 'aiida.storage:core.invalid_base') @@ -460,7 +460,7 @@ def raise_storage_migration_error(*args, **kwargs): with pytest.raises(ValueError, match=r'The entry point `.*` could not be loaded'): config.create_profile(profile_name, 'core.non_existant', {}) - with pytest.raises(exceptions.StorageMigrationError, match='Storage backend initialisation failed.*'): + with pytest.raises(exceptions.StorageMigrationError, match='During initialisation of storage backend. Please check above traceback for cause.*'): config.create_profile(profile_name, 'core.sqlite_temp', {}) diff --git a/tests/storage/sqlite/test_container.py b/tests/storage/sqlite/test_container.py new file mode 100644 index 0000000000..e0ef1e7361 --- /dev/null +++ b/tests/storage/sqlite/test_container.py @@ -0,0 +1,18 @@ +"""Test container initialization.""" +import psutil, os + +def test_file_descriptor_closed(aiida_profile): + """Checks if the number of open file descriptors change during a reset.""" + def list_open_file_descriptors(): + process = psutil.Process(os.getpid()) + return process.open_files() + # We have some connections active due to aiida profile during the first + # reset these are destroyed. We check the second time changes the number of + # open file descriptors. + # TODO The fix should keep the existing connections alive and just reuse them + # then one does not need to do two resets. + aiida_profile.reset_storage() + open_file_descriptors_before = list_open_file_descriptors() + aiida_profile.reset_storage() + open_file_descriptors_after = list_open_file_descriptors() + assert len(open_file_descriptors_before) == len(open_file_descriptors_after), f"Before these file descriptor were open:\n{open_file_descriptors_before}\n Now these are open:\n{open_file_descriptors_after}"