|
| 1 | +# # |
| 2 | +# Copyright 2013-2025 Ghent University |
| 3 | +# |
| 4 | +# This file is part of EasyBuild, |
| 5 | +# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en), |
| 6 | +# with support of Ghent University (http://ugent.be/hpc), |
| 7 | +# the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be), |
| 8 | +# Flemish Research Foundation (FWO) (http://www.fwo.be/en) |
| 9 | +# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en). |
| 10 | +# |
| 11 | +# https://github.com/easybuilders/easybuild |
| 12 | +# |
| 13 | +# EasyBuild is free software: you can redistribute it and/or modify |
| 14 | +# it under the terms of the GNU General Public License as published by |
| 15 | +# the Free Software Foundation v2. |
| 16 | +# |
| 17 | +# EasyBuild is distributed in the hope that it will be useful, |
| 18 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | +# GNU General Public License for more details. |
| 21 | +# |
| 22 | +# You should have received a copy of the GNU General Public License |
| 23 | +# along with EasyBuild. If not, see <http://www.gnu.org/licenses/>. |
| 24 | +# # |
| 25 | +""" |
| 26 | +Unit tests for EasyBuild configuration. |
| 27 | +
|
| 28 | +@author: Davide Grassano (CECAM - EPFL) |
| 29 | +""" |
| 30 | + |
| 31 | +import os |
| 32 | +import re |
| 33 | +import shutil |
| 34 | +import sys |
| 35 | +import tempfile |
| 36 | +from importlib import reload |
| 37 | +from test.framework.utilities import EnhancedTestCase, TestLoaderFiltered, init_config |
| 38 | +from unittest import TextTestRunner |
| 39 | + |
| 40 | +import easybuild.tools.options as eboptions |
| 41 | +from easybuild.tools.build_log import EasyBuildError |
| 42 | +from easybuild.tools.config import ERROR, IGNORE, WARN, BuildOptions, ConfigurationVariables |
| 43 | +from easybuild.tools.config import build_option, build_path, get_build_log_path, get_log_filename, get_repositorypath |
| 44 | +from easybuild.tools.config import install_path, log_file_format, log_path, source_paths |
| 45 | +from easybuild.tools.config import update_build_option, update_build_options |
| 46 | +from easybuild.tools.config import DEFAULT_PATH_SUBDIRS, init_build_options |
| 47 | +from easybuild.tools.filetools import copy_dir, mkdir, write_file |
| 48 | +from easybuild.tools.options import CONFIG_ENV_VAR_PREFIX |
| 49 | +from easybuild.tools.docs import list_easyblocks, list_software, list_toolchains |
| 50 | +from easybuild.tools.entrypoints import ( |
| 51 | + get_group_entrypoints, HOOKS_ENTRYPOINT, EASYBLOCK_ENTRYPOINT, TOOLCHAIN_ENTRYPOINT, |
| 52 | + HAVE_ENTRY_POINTS |
| 53 | +) |
| 54 | +from easybuild.framework.easyconfig.easyconfig import get_module_path |
| 55 | +from easybuild.framework.easyblock import EasyBlock |
| 56 | + |
| 57 | + |
| 58 | +if HAVE_ENTRY_POINTS: |
| 59 | + from importlib.metadata import DistributionFinder, Distribution |
| 60 | + |
| 61 | + |
| 62 | +MOCK_HOOK_EP_NAME = "mock_hook" |
| 63 | +MOCK_EASYBLOCK_EP_NAME = "mock_easyblock" |
| 64 | +MOCK_TOOLCHAIN_EP_NAME = "mock_toolchain" |
| 65 | + |
| 66 | +MOCK_HOOK = "hello_world" |
| 67 | +MOCK_EASYBLOCK = "TestEasyBlock" |
| 68 | +MOCK_TOOLCHAIN = "MockTc" |
| 69 | + |
| 70 | + |
| 71 | +MOCK_EP_FILE=f""" |
| 72 | +from easybuild.tools.entrypoints import register_entrypoint_hooks |
| 73 | +from easybuild.tools.hooks import CONFIGURE_STEP, START |
| 74 | +
|
| 75 | +
|
| 76 | +@register_entrypoint_hooks(START) |
| 77 | +def {MOCK_HOOK}(): |
| 78 | + print("Hello, World! ----------------------------------------") |
| 79 | +
|
| 80 | +########################################################################## |
| 81 | +from easybuild.framework.easyblock import EasyBlock |
| 82 | +from easybuild.tools.entrypoints import register_easyblock_entrypoint |
| 83 | +
|
| 84 | +@register_easyblock_entrypoint() |
| 85 | +class {MOCK_EASYBLOCK}(EasyBlock): |
| 86 | + def configure_step(self): |
| 87 | + print("{MOCK_EASYBLOCK}: configure_step called.") |
| 88 | +
|
| 89 | + def build_step(self): |
| 90 | + print("{MOCK_EASYBLOCK}: build_step called.") |
| 91 | +
|
| 92 | + def install_step(self): |
| 93 | + print("{MOCK_EASYBLOCK}: install_step called.") |
| 94 | +
|
| 95 | + def sanity_check_step(self): |
| 96 | + print("{MOCK_EASYBLOCK}: sanity_check_step called.") |
| 97 | +
|
| 98 | +########################################################################## |
| 99 | +from easybuild.tools.entrypoints import register_toolchain_entrypoint |
| 100 | +from easybuild.tools.toolchain.compiler import DEFAULT_OPT_LEVEL, Compiler |
| 101 | +from easybuild.tools.toolchain.toolchain import SYSTEM_TOOLCHAIN_NAME |
| 102 | +
|
| 103 | +TC_CONSTANT_MOCK = "Mock" |
| 104 | +
|
| 105 | +class MockCompiler(Compiler): |
| 106 | + COMPILER_FAMILY = TC_CONSTANT_MOCK |
| 107 | + SUBTOOLCHAIN = SYSTEM_TOOLCHAIN_NAME |
| 108 | +
|
| 109 | +@register_toolchain_entrypoint() |
| 110 | +class {MOCK_TOOLCHAIN}(MockCompiler): |
| 111 | + NAME = '{MOCK_TOOLCHAIN}' # Using `...tc` to distinguish toolchain from package |
| 112 | + COMPILER_MODULE_NAME = [NAME] |
| 113 | + SUBTOOLCHAIN = [SYSTEM_TOOLCHAIN_NAME] |
| 114 | +""" |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +MOCK_EP_META_FILE = f""" |
| 119 | +[{HOOKS_ENTRYPOINT}] |
| 120 | +{MOCK_HOOK_EP_NAME} = {{module}}:hello_world |
| 121 | +
|
| 122 | +[{EASYBLOCK_ENTRYPOINT}] |
| 123 | +{MOCK_EASYBLOCK_EP_NAME} = {{module}}:TestEasyBlock |
| 124 | +
|
| 125 | +[{TOOLCHAIN_ENTRYPOINT}] |
| 126 | +{MOCK_TOOLCHAIN_EP_NAME} = {{module}}:MockTc |
| 127 | +""" |
| 128 | + |
| 129 | + |
| 130 | +class MockDistribution(Distribution): |
| 131 | + """Mock distribution for testing entry points.""" |
| 132 | + def __init__(self, module): |
| 133 | + self.module = module |
| 134 | + |
| 135 | + def read_text(self, filename): |
| 136 | + if filename == "entry_points.txt": |
| 137 | + return MOCK_EP_META_FILE.format(module=self.module) |
| 138 | + |
| 139 | + if filename == "METADATA": |
| 140 | + return "Name: mock_hook\nVersion: 0.1.0\n" |
| 141 | + |
| 142 | +class MockDistributionFinder(DistributionFinder): |
| 143 | + """Mock distribution finder for testing entry points.""" |
| 144 | + def __init__(self, *args, module, **kwargs): |
| 145 | + super().__init__(*args, **kwargs) |
| 146 | + self.module = module |
| 147 | + |
| 148 | + def find_distributions(self, context=None): |
| 149 | + yield MockDistribution(self.module) |
| 150 | + |
| 151 | + |
| 152 | +class EasyBuildEntrypointsTest(EnhancedTestCase): |
| 153 | + """Test cases for EasyBuild configuration.""" |
| 154 | + |
| 155 | + tmpdir = None |
| 156 | + |
| 157 | + def setUp(self): |
| 158 | + """Set up the test environment.""" |
| 159 | + reload(eboptions) |
| 160 | + super().setUp() |
| 161 | + self.tmpdir = tempfile.mkdtemp(prefix='easybuild_test_') |
| 162 | + |
| 163 | + if HAVE_ENTRY_POINTS: |
| 164 | + filename_root = "mock" |
| 165 | + dirname, dirpath = os.path.split(self.tmpdir) |
| 166 | + |
| 167 | + self.module = '.'.join([dirpath, filename_root]) |
| 168 | + sys.path.insert(0, dirname) |
| 169 | + sys.meta_path.insert(0, MockDistributionFinder(module=self.module)) |
| 170 | + |
| 171 | + # Create a mock entry point for testing |
| 172 | + mock_hook_file = os.path.join(self.tmpdir, f'{filename_root}.py') |
| 173 | + write_file(mock_hook_file, MOCK_EP_FILE) |
| 174 | + |
| 175 | + def tearDown(self): |
| 176 | + """Clean up the test environment.""" |
| 177 | + if self.tmpdir and os.path.isdir(self.tmpdir): |
| 178 | + shutil.rmtree(self.tmpdir) |
| 179 | + |
| 180 | + if HAVE_ENTRY_POINTS: |
| 181 | + # Remove the entry point from the working set |
| 182 | + torm = [] |
| 183 | + for idx,cls in enumerate(sys.meta_path): |
| 184 | + if isinstance(cls, MockDistributionFinder): |
| 185 | + torm.append(idx) |
| 186 | + for idx in reversed(torm): |
| 187 | + del sys.meta_path[idx] |
| 188 | + |
| 189 | + def test_entrypoints_get_group_too_old_python(self): |
| 190 | + """Test retrieving entrypoints for a specific group with too old Python version.""" |
| 191 | + if HAVE_ENTRY_POINTS: |
| 192 | + self.skipTest("Entry points available in this Python version") |
| 193 | + self.assertRaises(EasyBuildError, get_group_entrypoints, HOOKS_ENTRYPOINT) |
| 194 | + |
| 195 | + def test_entrypoints_get_group(self): |
| 196 | + """Test retrieving entrypoints for a specific group.""" |
| 197 | + if not HAVE_ENTRY_POINTS: |
| 198 | + self.skipTest("Entry points not available in this Python version") |
| 199 | + |
| 200 | + expected = { |
| 201 | + HOOKS_ENTRYPOINT: MOCK_HOOK_EP_NAME, |
| 202 | + EASYBLOCK_ENTRYPOINT: MOCK_EASYBLOCK_EP_NAME, |
| 203 | + TOOLCHAIN_ENTRYPOINT: MOCK_TOOLCHAIN_EP_NAME, |
| 204 | + } |
| 205 | + |
| 206 | + # init_config() |
| 207 | + for group in [HOOKS_ENTRYPOINT, EASYBLOCK_ENTRYPOINT, TOOLCHAIN_ENTRYPOINT]: |
| 208 | + epts = get_group_entrypoints(group) |
| 209 | + self.assertIsInstance(epts, set, f"Expected set for group {group}") |
| 210 | + self.assertEqual(len(epts), 0, f"Expected non-empty set for group {group}") |
| 211 | + |
| 212 | + init_config(build_options={'use_entrypoints': True}) |
| 213 | + for group in [HOOKS_ENTRYPOINT, EASYBLOCK_ENTRYPOINT, TOOLCHAIN_ENTRYPOINT]: |
| 214 | + epts = get_group_entrypoints(group) |
| 215 | + self.assertIsInstance(epts, set, f"Expected set for group {group}") |
| 216 | + self.assertGreater(len(epts), 0, f"Expected non-empty set for group {group}") |
| 217 | + |
| 218 | + loaded_names = [ep.name for ep in epts] |
| 219 | + self.assertIn(expected[group], loaded_names, f"Expected entry point {expected[group]} in group {group}") |
| 220 | + |
| 221 | + def test_entrypoints_list_easyblocks(self): |
| 222 | + """ |
| 223 | + Tests for list_easyblocks function with entry points enabled. |
| 224 | + """ |
| 225 | + if not HAVE_ENTRY_POINTS: |
| 226 | + self.skipTest("Entry points not available in this Python version") |
| 227 | + |
| 228 | + # init_config() |
| 229 | + # print('-------', build_option('use_entrypoints', default='1234')) |
| 230 | + txt = list_easyblocks() |
| 231 | + self.assertNotIn("TestEasyBlock", txt, "TestEasyBlock should not be listed without entry points enabled") |
| 232 | + |
| 233 | + init_config(build_options={'use_entrypoints': True}) |
| 234 | + txt = list_easyblocks() |
| 235 | + self.assertIn("TestEasyBlock", txt, "TestEasyBlock should be listed with entry points enabled") |
| 236 | + |
| 237 | + def test_entrypoints_list_toolchains(self): |
| 238 | + """ |
| 239 | + Tests for list_toolchains function with entry points enabled. |
| 240 | + """ |
| 241 | + if not HAVE_ENTRY_POINTS: |
| 242 | + self.skipTest("Entry points not available in this Python version") |
| 243 | + |
| 244 | + # init_config() |
| 245 | + txt = list_toolchains() |
| 246 | + self.assertNotIn(MOCK_TOOLCHAIN, txt, f"{MOCK_TOOLCHAIN} should not be listed without entry points enabled") |
| 247 | + |
| 248 | + init_config(build_options={'use_entrypoints': True}) |
| 249 | + |
| 250 | + txt = list_toolchains() |
| 251 | + self.assertIn(MOCK_TOOLCHAIN, txt, f"{MOCK_TOOLCHAIN} should be listed with entry points enabled") |
| 252 | + |
| 253 | + def test_entrypoints_get_module_path(self): |
| 254 | + """ |
| 255 | + Tests for get_module_path function with entry points enabled. |
| 256 | + """ |
| 257 | + if not HAVE_ENTRY_POINTS: |
| 258 | + self.skipTest("Entry points not available in this Python version") |
| 259 | + |
| 260 | + module_path = get_module_path(MOCK_EASYBLOCK) |
| 261 | + self.assertIn('.generic.', module_path, "Module path should contain '.generic.'") |
| 262 | + |
| 263 | + init_config(build_options={'use_entrypoints': True}) |
| 264 | + # Reload the EasyBlock module to ensure it is recognized |
| 265 | + module_path = get_module_path(MOCK_EASYBLOCK) |
| 266 | + self.assertEqual(module_path, self.module, "Module path should match the mock module path") |
| 267 | + |
| 268 | + |
| 269 | +def suite(): |
| 270 | + return TestLoaderFiltered().loadTestsFromTestCase(EasyBuildEntrypointsTest, sys.argv[1:]) |
| 271 | + |
| 272 | + |
| 273 | +if __name__ == '__main__': |
| 274 | + res = TextTestRunner(verbosity=1).run(suite()) |
| 275 | + sys.exit(len(res.failures)) |
0 commit comments