Skip to main content

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'

Example 4

This example shows how to mock an SFTP connection to a host named some_host_4 on port 22. The ls -l command is mocked to return the string 'ls output'. The content of a remote file is mocked to return the string 'Something from the remote file'.

def test_example_function_sftp_read():
    ssh_mock = SSHClientMock()

    SSHMockEnvron().add_responses_for_host('some_host_4', 22, {
        'ls -l': SSHCommandMock('', 'ls output', '')
    }, 'root', 'root')
    ssh_mock.sftp_client_mock.sftp_file_mock.file_content = 'Something from the remote file'
    # patch the paramiko.SSHClient with the mock
    with patch('paramiko.SSHClient', new=SSHClientMock): 
        output = example_function_sftp_read()
        assert 'Something from the remote file' == output

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.

Github

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

paramiko_mock-0.0.2.tar.gz (5.0 kB view details)

Uploaded Source

File details

Details for the file paramiko_mock-0.0.2.tar.gz.

File metadata

  • Download URL: paramiko_mock-0.0.2.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for paramiko_mock-0.0.2.tar.gz
Algorithm Hash digest
SHA256 cd75a61168a77a076734ee4ba8c11c9d431718514eea26c80977a93aa07fcf43
MD5 3dac491e1b4f13f4b433b1af3dbeef10
BLAKE2b-256 5d705f90de29d34ea61a0d4c5ec003451aa8186e1429b6a789d1f7749061d133

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page