Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • ri/tech-hub/platform/esd/dedal
1 result
Show changes
from dedal.spack_factory.SpackOperationCreateCache import SpackOperationCreateCache
from dedal.configuration.SpackConfig import SpackConfig
from dedal.model.SpackDescriptor import SpackDescriptor
from dedal.spack_factory.SpackOperation import SpackOperation
from dedal.spack_factory.SpackOperationCreator import SpackOperationCreator
from dedal.spack_factory.SpackOperationUseCache import SpackOperationUseCache
from dedal.tests.testing_variables import ebrains_spack_builds_git, test_spack_env_git
def test_spack_creator_scratch_1(tmp_path):
install_dir = tmp_path
env = SpackDescriptor('test-spack-env', install_dir, test_spack_env_git)
repo = SpackDescriptor('ebrains-spack-builds', install_dir, ebrains_spack_builds_git)
spack_config = SpackConfig(env, install_dir=install_dir)
spack_config.add_repo(repo)
spack_operation = SpackOperationCreator.get_spack_operator(spack_config)
assert isinstance(spack_operation, SpackOperation)
def test_spack_creator_scratch_2(tmp_path):
spack_config = None
spack_operation = SpackOperationCreator.get_spack_operator(spack_config)
assert isinstance(spack_operation, SpackOperation)
def test_spack_creator_scratch_3():
spack_config = SpackConfig()
spack_operation = SpackOperationCreator.get_spack_operator(spack_config)
assert isinstance(spack_operation, SpackOperation)
def test_spack_creator_create_cache(tmp_path):
install_dir = tmp_path
env = SpackDescriptor('test-spack-env', install_dir, test_spack_env_git)
repo = SpackDescriptor('ebrains-spack-builds', install_dir, ebrains_spack_builds_git)
spack_config = SpackConfig(env, install_dir=install_dir, concretization_dir=install_dir, buildcache_dir=install_dir)
spack_config.add_repo(repo)
spack_operation = SpackOperationCreator.get_spack_operator(spack_config)
assert isinstance(spack_operation, SpackOperationCreateCache)
def test_spack_creator_use_cache(tmp_path):
install_dir = tmp_path
env = SpackDescriptor('test-spack-env', install_dir, test_spack_env_git)
repo = SpackDescriptor('ebrains-spack-builds', install_dir, ebrains_spack_builds_git)
spack_config = SpackConfig(env, install_dir=install_dir, concretization_dir=install_dir, buildcache_dir=install_dir)
spack_config.add_repo(repo)
spack_operation = SpackOperationCreator.get_spack_operator(spack_config, use_cache=True)
assert isinstance(spack_operation, SpackOperationUseCache)
import os
import pytest
from unittest.mock import patch, MagicMock
from click.testing import CliRunner
from dedal.cli.spack_manager_api import show_config, clear_config, install_spack, add_spack_repo, install_packages, \
setup_spack_env, concretize, set_config
from dedal.model.SpackDescriptor import SpackDescriptor
@pytest.fixture
def runner():
return CliRunner()
@pytest.fixture
def mocked_session_path():
return '/mocked/tmp/session.json'
@pytest.fixture
def mock_spack_manager():
mock_spack_manager = MagicMock()
mock_spack_manager.install_spack = MagicMock()
mock_spack_manager.add_spack_repo = MagicMock()
mock_spack_manager.setup_spack_env = MagicMock()
mock_spack_manager.concretize_spack_env = MagicMock()
mock_spack_manager.install_packages = MagicMock()
return mock_spack_manager
@pytest.fixture
def mock_load_config():
with patch('dedal.cli.spack_manager_api.load_config') as mock_load:
yield mock_load
@pytest.fixture
def mock_save_config():
with patch('dedal.cli.spack_manager_api.save_config') as mock_save:
yield mock_save
@pytest.fixture
def mock_clear_config():
with patch('dedal.cli.spack_manager_api.clear_config') as mock_clear:
yield mock_clear
def test_show_config_no_config(runner, mock_load_config):
mock_load_config.return_value = None
result = runner.invoke(show_config)
assert 'No configuration set. Use `set-config` first.' in result.output
def test_show_config_with_config(runner, mock_load_config):
"""Test the show_config command when config is present."""
mock_load_config.return_value = {"key": "value"}
result = runner.invoke(show_config)
assert result.exit_code == 0
assert '"key": "value"' in result.output
def test_clear_config(runner, mock_clear_config):
"""Test the clear_config command."""
with patch('os.path.exists', return_value=True), patch('os.remove') as mock_remove:
result = runner.invoke(clear_config)
assert 'Configuration cleared!' in result.output
mock_remove.assert_called_once()
def test_install_spack_no_context_1(runner, mock_spack_manager):
"""Test install_spack with no context, using SpackManager."""
with patch('dedal.cli.spack_manager_api.SpackManager', return_value=mock_spack_manager):
result = runner.invoke(install_spack, ['--spack_version', '0.24.0'])
mock_spack_manager.install_spack.assert_called_once_with('0.24.0', os.path.expanduser("~/.bashrc"))
assert result.exit_code == 0
def test_install_spack_no_context_2(runner, mock_spack_manager):
"""Test install_spack with no context, using SpackManager and the default value for spack_version."""
with patch('dedal.cli.spack_manager_api.SpackManager', return_value=mock_spack_manager):
result = runner.invoke(install_spack)
mock_spack_manager.install_spack.assert_called_once_with('0.23.0', os.path.expanduser("~/.bashrc"))
assert result.exit_code == 0
def test_install_spack_with_mocked_context_1(runner, mock_spack_manager):
"""Test install_spack with a mocked context, using ctx.obj as SpackManager."""
result = runner.invoke(install_spack, ['--spack_version', '0.24.0', '--bashrc_path', '/home/.bahsrc'], obj=mock_spack_manager)
mock_spack_manager.install_spack.assert_called_once_with('0.24.0', '/home/.bahsrc')
assert result.exit_code == 0
def test_install_spack_with_mocked_context_2(runner, mock_spack_manager):
"""Test install_spack with a mocked context, using ctx.obj as SpackManager and the default value for spack_version."""
result = runner.invoke(install_spack, obj=mock_spack_manager)
mock_spack_manager.install_spack.assert_called_once_with('0.23.0', os.path.expanduser("~/.bashrc"))
assert result.exit_code == 0
def test_setup_spack_env(runner, mock_spack_manager):
"""Test setup_spack_env with a mocked context, using ctx.obj as SpackManager."""
result = runner.invoke(setup_spack_env, obj=mock_spack_manager)
mock_spack_manager.setup_spack_env.assert_called_once_with()
assert result.exit_code == 0
def test_concretize(runner, mock_spack_manager):
"""Test install_spack with a mocked context, using ctx.obj as SpackManager."""
result = runner.invoke(concretize, obj=mock_spack_manager)
mock_spack_manager.concretize_spack_env.assert_called_once_with()
assert result.exit_code == 0
def test_install_packages_1(runner, mock_spack_manager):
"""Test install_packages with a mocked context, using ctx.obj as SpackManager."""
result = runner.invoke(install_packages, obj=mock_spack_manager)
mock_spack_manager.install_packages.assert_called_once_with(jobs=2)
assert result.exit_code == 0
def test_install_packages(runner, mock_spack_manager):
"""Test install_packages with a mocked context, using ctx.obj as SpackManager."""
result = runner.invoke(install_packages, ['--jobs', 3], obj=mock_spack_manager)
mock_spack_manager.install_packages.assert_called_once_with(jobs=3)
assert result.exit_code == 0
@patch('dedal.cli.spack_manager_api.resolve_path')
@patch('dedal.cli.spack_manager_api.SpackDescriptor')
def test_add_spack_repo(mock_spack_descriptor, mock_resolve_path, mock_load_config, mock_save_config,
mocked_session_path, runner):
"""Test adding a spack repository with mocks."""
expected_config = {'repos': [SpackDescriptor(name='test-repo')]}
repo_name = 'test-repo'
path = '/path'
git_path = 'https://example.com/repo.git'
mock_resolve_path.return_value = '/resolved/path'
mock_load_config.return_value = expected_config
mock_repo_instance = MagicMock()
mock_spack_descriptor.return_value = mock_repo_instance
with patch('dedal.cli.spack_manager_api.SESSION_CONFIG_PATH', mocked_session_path):
result = runner.invoke(add_spack_repo, ['--repo_name', repo_name, '--path', path, '--git_path', git_path])
assert result.exit_code == 0
assert 'dedal setup_spack_env must be reran after each repo is added' in result.output
mock_resolve_path.assert_called_once_with(path)
mock_spack_descriptor.assert_called_once_with(repo_name, '/resolved/path', git_path)
assert mock_repo_instance in expected_config['repos']
mock_save_config.assert_called_once_with(expected_config, mocked_session_path)
def test_set_config(runner, mock_save_config, mocked_session_path):
"""Test set_config."""
with patch('dedal.cli.spack_manager_api.SESSION_CONFIG_PATH', mocked_session_path):
result = runner.invoke(set_config, ['--env_name', 'test', '--system_name', 'sys'])
expected_config = {
'use_cache': False,
'env_name': 'test',
'env_path': None,
'env_git_path': None,
'install_dir': None,
'upstream_instance': None,
'system_name': 'sys',
'concretization_dir': None,
'buildcache_dir': None,
'gpg_name': None,
'gpg_mail': None,
'use_spack_global': False,
'repos': [],
'cache_version_concretize': 'v1',
'cache_version_build': 'v1',
}
mock_save_config.assert_called_once()
saved_config, saved_path = mock_save_config.call_args[0]
assert saved_path == mocked_session_path
assert saved_config == expected_config
assert result.exit_code == 0
assert 'Configuration saved.' in result.output
# Minimal prerequisites for installing the esd_library
# Minimal prerequisites for installing the dedal library
# pip must be installed on the OS
echo "Bootstrapping..."
set -euo pipefail
shopt -s inherit_errexit 2>/dev/null
export DEBIAN_FRONTEND=noninteractive
apt update
apt install -y bzip2 ca-certificates g++ gcc gfortran git gzip lsb-release patch python3 python3-pip tar unzip xz-utils zstd
apt install -o DPkg::Options::=--force-confold -y -q --reinstall \
bzip2 ca-certificates g++ gcc make gfortran git gzip lsb-release \
patch python3 python3-pip tar unzip xz-utils zstd gnupg2 vim curl rsync
python3 -m pip install --upgrade pip setuptools wheel
......@@ -2,6 +2,7 @@ import logging
import os
import shutil
import subprocess
import tempfile
from pathlib import Path
from dedal.error_handling.exceptions import BashCommandException
......@@ -78,6 +79,21 @@ def log_command(results, log_file: str):
log_file.write(results.stderr)
def copy_to_tmp(file_path: Path) -> Path:
"""
Creates a temporary directory and copies the given file into it.
:param file_path: Path to the file that needs to be copied.
:return: Path to the copied file inside the temporary directory.
"""
if not file_path.is_file():
raise FileNotFoundError(f"File not found: {file_path}")
tmp_dir = Path(tempfile.mkdtemp())
tmp_file_path = tmp_dir / file_path.name
shutil.copy(file_path, tmp_file_path)
return tmp_file_path
def set_bashrc_variable(var_name: str, value: str, bashrc_path: str = os.path.expanduser("~/.bashrc"),
logger: logging = logging.getLogger(__name__)):
"""Update or add an environment variable in ~/.bashrc."""
......@@ -98,3 +114,43 @@ def set_bashrc_variable(var_name: str, value: str, bashrc_path: str = os.path.ex
logger.info(f"Updated {bashrc_path} with: export {var_name}={value}")
with open(bashrc_path, "w") as file:
file.writelines(lines)
def copy_file(src: Path, dst: Path, logger: logging = logging.getLogger(__name__)) -> None:
"""
Copy a file from src to dest.
"""
if not os.path.exists(src):
raise FileNotFoundError(f"Source file '{src}' does not exist.")
src.resolve().as_posix()
dst.resolve().as_posix()
os.makedirs(os.path.dirname(dst), exist_ok=True)
shutil.copy2(src, dst)
logger.debug(f"File copied from '{src}' to '{dst}'")
def delete_file(file_path: str, logger: logging = logging.getLogger(__name__)) -> bool:
"""
Deletes a file at the given path. Returns True if successful, False if the file doesn't exist.
"""
try:
os.remove(file_path)
logger.debug(f"File '{file_path}' deleted.")
return True
except FileNotFoundError:
logger.error(f"File not found: {file_path}")
return False
except PermissionError:
logger.error(f"Permission denied: {file_path}")
return False
except Exception as e:
logger.error(f"Error deleting file {file_path}: {e}")
return False
def resolve_path(path: str):
if path is None:
path = Path(os.getcwd()).resolve()
else:
path = Path(path).resolve()
return path
import os
SPACK_ENV_ACCESS_TOKEN = os.getenv("SPACK_ENV_ACCESS_TOKEN")
test_spack_env_git = f'https://oauth2:{SPACK_ENV_ACCESS_TOKEN}@gitlab.ebrains.eu/ri/projects-and-initiatives/virtualbraintwin/tools/test-spack-env.git'
ebrains_spack_builds_git = 'https://gitlab.ebrains.eu/ri/tech-hub/platform/esd/ebrains-spack-builds.git'
......@@ -19,7 +19,12 @@ dependencies = [
"pytest",
"pytest-mock",
"pytest-ordering",
"click",
"jsonpickle",
]
[project.scripts]
dedal = "dedal.cli.spack_manager_api:cli"
[tool.setuptools.data-files]
"dedal" = ["dedal/logger/logging.conf"]
\ No newline at end of file