Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BoPeng committed Feb 16, 2024
1 parent dccf067 commit 5785258
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 136 deletions.
1 change: 1 addition & 0 deletions src/sos/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def _load_actions():


def sos_run_script(action, script, *args, **kwargs):
'''Call script-execution actions.'''
if not g_action_map:
_load_actions()
try:
Expand Down
13 changes: 2 additions & 11 deletions src/sos/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def _prepare_task(self, task_id):

# map variables
runtime["_runtime"]["workdir"] = (
task_vars["_runtime"]["workdir"] if "workdir" in task_vars["_runtime"] else path.cwd().to_named_path())
task_vars["_runtime"]["workdir"] if "workdir" in task_vars["_runtime"] else str(path.cwd()))

if runtime["_runtime"]["workdir"].startswith("#"):
try:
Expand Down Expand Up @@ -679,18 +679,9 @@ def receive_result(self, task_id: str) -> Dict[str, int]:
job_dict = params.sos_dict
if ("_output" in job_dict and job_dict["_output"] and not isinstance(job_dict["_output"], Undetermined) and
env.config["run_mode"] != "dryrun"):
received = self.receive_from_host([x for x in job_dict["_output"] if isinstance(x, (str, path))])
received = {x:x for x in job_dict["_output"] if isinstance(x, (str, path))}
if received:
env.logger.info(f"{task_id} ``received`` {short_repr(received.keys())} from {self.alias}")
if "from_host" in job_dict["_runtime"] and env.config["run_mode"] != "dryrun":
if isinstance(job_dict["_runtime"]["from_host"], (Sequence, str)):
received = self.receive_from_host(job_dict["_runtime"]["from_host"])
if received:
env.logger.info(f"{task_id} ``received`` {short_repr(received.keys())} from {self.alias}")
else:
env.logger.warning(
f"Expecting a string or list of string from from_host: {job_dict['_runtime']['from_host']} received"
)
# we need to translate result from remote path to local
if "output" in res:
if "_output" not in job_dict:
Expand Down
3 changes: 2 additions & 1 deletion src/sos/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,8 @@ def wrap_script(self) -> None:
self._script = repr(self._script)
self.statements[-1] = [
"!",
f'sos_run_script("{self._action}", {self._script}{(", " + opt) if opt else ""})\n',
#f'sos_run_script("{self._action}", {self._script}{(", " + opt) if opt else ""})\n',
f'{self._action}({self._script}{(", " + opt) if opt else ""})\n',
]
self.values = []
self._action = None
Expand Down
2 changes: 0 additions & 2 deletions src/sos/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
"env",
"prepend_path",
"queue",
"to_host",
"from_host",
"trunk_size",
"trunk_workers",
"tags",
Expand Down
4 changes: 2 additions & 2 deletions src/sos/templates/parts/hover_doc.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ let sos_keywords = new Set([
'sos_run', 'fail_if', 'done_if', 'warn_if', 'skip_if',
'get_output', 'expand_pattern',
// task options
'walltime', 'cores', 'mem', 'queue', 'to_host', 'nodes',
'from_host', 'trunk_size', 'trunk_workers', 'workdir',
'walltime', 'cores', 'mem', 'queue', 'nodes',
'trunk_size', 'trunk_workers', 'workdir',
'shared', 'env', 'prepend_path', 'tags',
// targets
'file_target', 'executable', 'sos_variable', 'env_variable',
Expand Down
5 changes: 2 additions & 3 deletions test/build_test_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ ADD authorized_keys /root/.ssh/authorized_keys
ARG SHA=LATEST
RUN SHA=$SHA git clone http://github.com/vatlab/sos sos
RUN cd sos && pip install . -U
RUN pip install sos-pbs
RUN pip install sos sos-pbs
RUN echo "export TS_SLOTS=10" >> /root/.bash_profile
Expand All @@ -62,7 +61,7 @@ docker build --build-arg SHA=$SHA -t eg_sshd .

#
# start docker image
docker run -d -P --env TS_SLOTS=10 --name test_sos eg_sshd
docker run -d -v `pwd`:`pwd` -P --env TS_SLOTS=10 --name test_sos eg_sshd

# get the port
PORT22=$(docker port test_sos 22 | cut -f2 -d:)
Expand Down
119 changes: 2 additions & 117 deletions test/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,63 +23,6 @@
has_docker = False



@pytest.mark.skipif(not has_docker, reason="Docker container not usable")
def test_to_host_option(clear_now_and_after):
"""Test from_remote option"""
clear_now_and_after("to_host_testfile.txt", "to_host_linecount.txt")
with open("to_host_testfile.txt", "w") as tf:
for i in range(100):
tf.write(f"line {i+1}\n")
execute_workflow(
"""
[10]
output: 'to_host_linecount.txt'
task: to_host='to_host_testfile.txt'
sh:
wc -l to_host_testfile.txt > to_host_linecount.txt
""",
options={
"config_file": "~/docker.yml",
"default_queue": "docker",
"sig_mode": "force",
},
)
assert os.path.isfile("to_host_linecount.txt")
with open("to_host_linecount.txt") as lc:
assert lc.read().strip().startswith("100")



@pytest.mark.skipif(not has_docker, reason="Docker container not usable")
def test_to_host_option_with_named_path(clear_now_and_after):
"""Test from_remote option"""
clear_now_and_after(
os.path.expanduser("~/to_host_named_testfile.txt"),
"to_host_named_linecount.txt",
)
with open(os.path.expanduser("~/to_host_named_testfile.txt"), "w") as tf:
for i in range(200):
tf.write(f"line {i+1}\n")
execute_workflow(
"""
[10]
output: 'to_host_named_linecount.txt'
task: to_host='#home/to_host_named_testfile.txt'
sh:
wc -l ~/to_host_named_testfile.txt > to_host_named_linecount.txt
""",
options={
"config_file": "~/docker.yml",
"default_queue": "docker",
"sig_mode": "force",
},
)
assert os.path.isfile("to_host_named_linecount.txt")
with open("to_host_named_linecount.txt") as lc:
assert lc.read().strip().startswith("200")


def test_worker_procs():
# test -j option
execute_workflow(
Expand Down Expand Up @@ -117,7 +60,6 @@ def test_worker_procs_with_task():
)



@pytest.mark.skipif(not has_docker, reason="Docker container not usable")
def test_remote_execute(clear_now_and_after, script_factory):
clear_now_and_after("result_remote.txt", "result_remote1.txt", "local.txt")
Expand Down Expand Up @@ -180,11 +122,9 @@ def test_remote_workflow_remote_queue(script_factory):
assert 0 == subprocess.call(f"sos run {test_r_q} -c ~/docker.yml -r ts -q ts", shell=True)



@pytest.mark.skipif(not has_docker, reason="Docker container not usable")
def test_remote_exec(clear_now_and_after):
clear_now_and_after("result_exec.txt")
root_dir = "/root/build" if "TRAVIS" in os.environ else "/root"
execute_workflow(
"""
output: 'result_exec.txt'
Expand All @@ -204,66 +144,11 @@ def test_remote_exec(clear_now_and_after):
with open(file_target("result_exec.txt")) as res:
result = res.read()
assert "Output: result_exec.txt" in result
assert f"PWD: {root_dir}" in result



@pytest.mark.skipif(not has_docker, reason="Docker container not usable")
def test_remote_exec_named_path(clear_now_and_after):
clear_now_and_after("result_named_path.txt")
root_dir = "/root/build" if "TRAVIS" in os.environ else "/root"

execute_workflow(
"""
output: '#home/result_named_path.txt'
task:
sh: expand=True
echo Output: {_output} > {_output}
echo PWD: `pwd`. >> {_output}
""",
options={
"config_file": "~/docker.yml",
"default_queue": "docker",
"sig_mode": "force",
},
)
assert file_target("#home/result_named_path.txt").target_exists()
with open(file_target("#home/result_named_path.txt")) as res:
result = res.read()
print(result)
assert "Output: /root/result_named_path.txt" in result
assert f"PWD: {root_dir}" in result



@pytest.mark.skipif(not has_docker, reason="Docker container not usable")
def test_remote_exec_workdir_named_path(clear_now_and_after):
clear_now_and_after(file_target("#home/wd/result_workdir_named_path.txt"))
execute_workflow(
"""
output: '#home/wd/result_workdir_named_path.txt'
task: workdir='/root'
sh: expand=True
echo Output: {_output} > {_output}
echo PWD: `pwd`. >> {_output}
""",
options={
"config_file": "~/docker.yml",
"default_queue": "docker",
"sig_mode": "force",
},
)
assert file_target("#home/wd/result_workdir_named_path.txt").target_exists()
with open(file_target("#home/wd/result_workdir_named_path.txt")) as res:
result = res.read()
assert "Output: /root/wd/result_workdir_named_path.txt" in result
assert "PWD: /root." in result
assert f"PWD: {os.getcwd()}" in result


@pytest.mark.skipif(not has_docker, reason="Docker container not usable")
def test_remote_exec_workdir_wo_named_path(clear_now_and_after):
def test_remote_exec_workdir(clear_now_and_after):
clear_now_and_after(file_target("result_workdir_wo_named.txt"))
with pytest.raises(Exception):
execute_workflow(
Expand Down

0 comments on commit 5785258

Please sign in to comment.