Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ImageService.set_next_boot for GNOI Activate OS. #207

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions host_modules/image_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,29 @@ def list_images(self):
logger.error(msg)
return e.returncode, msg

@host_service.method(
host_service.bus_name(MOD_NAME), in_signature="s", out_signature="is"
)
def set_next_boot(self, image):
"""
Set the image to be used for the next boot.

Args:
image: The name of the image to set for the next boot.
"""
logger.info("Setting the next boot image to {}".format(image))
cmd = ["/usr/local/bin/sonic-installer", "set-next-boot", image]
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
msg = ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no successful message?

if result.returncode:
lines = result.stderr.decode().split("\n")
for line in lines:
if "Error" in line:
msg = line
break
return result.returncode, msg


def _parse_sonic_installer_list(self, output):
"""
Parse the output of the sonic-installer list command.
Expand Down
56 changes: 56 additions & 0 deletions tests/host_modules/image_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,3 +568,59 @@ def test_list_images_failed(self, mock_check_output, MockInit, MockBusName, Mock
stderr=subprocess.STDOUT,
)

@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_success(self, mock_run, MockInit, MockBusName, MockSystemBus):
"""
Test that the `set_next_boot` method successfully sets the next boot image.
"""
# Arrange
image_service = ImageService(mod_name="image_service")
image = "sonic_image"
mock_result = mock.Mock()
mock_result.returncode = 0
mock_result.stderr = b""
mock_run.return_value = mock_result

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

# Assert
assert rc == 0, "wrong return value"
assert msg == "", "message should be empty on success"
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(self, mock_run, MockInit, MockBusName, MockSystemBus):
"""
Test that the `set_next_boot` method fails when the image does not exist.
"""
# Arrange
image_service = ImageService(mod_name="image_service")
image = "nonexistent_image"
mock_result = mock.Mock()
mock_result.returncode = 1
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 != 0, "wrong return value"
assert "Error: Image does not exist" in msg, "message should contain 'Error: Image does not exist'"
mock_run.assert_called_once_with(
["/usr/local/bin/sonic-installer", "set-next-boot", image],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)

Loading