Mock for paramiko library
Project description
ParamikoMock
ParamikoMock is a Python library for mocking the paramiko
SSH client for testing purposes. It allows you to define responses for specific SSH commands and hosts, making it easier to test code that interacts with remote servers via SSH.
Installation
pip install paramiko-mock
Usage
Here are some examples of how to use ParamikoMock:
Example 1
This example shows how to mock an SSH connection to a host named some_host
on port 22
. The ls -l
command is mocked to return the string 'ls output'
.
from ParamikoMock import SSHMockEnvron, SSHCommandMock
from unittest.mock import patch
import paramiko
def example_function_1():
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('some_host', port=22, username='root', password='root', banner_timeout=10)
stdin, stdout, stderr = client.exec_command('ls -l')
return stdout.read()
def test_example_function_1():
SSHMockEnvron().add_responses_for_host('some_host', 22, {
'ls -l': SSHCommandMock('', 'ls output', '')
}, 'root', 'root')
with patch('paramiko.SSHClient', new=SSHClientMock):
output = example_function_1()
assert output == 'ls output'
Example 2
This example shows how to mock an SSH connection to a host named some_host_2
on port 4826
. The sudo docker ps
command is mocked to return the string 'docker-ps-output'
.
from ParamikoMock import SSHMockEnvron, SSHCommandMock
from unittest.mock import patch
import paramiko
def example_function_2():
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('some_host_2', port=4826, username='root', password='root', banner_timeout=10)
stdin, stdout, stderr = client.exec_command('sudo docker ps')
return stdout.read()
def test_example_function_2():
SSHMockEnvron().add_responses_for_host('some_host_2', 4826, {
'sudo docker ps': SSHCommandMock('', 'docker-ps-output', '')
}, 'root', 'root')
with patch('paramiko.SSHClient', new=SSHClientMock):
output = example_function_2()
assert output == 'docker-ps-output'
Example 3
This example shows how to mock an SSH connection to a host named some_host_3
on port 22
. The custom_command --param1 value1
command is mocked to return the string 'value1'
.
from ParamikoMock import SSHMockEnvron, SSHCommandFunctionMock
from unittest.mock import patch
import paramiko
def example_function_3():
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('some_host_3', port=22, username='root', password='root', banner_timeout=10)
stdin, stdout, stderr = client.exec_command('custom_command --param1 value1')
return stdout.read()
def test_example_function_3():
def custom_command_processor(ssh_client_mock: SSHClientMock, command: str):
if 'param1' in command and 'value1' in command:
return StringIO(''), StringIO('value1'), StringIO('')
SSHMockEnvron().add_responses_for_host('some_host_3', 22, {
r're(custom_command .*)': SSHCommandFunctionMock(custom_command_processor)
}, 'root', 'root')
with patch('paramiko.SSHClient', new=SSHClientMock):
output = example_function_3()
assert output == 'value1'
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file paramiko_mock-0.0.1.tar.gz
.
File metadata
- Download URL: paramiko_mock-0.0.1.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 634c441f43f9c971a20412170cb289e68ba9d65a338c6b5a4b528c43e5523375 |
|
MD5 | b201c863690c71333ff5a99deb292099 |
|
BLAKE2b-256 | 3b6439bcdc5b328eab8fd01795b0406c22106bd8de0b7562bd8dad026a410f05 |