用于测试的模拟ssh服务器
mock-ssh-server的Python项目详细描述
mock-ssh-server打包实现ssh的python上下文管理器 用于测试的服务器。它是建立在paramiko之上的,所以它是 不需要安装openssh二进制文件。
示例用法
作为py.test固定装置:
import os from pytest import yield_fixture import mockssh @yield_fixture() def server(): users = { "sample-user": "/path/to/user-private-key, } with mockssh.Server(users) as s: yield s def test_ssh_session(server): for uid in server.users: with server.client(uid) as c: _, stdout, _ = c.exec_command("ls /") assert stdout.read() def test_sftp_session(server): for uid in server.users: target_dir = tempfile.mkdtemp() target_fname = os.path.join(target_dir, "foo") assert not os.access(target_fname, os.F_OK) with server.client(uid) as c: sftp = c.open_sftp() sftp.put(__file__, target_fname, confirm=True) assert os.access(target_fname, os.F_OK)