Skip to content

Commit 354dfe8

Browse files
authored
[sonic_installer]: Improve exception handling: introduce notes. (#3028)
What I did Extended exception class with user notes How I did it Implemented new API How to verify it Run UTs Tested branch (Please provide the tested image version) 202305 Details if related Backport from master: [sonic_installer]: Improve exception handling: introduce notes. #3029
1 parent fb98e41 commit 354dfe8

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

sonic_installer/common.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,15 @@ def run_command_or_raise(argv, raise_exception=True):
4545
click.echo(click.style("Command: ", fg='cyan') + click.style(' '.join(argv), fg='green'))
4646

4747
proc = subprocess.Popen(argv, text=True, stdout=subprocess.PIPE)
48-
out, _ = proc.communicate()
48+
out, err = proc.communicate()
4949

5050
if proc.returncode != 0 and raise_exception:
51-
raise SonicRuntimeException("Failed to run command '{0}'".format(argv))
51+
sre = SonicRuntimeException("Failed to run command '{0}'".format(argv))
52+
if out:
53+
sre.add_note("\nSTDOUT:\n{}".format(out.rstrip("\n")))
54+
if err:
55+
sre.add_note("\nSTDERR:\n{}".format(err.rstrip("\n")))
56+
raise sre
5257

5358
return out.rstrip("\n")
5459

sonic_installer/exception.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,15 @@
55
class SonicRuntimeException(Exception):
66
"""SONiC Runtime Excpetion class used to report SONiC related errors
77
"""
8-
pass
8+
def __init__(self, *args, **kwargs):
9+
super().__init__(*args, **kwargs)
10+
self.notes = []
11+
12+
def __str__(self):
13+
msg = super().__str__()
14+
if self.notes:
15+
msg += "\n" + "\n".join(self.notes)
16+
return msg
17+
18+
def add_note(self, note):
19+
self.notes.append(note)

tests/test_sonic_installer.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,18 @@ def test_set_fips(get_bootloader):
129129
mock_bootloader.get_fips = Mock(return_value=True)
130130
result = runner.invoke(sonic_installer.commands["get-fips"], [next_image])
131131
assert "FIPS is enabled" in result.output
132+
133+
@patch("sonic_installer.common.subprocess.Popen")
134+
def test_runtime_exception(mock_popen):
135+
""" This test covers the "sonic-installer" exception handling. """
136+
137+
mock_popen.return_value.returncode = 1
138+
mock_popen.return_value.communicate.return_value = ('Running', 'Failed')
139+
140+
with pytest.raises(sonic_installer_common.SonicRuntimeException) as sre:
141+
sonic_installer_common.run_command_or_raise(["test.sh"])
142+
143+
assert '\nSTDOUT:\nRunning' in sre.value.notes, "Invalid STDOUT"
144+
assert '\nSTDERR:\nFailed' in sre.value.notes, "Invalid STDERR"
145+
146+
assert all(v in str(sre.value) for v in ['test.sh', 'Running', 'Failed']), "Invalid message"

0 commit comments

Comments
 (0)