Skip to content

Commit 2e612d8

Browse files
authored
817 automatically perform module purge before building tests (#834)
* Perform module purge before building * Various changes * Purge modules before building and running * Only perform module purge when modules are specified * Add argument to disable module purging * Add purge_modules option to build and run * Revert commits on wrong branch * Remove module purge cmd line arg * Make module purge unit test more robust * Made some progress * Fix module purge in build/run script * Fix module purge run test * Fix module purge build unit test * Fix log test * Skip module purge tests when no module syste * Attempt to fix module purge * Fix log command test * Fix purging behavior in unit tests (maybe)
1 parent d3cb4b8 commit 2e612d8

File tree

6 files changed

+92
-2
lines changed

6 files changed

+92
-2
lines changed

lib/pavilion/scriptcomposer.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ def module_change(self, module, sys_vars, config_wrappers):
129129

130130
self.env_change(mod_env)
131131

132+
def module_purge(self) -> None:
133+
"""Add a module purge to the script."""
134+
135+
self._script_lines.extend([
136+
"# Check whether the module command exists",
137+
"if declare -F module; then",
138+
"\tmodule purge",
139+
"fi"
140+
])
141+
132142
def newline(self):
133143
"""Function that just adds a newline to the script lines."""
134144
# This will create a blank line with just a newline.

lib/pavilion/test_config/file_format.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,11 @@ class TestConfigLoader(yc.YamlConfigLoader):
747747
help_text='If True, test will fail if any of its build '
748748
'commands fail, rather than just the last '
749749
'command.'),
750+
yc.StrElem(
751+
'purge_modules', choices=["true", "false", "True", "False"],
752+
default="True",
753+
help_text="Whether or not to perform a module purge "
754+
"before building."),
750755
],
751756
help_text="The test build configuration. This will be "
752757
"used to dynamically generate a build script for "
@@ -835,6 +840,11 @@ class TestConfigLoader(yc.YamlConfigLoader):
835840
'but may vary. '
836841
'(In particular, the \'raw\' scheduler has a much higher limit.) '
837842
'Tests that use MPI should use this cautiously.'),
843+
yc.StrElem(
844+
'purge_modules', choices=["true", "false", "True", "False"],
845+
default="True",
846+
help_text="Whether or not to perform a module purge "
847+
"before running."),
838848
],
839849
help_text="The test run configuration. This will be used "
840850
"to dynamically generate a run script for the "

lib/pavilion/test_run/test_run.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,16 @@ def _write_script(self, stype: str, path: Path, config: dict, module_wrappers: d
11471147
script.comment('To be built in an allocation.')
11481148

11491149
script.command(f'echo "(pav) Setting up {stype} environment."')
1150+
1151+
purge = utils.str_bool(config.get("purge_modules"))
1152+
1153+
if purge:
1154+
script.newline()
1155+
script.comment("Start with a fresh environment")
1156+
script.module_purge()
1157+
11501158
modules = config.get('modules', [])
1159+
11511160
if modules:
11521161
script.newline()
11531162
script.comment('Perform module related changes to the environment.')

lib/pavilion/unittest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,14 +313,15 @@ def _load_test(self, name: str, platform: str = 'this', host: str = 'this',
313313
del __config_lines
314314

315315
def _quick_test(self, cfg=None, name="quick_test",
316-
build=True, finalize=True):
316+
build=True, finalize=True, purge=True):
317317
"""Create a test run object to work with.
318318
The default is a simple hello world test with the raw scheduler.
319319
320320
:param dict cfg: An optional config dict to create the test from.
321321
:param str name: The name of the test.
322322
:param bool build: Build this test, while we're at it.
323323
:param bool finalize: Finalize this test.
324+
:param bool purge: Perform a module purge before building/running
324325
:rtype: TestRun
325326
"""
326327

@@ -333,6 +334,8 @@ def _quick_test(self, cfg=None, name="quick_test",
333334
cfg = loader.validate(loader.normalize(cfg))
334335

335336
cfg['name'] = name
337+
cfg["run"]["purge_modules"] = str(purge)
338+
cfg["build"]["purge_modules"] = str(purge)
336339

337340
var_man = VariableSetManager()
338341
var_man.add_var_set('var', cfg['variables'])

test/tests/log_cmd_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_log_arguments(self):
1919
log_cmd._setup_arguments(parser)
2020

2121
# run a simple test
22-
test = self._quick_test(finalize=False)
22+
test = self._quick_test(finalize=False, purge=False)
2323
raw = schedulers.get_plugin('raw')
2424

2525
raw.schedule_tests(self.pav_cfg, [test])

test/tests/mod_wrapper_tests.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,61 @@ def test_parse_module(self):
321321
for mod, result in checks:
322322
self.assertEqual(module_wrapper.parse_module(mod), result)
323323

324+
@unittest.skipIf(not has_module_cmd() and find_module_init() is None,
325+
"Could not find a module system.")
326+
def test_run_module_purge(self):
327+
"""Test that a module purge is performed when running tests."""
328+
329+
# Test that a module purge is performed by default
330+
test_cfg = self._quick_test_cfg()
331+
test_cfg['run']['cmds'] = [
332+
'[[ $(module -t list 2>&1) = "No modules loaded" ]] || exit 1',
333+
]
334+
test_cfg['run']['preamble'].append('module load test_mod1 || exit 2')
335+
336+
test = self._quick_test(test_cfg)
337+
338+
run_result = test.run()
339+
340+
self.assertEqual(run_result, 0)
341+
342+
# Check that we can disable purging
343+
test_cfg = self._quick_test_cfg()
344+
test_cfg['run']['cmds'] = [
345+
'[[ $(module -t list 2>&1) = "No modules loaded" ]] || exit 1',
346+
]
347+
test_cfg['run']['preamble'].append('module load test_mod1 || exit 2')
348+
test_cfg["run"]["purge_modules"] = False
349+
test = self._quick_test(test_cfg)
350+
run_result = test.run()
351+
352+
self.assertEqual(run_result, 1)
353+
354+
@unittest.skipIf(not has_module_cmd() and find_module_init() is None,
355+
"Could not find a module system.")
356+
def test_build_module_purge(self):
357+
"""Test that a module purge is performed when building tests."""
358+
359+
# Test that a module purge is performed by default
360+
test_cfg = self._quick_test_cfg()
361+
test_cfg['build']['cmds'] = [
362+
'[[ $(module -t list 2>&1) = "No modules loaded" ]] || exit 1',
363+
]
364+
test_cfg['run']['preamble'].append('module load test_mod1 || exit 2')
365+
366+
test = self._quick_test(test_cfg, build=False)
367+
build_result = test.build()
368+
369+
self.assertEqual(build_result, True)
370+
371+
# Check that we can disable purging
372+
test_cfg = self._quick_test_cfg()
373+
test_cfg['build']['cmds'] = [
374+
'[[ $(module -t list 2>&1) = "No modules loaded" ]] || exit 1',
375+
]
376+
test_cfg['build']['preamble'] = ['module load test_mod1 || exit 2']
377+
test_cfg["build"]["purge_modules"] = False
378+
test = self._quick_test(test_cfg, build=False)
379+
build_result = test.build()
380+
381+
self.assertEqual(build_result, False)

0 commit comments

Comments
 (0)