Skip to content
Snippets Groups Projects

force `subprocess.run` to check return code is 0

Merged Hans Fangohr requested to merge force-checking-of-error-code into linux-debian11
1 file
+ 5
6
Compare changes
  • Side-by-side
  • Inline
+ 79
28
@@ -11,6 +11,41 @@ import pytest
@@ -11,6 +11,41 @@ import pytest
mod = importlib.import_module("mpsd-software-environment")
mod = importlib.import_module("mpsd-software-environment")
 
def create_mock_git_repository(target_directory, create_directory=True):
 
"""
 
Create a git repository in the directory `target_directory`.
 
 
Arguments
 
---------
 
target_directory : pathlib.Path
 
- path at which the root of the repository should be located (i.e. `.git` folder)
 
 
create_directory : bool
 
- create `target_directory` and parent directories if True
 
 
"""
 
# create directory first
 
if create_directory:
 
target_directory.mkdir(parents=True)
 
 
# then create git repository:
 
with mod.os_chdir(str(target_directory)):
 
subprocess.run("git init .", shell=True, check=True)
 
subprocess.run("echo 'fake content' > readme.txt", shell=True, check=True)
 
subprocess.run("git add readme.txt", shell=True, check=True)
 
subprocess.run("pwd", shell=True)
 
 
# if email and username are not available (such as on naked test container),
 
# git may complain. We set a temporary user for this one commit to work around
 
# that.
 
user_details = "-c user.name='Tes Ta' -c user.email='tester@some-ci.org'"
 
subprocess.run(
 
f'git {user_details} commit -m "first commit" readme.txt',
 
shell=True,
 
check=True,
 
)
 
 
def test_os_chdir(tmp_path):
def test_os_chdir(tmp_path):
"""Test the os_chdir context manager."""
"""Test the os_chdir context manager."""
# create a temporary directory for testing
# create a temporary directory for testing
@@ -30,39 +65,47 @@ def test_os_chdir(tmp_path):
@@ -30,39 +65,47 @@ def test_os_chdir(tmp_path):
def test_prepare_environment(tmp_path):
def test_prepare_environment(tmp_path):
"""Simulate running preparation of environment.
"""Simulate running preparation of environment.
Simulate running ./install-software-environment.py --release dev-23a \
Simulate running ./install-software-environment.py --release dev-23a \
--target-directory /tmp/test_prepare_env
--target-directory /tmp/test_prepare_env
prepare_env is run when cmd is not specified, we can test cmd='prepare'
prepare_env is run when cmd is not specified, we can test cmd='prepare'
and cmd=None to check both cases
and cmd=None to check both cases
"""
"""
script_dir = tmp_path / "test_prepare_env"
script_dir = tmp_path / "mpsd_opt" / "linux_debian_11"
spack_environments = "spack-environments"
spack_environments = "spack-environments"
mpsd_release_to_test = "dev-23a"
mpsd_release_to_test = "dev-23a"
release_base_dir = script_dir / mpsd_release_to_test
release_base_dir = script_dir / mpsd_release_to_test
# check that the test directory does not exist
# check that the test directory does not exist
assert not script_dir.exists()
assert not script_dir.exists()
 
# prepare_environment expects to be executed in git repository
 
# (mpsd-software-environments). It queries the commit on which we are to
 
# log that information. For this to work, we need to execute the command
 
# within a directory tree that has a git repository at the same or high
 
# level. Let's create one:
 
create_mock_git_repository(script_dir)
 
 
# now call the function we want to test
result = mod.prepare_environment(
result = mod.prepare_environment(
mpsd_release=mpsd_release_to_test, script_dir=(script_dir)
mpsd_release=mpsd_release_to_test, script_dir=script_dir
)
)
# wait for 20 seconds for the git clone to finish
# time.sleep(20)
# check if the directory now is created
# check if the directory now is created
assert release_base_dir.exists()
assert release_base_dir.exists()
# check for spack-environments directory
# check for spack-environments directory
assert spack_environments in os.listdir(release_base_dir)
assert spack_environments in os.listdir(release_base_dir)
# check if the git branch is correctly checked out
assert (
# check if the git branch is correctly checked out. We expect output such as
subprocess.run(
# git_branch_stdout = '* dev-23a\n develop\n'
f"cd {str(release_base_dir/spack_environments)} && git branch",
# The entry with the '* ' prefix is the active branch.
shell=True,
git_branch_output_raw = subprocess.run(
capture_output=True,
f"cd {str(release_base_dir/spack_environments)} && git branch",
)
shell=True,
.stdout.decode("utf-8")
capture_output=True,
.split("\n")[0]
== f"* {mpsd_release_to_test}"
)
)
 
git_branch_stdout = git_branch_output_raw.stdout.decode("utf-8")
 
assert f"* {mpsd_release_to_test}" in git_branch_stdout
 
# check that result is a list and contains atleast ['global','foss2021a-mpi']
# check that result is a list and contains atleast ['global','foss2021a-mpi']
assert isinstance(result, list)
assert isinstance(result, list)
assert "global" in result
assert "global" in result
@@ -91,6 +134,7 @@ def test_setup_log_cmd(tmp_path):
@@ -91,6 +134,7 @@ def test_setup_log_cmd(tmp_path):
initial_bytes = 0
initial_bytes = 0
# run the prepare_env functionality
# run the prepare_env functionality
 
create_mock_git_repository(target_directory=script_dir, create_directory=True)
mod.prepare_environment(mpsd_release=mpsd_release_to_test, script_dir=(script_dir))
mod.prepare_environment(mpsd_release=mpsd_release_to_test, script_dir=(script_dir))
# check that logs/install-software-environment.log is updated
# check that logs/install-software-environment.log is updated
@@ -103,20 +147,19 @@ def test_setup_log_cmd(tmp_path):
@@ -103,20 +147,19 @@ def test_setup_log_cmd(tmp_path):
assert "Spack environments branch: dev-23a " in last_line
assert "Spack environments branch: dev-23a " in last_line
def test_install_environment(tmp_path):
def test_install_environment_wrong_toolchain(tmp_path):
"""Test the installation of a toolchain.
"""Test exception is raised for non-existing toolchain."""
# Expect an Exception when wrong toolchains are provided
This is a long test, its handy to test this with print statements printed to
stdout, use:
pytest -s
Expect an Exception when wrong toolchains are provided
"""
with pytest.raises(Exception):
with pytest.raises(Exception):
mod.install_environment(
mod.install_environment(
mpsd_release="dev-23a",
mpsd_release="dev-23a",
toolchains=["wrong-toolchain"],
toolchains=["wrong-toolchain"],
script_dir=(tmp_path),
script_dir=(tmp_path),
)
)
 
 
 
def test_install_environment_wrong_mpsd_release(tmp_path):
 
"""Test exception is raised for non-existing mpsd release."""
# Expect an Exception when wrong mpsd_release is provided (part of
# Expect an Exception when wrong mpsd_release is provided (part of
# prepare_environment)
# prepare_environment)
with pytest.raises(Exception):
with pytest.raises(Exception):
@@ -125,11 +168,18 @@ def test_install_environment(tmp_path):
@@ -125,11 +168,18 @@ def test_install_environment(tmp_path):
toolchains=["foss2021a-mpi"],
toolchains=["foss2021a-mpi"],
script_dir=(tmp_path),
script_dir=(tmp_path),
)
)
# prepare a test of global generic with only zlib to test the installation
# prepare dev-23a release
# script_dir = tmp_path / "test_global_generic"
def test_install_environment_zlib():
# for actaual installation avoid tmp_path as the lenght of the path is too long
"""Test installation of toolchain."""
# and spack complains
# Prepare a test installation of global generic
 
# with only zlib to test the installation
 
# This is a long test,
 
# its handy to test this with print statements printed to
 
# stdout, use:
 
# pytest -s
 
# for this installation avoid tmp_path as
 
# the length of the path becomes too long and spack complains
script_dir = Path("/tmp/test_global_generic")
script_dir = Path("/tmp/test_global_generic")
if script_dir.exists():
if script_dir.exists():
shutil.rmtree(script_dir)
shutil.rmtree(script_dir)
@@ -138,6 +188,7 @@ def test_install_environment(tmp_path):
@@ -138,6 +188,7 @@ def test_install_environment(tmp_path):
toolchain_to_test = "global_generic"
toolchain_to_test = "global_generic"
mpsd_microarch = os.getenv("MPSD_MICROARCH", "UNKNOWN_MICROARCH")
mpsd_microarch = os.getenv("MPSD_MICROARCH", "UNKNOWN_MICROARCH")
release_base_dir = script_dir / mpsd_release_to_test
release_base_dir = script_dir / mpsd_release_to_test
 
create_mock_git_repository(target_directory=script_dir, create_directory=False)
mod.prepare_environment(mpsd_release=mpsd_release_to_test, script_dir=(script_dir))
mod.prepare_environment(mpsd_release=mpsd_release_to_test, script_dir=(script_dir))
# Patch the spack environments to create a fake global_generic
# Patch the spack environments to create a fake global_generic
# create a test toolchain
# create a test toolchain
Loading