ncouture / MockSSH

Mock an SSH server and define all commands it supports (Python, Twisted)
Other
123 stars 23 forks source link

Getting OSError: [Errno 9] Bad file descriptor in tests #21

Closed Suor closed 4 years ago

Suor commented 4 years ago

I occasionally get:

Exception in thread Thread-9:
Traceback (most recent call last):
  File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/suor/.virtualenvs/dvc/lib/python3.7/site-packages/mockssh/server.py", line 126, in _run
    conn, addr = sock.accept()
  File "/usr/lib/python3.7/socket.py", line 212, in accept
    fd, addr = self._accept()
OSError: [Errno 9] Bad file descriptor

which doesn't break anything, only annoys us.

Here is how we use it in our pytest suite:

here = os.path.abspath(os.path.dirname(__file__))

user = "user"
key_path = os.path.join(here, "{0}.key".format(user))

@pytest.fixture
def ssh_server():
    users = {user: key_path}
    with mockssh.Server(users) as s:
        s.test_creds = {
            "host": s.host,
            "port": s.port,
            "username": user,
            "key_filename": key_path,
        }
        yield s

@pytest.fixture
def ssh(ssh_server):
    # This is our class encapsulating paramiko.SSHClient
    yield SSHConnection(**ssh_server.test_creds)
Suor commented 4 years ago

Sorry, wrong repo.