Skip to content
Snippets Groups Projects
Commit a4f8cba7 authored by Thomas Kluyver's avatar Thomas Kluyver
Browse files

Format test code

parent 3741c366
No related branches found
No related tags found
1 merge request!933[Tests] clearer comparison of HDF5 files
import hashlib
import io
import logging
import multiprocessing
import pathlib
import tempfile
import time
from contextlib import redirect_stdout
from dataclasses import dataclass
......@@ -46,19 +44,23 @@ class ComparisonResult:
for ds in self.changed_dsets:
print(f" ~ CHANGED: {ds}")
def gather_dsets(f: h5py.File):
res = set()
def visitor(name, obj):
if isinstance(obj, h5py.Dataset):
res.add(name)
f.visititems(visitor)
return res
def validate_file(
ref_folder: pathlib.PosixPath,
out_folder: pathlib.PosixPath,
exclude_dsets: set,
test_file: str
test_file: str,
) -> ComparisonResult:
res = ComparisonResult(test_file, [], [], [])
ref_file = ref_folder / test_file
......@@ -77,7 +79,7 @@ def validate_file(
eq = np.array_equal(ref_arr, out_arr, equal_nan=True)
else:
# Both single values
eq = (ref_arr == out_arr)
eq = ref_arr == out_arr
if not eq:
changed.append(dsname)
......@@ -85,9 +87,10 @@ def validate_file(
test_file,
new_dsets=sorted(out_dsets - ref_dsets),
missing_dsets=sorted(ref_dsets - out_dsets),
changed_dsets=changed
changed_dsets=changed,
)
def parse_config(cmd: List[str], config: Dict[str, Any], out_folder: str) -> List[str]:
"""Convert a dictionary to a list of arguments.
......@@ -177,11 +180,7 @@ def validate_hdf5_files(
assert ok, "HDF5 files changed - see details above"
def slurm_watcher(
test_key: str,
std_out: str
):
def slurm_watcher(test_key: str, std_out: str):
"""
Watch for submitted slurm jobs and wait for them to finish.
After they finish apply first test and check
......@@ -208,19 +207,25 @@ def slurm_watcher(
res = run(cmd, stdout=PIPE)
states = res.stdout.decode().split("\n")[2:-1]
if not any(s.strip() in [
"COMPLETING",
"RUNNING",
"CONFIGURING",
"PENDING",
] for s in states):
if not any(
s.strip()
in [
"COMPLETING",
"RUNNING",
"CONFIGURING",
"PENDING",
]
for s in states
):
slurm_watcher = False
else:
time.sleep(2)
# 1st check that all jobs were COMPLETED without errors.
states = res.stdout.decode().split("\n")[2:-1]
assert all(s.strip() == "COMPLETED" for s in states), f"{test_key} failure, calibration jobs were not completed. {jobids}: {states}" # noqa
assert all(
s.strip() == "COMPLETED" for s in states
), f"{test_key} failure, calibration jobs were not completed. {jobids}: {states}" # noqa
LOGGER.info(f"{test_key}'s jobs were COMPLETED")
......@@ -230,11 +235,8 @@ def slurm_watcher(
list(automated_test_config.items()),
ids=list(automated_test_config.keys()),
)
def test_xfel_calibrate(
test_key: str, val_dict: dict,
release_test_config: Tuple[bool, bool, bool, bool]
):
""" Test xfel calibrate detectors and calibrations written
def test_xfel_calibrate(test_key: str, val_dict: dict, release_test_config: Tuple):
"""Test xfel calibrate detectors and calibrations written
in the given callab_test YAML file.
Args:
test_key : Key for the xfel-calibrate test.
......@@ -244,9 +246,14 @@ def test_xfel_calibrate(
"""
(
detectors, calibration, picked_test,
skip_numerical_validation, only_validate,
use_slurm, reference_dir_base, out_dir_base,
detectors,
calibration,
picked_test,
skip_numerical_validation,
only_validate,
use_slurm,
reference_dir_base,
out_dir_base,
) = release_test_config
cal_type = val_dict["cal_type"]
......@@ -254,10 +261,9 @@ def test_xfel_calibrate(
if not picked_test:
# Skip non-selected detectors
if (
detectors != ["all"] and
det_type.lower() not in [d.lower() for d in detectors]
):
if detectors != ["all"] and det_type.lower() not in [
d.lower() for d in detectors
]:
pytest.skip()
# Skip non-selected calibration
......@@ -271,14 +277,16 @@ def test_xfel_calibrate(
cal_conf = val_dict["config"]
out_folder = pathlib.Path(cal_conf["out-folder"].format(
out_dir_base, cal_conf["karabo-id"], test_key))
reference_folder = pathlib.Path(val_dict["reference-folder"].format(
reference_dir_base, cal_conf["karabo-id"], test_key))
out_folder = pathlib.Path(
cal_conf["out-folder"].format(out_dir_base, cal_conf["karabo-id"], test_key)
)
reference_folder = pathlib.Path(
val_dict["reference-folder"].format(
reference_dir_base, cal_conf["karabo-id"], test_key
)
)
report_name = (
out_folder /
f"{test_key}_{datetime.now().strftime('%y%m%d_%H%M%S')}")
report_name = out_folder / f"{test_key}_{datetime.now().strftime('%y%m%d_%H%M%S')}"
cal_conf["report-to"] = str(report_name)
......@@ -290,13 +298,18 @@ def test_xfel_calibrate(
out_folder,
reference_folder,
cal_type,
)
)
return
if not use_slurm: # e.g. for Gitlab CI.
cmd += ["--no-cluster-job"]
cmd += ["--slurm-name", test_key, "--cal-db-interface", "tcp://max-exfl-cal001:8015#8045"]
cmd += [
"--slurm-name",
test_key,
"--cal-db-interface",
"tcp://max-exfl-cal001:8015#8045",
]
f = io.StringIO()
LOGGER.info(f"Submitting CL: {cmd}")
with redirect_stdout(f):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment