HPS-MC
test_job.py
Go to the documentation of this file.
1 import unittest
2 import os
3 import sys
4 import logging
5 import configparser
6 
7 from os.path import expanduser
8 from hpsmc.job import JobConfig, Job, JobStore, JobScriptDatabase
9 from hpsmc.tools import SLIC, HPSTR
10 
11 
12 class TestJobConfig(unittest.TestCase):
13 
14  def test_init(self):
15  job_config = JobConfig()
16  self.assertEqual(job_config.config_files, [os.path.join(expanduser("~"), ".hpsmc"), os.path.abspath(".hpsmc")])
17 
18  def test_init_config(self):
19  job_config = JobConfig(config_files=['test_helpers/.hpsmc_test_cfg'], include_default_locations=False)
20  self.assertEqual(job_config.config_files, ['test_helpers/.hpsmc_test_cfg'])
21  # JobConfig uses configparser to load config. If the default config files don't exist, configparser will not crash but ignore those files instead.
22 
23  def test_config(self):
24  job_config = JobConfig(config_files=['test_helpers/.hpsmc_test_cfg'], include_default_locations=False)
25  component = SLIC()
26  job_config.config(component, required_names=['detector_dir'])
27  self.assertEqual(component.detector_dir, 'some/detector/dir')
28 
30  job_config = JobConfig(config_files=['test_helpers/.hpsmc_test_cfg_invalid'], include_default_locations=False)
31  component = HPSTR()
32  self.assertRaises(Exception, lambda: job_config.config(component, required_names=['hpstr_install_dir']), "Missing required config section HPSTR")
33  job_config.config(component, required_names=['hpstr_install_dir'], require_section=False)
34  self.assertEqual(component.hpstr_install_dir, None)
35 
37  job_config = JobConfig(config_files=['test_helpers/.hpsmc_test_cfg'], include_default_locations=False)
38  component = SLIC()
39  self.assertRaises(Exception, lambda: job_config.config(component, required_names=['not_in_config']), "Missing required config not_in_config")
40 
42  job_config = JobConfig(config_files=['test_helpers/.hpsmc_test_cfg'], include_default_locations=False)
43  component = SLIC()
44  self.assertRaises(Exception, lambda: job_config.config(component, allowed_names=['not_in_config']), "Config name detector_dir is not allowed for SLIC")
45 
46 
47 class TestJobStore(unittest.TestCase):
48 
49  def test_load(self):
50  job_store = JobStore()
51  job_store.load("test_helpers/job_files/job_params.json")
52  self.assertEqual(job_store.data, {1: {"input_files": {"input1.stdhep": "path/to/input1.stdhep", "input2.stdhep": "path/to/input2.stdhep"}, "output_files": {"output.slcio": "output_file.slcio"}, "output_dir": "some/output_dir", "job_id": 1}, 2: {"input_files": {"input1.stdhep": "path/to/input1.stdhep", "input2.stdhep": "path/to/input2.stdhep"}, "output_files": {"output.slcio": "output_file.slcio"}, "output_dir": "some/output_dir", "job_id": 2}})
53 
55  job_store = JobStore()
56  self.assertRaises(Exception, lambda: job_store.load("path/does/not/exist.json"), "JSON job store does not exist: path/does/not/exist.json")
57 
58  def test_init(self):
59  job_store = JobStore("test_helpers/job_files/job_params.json")
60  self.assertEqual(job_store.path, "test_helpers/job_files/job_params.json")
61  self.assertEqual(job_store.data, {1: {"input_files": {"input1.stdhep": "path/to/input1.stdhep", "input2.stdhep": "path/to/input2.stdhep"}, "output_files": {"output.slcio": "output_file.slcio"}, "output_dir": "some/output_dir", "job_id": 1}, 2: {"input_files": {"input1.stdhep": "path/to/input1.stdhep", "input2.stdhep": "path/to/input2.stdhep"}, "output_files": {"output.slcio": "output_file.slcio"}, "output_dir": "some/output_dir", "job_id": 2}})
62 
63  def test_get_job(self):
64  job_store = JobStore("test_helpers/job_files/job_params.json")
65  job = job_store.get_job(1)
66  self.assertEqual(job['job_id'], 1)
67  self.assertEqual(job['output_dir'], "some/output_dir")
68  self.assertEqual(job['input_files'], {"input1.stdhep": "path/to/input1.stdhep", "input2.stdhep": "path/to/input2.stdhep"})
69  self.assertEqual(job['output_files'], {"output.slcio": "output_file.slcio"})
70 
71  def test_get_job_ids(self):
72  job_store = JobStore("test_helpers/job_files/job_params.json")
73  self.assertEqual(job_store.get_job_ids(), [1, 2])
74 
75  def test_has_job_id(self):
76  job_store = JobStore("test_helpers/job_files/job_params.json")
77  self.assertTrue(job_store.has_job_id(1))
78  self.assertFalse(job_store.has_job_id(3))
79 
80 
81 class TestJobScriptDatabase(unittest.TestCase):
82 
83  @unittest.skip("Test is broken")
84  def test_init(self):
85  job_script_database = JobScriptDatabase()
86  self.assertEqual(len(job_script_database.scripts), 33)
87 
89  job_script_database = JobScriptDatabase()
90  basepath = os.path.join(os.environ['HPSMC_DIR'], 'lib', 'python', 'jobs')
91  self.assertEqual(job_script_database.get_script_path("slic"), os.path.join(basepath, 'slic_job.py'))
92  self.assertEqual(job_script_database.get_script_path("tritrig_gen"), os.path.join(basepath, 'tritrig_gen_job.py'))
93 
94  @unittest.skip("Test is broken")
96  job_script_database = JobScriptDatabase()
97  job_name_list = sorted(job_script_database.get_script_names())
98  self.assertEqual(job_name_list, ["ap_gen", "ap_gen_to_slic", "ap_slic", "beam_coords", "beam_gen", "beam_gen_sample", "beam_prep_and_slic", "data_cnv", "dummy", "fee_gen_to_recon", "hpstr", "lcio_count", "moller_gen", "rad_gen", "readout_recon", "signal_beam_merge_to_recon", "signal_beam_merge_to_recon_2016", "signal_pulser_overlay_to_recon", "sim_to_ana", "simp", "slic", "slic_to_ana", "slic_to_anaMC", "slic_to_recon", "tritrig_beam", "tritrig_beam_slic_to_reco", "tritrig_gen", "tritrig_gen_to_beam_coords", "tritrig_prep_and_slic", "tritrig_sim_full_chain", "tritrig_slic_full_chain", "wab_gen_sample", "wab_gen_to_slic"])
99 
100  def test_exists(self):
101  job_script_database = JobScriptDatabase()
102  self.assertTrue(job_script_database.exists("slic"))
103  self.assertFalse(job_script_database.exists("does_not_exist"))
104 
105 
106 class TestJob(unittest.TestCase):
107 
108  def test_init(self):
109  job = Job(args=['-d', 'some/run/dir', '-o', 'output/dir', '-i', '1'])
110  self.assertEqual(job.args, ['-d', 'some/run/dir', '-o', 'output/dir', '-i', '1'])
111  self.assertEqual(job.description, 'HPS MC Job')
112  self.assertEqual(job.job_id, None)
113  self.assertEqual(job.param_file, None)
114  self.assertEqual(job.components, [])
115  self.assertEqual(job.rundir, os.getcwd())
116  self.assertEqual(job.params, {})
117  self.assertEqual(job.output_dir, os.getcwd())
118  self.assertEqual(job.input_files, {})
119  self.assertEqual(job.output_files, {})
120  self.assertEqual(job.ptags, {})
121  self.assertEqual(job.log, sys.stdout)
122  self.assertEqual(job.out, sys.stdout)
123  self.assertEqual(job.err, sys.stderr)
124  self.assertEqual(job.enable_copy_output_files, True)
125  self.assertEqual(job.enable_copy_input_files, True)
126  self.assertEqual(job.delete_existing, False)
127  self.assertEqual(job.delete_rundir, False)
128  self.assertEqual(job.dry_run, False)
129  self.assertEqual(job.ignore_return_codes, True)
130  self.assertEqual(job.check_output_files, True)
131  self.assertEqual(job.check_commands, False)
132  self.assertEqual(job.enable_file_chaining, True)
133  self.assertEqual(job.enable_env_config, False)
134  self.assertEqual(job.log_level, logging.INFO)
135 
136  def test_add(self):
137  job = Job()
138  job.add('test1')
139  self.assertEqual(job.components, ['test1'])
140  job.add(['test2', 'test3'])
141  self.assertEqual(job.components, ['test1', 'test2', 'test3'])
142 
143  def test_set_params(self):
144  job = Job()
145  job.set_parameters({'test1': 1, 'test2': 2})
146  self.assertEqual(job.params, {'test1': 1, 'test2': 2})
147  job.set_parameters({'test3': 3})
148  self.assertEqual(job.params, {'test1': 1, 'test2': 2, 'test3': 3})
149 
150  def test_parse_args(self):
151  job = Job(args=['-c', 'test_helpers/.hpsmc_test_cfg', '-d', 'some/run/dir', '-o', 'test_helpers/job_files/out_file', '-e', 'test_helpers/job_files/err_file', '-l', 'test_helpers/job_files/log_file', '-s', '1', '-i', '1', 'test_helpers/job_files/some_job_script.py', 'test_helpers/job_files/job_params.json'])
152  self.assertEqual(job.args, ['-c', 'test_helpers/.hpsmc_test_cfg', '-d', 'some/run/dir', '-o', 'test_helpers/job_files/out_file', '-e', 'test_helpers/job_files/err_file', '-l', 'test_helpers/job_files/log_file', '-s', '1', '-i', '1', 'test_helpers/job_files/some_job_script.py', 'test_helpers/job_files/job_params.json'])
153  job.parse_args()
154  self.assertEqual(job.log_level, 10)
155  self.assertEqual(job.rundir, 'some/run/dir')
156  self.assertEqual(job.job_steps, 1)
157  self.assertEqual(job.script, 'test_helpers/job_files/some_job_script.py')
158  self.assertEqual(job.param_file, os.path.abspath('test_helpers/job_files/job_params.json'))
159  self.assertEqual(job.job_id, 1)
160  self.assertEqual(job.params, {"input_files": {"input1.stdhep": "path/to/input1.stdhep", "input2.stdhep": "path/to/input2.stdhep"}, "output_files": {"output.slcio": "output_file.slcio"}, "output_dir": "some/output_dir", "job_id": 1})
161  self.assertEqual(job.output_dir, os.path.abspath('some/output_dir'))
162  self.assertEqual(job.input_files, {"input1.stdhep": "path/to/input1.stdhep", "input2.stdhep": "path/to/input2.stdhep"})
163  self.assertEqual(job.output_files, {"output.slcio": "output_file.slcio"})
164 
166  job = Job(args=[])
167  self.assertRaises(Exception, lambda: job.parse_args(), 'Missing required script name or location.')
Wrapper for accessing config information from parser.
Definition: job.py:29
Database of job scripts.
Definition: job.py:125
Simple JSON based store of job data.
Definition: job.py:73
Primary class to run HPS jobs from a Python script.
Definition: job.py:160
Run the hpstr analysis tool.
Definition: tools.py:344
Run the SLIC Geant4 simulation.
Definition: tools.py:15
def test_config_missing_section(self)
Definition: test_job.py:29
def test_config_not_allowed_config(self)
Definition: test_job.py:41
def test_config_missing_config(self)
Definition: test_job.py:36
def test_load_file_not_exist(self)
Definition: test_job.py:54
def test_get_job_ids(self)
Definition: test_job.py:71
def test_parse_args(self)
Definition: test_job.py:150
def test_set_params(self)
Definition: test_job.py:143
def test_parse_args_no_script(self)
Definition: test_job.py:165
def test_init(self)
Definition: test_job.py:108
def test_add(self)
Definition: test_job.py:136
Definition: job.py:1
Tools that can be used in HPSMC jobs.
Definition: tools.py:1