Emulate SSH server for testing purposes
Asked Answered
D

3

11

I have to write test for deployment script which uploads files through SSH, but I'd like to have it not depending on external servers configuration. This is how i see it:

  1. Create 2 SSH daemons without authentication on different ports of loopback interface.
  2. Run the deployment script on these two ports

The only question is how to run these dummy SSH daemons.

I use Python and Fabric.

Disturbing answered 6/7, 2010 at 12:56 Comment(0)
V
7

If you want full control over the server's actions (e.g. in order to simulate various problem conditions and thereby do a really thorough testing) I recommend twisted: as this article shows, it makes it really easy to set up your own custom SSH server.

If you'd rather use an existing ssh server, pick one from the list here (or use the one that comes with your system, if any; or maybe sshwindows if you're on windows) and run it with subprocess from Python as a part of starting up your tests.

Velum answered 6/7, 2010 at 14:42 Comment(1)
Thanks for useful idea, but I just monkey patched Fabric API to run all the commands on local server. I'll mark your comment as it has the best solution anyway.Disturbing
A
5

Another option is to spin up a dockerized container with sshd service running. You can use a docker image like these:

I've used this for testing out a deployment script (made on top of fabric ).

Here's how you use it.

Pull the image.

➜ docker pull kabirbaidhya/fakeserver

Set authorized keys for the server.

➜ cat ~/.ssh/id_rsa.pub > /path/to/authorized_keys

Run the fakeserver.

➜ docker run -d -p 2222:22 \
             -v "/path/to/authorized_keys:/etc/authorized_keys/tester" \
             -e SSH_USERS="tester:1001:1001" \
             --name=fakeserver kabirbaidhya/fakeserver

You can now use the fakeserver from any ssh client. For instance:

➜ ssh tester@localhost -p 2222
➜ ssh tester@localhost -p 2222 "echo 'Hello World'"

If this works, you can then use any ssh clients or scripts on top of paramiko or fabric to test against this mock server.

Hope this helps.

Alverson answered 17/10, 2018 at 18:14 Comment(0)
L
1

Reimplementing an SSH daemon is not trivial.

If your only problem is you don't want them depending on existing configurations, you can start up new sshd with -f to specify a specific configuration and -p to run on a given port.

You can use os.system to make calls to the shell:

os.system('sshd -f myconfig -p 22022')
Lazuli answered 6/7, 2010 at 13:17 Comment(1)
This is a great idea and I almost got it working, but I got stuck with ssh-agent which can't run twice with specific authorized_keys file. Anyway, thanks for your response!Disturbing

© 2022 - 2024 — McMap. All rights reserved.