diff --git a/.env b/.env index 93e626530f31129609801d8e4bb3e61003ad3362..786f93e2274be154065ab3b3f4e9f937c8a2c102 100644 --- a/.env +++ b/.env @@ -2,8 +2,7 @@ BUILDCACHE_OCI_HOST="" BUILDCACHE_OCI_PASSWORD="" BUILDCACHE_OCI_PROJECT="" BUILDCACHE_OCI_USERNAME="" - CONCRETIZE_OCI_HOST="" CONCRETIZE_OCI_PASSWORD="" CONCRETIZE_OCI_PROJECT="" -CONCRETIZE_OCI_USERNAME"" +CONCRETIZE_OCI_USERNAME="" diff --git a/README.md b/README.md index dd68dcfa9fecfd80b6cf28a890a71a80c4bed9b1..5dc3fcc178a6fa777d80e5ced20dab708ed7f69b 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,6 @@ This repository provides functionalities to easily ```managed spack environments``` and ```helpers for the container image build flow```. -This library runs only on different kinds of linux distribution operating systems. - -The lowest ```spack version``` compatible with this library is ```v0.23.0```. - - - This repository also provied CLI interface. For more informations, after installing this library, call dedal --help. - - **Setting up the needed environment variables** The ````<checkout path>\dedal\.env```` file contains the environment variables required for OCI registry used for caching. Ensure that you edit the ````<checkout path>\dedal\.env```` file to match your environment. @@ -51,3 +43,11 @@ The lowest ```spack version``` compatible with this library is ```v0.23.0```. For both concretization and binary caches, the cache version can be changed via the attributes ```cache_version_concretize``` and ```cache_version_build```. The default values are ```v1```. + +Before using this library, the following tool must be installed on Linux distribution: +```` + apt install -y bzip2 ca-certificates g++ gcc gfortran git gzip lsb-release patch python3 python3-pip tar unzip xz-utils zstd +```` +```` + python3 -m pip install --upgrade pip setuptools wheel +```` diff --git a/dedal/bll/SpackManager.py b/dedal/bll/SpackManager.py index 252cfe97c65ebd4cbcc0bfde4c14cf9a5936469e..e5fae221c7093bff86a4adf541e4664fda353dff 100644 --- a/dedal/bll/SpackManager.py +++ b/dedal/bll/SpackManager.py @@ -1,21 +1,27 @@ +import os from dedal.model.SpackDescriptor import SpackDescriptor from dedal.spack_factory.SpackOperationCreator import SpackOperationCreator from dedal.configuration.SpackConfig import SpackConfig class SpackManager: - def __init__(self, spack_config: SpackConfig = None): + """ + This class defines the logic used by the CLI + """ + + def __init__(self, spack_config: SpackConfig = None, use_cache=False): self._spack_config = spack_config + self._use_cache = use_cache def _get_spack_operation(self): - return SpackOperationCreator.get_spack_operator(self._spack_config) + return SpackOperationCreator.get_spack_operator(self._spack_config, self._use_cache) - def install_spack(self, version: str): - self._get_spack_operation().install_spack(spack_version=f'v{version}') + def install_spack(self, version: str, bashrc_path=os.path.expanduser("~/.bashrc")): + self._get_spack_operation().install_spack(spack_version=f'v{version}', bashrc_path=bashrc_path) def add_spack_repo(self, repo: SpackDescriptor): """ - After additional repo was added, setup_spack_env must be reinvoked + After additional repo was added, setup_spack_env must be invoked """ self._spack_config.add_repo(repo) diff --git a/dedal/cli/spack_manager_api.py b/dedal/cli/spack_manager_api.py index 5021b1f56f18b18ee11a9dd65eeb42e80e967f2b..438670282b17437f752ec9fb183e054a9a3d3549 100644 --- a/dedal/cli/spack_manager_api.py +++ b/dedal/cli/spack_manager_api.py @@ -10,21 +10,19 @@ from dedal.configuration.SpackConfig import SpackConfig from dedal.model.SpackDescriptor import SpackDescriptor from dedal.utils.utils import resolve_path -SESSION_CONFIG_PATH = os.path.expanduser(f"~/tmp/dedal/dedal_session.json") +SESSION_CONFIG_PATH = os.path.expanduser(f'~/tmp/dedal/dedal_session.json') os.makedirs(os.path.dirname(SESSION_CONFIG_PATH), exist_ok=True) @click.group() @click.pass_context -def cli(ctx): +def cli(ctx: click.Context): config = load_config(SESSION_CONFIG_PATH) if ctx.invoked_subcommand not in ['set-config', 'install-spack'] and not config: - click.echo("No configuration set. Use `set-config` first.") + click.echo('No configuration set. Use `set-config` first.') ctx.exit(1) if config: config['env_path'] = resolve_path(config['env_path']) - config['concretization_dir'] = resolve_path(config['concretization_dir']) - config['buildcache_dir'] = resolve_path(config['buildcache_dir']) env = SpackDescriptor(config['env_name'], config['env_path'], config['env_git_path']) gpg = GpgConfig(config['gpg_name'], config['gpg_mail']) if config['gpg_name'] and config['gpg_mail'] else None spack_config = SpackConfig(env=env, repos=None, install_dir=config['install_dir'], @@ -33,43 +31,50 @@ def cli(ctx): buildcache_dir=config['buildcache_dir'], system_name=config['system_name'], gpg=gpg, use_spack_global=config['use_spack_global']) - ctx.obj = SpackManager(spack_config) + ctx.obj = SpackManager(spack_config, use_cache=config['use_cache']) @cli.command() -@click.option('--use_spack_global', is_flag=True, default=False, help="Uses spack installed globally on the os") -@click.option("--env_name", type=str, default=None, help="Environment name") -@click.option("--env_path", type=str, default=None, help="Environment path to download locally") -@click.option("--env_git_path", type=str, default=None, help="Git path to download the environment") -@click.option("--install_dir", type=str, - help="Install directory for installing spack; spack environments and repositories are stored here") -@click.option("--upstream_instance", type=str, default=None, help="Upstream instance for spack environment") -@click.option("--system_name", type=str, default=None, help="System name; it is used inside the spack environment") -@click.option("--concretization_dir", type=str, default=None, - help="Directory where the concretization caching (spack.lock) will be downloaded") -@click.option("--buildcache_dir", type=str, default=None, - help="Directory where the binary caching is downloaded for the spack packages") -@click.option("--gpg_name", type=str, default=None, help="Gpg name") -@click.option("--gpg_mail", type=str, default=None, help="Gpg mail contact address") -def set_config(env_name, env_path, env_git_path, install_dir, upstream_instance, system_name, concretization_dir, - buildcache_dir, gpg_name, gpg_mail, use_spack_global): - """Set configuration parameters for the session.""" +@click.option('--use_cache', is_flag=True, default=False, help='Enables cashing') +@click.option('--use_spack_global', is_flag=True, default=False, help='Uses spack installed globally on the os') +@click.option('--env_name', type=str, default=None, help='Environment name') +@click.option('--env_path', type=str, default=None, help='Environment path to download locally') +@click.option('--env_git_path', type=str, default=None, help='Git path to download the environment') +@click.option('--install_dir', type=str, + help='Install directory for installing spack; spack environments and repositories are stored here') +@click.option('--upstream_instance', type=str, default=None, help='Upstream instance for spack environment') +@click.option('--system_name', type=str, default=None, help='System name; it is used inside the spack environment') +@click.option('--concretization_dir', type=str, default=None, + help='Directory where the concretization caching (spack.lock) will be downloaded') +@click.option('--buildcache_dir', type=str, default=None, + help='Directory where the binary caching is downloaded for the spack packages') +@click.option('--gpg_name', type=str, default=None, help='Gpg name') +@click.option('--gpg_mail', type=str, default=None, help='Gpg mail contact address') +@click.option('--cache_version_concretize', type=str, default='v1', help='Cache version for concretizaion data') +@click.option('--cache_version_build', type=str, default='v1', help='Cache version for binary caches data') +def set_config(use_cache, env_name, env_path, env_git_path, install_dir, upstream_instance, system_name, + concretization_dir, + buildcache_dir, gpg_name, gpg_mail, use_spack_global, cache_version_concretize, cache_version_build): + """Set configuration parameters for tahe session.""" spack_config_data = { - "env_name": env_name, - "env_path": env_path, - "env_git_path": env_git_path, - "install_dir": install_dir, - "upstream_instance": upstream_instance, - "system_name": system_name, - "concretization_dir": Path(concretization_dir), - "buildcache_dir": Path(buildcache_dir), - "gpg_name": gpg_name, - "gpg_mail": gpg_mail, - "use_spack_global": use_spack_global, - "repos": [] + 'use_cache': use_cache, + 'env_name': env_name, + 'env_path': env_path, + 'env_git_path': env_git_path, + 'install_dir': install_dir, + 'upstream_instance': upstream_instance, + 'system_name': system_name, + 'concretization_dir': Path(concretization_dir) if concretization_dir else None, + 'buildcache_dir': Path(buildcache_dir) if buildcache_dir else None, + 'gpg_name': gpg_name, + 'gpg_mail': gpg_mail, + 'use_spack_global': use_spack_global, + 'repos': [], + 'cache_version_concretize': cache_version_concretize, + 'cache_version_build': cache_version_build, } save_config(spack_config_data, SESSION_CONFIG_PATH) - click.echo("Configuration saved.") + click.echo('Configuration saved.') @click.command() @@ -79,24 +84,25 @@ def show_config(): if config: click.echo(jsonpickle.encode(config, indent=2)) else: - click.echo("No configuration set. Use `set-config` first.") + click.echo('No configuration set. Use `set-config` first.') @cli.command() -@click.option("--spack_version", type=str, default='0.23.0', help="Spack version") +@click.option('--spack_version', type=str, default='0.23.0', help='Spack version') +@click.option('--bashrc_path', type=str, default='~/.bashrc', help='Path to .bashrc') @click.pass_context -def install_spack(ctx, spack_version: str): +def install_spack(ctx: click.Context, spack_version: str, bashrc_path: str): """Install spack in the install_dir folder""" if ctx.obj is None: - SpackManager().install_spack(spack_version) + SpackManager().install_spack(spack_version, bashrc_path) else: - ctx.obj.install_spack(spack_version) + ctx.obj.install_spack(spack_version, bashrc_path) @cli.command() -@click.option("--repo_name", type=str, required=True, default=None, help="Repository name") -@click.option("--path", type=str, required=True, default=None, help="Repository path to download locally") -@click.option("--git_path", type=str, required=True, default=None, help="Git path to download the repository") +@click.option('--repo_name', type=str, required=True, default=None, help='Repository name') +@click.option('--path', type=str, required=True, default=None, help='Repository path to download locally') +@click.option('--git_path', type=str, required=True, default=None, help='Git path to download the repository') def add_spack_repo(repo_name: str, path: str, git_path: str = None): """Adds a spack repository to the spack environments. The setup command must be rerun.""" path = resolve_path(path) @@ -104,27 +110,27 @@ def add_spack_repo(repo_name: str, path: str, git_path: str = None): config = load_config(SESSION_CONFIG_PATH) config['repos'].append(repo) save_config(config, SESSION_CONFIG_PATH) - click.echo("dedal setup_spack_env must be reran after each repo is added for the environment.") + click.echo('dedal setup_spack_env must be reran after each repo is added for the environment.') @cli.command() @click.pass_context -def setup_spack_env(ctx): +def setup_spack_env(ctx: click.Context): """Setups a spack environment according to the given configuration.""" ctx.obj.setup_spack_env() @cli.command() @click.pass_context -def concretize(ctx): +def concretize(ctx: click.Context): """Spack concretization step""" ctx.obj.concretize_spack_env() @cli.command() -@click.option("--jobs", type=int, default=2, help="Number of parallel jobs for spack installation") +@click.option('--jobs', type=int, default=2, help='Number of parallel jobs for spack installation') @click.pass_context -def install_packages(ctx, jobs): +def install_packages(ctx: click.Context, jobs): """Installs spack packages present in the spack environment defined in configuration""" ctx.obj.install_packages(jobs=jobs) @@ -134,13 +140,13 @@ def clear_config(): """Clear stored configuration""" if os.path.exists(SESSION_CONFIG_PATH): os.remove(SESSION_CONFIG_PATH) - click.echo("Configuration cleared!") + click.echo('Configuration cleared!') else: - click.echo("No configuration to clear.") + click.echo('No configuration to clear.') cli.add_command(show_config) cli.add_command(clear_config) -if __name__ == "__main__": +if __name__ == '__main__': cli() diff --git a/dedal/configuration/SpackConfig.py b/dedal/configuration/SpackConfig.py index f0037702d6837761276e870ddf06e45a40f0bab5..d76783ec507bd1d0d71f4cf14cc068e831f6a357 100644 --- a/dedal/configuration/SpackConfig.py +++ b/dedal/configuration/SpackConfig.py @@ -1,34 +1,30 @@ import os from pathlib import Path - from dedal.configuration.GpgConfig import GpgConfig from dedal.model import SpackDescriptor +from dedal.utils.utils import resolve_path class SpackConfig: def __init__(self, env: SpackDescriptor = None, repos: list[SpackDescriptor] = None, install_dir=Path(os.getcwd()).resolve(), upstream_instance=None, system_name=None, concretization_dir: Path = None, buildcache_dir: Path = None, gpg: GpgConfig = None, - use_spack_global=False): + use_spack_global=False, cache_version_concretize='v1', + cache_version_build='v1'): self.env = env if repos is None: self.repos = [] else: self.repos = repos - self.install_dir = install_dir - if self.install_dir is None: - self.install_dir = Path(os.getcwd()).resolve() - os.makedirs(self.install_dir, exist_ok=True) self.upstream_instance = upstream_instance self.system_name = system_name - self.concretization_dir = concretization_dir - if self.concretization_dir: - os.makedirs(self.concretization_dir, exist_ok=True) - self.buildcache_dir = buildcache_dir - if self.buildcache_dir: - os.makedirs(self.buildcache_dir, exist_ok=True) + self.concretization_dir = concretization_dir if concretization_dir is None else resolve_path(concretization_dir) + self.buildcache_dir = buildcache_dir if buildcache_dir is None else resolve_path(buildcache_dir) + self.install_dir = resolve_path(install_dir) self.gpg = gpg self.use_spack_global = use_spack_global + self.cache_version_concretize = cache_version_concretize + self.cache_version_build = cache_version_build def add_repo(self, repo: SpackDescriptor): if self.repos is None: diff --git a/dedal/model/SpackDescriptor.py b/dedal/model/SpackDescriptor.py index 70e484fb3d39e4333389682d14a32ac46c08a912..421c4824e00a1eb1315fb4204f31cf6245ffa96c 100644 --- a/dedal/model/SpackDescriptor.py +++ b/dedal/model/SpackDescriptor.py @@ -9,5 +9,5 @@ class SpackDescriptor: def __init__(self, env_name: str, path: Path = Path(os.getcwd()).resolve(), git_path: str = None): self.env_name = env_name - self.path = path + self.path = path if isinstance(path,Path) else Path(path) self.git_path = git_path diff --git a/dedal/spack_factory/SpackOperation.py b/dedal/spack_factory/SpackOperation.py index 497bce1240673a568973a4777a3b897090dd6152..58dcad8bc08ade485da314d9727f95abb0d85514 100644 --- a/dedal/spack_factory/SpackOperation.py +++ b/dedal/spack_factory/SpackOperation.py @@ -27,13 +27,20 @@ class SpackOperation: def __init__(self, spack_config: SpackConfig = SpackConfig(), logger=get_logger(__name__)): self.spack_config = spack_config - self.spack_config.install_dir.mkdir(parents=True, exist_ok=True) + self.spack_config.install_dir = spack_config.install_dir + os.makedirs(self.spack_config.install_dir, exist_ok=True) self.spack_dir = self.spack_config.install_dir / 'spack' self.spack_setup_script = "" if self.spack_config.use_spack_global else f"source {self.spack_dir / 'share' / 'spack' / 'setup-env.sh'} &&" self.logger = logger + self.spack_config.concretization_dir = spack_config.concretization_dir + if self.spack_config.concretization_dir: + os.makedirs(self.spack_config.concretization_dir, exist_ok=True) + self.spack_config.buildcache_dir = spack_config.buildcache_dir + if self.spack_config.buildcache_dir: + os.makedirs(self.spack_config.buildcache_dir, exist_ok=True) if self.spack_config.env and spack_config.env.path: - self.spack_config.env.path = spack_config.env.path.resolve() + self.spack_config.env.path = spack_config.env.path self.spack_config.env.path.mkdir(parents=True, exist_ok=True) self.env_path: Path = spack_config.env.path / spack_config.env.env_name self.spack_command_on_env = f'{self.spack_setup_script} spack env activate -p {self.env_path}' @@ -213,7 +220,7 @@ class SpackOperation: fresh = '--fresh' if fresh else '' debug = '--debug' if debug else '' install_result = run_command("bash", "-c", - f'{self.spack_command_on_env} && spack {debug} install -v {signed} --j {jobs} {fresh}', + f'{self.spack_command_on_env} && spack {debug} install -v {signed} -j {jobs} {fresh}', stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, @@ -224,7 +231,8 @@ class SpackOperation: log_command(install_result, str(Path(os.getcwd()).resolve() / ".generate_cache.log")) return install_result - def install_spack(self, spack_version=f'v{SPACK_VERSION}', spack_repo='https://github.com/spack/spack'): + def install_spack(self, spack_version=f'v{SPACK_VERSION}', spack_repo='https://github.com/spack/spack', + bashrc_path=os.path.expanduser("~/.bashrc")): try: user = os.getlogin() except OSError: @@ -242,7 +250,6 @@ class SpackOperation: else: self.logger.debug("Spack already cloned.") - bashrc_path = os.path.expanduser("~/.bashrc") # ensure the file exists before opening it if not os.path.exists(bashrc_path): open(bashrc_path, "w").close() @@ -251,15 +258,16 @@ class SpackOperation: bashrc.write(f'export PATH="{self.spack_dir}/bin:$PATH"\n') spack_setup_script = f"source {self.spack_dir / 'share' / 'spack' / 'setup-env.sh'}" bashrc.write(f"{spack_setup_script}\n") - bashrc.write(f"{spack_setup_script}\n") self.logger.info("Added Spack PATH to .bashrc") if user: run_command("chown", "-R", f"{user}:{user}", self.spack_dir, check=True, logger=self.logger, info_msg='Adding permissions to the logged in user') - run_command("bash", "-c", f"source {bashrc_path}", check=True, logger=self.logger, info_msg='Restart bash') self.logger.info("Spack install completed") - # Restart Bash after the installation ends - os.system("exec bash") + if self.spack_config.use_spack_global is True: + # Restart the bash only of the spack is used globally + self.logger.info('Restarting bash') + run_command("bash", "-c", f"source {bashrc_path}", check=True, logger=self.logger, info_msg='Restart bash') + os.system("exec bash") # Configure upstream Spack instance if specified if self.spack_config.upstream_instance: upstreams_yaml_path = os.path.join(self.spack_dir, "etc/spack/defaults/upstreams.yaml") diff --git a/dedal/spack_factory/SpackOperationCreateCache.py b/dedal/spack_factory/SpackOperationCreateCache.py index 68dc9c56232a304f22c738b378d31b876799a501..f04eae3ad58722aea18d0c0e51a2fb9922093477 100644 --- a/dedal/spack_factory/SpackOperationCreateCache.py +++ b/dedal/spack_factory/SpackOperationCreateCache.py @@ -15,19 +15,18 @@ class SpackOperationCreateCache(SpackOperation): This class creates caching for the concretization step and for the installation step. """ - def __init__(self, spack_config: SpackConfig = SpackConfig(), cache_version_concretize='cache', - cache_version_build='cache'): + def __init__(self, spack_config: SpackConfig = SpackConfig()): super().__init__(spack_config, logger=get_logger(__name__)) self.cache_dependency = BuildCacheManager(os.environ.get('CONCRETIZE_OCI_HOST'), os.environ.get('CONCRETIZE_OCI_PROJECT'), os.environ.get('CONCRETIZE_OCI_USERNAME'), os.environ.get('CONCRETIZE_OCI_PASSWORD'), - cache_version=cache_version_concretize) + cache_version=spack_config.cache_version_concretize) self.build_cache = BuildCacheManager(os.environ.get('BUILDCACHE_OCI_HOST'), os.environ.get('BUILDCACHE_OCI_PROJECT'), os.environ.get('BUILDCACHE_OCI_USERNAME'), os.environ.get('BUILDCACHE_OCI_PASSWORD'), - cache_version=cache_version_build) + cache_version=spack_config.cache_version_build) @check_spack_env def concretize_spack_env(self): diff --git a/dedal/spack_factory/SpackOperationUseCache.py b/dedal/spack_factory/SpackOperationUseCache.py index d3b2480952152305e00da84b9d277bcad188fa40..cb2b3ac82c6e202ade309ec277d2fab76ac89b2d 100644 --- a/dedal/spack_factory/SpackOperationUseCache.py +++ b/dedal/spack_factory/SpackOperationUseCache.py @@ -16,19 +16,18 @@ class SpackOperationUseCache(SpackOperation): This class uses caching for the concretization step and for the installation step. """ - def __init__(self, spack_config: SpackConfig = SpackConfig(), cache_version_concretize='v1', - cache_version_build='v1'): + def __init__(self, spack_config: SpackConfig = SpackConfig()): super().__init__(spack_config, logger=get_logger(__name__)) self.cache_dependency = BuildCacheManager(os.environ.get('CONCRETIZE_OCI_HOST'), os.environ.get('CONCRETIZE_OCI_PROJECT'), os.environ.get('CONCRETIZE_OCI_USERNAME'), os.environ.get('CONCRETIZE_OCI_PASSWORD'), - cache_version=cache_version_concretize) + cache_version=spack_config.cache_version_concretize) self.build_cache = BuildCacheManager(os.environ.get('BUILDCACHE_OCI_HOST'), os.environ.get('BUILDCACHE_OCI_PROJECT'), os.environ.get('BUILDCACHE_OCI_USERNAME'), os.environ.get('BUILDCACHE_OCI_PASSWORD'), - cache_version=cache_version_build) + cache_version=spack_config.cache_version_build) def setup_spack_env(self): super().setup_spack_env() @@ -56,7 +55,7 @@ class SpackOperationUseCache(SpackOperation): signed = '' if signed else '--no-check-signature' debug = '--debug' if debug else '' install_result = run_command("bash", "-c", - f'{self.spack_command_on_env} && spack {debug} install -v --reuse {signed} --j {jobs}', + f'{self.spack_command_on_env} && spack {debug} install -v --reuse {signed} -j {jobs}', stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, diff --git a/dedal/tests/integration_tests/spack_from_scratch_test.py b/dedal/tests/integration_tests/spack_from_scratch_test.py index fa86230166d8c3bb70589b13cbc8f0bab8f2f056..794caef115dbe088224f94264ffc690030f7d7f4 100644 --- a/dedal/tests/integration_tests/spack_from_scratch_test.py +++ b/dedal/tests/integration_tests/spack_from_scratch_test.py @@ -6,6 +6,7 @@ from dedal.spack_factory.SpackOperationCreator import SpackOperationCreator from dedal.model.SpackDescriptor import SpackDescriptor from dedal.tests.testing_variables import test_spack_env_git, ebrains_spack_builds_git from dedal.utils.utils import file_exists_and_not_empty +from dedal.spack_factory.SpackOperation import SpackOperation def test_spack_repo_exists_1(tmp_path): @@ -23,8 +24,8 @@ def test_spack_repo_exists_2(tmp_path): env = SpackDescriptor('ebrains-spack-builds', install_dir) config = SpackConfig(env=env, install_dir=install_dir) spack_operation = SpackOperationCreator.get_spack_operator(config) + assert isinstance(spack_operation, SpackOperation) spack_operation.install_spack() - print(spack_operation.get_spack_installed_version()) spack_operation.setup_spack_env() assert spack_operation.spack_repo_exists(env.env_name) == False @@ -34,6 +35,7 @@ def test_spack_from_scratch_setup_1(tmp_path): env = SpackDescriptor('ebrains-spack-builds', install_dir, ebrains_spack_builds_git) config = SpackConfig(env=env, system_name='ebrainslab', install_dir=install_dir) spack_operation = SpackOperationCreator.get_spack_operator(config) + assert isinstance(spack_operation, SpackOperation) spack_operation.install_spack() spack_operation.setup_spack_env() assert spack_operation.spack_repo_exists(env.env_name) == False @@ -47,6 +49,7 @@ def test_spack_from_scratch_setup_2(tmp_path): config.add_repo(repo) config.add_repo(repo) spack_operation = SpackOperationCreator.get_spack_operator(config) + assert isinstance(spack_operation, SpackOperation) spack_operation.install_spack() spack_operation.setup_spack_env() assert spack_operation.spack_repo_exists(env.env_name) == True @@ -60,17 +63,21 @@ def test_spack_from_scratch_setup_3(tmp_path): config.add_repo(repo) config.add_repo(repo) spack_operation = SpackOperationCreator.get_spack_operator(config) + assert isinstance(spack_operation, SpackOperation) spack_operation.install_spack() with pytest.raises(BashCommandException): spack_operation.setup_spack_env() -def test_spack_from_scratch_setup_4(): - install_dir = Path('./install').resolve() - env = SpackModel('new_environment', install_dir, ) - spack_manager = SpackManagerScratch(env, system_name='ebrainslab') - spack_manager.setup_spack_env() - assert spack_manager.spack_repo_exists(env.env_name) == True +def test_spack_from_scratch_setup_4(tmp_path): + install_dir = tmp_path + env = SpackDescriptor('new_env2', install_dir) + config = SpackConfig(env=env, install_dir=install_dir) + spack_operation = SpackOperationCreator.get_spack_operator(config) + assert isinstance(spack_operation, SpackOperation) + spack_operation.install_spack() + spack_operation.setup_spack_env() + assert spack_operation.spack_env_exists() == True def test_spack_not_a_valid_repo(): @@ -79,6 +86,7 @@ def test_spack_not_a_valid_repo(): config = SpackConfig(env=env, system_name='ebrainslab') config.add_repo(repo) spack_operation = SpackOperationCreator.get_spack_operator(config) + assert isinstance(spack_operation, SpackOperation) with pytest.raises(BashCommandException): spack_operation.add_spack_repo(repo.path, repo.env_name) @@ -93,6 +101,7 @@ def test_spack_from_scratch_concretize_1(tmp_path): config.add_repo(repo) config.add_repo(repo) spack_operation = SpackOperationCreator.get_spack_operator(config) + assert isinstance(spack_operation, SpackOperation) spack_operation.install_spack() spack_operation.install_spack() spack_operation.setup_spack_env() @@ -111,6 +120,7 @@ def test_spack_from_scratch_concretize_2(tmp_path): config.add_repo(repo) config.add_repo(repo) spack_operation = SpackOperationCreator.get_spack_operator(config) + assert isinstance(spack_operation, SpackOperation) spack_operation.install_spack() spack_operation.setup_spack_env() spack_operation.concretize_spack_env(force=False) @@ -126,6 +136,7 @@ def test_spack_from_scratch_concretize_3(tmp_path): config.add_repo(repo) config.add_repo(repo) spack_operation = SpackOperationCreator.get_spack_operator(config) + assert isinstance(spack_operation, SpackOperation) spack_operation.install_spack() spack_operation.setup_spack_env() concretization_file_path = spack_operation.env_path / 'spack.lock' @@ -137,6 +148,7 @@ def test_spack_from_scratch_concretize_4(tmp_path): env = SpackDescriptor('test-spack-env', install_dir, test_spack_env_git) config = SpackConfig(env=env, install_dir=install_dir) spack_operation = SpackOperationCreator.get_spack_operator(config) + assert isinstance(spack_operation, SpackOperation) spack_operation.install_spack() spack_operation.setup_spack_env() spack_operation.concretize_spack_env(force=False) @@ -149,6 +161,7 @@ def test_spack_from_scratch_concretize_5(tmp_path): env = SpackDescriptor('test-spack-env', install_dir, test_spack_env_git) config = SpackConfig(env=env, install_dir=install_dir) spack_operation = SpackOperationCreator.get_spack_operator(config) + assert isinstance(spack_operation, SpackOperation) spack_operation.install_spack() spack_operation.setup_spack_env() spack_operation.concretize_spack_env(force=True) @@ -163,6 +176,7 @@ def test_spack_from_scratch_concretize_6(tmp_path): config = SpackConfig(env=env, install_dir=install_dir) config.add_repo(repo) spack_operation = SpackOperationCreator.get_spack_operator(config) + assert isinstance(spack_operation, SpackOperation) spack_operation.install_spack() spack_operation.setup_spack_env() spack_operation.concretize_spack_env(force=False) @@ -177,6 +191,7 @@ def test_spack_from_scratch_concretize_7(tmp_path): config = SpackConfig(env=env) config.add_repo(repo) spack_operation = SpackOperationCreator.get_spack_operator(config) + assert isinstance(spack_operation, SpackOperation) spack_operation.install_spack() spack_operation.setup_spack_env() spack_operation.concretize_spack_env(force=True) @@ -191,6 +206,7 @@ def test_spack_from_scratch_install(tmp_path): config = SpackConfig(env=env) config.add_repo(repo) spack_operation = SpackOperationCreator.get_spack_operator(config) + assert isinstance(spack_operation, SpackOperation) spack_operation.install_spack() spack_operation.setup_spack_env() spack_operation.concretize_spack_env(force=True) diff --git a/dedal/tests/unit_tests/spack_manager_api_test.py b/dedal/tests/unit_tests/spack_manager_api_test.py new file mode 100644 index 0000000000000000000000000000000000000000..4184a864c08019ae6ec6e52fdbab4849b4c6211c --- /dev/null +++ b/dedal/tests/unit_tests/spack_manager_api_test.py @@ -0,0 +1,181 @@ +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', '~/.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', '~/.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', '~/.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(env_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