Skip to content

Commit

Permalink
fix: add handling for 0 or negative job name calculations in job hist…
Browse files Browse the repository at this point in the history
…ory dir

Signed-off-by: Joel Wong <[email protected]>
  • Loading branch information
joel-wong-aws committed Jan 27, 2025
1 parent 4e1507b commit 62d42bf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/deadline/client/job_bundle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def create_job_history_bundle_dir(submitter_name: str, job_name: str) -> str:
max_job_name_prefix = min(
207 - len(os.path.abspath(os.path.join(month_dir, job_dir_prefix))), max_job_name_prefix
)
if max_job_name_prefix < 1:
raise RuntimeError(
"Job history directory is too long. Please update your 'settings.job_history_dir' to a shorter path."
)

# Clean the job_name's characters and truncate for the filename
job_name_cleaned = "".join(char for char in job_name if char.isalnum() or char in " -_")
Expand Down
33 changes: 32 additions & 1 deletion test/unit/deadline_client/job_bundle/test_job_history_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def test_job_history_dir_truncation_from_job_name_with_129_chars(fresh_deadline_
# Use a temporary directory for the job history
if sys.platform in ["win32", "cygwin"]:
# Windows has a long tmp dir path so it will truncate anyway - we only want to test job name truncation
tmp_parent_dir = "C:\ProgramData"
tmp_parent_dir = "C:\\ProgramData"
else:
tmp_parent_dir = None
with tempfile.TemporaryDirectory(dir=tmp_parent_dir) as tmpdir, freeze_time(
Expand All @@ -126,6 +126,37 @@ def test_job_history_dir_truncation_from_job_name_with_129_chars(fresh_deadline_
assert os.path.isdir(output_path)


def test_job_history_dir_exceeds_256_characters(fresh_deadline_config):
# Use a temporary directory for the job history
with tempfile.TemporaryDirectory() as tmpdir, freeze_time("2023-11-12T13:14:15"):
tmpdir_path_length = len(os.path.abspath(tmpdir))
long_tmpdir = os.path.join(
tmpdir,
"dir" + "1" * (256 - tmpdir_path_length),
)
config.set_setting("settings.job_history_dir", long_tmpdir)

job_name_1_char = "a"

if sys.platform in ["win32", "cygwin"]:
with pytest.raises(RuntimeError, match="Job history directory is too long"):
job_bundle.create_job_history_bundle_dir(
"SubmitterFive",
job_name_1_char,
)
else:
output_path = job_bundle.create_job_history_bundle_dir(
"SubmitterFive",
job_name_1_char,
)
assert output_path == os.path.join(
long_tmpdir,
"2023-11",
"2023-11-12-01-SubmitterFive-a",
)
assert os.path.isdir(output_path)


@pytest.mark.parametrize(
"submitter_name, job_name, freeze_date, expected_output_path",
[
Expand Down

0 comments on commit 62d42bf

Please sign in to comment.