mock paramiko.SSHClient().exec_command()

血红的双手。 提交于 2019-12-24 00:39:54

问题


I need mock execution of some remote command via ssh.exec_command() It returns tuple (stdin, stdout, stderr) as paramiko.ChanelFile object.

So, I have output of command as string (which I want, exec_command() to return) and the question how to create ChanelFile object with my output string.

Pseudo code:

    def send_command(self, cmd)
        self.client.connect(hostname=self.host,
                            username=self.user,
                            password=self.password,
                            timeout=self.timeout)
        stdin, stdout, stderr = self.client.exec_command(cmd)

        return stdin, stdout, stderr

    if __name__ == '__main__':
        stdin, stdout, stderr = report.send_command('ls -la')
        resp = stdout.read().strip()

So I need create stout to be able run read() method on it.


回答1:


See https://stackoverflow.com/a/8168742/5512755 A MagicMock works here too.

stdout = mock.MagicMock()
stdout.read().strip.return_value = "file1\nfile2\nfile3\n"

will do the trick.



来源:https://stackoverflow.com/questions/36135687/mock-paramiko-sshclient-exec-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!