I need mock execution of some remote command via ssh.exec_command() It returns tuple (stdin, stdout, stderr) as paramiko.ChanelFile object.
I know I can use mock.MagicMock() and that is what I have used. Now I want to get different stdout o/p for each call and I am stuck.
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
def func():
stdin, stdout, stderr = report.send_command('ls -la')
resp = stdout.read().decode()
stdin, stdout, stderr = report.send_command('pwd')
resp2 = stdout.read().decode()
return [resp, resp2]
Tests:
@pytest.fixture
def mock_paramiko_ssh_client(items):
"""Mocks the paramiko SSHClient.s"""
with mock.patch('paramiko.SSHClient', autospec=True) as mock_client:
# Mock exec_command()
stdin = mock.MagicMock()
stdout = mock.MagicMock()
stderr = mock.MagicMock()
stdout.read.side_effect= items.param
stderr.read.return_value = b''
mock_client.return_value.exec_command.side_effect = (stdin, stdout, stderr)
yield mock_client
@pytest.mark.parametrize(mock_paramiko_ssh_client, [[b'file1', b'/root']], indirect=True)
def test():
result = func()
assert (result == ['file1', '/root']
This seems not to be working obviously. But is there a way to mock the stdout.read() value??
1