@@ -568,3 +568,59 @@ def test_list_images_failed(self, mock_check_output, MockInit, MockBusName, Mock
568568 stderr = subprocess .STDOUT ,
569569 )
570570
571+ @mock .patch ("dbus.SystemBus" )
572+ @mock .patch ("dbus.service.BusName" )
573+ @mock .patch ("dbus.service.Object.__init__" )
574+ @mock .patch ("subprocess.run" )
575+ def test_image_set_next_boot_success (self , mock_run , MockInit , MockBusName , MockSystemBus ):
576+ """
577+ Test that the `set_next_boot` method successfully sets the next boot image.
578+ """
579+ # Arrange
580+ image_service = ImageService (mod_name = "image_service" )
581+ image = "sonic_image"
582+ mock_result = mock .Mock ()
583+ mock_result .returncode = 0
584+ mock_result .stderr = b""
585+ mock_run .return_value = mock_result
586+
587+ # Act
588+ rc , msg = image_service .set_next_boot (image )
589+
590+ # Assert
591+ assert rc == 0 , "wrong return value"
592+ assert image in msg , "message should contain the name of the new image"
593+ mock_run .assert_called_once_with (
594+ ["/usr/local/bin/sonic-installer" , "set-next-boot" , image ],
595+ stdout = subprocess .PIPE ,
596+ stderr = subprocess .PIPE ,
597+ )
598+
599+ @mock .patch ("dbus.SystemBus" )
600+ @mock .patch ("dbus.service.BusName" )
601+ @mock .patch ("dbus.service.Object.__init__" )
602+ @mock .patch ("subprocess.run" )
603+ def test_image_set_next_boot_fail_not_exists (self , mock_run , MockInit , MockBusName , MockSystemBus ):
604+ """
605+ Test that the `set_next_boot` method fails when the image does not exist.
606+ """
607+ # Arrange
608+ image_service = ImageService (mod_name = "image_service" )
609+ image = "nonexistent_image"
610+ mock_result = mock .Mock ()
611+ mock_result .returncode = 1
612+ mock_result .stderr = b"Error: Image does not exist"
613+ mock_run .return_value = mock_result
614+
615+ # Act
616+ rc , msg = image_service .set_next_boot (image )
617+
618+ # Assert
619+ assert rc != 0 , "wrong return value"
620+ assert "Error: Image does not exist" in msg , "message should contain 'Error: Image does not exist'"
621+ mock_run .assert_called_once_with (
622+ ["/usr/local/bin/sonic-installer" , "set-next-boot" , image ],
623+ stdout = subprocess .PIPE ,
624+ stderr = subprocess .PIPE ,
625+ )
626+
0 commit comments