Skip to content

Commit

Permalink
Add additional tests and fix return code when no exception
Browse files Browse the repository at this point in the history
  • Loading branch information
adammcdonagh authored Oct 31, 2023
1 parent abf8f1e commit d49e170
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/opentaskpy/cli/task_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,16 @@ def main() -> None:
task_run_obj = taskrun.TaskRun(args.taskId, CONFIG_PATH, noop=args.noop)

try:
task_run_obj.run()
result = task_run_obj.run()
except Exception as ex: # pylint: disable=broad-exception-caught
logger.error(f"Error running task: {ex}")
if logger.getEffectiveLevel() <= 12:
raise ex
sys.exit(1)

if not result:
sys.exit(1)


if __name__ == "__main__":
main()
10 changes: 10 additions & 0 deletions test/cfg/batch/batch-basic-invalid-execution-host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type": "batch",
"tasks": [
{
"order_id": 1,
"task_id": "df-invalid-host",
"timeout": 60
}
]
}
12 changes: 12 additions & 0 deletions test/cfg/executions/df-invalid-host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "execution",
"hosts": ["123"],
"directory": "/tmp",
"command": "df -h",
"protocol": {
"name": "ssh",
"credentials": {
"username": "{{ SSH_USERNAME }}"
}
}
}
12 changes: 12 additions & 0 deletions tests/test_task_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ def test_execution_basic_binary(env_vars, setup_ssh_keys, root_dir):
assert run_task_run("df")["returncode"] == 0


def test_execution_invalid_host(env_vars, setup_ssh_keys, root_dir):
# Use the "binary" to trigger the job with command line arguments

assert run_task_run("df-invalid-host")["returncode"] == 1


def test_batch_basic_binary(env_vars, setup_ssh_keys, root_dir):
# Use the "binary" to trigger the job with command line arguments

Expand All @@ -103,6 +109,12 @@ def test_batch_basic_binary(env_vars, setup_ssh_keys, root_dir):
assert run_task_run("batch-basic")["returncode"] == 0


def test_batch_execution_invalid_host(env_vars, setup_ssh_keys, root_dir):
# Use the "binary" to trigger the job with command line arguments

assert run_task_run("batch-basic-invalid-execution-host")["returncode"] == 1


def test_binary_invalid_config_file(env_vars, setup_ssh_keys, root_dir):
# Use the "binary" to trigger the job with command line arguments

Expand Down
31 changes: 31 additions & 0 deletions tests/test_taskhandler_transfer_sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@
],
}

# Non existent source directory
sftp_task_non_existent_source_dir_definition = {
"type": "transfer",
"source": {
"hostname": "172.16.0.21",
"directory": "/home/application/testFiles/src/nonexistentdir",
"fileRegex": ".*taskhandler.*\\.txt",
"protocol": {"name": "sftp", "credentials": {"username": "application"}},
},
"destination": [
{
"hostname": "172.16.0.22",
"directory": "/home/application/testFiles/dest",
"protocol": {"name": "sftp", "credentials": {"username": "application"}},
},
],
}


sftp_task_definition_no_permissions = {
"type": "transfer",
Expand Down Expand Up @@ -335,6 +353,19 @@ def test_sftp_basic(root_dir, setup_sftp_keys):
)


def test_sftp_basic_non_existent_source_directory(root_dir, setup_sftp_keys):
# Create a transfer object
transfer_obj = transfer.Transfer(
None,
"sftp-basic-non-existent-directory",
sftp_task_non_existent_source_dir_definition,
)

# Run the transfer and expect a true status
with pytest.raises(exceptions.FilesDoNotMeetConditionsError):
transfer_obj.run()


def test_sftp_basic_no_permissions(root_dir, setup_sftp_keys):
# Create a test file
fs.create_files(
Expand Down

0 comments on commit d49e170

Please sign in to comment.