Skip to content
Snippets Groups Projects

Restructure log location

Merged Ashwin Kumar Karnad requested to merge restructure-log-location into main
1 file
+ 0
1
Compare changes
  • Side-by-side
  • Inline
+ 89
18
@@ -6,13 +6,15 @@ import shutil
import subprocess
from pathlib import Path
import logging
import datetime
import pytest
mod = importlib.import_module("mpsd-software-environment")
# set loglevel to debug - useful for understanding problems.
# (if the tests pass, pytest doesn't show any output)
mod.set_up_logging(loglevel="debug", filename="tests.log")
mod.set_up_logging(loglevel="debug", file_path="tests.log")
logging.debug(f"We have set up logging from {__file__}")
@@ -162,13 +164,13 @@ def test_setup_log_cmd(tmp_path):
Check that logs/install-software-environment.log is updated when the module is run
"""
log_file = "install.log"
cmd_log_file = mod.config_vars["cmd_log_file"]
script_dir = tmp_path / "test_prepare_env"
mpsd_release_to_test = "dev-23a"
release_base_dir = script_dir / mpsd_release_to_test
if os.path.exists(release_base_dir / log_file):
initial_bytes = os.path.getsize(log_file)
if os.path.exists(release_base_dir / cmd_log_file):
initial_bytes = os.path.getsize(cmd_log_file)
else:
initial_bytes = 0
@@ -177,11 +179,11 @@ def test_setup_log_cmd(tmp_path):
mod.prepare_environment(mpsd_release=mpsd_release_to_test, script_dir=(script_dir))
# check that logs/install-software-environment.log is updated
assert os.path.exists(release_base_dir / log_file)
assert os.path.getsize(release_base_dir / log_file) > initial_bytes
assert os.path.exists(release_base_dir / cmd_log_file)
assert os.path.getsize(release_base_dir / cmd_log_file) > initial_bytes
# Check that the log file has "Spack environments branch: dev-23a " in the last line
with open(release_base_dir / log_file, "r") as f:
with open(release_base_dir / cmd_log_file, "r") as f:
last_line = f.readlines()[-1]
assert "Spack environments branch: dev-23a " in last_line
@@ -225,7 +227,8 @@ def test_install_environment_zlib():
script_dir.mkdir(exist_ok=True, parents=True)
mpsd_release_to_test = "dev-23a"
toolchain_to_test = "global_generic"
mpsd_microarch = os.getenv("MPSD_MICROARCH", "UNKNOWN_MICROARCH")
cmd_log_file = mod.config_vars["cmd_log_file"]
mpsd_microarch = mod.get_native_microarchitecture()
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))
@@ -266,7 +269,12 @@ def test_install_environment_zlib():
)
with open(setup_file, "w") as f:
f.write(lines)
# install global_generic toolchain
mod.set_up_logging(
"WARNING",
mod.get_installer_log_file_path(mpsd_release_to_test, "install", script_dir),
)
mod.install_environment(
mpsd_release=mpsd_release_to_test,
toolchains=[toolchain_to_test],
@@ -278,30 +286,30 @@ def test_install_environment_zlib():
# release_base_dir/mpsd_microarch
# print("Debug here ")
# time.sleep(10)
build_log = list(
(release_base_dir / mpsd_microarch / "logs").glob(
f"{mpsd_release_to_test}_{toolchain_to_test}_*.log"
(release_base_dir / "logs").glob(
f"{mpsd_release_to_test}_{mpsd_microarch}_*_install.log"
)
)
assert len(build_log) > 0
assert len(build_log) == 2
# take the most recent build log
build_log = sorted(build_log)[0]
build_log = sorted(build_log)[1]
# check that the build log contains statement ##### Installation finished
with open(build_log, "r") as f:
lines = f.read()
assert "##### Installation finished" in lines
build_log_file_name = os.path.basename(build_log)
os.path.basename(build_log)
# assert that install log files exists
assert os.path.exists(release_base_dir / "install.log")
assert os.path.exists(release_base_dir / cmd_log_file)
# assert that the build log is written to the install log file
os.path.basename(build_log)
with open(release_base_dir / "install.log", "r") as f:
with open(release_base_dir / cmd_log_file, "r") as f:
lines = f.read()
assert (
f"installing {toolchain_to_test} and logging at logs/{build_log_file_name}"
in lines
f"installing {toolchain_to_test} and logging at {str(build_log)}" in lines
)
# assert that the module files are created correctly
assert os.path.exists(release_base_dir / mpsd_microarch)
@@ -313,13 +321,35 @@ def test_install_environment_zlib():
lines = f.read()
assert "zlib" in lines
# install again to ensure that
# commands that skip creation of folders when
# they are already present works as expected
# reload the module to ensure that date changes
importlib.reload(mod)
mod.set_up_logging(
"WARNING",
mod.get_installer_log_file_path(mpsd_release_to_test, "install", script_dir),
)
mod.install_environment(
mpsd_release=mpsd_release_to_test,
toolchains=[toolchain_to_test],
script_dir=script_dir,
enable_build_cache=False,
)
build_log = list(
(release_base_dir / "logs").glob(
f"{mpsd_release_to_test}_{mpsd_microarch}_*_install.log"
)
)
assert len(build_log) == 4
def test_metadata_logging(tmp_path):
"""Test that metadata is logged and read correctly."""
# Test that the metadata is logged correctly
filename = tmp_path / "test-metadata.log"
print(f"Writing to {filename}")
mod.set_up_logging(loglevel="debug", filename=filename)
mod.set_up_logging(loglevel="debug", file_path=filename)
# our test data
keys = ["important_key", "important_key2"]
@@ -353,6 +383,47 @@ def test_metadata_logging(tmp_path):
assert len(read_dict) == len(keys)
def test_create_log_file_names():
"""Test that the log file names are created correctly."""
create_log_file_names = mod.create_log_file_names
mpsd_release = "dev-23a"
mpsd_microarch = "sandybridge"
date = datetime.datetime.now().replace(microsecond=0).isoformat()
action = "install"
toolchain = "foss2021a"
# test build_log_file_name generation
build_log_file_name = create_log_file_names(
mpsd_microarch=mpsd_microarch,
mpsd_release=mpsd_release,
date=date,
action=action,
toolchain=toolchain,
)
assert (
build_log_file_name
== f"{mpsd_release}_{mpsd_microarch}_{date}_BUILD_{toolchain}_{action}.log"
)
installer_log_file_name = create_log_file_names(
mpsd_microarch=mpsd_microarch,
mpsd_release=mpsd_release,
date=date,
action=action,
)
assert (
installer_log_file_name
== f"{mpsd_release}_{mpsd_microarch}_{date}_APEX_{action}.log"
)
# test no build log file for incorrect action
build_log_file_name = create_log_file_names(
mpsd_microarch=mpsd_microarch,
mpsd_release=mpsd_release,
date=date,
action="status",
toolchain=toolchain,
)
assert build_log_file_name is None
def test_interface(tmp_path):
"""Test other things (not implemented yet)."""
pass
Loading