@@ -834,3 +834,72 @@ def test_ssh_single__cmd_str_sudo_passwd_user(opts):
834834 )
835835
836836 assert expected in cmd
837+
838+
839+ def test_run_ssh_pre_hook_success (opts , target , tmp_path ):
840+ """
841+ Test run_ssh_pre_hook when ssh_pre_hook is successful.
842+ """
843+ target ["ssh_pre_hook" ] = "echo 'Pre-hook success'"
844+ single_instance = ssh .Single (opts , opts ["argv" ], "localhost" , ** target )
845+ mock_exec_cmd = MagicMock (return_value = ("Output" , "No errors" , 0 ))
846+ with patch .object (single_instance .shell , "exec_cmd" , mock_exec_cmd ):
847+ result = single_instance .run_ssh_pre_hook ()
848+ assert result == ("Output" , "No errors" , 0 )
849+
850+
851+ def test_run_ssh_pre_hook_failure (opts , target ):
852+ """
853+ Test run_ssh_pre_hook when ssh_pre_hook fails.
854+ """
855+ target ["ssh_pre_hook" ] = "echo 'Pre-hook failure'"
856+ single_instance = ssh .Single (opts , opts ["argv" ], "localhost" , ** target )
857+ mock_exec_cmd = MagicMock (return_value = ("Error output" , "Failed to execute" , 1 ))
858+ with patch .object (single_instance .shell , "exec_cmd" , mock_exec_cmd ):
859+ result = single_instance .run_ssh_pre_hook ()
860+ assert result == ("Error output" , "Failed to execute" , 1 )
861+
862+
863+ def test_run_integration_with_pre_hook_success (opts , target ):
864+ """
865+ Test the run method integrates run_ssh_pre_hook and proceeds on success.
866+ """
867+ target ["ssh_pre_hook" ] = "echo 'Pre-hook success'"
868+ target ["ssh_pre_flight" ] = None
869+ single_instance = ssh .Single (opts , opts ["argv" ], "localhost" , ** target )
870+ mock_pre_hook = MagicMock (return_value = ("" , "" , 0 ))
871+ mock_cmd_block = MagicMock (return_value = ("" , "" , 0 ))
872+ with patch .object (single_instance , "run_ssh_pre_hook" , mock_pre_hook ), patch .object (
873+ single_instance , "cmd_block" , mock_cmd_block
874+ ):
875+ stdout , stderr , retcode = single_instance .run ()
876+ assert retcode == 0
877+ mock_pre_hook .assert_called_once ()
878+
879+
880+ def test_run_integration_with_pre_hook_failure (opts , target ):
881+ """
882+ Test the run method handles pre_hook failure correctly and skips further steps.
883+ """
884+ target ["ssh_pre_hook" ] = "echo 'Pre-hook failure'"
885+ target ["ssh_pre_flight" ] = None
886+ single_instance = ssh .Single (opts , opts ["argv" ], "localhost" , ** target )
887+ mock_pre_hook = MagicMock (return_value = ("Error output" , "Failed to execute" , 1 ))
888+ with patch .object (single_instance , "run_ssh_pre_hook" , mock_pre_hook ):
889+ stdout , stderr , retcode = single_instance .run ()
890+ assert retcode == 1
891+ assert "Failed to execute" in stderr
892+ mock_pre_hook .assert_called_once ()
893+
894+
895+ def test_run_integration_with_no_pre_hook (opts , target ):
896+ """
897+ Test the run method succeeds with no ssh_pre_hook
898+ """
899+ target ["ssh_pre_hook" ] = None
900+ target ["ssh_pre_flight" ] = None
901+ single_instance = ssh .Single (opts , opts ["argv" ], "localhost" , ** target )
902+ mock_cmd_block = MagicMock (return_value = ("" , "" , 0 ))
903+ with patch .object (single_instance , "cmd_block" , mock_cmd_block ):
904+ stdout , stderr , retcode = single_instance .run ()
905+ assert retcode == 0
0 commit comments