@@ -224,3 +224,59 @@ def test_command_exists_su_config_only(self):
224224 host = state .inventory .get_host ("somehost" )
225225 command = make_unix_command_for_host (state , host , "echo Šablony" )
226226 assert command .get_raw_value () == "sh -c 'echo Šablony'"
227+
228+
229+ class TestRemoveAnySudoAskpassFile (TestCase ):
230+ def test_clears_state_and_runs_rm (self ):
231+ commands = []
232+ host = MagicMock ()
233+ host .connector_data = {
234+ "sudo_askpass_path" : "/tmp/sudo-askpass" ,
235+ "su_askpass_path" : "/tmp/su-askpass" ,
236+ }
237+ host .run_shell_command = lambda cmd : commands .append (cmd .get_raw_value ())
238+
239+ remove_any_sudo_askpass_file (host )
240+
241+ assert commands == [
242+ "rm -f /tmp/sudo-askpass" ,
243+ "rm -f /tmp/su-askpass" ,
244+ ]
245+ assert host .connector_data ["sudo_askpass_path" ] is None
246+ assert host .connector_data ["su_askpass_path" ] is None
247+
248+ def test_swallows_errors_and_clears_state (self ):
249+ """
250+ Regression test for #1645: `host.disconnect()` → `remove_any_sudo_askpass_file`
251+ is called after ``server.reboot`` when the SSH session is already dead.
252+ The remote ``rm`` must fail gracefully and the stored path must still
253+ be cleared so a reconnect will regenerate a fresh askpass file.
254+ """
255+ host = MagicMock ()
256+ host .connector_data = {
257+ "sudo_askpass_path" : "/tmp/sudo-askpass" ,
258+ "su_askpass_path" : "/tmp/su-askpass" ,
259+ }
260+
261+ def failing_run (cmd ):
262+ raise Exception ("SSH session not active" )
263+
264+ host .run_shell_command = failing_run
265+
266+ # Must not raise — this is a best-effort cleanup.
267+ remove_any_sudo_askpass_file (host )
268+
269+ assert host .connector_data ["sudo_askpass_path" ] is None
270+ assert host .connector_data ["su_askpass_path" ] is None
271+
272+ def test_noop_when_no_state (self ):
273+ host = MagicMock ()
274+ host .connector_data = {
275+ "sudo_askpass_path" : None ,
276+ "su_askpass_path" : None ,
277+ }
278+ host .run_shell_command = MagicMock ()
279+
280+ remove_any_sudo_askpass_file (host )
281+
282+ host .run_shell_command .assert_not_called ()
0 commit comments