55import stat
66import pytest
77import json
8+ import errno
89from unittest import mock
910from host_modules .image_service import ImageService
1011
@@ -608,7 +609,7 @@ def test_image_set_next_boot_fail_not_exists(self, mock_run, MockInit, MockBusNa
608609 image_service = ImageService (mod_name = "image_service" )
609610 image = "nonexistent_image"
610611 mock_result = mock .Mock ()
611- mock_result .returncode = 1
612+ mock_result .returncode = errno . ENOENT
612613 mock_result .stderr = b"Error: Image does not exist"
613614 mock_run .return_value = mock_result
614615
@@ -617,7 +618,40 @@ def test_image_set_next_boot_fail_not_exists(self, mock_run, MockInit, MockBusNa
617618
618619 # Assert
619620 assert rc != 0 , "wrong return value"
620- assert "Error: Image does not exist" in msg , "message should contain 'Error: Image does not exist'"
621+ assert (
622+ "not" in msg .lower () and ("exist" in msg .lower () or "found" in msg .lower ())
623+ ), "message should contain 'not' and 'exist' or 'found'"
624+ mock_run .assert_called_once_with (
625+ ["/usr/local/bin/sonic-installer" , "set-next-boot" , image ],
626+ stdout = subprocess .PIPE ,
627+ stderr = subprocess .PIPE ,
628+ )
629+
630+ @mock .patch ("dbus.SystemBus" )
631+ @mock .patch ("dbus.service.BusName" )
632+ @mock .patch ("dbus.service.Object.__init__" )
633+ @mock .patch ("subprocess.run" )
634+ def test_image_set_next_boot_fail_not_exists_generic_rc (self , mock_run , MockInit , MockBusName , MockSystemBus ):
635+ """
636+ Test that the `set_next_boot` method fails when the image does not exist, and sonic-installer returns a generic error code
637+ instead of errno.ENOENT.
638+ """
639+ # Arrange
640+ image_service = ImageService (mod_name = "image_service" )
641+ image = "nonexistent_image"
642+ mock_result = mock .Mock ()
643+ mock_result .returncode = 1 # returns generic error code
644+ mock_result .stderr = b"Error: Image does not exist"
645+ mock_run .return_value = mock_result
646+
647+ # Act
648+ rc , msg = image_service .set_next_boot (image )
649+
650+ # Assert
651+ assert rc == errno .ENOENT , "wrong return value"
652+ assert (
653+ "not" in msg .lower () and ("exist" in msg .lower () or "found" in msg .lower ())
654+ ), "message should contain 'not' and 'exist' or 'found'"
621655 mock_run .assert_called_once_with (
622656 ["/usr/local/bin/sonic-installer" , "set-next-boot" , image ],
623657 stdout = subprocess .PIPE ,
0 commit comments