Skip to content

Commit

Permalink
Added run testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lamsoa729 committed Jan 20, 2024
1 parent c71fae4 commit 76edc1b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 34 deletions.
50 changes: 28 additions & 22 deletions chi_pet/chi_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,57 @@
'''


def run_args(workdir, state, args):
current_dir = Path.cwd()
try:
action = state + '-ing'
action_file = Path('.' + action)
print("Started {} sim using args {}".format(action, args))
sys.stdout.flush()
if workdir.exists():
os.chdir(workdir)
action_file.touch() # Keeps track of current pipeline state
status = run(args)
if status.returncode:
print(f"Run failed: State {state} did not succeed.")
action_file.unlink() # Remove once completed successfully
return status.returncode
else:
print(
f"Run failed: could not find work directory {workdir} to do action {state}.")
return 1
except:
print(f"Run failed: State {state} did not succeed.")
return 1
finally:
os.chdir(current_dir)


class ChiRun(object):
def __init__(self, opts):
self._opts = opts
self._arg_dict = opts.args_dict
self._args_dict = opts.args_dict
self._states = opts.states

def run(self):
"""Run simulation pipeline defined in the the args.yaml file defined as self._opts.args_file.
"""

# TODO Add option to see if sim_action file exists and only run those states.
for run_state, vals in self._args_dict.items():
args = [str(a) for a in vals]
sim_state = Path('sim.{}'.format(run_state))

# If sim action file exists then run the state
if run_state in self._states:
if self.run_args(self._workdir, run_state, args):
if run_args(self._opts.workdir, run_state, args):
(self._opts.workdir / '.error').touch()
elif sim_state.exists():
sim_state.unlink()

def get_run_states(self):
return self._states

@classmethod
def run_args(workdir, state, args):
action = state + '-ing'
action_file = Path('.' + action)
print("Started {} sim using args {}".format(action, args))
sys.stdout.flush()
if workdir.exists():
os.chdir(workdir)
action_file.touch() # Keeps track of current pipeline state
status = run(args)
if status.returncode:
print(f"Run failed: State {state} did not succeed.")
action_file.unlink() # Remove once completed successfully
return status.returncode
else:
print(
f"Run failed: could not find work directory {workdir} to do action {state}.")
return 1


if __name__ == '__main__':
opts = parse_chi_options()
Expand Down
12 changes: 3 additions & 9 deletions tests/mhelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
MOCK_ARGS_FILE_DICT = {'stage1': ['arg1', 'arg2', 'arg3'],
'stage2': ['arg4', 'arg5', 'arg6']}

MOCK_SHELL_ARGS_FILE_DICT = {'touch': ['touch', 'mock_command.txt']}
MOCK_SHELL_ARGS_FILE_DICT = {'touch1': ['touch', 'mock_output1.txt'],
'touch2': ['touch', 'mock_output2.txt'], }

MOCK_PARAM_DICT_PATH = 'mock_param.yaml'
MOCK_CHI_PARAM_DICT_PATH = 'mock_param_chi_param.yaml'
Expand All @@ -33,7 +34,7 @@ def clean_mocks():
"""Clean up mock directories and files before testing to make sure we are
working from a clean slate.
"""
for mpath in Path('.').glob('tests/mock*'):
for mpath in Path.cwd().glob('tests/mock*'):
if mpath.is_file():
mpath.unlink()
elif mpath.is_dir():
Expand Down Expand Up @@ -90,11 +91,6 @@ def mock_leaf_dir():
with yaml_param_path.open('w') as ypp:
yaml.dump(MOCK_PARAM_DICT, ypp)

args_file_path = chi_leaf_path / 'mock_args.yaml'
with args_file_path.open('w') as aff:
yaml.dump(MOCK_ARGS_FILE_DICT, aff)
yield args_file_path

yield chi_leaf_path


Expand Down Expand Up @@ -134,8 +130,6 @@ def mock_run_opts():
def opts(x): return None
opts.command = 'run'
opts.use_sim_states = 'False'
opts.replace = 'False'
opts.non_yaml = []
opts.args_dict = MOCK_ARGS_FILE_DICT
opts.states = list(opts.args_dict.keys())
yield opts
31 changes: 28 additions & 3 deletions tests/test_chi_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,39 @@ def test_chi_run_states(mock_run_opts):
assert crun.get_run_states() == mock_run_opts.states


def test_chi_run_with_touch(mock_run_opts):
def test_chi_run_touch(mock_leaf_dir, mock_run_opts):
"""Test chi_run."""
mock_leaf_path = mock_leaf_dir
# Create and modify run optoins
mock_run_opts.args_dict = MOCK_SHELL_ARGS_FILE_DICT
mock_run_opts.states = mock_run_opts.args_dict.keys()
mock_run_opts.workdir = mock_leaf_path

# Make a ChiRun object
crun = ChiRun(mock_run_opts)
# Run the pipeline
crun.run()

# Make sure action is carried out
assert (mock_leaf_path / 'mock_output1.txt').exists()
assert (mock_leaf_path / 'mock_output2.txt').exists()

assert crun.get_run_states() == mock_run_opts.states

def test_chi_run_only_one_touch(mock_leaf_dir, mock_run_opts):
"""Test chi_run."""
mock_leaf_path = mock_leaf_dir
# Create and modify run optoins
mock_run_opts.args_dict = MOCK_SHELL_ARGS_FILE_DICT
mock_run_opts.states = ['touch1']
mock_run_opts.workdir = mock_leaf_path

# Make a ChiRun object
crun = ChiRun(mock_run_opts)
# Run the pipeline
crun.run()

# Make sure action is carried out
assert (mock_leaf_path / 'mock_output1.txt').exists()
assert not (mock_leaf_path / 'mock_output2.txt').exists()

# TODO NEXT TEST add tests for chi_run.py
# TODO NEXT TEST test run args to make sure it always returns you to the right directory

0 comments on commit 76edc1b

Please sign in to comment.