@@ -53,7 +53,7 @@ def __iter__(self):
5353username = 'user'
5454password = 'pass'
5555command = 'ls ~\n line 2\n line 3\n line с кирилицей'
56- command_log = u"Executing command:\n {!s }\n " .format (command .rstrip ())
56+ command_log = u"Executing command:\n {!r }\n " .format (command .rstrip ())
5757stdout_list = [b' \n ' , b'2\n ' , b'3\n ' , b' \n ' ]
5858stdout_str = b'' .join (stdout_list ).strip ().decode ('utf-8' )
5959stderr_list = [b' \n ' , b'0\n ' , b'1\n ' , b' \n ' ]
@@ -64,7 +64,7 @@ def __iter__(self):
6464print_stdin = 'read line; echo "$line"'
6565
6666
67- @mock .patch ('exec_helpers._ssh_client_base.logger ' , autospec = True )
67+ @mock .patch ('logging.getLogger ' , autospec = True )
6868@mock .patch ('paramiko.AutoAddPolicy' , autospec = True , return_value = 'AutoAddPolicy' )
6969@mock .patch ('paramiko.SSHClient' , autospec = True )
7070class TestExecute (unittest .TestCase ):
@@ -89,8 +89,7 @@ def get_ssh():
8989
9090 @staticmethod
9191 def gen_cmd_result_log_message (result ):
92- return (u"Command exit code '{code!s}':\n {cmd!s}\n "
93- .format (cmd = result .cmd .rstrip (), code = result .exit_code ))
92+ return u"Command {result.cmd!r} exit code: {result.exit_code!s}" .format (result = result )
9493
9594 def test_001_execute_async (self , client , policy , logger ):
9695 chan = mock .Mock ()
@@ -116,7 +115,8 @@ def test_001_execute_async(self, client, policy, logger):
116115 mock .call .makefile_stderr ('rb' ),
117116 mock .call .exec_command ('{}\n ' .format (command ))
118117 ))
119- log = logger .getChild ('{host}:{port}' .format (host = host , port = port ))
118+ # raise ValueError(logger.mock_calls)
119+ log = logger (ssh .__class__ .__name__ ).getChild ('{host}:{port}' .format (host = host , port = port ))
120120 self .assertIn (
121121 mock .call .log (level = logging .DEBUG , msg = command_log ),
122122 log .mock_calls
@@ -151,7 +151,7 @@ def test_002_execute_async_pty(self, client, policy, logger):
151151 mock .call .makefile_stderr ('rb' ),
152152 mock .call .exec_command ('{}\n ' .format (command ))
153153 ))
154- log = logger .getChild ('{host}:{port}' .format (host = host , port = port ))
154+ log = logger ( ssh . __class__ . __name__ ) .getChild ('{host}:{port}' .format (host = host , port = port ))
155155 self .assertIn (
156156 mock .call .log (level = logging .DEBUG , msg = command_log ),
157157 log .mock_calls
@@ -235,7 +235,7 @@ def test_004_execute_async_sudo(self, client, policy, logger):
235235 "sudo -S bash -c '"
236236 "eval \" $(base64 -d <(echo \" {0}\" ))\" '" .format (encoded_cmd ))
237237 ))
238- log = logger .getChild ('{host}:{port}' .format (host = host , port = port ))
238+ log = logger ( ssh . __class__ . __name__ ) .getChild ('{host}:{port}' .format (host = host , port = port ))
239239 self .assertIn (
240240 mock .call .log (level = logging .DEBUG , msg = command_log ),
241241 log .mock_calls
@@ -271,7 +271,7 @@ def test_005_execute_async_with_sudo_enforce(self, client, policy, logger):
271271 "sudo -S bash -c '"
272272 "eval \" $(base64 -d <(echo \" {0}\" ))\" '" .format (encoded_cmd ))
273273 ))
274- log = logger .getChild ('{host}:{port}' .format (host = host , port = port ))
274+ log = logger ( ssh . __class__ . __name__ ) .getChild ('{host}:{port}' .format (host = host , port = port ))
275275 self .assertIn (
276276 mock .call .log (level = logging .DEBUG , msg = command_log ),
277277 log .mock_calls
@@ -303,7 +303,7 @@ def test_006_execute_async_with_no_sudo_enforce(self, client, policy, logger):
303303 mock .call .makefile_stderr ('rb' ),
304304 mock .call .exec_command ('{}\n ' .format (command ))
305305 ))
306- log = logger .getChild ('{host}:{port}' .format (host = host , port = port ))
306+ log = logger ( ssh . __class__ . __name__ ) .getChild ('{host}:{port}' .format (host = host , port = port ))
307307 self .assertIn (
308308 mock .call .log (level = logging .DEBUG , msg = command_log ),
309309 log .mock_calls
@@ -335,7 +335,7 @@ def test_007_execute_async_with_sudo_none_enforce(self, client, policy, logger):
335335 mock .call .makefile_stderr ('rb' ),
336336 mock .call .exec_command ('{}\n ' .format (command ))
337337 ))
338- log = logger .getChild ('{host}:{port}' .format (host = host , port = port ))
338+ log = logger ( ssh . __class__ . __name__ ) .getChild ('{host}:{port}' .format (host = host , port = port ))
339339 self .assertIn (
340340 mock .call .log (level = logging .DEBUG , msg = command_log ),
341341 log .mock_calls
@@ -380,7 +380,7 @@ def test_008_execute_async_sudo_password(
380380 "sudo -S bash -c '"
381381 "eval \" $(base64 -d <(echo \" {0}\" ))\" '" .format (encoded_cmd ))
382382 ))
383- log = logger .getChild ('{host}:{port}' .format (host = host , port = port ))
383+ log = logger ( ssh . __class__ . __name__ ) .getChild ('{host}:{port}' .format (host = host , port = port ))
384384 self .assertIn (
385385 mock .call .log (level = logging .DEBUG , msg = command_log ),
386386 log .mock_calls
@@ -410,7 +410,7 @@ def test_009_execute_async_verbose(self, client, policy, logger):
410410 mock .call .makefile_stderr ('rb' ),
411411 mock .call .exec_command ('{}\n ' .format (command ))
412412 ))
413- log = logger .getChild ('{host}:{port}' .format (host = host , port = port ))
413+ log = logger ( ssh . __class__ . __name__ ) .getChild ('{host}:{port}' .format (host = host , port = port ))
414414 self .assertIn (
415415 mock .call .log (level = logging .INFO , msg = command_log ),
416416 log .mock_calls
@@ -420,7 +420,7 @@ def test_010_execute_async_mask_command(self, client, policy, logger):
420420 cmd = "USE='secret=secret_pass' do task"
421421 log_mask_re = r"secret\s*=\s*([A-Z-a-z0-9_\-]+)"
422422 masked_cmd = "USE='secret=<*masked*>' do task"
423- cmd_log = u"Executing command:\n {!s }\n " .format (masked_cmd )
423+ cmd_log = u"Executing command:\n {!r }\n " .format (masked_cmd )
424424
425425 chan = mock .Mock ()
426426 open_session = mock .Mock (return_value = chan )
@@ -445,7 +445,7 @@ def test_010_execute_async_mask_command(self, client, policy, logger):
445445 mock .call .makefile_stderr ('rb' ),
446446 mock .call .exec_command ('{}\n ' .format (cmd ))
447447 ))
448- log = logger .getChild ('{host}:{port}' .format (host = host , port = port ))
448+ log = logger ( ssh . __class__ . __name__ ) .getChild ('{host}:{port}' .format (host = host , port = port ))
449449 self .assertIn (
450450 mock .call .log (level = logging .DEBUG , msg = cmd_log ),
451451 log .mock_calls
@@ -620,7 +620,7 @@ def test_014_check_stdin_closed(self, client, policy, logger):
620620 open_session .assert_called_once ()
621621 stdin .assert_not_called ()
622622
623- log = logger .getChild ('{host}:{port}' .format (host = host , port = port ))
623+ log = logger ( ssh . __class__ . __name__ ) .getChild ('{host}:{port}' .format (host = host , port = port ))
624624 log .warning .assert_called_once_with ('STDIN Send failed: closed channel' )
625625
626626 self .assertIn (chan , result )
@@ -777,7 +777,7 @@ def test_019_execute(
777777 execute_async .assert_called_once_with (command , verbose = False )
778778 chan .assert_has_calls ((mock .call .status_event .is_set (), ))
779779 message = self .gen_cmd_result_log_message (result )
780- log = logger .getChild ('{host}:{port}' .format (host = host , port = port )).log
780+ log = logger ( ssh . __class__ . __name__ ) .getChild ('{host}:{port}' .format (host = host , port = port )).log
781781 log .assert_has_calls (
782782 [
783783 mock .call (
@@ -824,7 +824,7 @@ def test_020_execute_verbose(
824824 chan .assert_has_calls ((mock .call .status_event .is_set (), ))
825825
826826 message = self .gen_cmd_result_log_message (result )
827- log = logger .getChild ('{host}:{port}' .format (host = host , port = port )).log
827+ log = logger ( ssh . __class__ . __name__ ) .getChild ('{host}:{port}' .format (host = host , port = port )).log
828828 log .assert_has_calls (
829829 [
830830 mock .call (
@@ -872,7 +872,7 @@ def test_021_execute_no_stdout(
872872 execute_async .assert_called_once_with (
873873 command , verbose = False , open_stdout = False )
874874 message = self .gen_cmd_result_log_message (result )
875- log = logger .getChild ('{host}:{port}' .format (host = host , port = port )).log
875+ log = logger ( ssh . __class__ . __name__ ) .getChild ('{host}:{port}' .format (host = host , port = port )).log
876876 log .assert_has_calls (
877877 [
878878 mock .call (
@@ -916,7 +916,7 @@ def test_022_execute_no_stderr(
916916 execute_async .assert_called_once_with (
917917 command , verbose = False , open_stderr = False )
918918 message = self .gen_cmd_result_log_message (result )
919- log = logger .getChild ('{host}:{port}' .format (host = host , port = port )).log
919+ log = logger ( ssh . __class__ . __name__ ) .getChild ('{host}:{port}' .format (host = host , port = port )).log
920920 log .assert_has_calls (
921921 [
922922 mock .call (
@@ -968,7 +968,7 @@ def test_023_execute_no_stdout_stderr(
968968 open_stderr = False
969969 )
970970 message = self .gen_cmd_result_log_message (result )
971- log = logger .getChild ('{host}:{port}' .format (host = host , port = port )).log
971+ log = logger ( ssh . __class__ . __name__ ) .getChild ('{host}:{port}' .format (host = host , port = port )).log
972972 log .assert_has_calls (
973973 [
974974 mock .call (level = logging .DEBUG , msg = message ),
@@ -1003,7 +1003,7 @@ def test_024_execute_timeout(
10031003 execute_async .assert_called_once_with (command , verbose = False )
10041004 chan .assert_has_calls ((mock .call .status_event .is_set (), ))
10051005 message = self .gen_cmd_result_log_message (result )
1006- log = logger .getChild ('{host}:{port}' .format (host = host , port = port )).log
1006+ log = logger ( ssh . __class__ . __name__ ) .getChild ('{host}:{port}' .format (host = host , port = port )).log
10071007 self .assertIn (
10081008 mock .call (level = logging .DEBUG , msg = message ),
10091009 log .mock_calls
@@ -1069,7 +1069,7 @@ def test_026_execute_mask_command(
10691069 cmd , log_mask_re = log_mask_re , verbose = False )
10701070 chan .assert_has_calls ((mock .call .status_event .is_set (),))
10711071 message = self .gen_cmd_result_log_message (result )
1072- log = logger .getChild ('{host}:{port}' .format (host = host , port = port )).log
1072+ log = logger ( ssh . __class__ . __name__ ) .getChild ('{host}:{port}' .format (host = host , port = port )).log
10731073 log .assert_has_calls (
10741074 [
10751075 mock .call (
@@ -1297,7 +1297,7 @@ def test_031_check_stderr(self, check_call, client, policy, logger):
12971297 error_info = None , raise_on_err = raise_on_err )
12981298
12991299
1300- @mock .patch ('exec_helpers._ssh_client_base.logger ' , autospec = True )
1300+ @mock .patch ('logging.getLogger ' , autospec = True )
13011301@mock .patch ('paramiko.AutoAddPolicy' , autospec = True , return_value = 'AutoAddPolicy' )
13021302@mock .patch ('paramiko.SSHClient' , autospec = True )
13031303@mock .patch ('paramiko.Transport' , autospec = True )
@@ -1528,7 +1528,7 @@ def test_03_execute_through_host_get_pty(
15281528 ))
15291529
15301530
1531- @mock .patch ('exec_helpers._ssh_client_base.logger ' , autospec = True )
1531+ @mock .patch ('logging.getLogger ' , autospec = True )
15321532@mock .patch ('paramiko.AutoAddPolicy' , autospec = True , return_value = 'AutoAddPolicy' )
15331533@mock .patch ('paramiko.SSHClient' , autospec = True )
15341534class TestSftp (unittest .TestCase ):
0 commit comments