Skip to content

Commit 4aa9b10

Browse files
authored
[SlurmPlugin] Log exceptions raised when reading json (#427)
While reading json files such as `run_instances_overrides.json`, the read operation may fail due to use of non-standard characters. This is difficult to debug since at the moment we use a default value when there is an error and furthermore the error is not logged. This commit logs the exception in situations where we fallback to a default value. Signed-off-by: Eddy Mwiti <[email protected]>
1 parent 6aebaa8 commit 4aa9b10

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

src/slurm_plugin/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def read_json(file_path, default=None):
7373
)
7474
raise
7575
else:
76-
logger.info("Unable to read file '%s'. Using default: %s", file_path, default)
76+
logger.info("Unable to read file '%s' due to an exception: %s. Using default: %s", file_path, e, default)
7777
return default
7878

7979

tests/slurm_plugin/test_common.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import pytest
1616
from assertpy import assert_that
1717
from common.utils import time_is_up
18-
from slurm_plugin.common import TIMESTAMP_FORMAT, get_clustermgtd_heartbeat
18+
from slurm_plugin.common import TIMESTAMP_FORMAT, get_clustermgtd_heartbeat, read_json
1919

2020

2121
@pytest.mark.parametrize(
@@ -76,3 +76,17 @@ def test_get_clustermgtd_heartbeat(time, expected_parsed_time, mocker):
7676
return_value=f"some_random_stdout\n{time.strftime(TIMESTAMP_FORMAT)}",
7777
)
7878
assert_that(get_clustermgtd_heartbeat("some file path")).is_equal_to(expected_parsed_time)
79+
80+
81+
@pytest.mark.parametrize(
82+
"json_file, log_assertion",
83+
[
84+
("test_read_json/faulty.json", lambda logs: "Failed with exception:" in logs),
85+
("test_read_json/standard.json", lambda logs: "Failed with exception:" not in logs),
86+
],
87+
)
88+
def test_read_json(test_datadir, caplog, json_file, log_assertion):
89+
json_file_path = str(test_datadir.joinpath(json_file))
90+
with pytest.raises(Exception):
91+
read_json(json_file_path)
92+
assert log_assertion
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
    "test_property_1": {
3+
                    "test_property_2": "test_value"
4+
                }
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"test_property_1": {
3+
"test_property_2": "test_value"
4+
}
5+
}

0 commit comments

Comments
 (0)