Belle II Software  release-08-03-00
test_caf.py
1 
8 import json
9 import subprocess
10 
11 import basf2
12 import b2test_utils
13 import validation_gt as vgt
14 
15 
17  b2test_utils.skip_test("Will not run the test because $BELLE2_IS_CI is not set.")
18 
19 calibration_path = b2test_utils.require_file('calibration', 'validation')
20 
22 
23  # Prepare the configuration dictionary for CAF and dump it into a JSON file.
24  config = {
25  "caf_script": "caf_klm_channel_status.py",
26  "database_chain": [globaltag for globaltag in reversed(vgt.get_validation_globaltags())],
27  "requested_iov": [0, 0, -1, -1]
28  }
29  with open("config.json", "w") as config_json:
30  json.dump(config, config_json)
31 
32  # Prepare the input files for the calibration and dump them into a JSON file.
33  input_files = {
34  "raw_physics": [
35  [f"{calibration_path}/cdst_e0008_r3121_hadronSkim/", [8, 3121]],
36  [f"{calibration_path}/cdst_e0010_r5095_hadronSkim/", [10, 5095]]
37  ],
38  "raw_cosmic": [
39  [f"{calibration_path}/cdst_e0008_r3121_cosmicSkim/", [8, 3121]],
40  [f"{calibration_path}/cdst_e0010_r5095_cosmicSkim/", [10, 5095]]
41  ],
42  "raw_beam": []
43  }
44  with open("input_files.json", "w") as input_files_json:
45  json.dump(input_files, input_files_json)
46 
47  # Now simply run the calibration locally (on our CI/CD servers we can not test different backends).
48  try:
49  subprocess.check_call(['b2caf-prompt-run', 'Local', 'config.json', 'input_files.json', '--heartbeat', '20'])
50  except subprocess.CalledProcessError as e:
51  # In case of failure, prints all the BELLE2 environment variables...
53  # ... and the submit.sh...
54  sub_name = basf2.find_file('calibration_results/KLMChannelStatus/0/collector_output/raw/0/submit.sh', '', True)
55  if sub_name:
56  with open(sub_name) as sub_file:
57  basf2.B2ERROR('Calibration failed, here is the submit.sh of the first collector job.')
58  print(sub_file.read())
59  # ... and the stdout...
60  out_name = basf2.find_file('calibration_results/KLMChannelStatus/0/collector_output/raw/0/stdout', '', True)
61  if out_name:
62  with open(out_name) as out_file:
63  basf2.B2ERROR('Calibration failed, here is the stdout of the first collector job.')
64  print(out_file.read())
65  # ... and the stderr.
66  err_name = basf2.find_file('calibration_results/KLMChannelStatus/0/collector_output/raw/0/stderr', '', True)
67  if err_name:
68  with open(err_name) as err_file:
69  basf2.B2ERROR('Calibration failed, here is the stderr of the first collector job.')
70  print(err_file.read())
71  basf2.B2FATAL(
72  f'The test failed because an exception was raised ({e}). Please re-run the build if this failure happened on bamboo.')
bool is_ci()
Definition: __init__.py:403
def require_file(filename, data_type="", py_case=None)
Definition: __init__.py:54
def clean_working_directory()
Definition: __init__.py:189
def print_belle2_environment()
Definition: __init__.py:368
def skip_test(reason, py_case=None)
Definition: __init__.py:31