问题
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