本文整理汇总了Python中st2common.runners.paramiko_ssh.ParamikoSSHClient.run方法的典型用法代码示例。如果您正苦于以下问题:Python ParamikoSSHClient.run方法的具体用法?Python ParamikoSSHClient.run怎么用?Python ParamikoSSHClient.run使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类st2common.runners.paramiko_ssh.ParamikoSSHClient
的用法示例。
在下文中一共展示了ParamikoSSHClient.run方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_sftp_connection_is_only_established_if_required
# 需要导入模块: from st2common.runners.paramiko_ssh import ParamikoSSHClient [as 别名]
# 或者: from st2common.runners.paramiko_ssh.ParamikoSSHClient import run [as 别名]
def test_sftp_connection_is_only_established_if_required(self):
# Verify that SFTP connection is lazily established only if and when needed.
conn_params = {'hostname': 'dummy.host.org',
'username': 'ubuntu'}
# Verify sftp connection and client hasn't been established yet
client = ParamikoSSHClient(**conn_params)
client.connect()
self.assertTrue(client.sftp_client is None)
# run method doesn't require sftp access so it shouldn't establish connection
client = ParamikoSSHClient(**conn_params)
client.connect()
client.run(cmd='whoami')
self.assertTrue(client.sftp_client is None)
# Methods bellow require SFTP access so they should cause SFTP connection to be established
# put
client = ParamikoSSHClient(**conn_params)
client.connect()
path = '/root/random_script.sh'
client.put(path, path, mirror_local_mode=False)
self.assertTrue(client.sftp_client is not None)
# exists
client = ParamikoSSHClient(**conn_params)
client.connect()
client.exists('/root/somepath.txt')
self.assertTrue(client.sftp_client is not None)
# mkdir
client = ParamikoSSHClient(**conn_params)
client.connect()
client.mkdir('/root/somedirfoo')
self.assertTrue(client.sftp_client is not None)
# Verify close doesn't throw if SFTP connection is not established
client = ParamikoSSHClient(**conn_params)
client.connect()
self.assertTrue(client.sftp_client is None)
client.close()
# Verify SFTP connection is closed if it's opened
client = ParamikoSSHClient(**conn_params)
client.connect()
client.mkdir('/root/somedirfoo')
self.assertTrue(client.sftp_client is not None)
client.close()
self.assertEqual(client.sftp_client.close.call_count, 1)
示例2: test_handle_stdout_and_stderr_line_funcs
# 需要导入模块: from st2common.runners.paramiko_ssh import ParamikoSSHClient [as 别名]
# 或者: from st2common.runners.paramiko_ssh.ParamikoSSHClient import run [as 别名]
def test_handle_stdout_and_stderr_line_funcs(self):
mock_handle_stdout_line_func = mock.Mock()
mock_handle_stderr_line_func = mock.Mock()
conn_params = {
'hostname': 'dummy.host.org',
'username': 'ubuntu',
'password': 'ubuntu',
'handle_stdout_line_func': mock_handle_stdout_line_func,
'handle_stderr_line_func': mock_handle_stderr_line_func
}
client = ParamikoSSHClient(**conn_params)
client.connect()
mock_get_transport = mock.Mock()
mock_chan = mock.Mock()
client.client.get_transport = mock.Mock()
client.client.get_transport.return_value = mock_get_transport
mock_get_transport.open_session.return_value = mock_chan
def mock_recv_ready_factory(chan):
chan.recv_counter = 0
def mock_recv_ready():
chan.recv_counter += 1
if chan.recv_counter < 2:
return True
return False
return mock_recv_ready
def mock_recv_stderr_ready_factory(chan):
chan.recv_stderr_counter = 0
def mock_recv_stderr_ready():
chan.recv_stderr_counter += 1
if chan.recv_stderr_counter < 2:
return True
return False
return mock_recv_stderr_ready
mock_chan.recv_ready = mock_recv_ready_factory(mock_chan)
mock_chan.recv_stderr_ready = mock_recv_stderr_ready_factory(mock_chan)
mock_chan.recv.return_value = 'stdout 1\nstdout 2\nstdout 3'
mock_chan.recv_stderr.return_value = 'stderr 1\nstderr 2\nstderr 3'
# call_line_handler_func is False so handler functions shouldn't be called
client.run(cmd='echo "test"', call_line_handler_func=False)
self.assertEqual(mock_handle_stdout_line_func.call_count, 0)
self.assertEqual(mock_handle_stderr_line_func.call_count, 0)
# Reset counters
mock_chan.recv_counter = 0
mock_chan.recv_stderr_counter = 0
# call_line_handler_func is True so handler functions should be called for each line
client.run(cmd='echo "test"', call_line_handler_func=True)
self.assertEqual(mock_handle_stdout_line_func.call_count, 3)
self.assertEqual(mock_handle_stdout_line_func.call_args_list[0][1]['line'], 'stdout 1\n')
self.assertEqual(mock_handle_stdout_line_func.call_args_list[1][1]['line'], 'stdout 2\n')
self.assertEqual(mock_handle_stdout_line_func.call_args_list[2][1]['line'], 'stdout 3\n')
self.assertEqual(mock_handle_stderr_line_func.call_count, 3)
self.assertEqual(mock_handle_stdout_line_func.call_args_list[0][1]['line'], 'stdout 1\n')
self.assertEqual(mock_handle_stdout_line_func.call_args_list[1][1]['line'], 'stdout 2\n')
self.assertEqual(mock_handle_stdout_line_func.call_args_list[2][1]['line'], 'stdout 3\n')