Skip to content

Commit

Permalink
check sonic-installer message and return proper error code for when i…
Browse files Browse the repository at this point in the history
…mage doesn't exist. (#210)
  • Loading branch information
hdwhdw authored Feb 11, 2025
1 parent 1fe9a76 commit 47f6feb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
3 changes: 3 additions & 0 deletions host_modules/image_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ def set_next_boot(self, image):
if result.returncode:
logger.error("Failed to set next boot image: {}".format(result.stderr.decode()))
msg = result.stderr.decode()
# sonic-installer might not return a proper error code, so we need to check the message.
if "not" in msg.lower() and ("exist" in msg.lower() or "found" in msg.lower()):
return errno.ENOENT, msg
return result.returncode, msg


Expand Down
38 changes: 36 additions & 2 deletions tests/host_modules/image_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import stat
import pytest
import json
import errno
from unittest import mock
from host_modules.image_service import ImageService

Expand Down Expand Up @@ -608,7 +609,7 @@ def test_image_set_next_boot_fail_not_exists(self, mock_run, MockInit, MockBusNa
image_service = ImageService(mod_name="image_service")
image = "nonexistent_image"
mock_result = mock.Mock()
mock_result.returncode = 1
mock_result.returncode = errno.ENOENT
mock_result.stderr = b"Error: Image does not exist"
mock_run.return_value = mock_result

Expand All @@ -617,7 +618,40 @@ def test_image_set_next_boot_fail_not_exists(self, mock_run, MockInit, MockBusNa

# Assert
assert rc != 0, "wrong return value"
assert "Error: Image does not exist" in msg, "message should contain 'Error: Image does not exist'"
assert (
"not" in msg.lower() and ("exist" in msg.lower() or "found" in msg.lower())
), "message should contain 'not' and 'exist' or 'found'"
mock_run.assert_called_once_with(
["/usr/local/bin/sonic-installer", "set-next-boot", image],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)

@mock.patch("dbus.SystemBus")
@mock.patch("dbus.service.BusName")
@mock.patch("dbus.service.Object.__init__")
@mock.patch("subprocess.run")
def test_image_set_next_boot_fail_not_exists_generic_rc(self, mock_run, MockInit, MockBusName, MockSystemBus):
"""
Test that the `set_next_boot` method fails when the image does not exist, and sonic-installer returns a generic error code
instead of errno.ENOENT.
"""
# Arrange
image_service = ImageService(mod_name="image_service")
image = "nonexistent_image"
mock_result = mock.Mock()
mock_result.returncode = 1 # returns generic error code
mock_result.stderr = b"Error: Image does not exist"
mock_run.return_value = mock_result

# Act
rc, msg = image_service.set_next_boot(image)

# Assert
assert rc == errno.ENOENT, "wrong return value"
assert (
"not" in msg.lower() and ("exist" in msg.lower() or "found" in msg.lower())
), "message should contain 'not' and 'exist' or 'found'"
mock_run.assert_called_once_with(
["/usr/local/bin/sonic-installer", "set-next-boot", image],
stdout=subprocess.PIPE,
Expand Down

0 comments on commit 47f6feb

Please sign in to comment.