summaryrefslogtreecommitdiff
path: root/tests/conftest.py
diff options
context:
space:
mode:
authorJeff Forcier <jeff@bitprophet.org>2017-10-23 14:11:10 -0700
committerJeff Forcier <jeff@bitprophet.org>2017-10-23 14:11:10 -0700
commit1d15a88758c39fc2024bf59bdd09deb160d841c7 (patch)
tree2574cc4ee43284ea899541802fe215bf847c7f7d /tests/conftest.py
parent67329edd7945a603a571c60d4fadc9e861b9cec9 (diff)
downloadparamiko-1d15a88758c39fc2024bf59bdd09deb160d841c7.tar.gz
Get big sftp tests passing w/ the sftp client + folder crap being a fixture
Diffstat (limited to 'tests/conftest.py')
-rw-r--r--tests/conftest.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
new file mode 100644
index 00000000..dbf2cb0f
--- /dev/null
+++ b/tests/conftest.py
@@ -0,0 +1,64 @@
+import os
+import threading
+
+import pytest
+from paramiko import RSAKey, SFTPServer, SFTP, Transport
+
+from .loop import LoopSocket
+from .stub_sftp import StubServer, StubSFTPServer
+from .util import _support
+
+
+# TODO: not a huge fan of conftest.py files, see if we can move these somewhere
+# 'nicer'.
+
+
+def make_sftp_folder(client):
+ """
+ Create some non-existing, new folder on the given SFTP connection.
+ """
+ path = os.environ.get('TEST_FOLDER', 'temp-testing000')
+ # TODO: this is disgusting and old, replace with something smarter/simpler
+ for i in range(1000):
+ path = path[:-3] + '%03d' % i
+ try:
+ client.mkdir(path)
+ return path
+ except (IOError, OSError):
+ pass
+
+
+# TODO: apply at module or session level
+# TODO: roll in SFTP folder setup and teardown?
+# NOTE: This is defined here for use by both SFTP (normal & 'big') suites.
+@pytest.fixture
+def sftp():
+ """
+ Set up an in-memory SFTP server, returning its corresponding SFTPClient.
+ """
+ # Sockets & transports
+ socks = LoopSocket()
+ sockc = LoopSocket()
+ sockc.link(socks)
+ tc = Transport(sockc)
+ ts = Transport(socks)
+ # Auth
+ host_key = RSAKey.from_private_key_file(_support('test_rsa.key'))
+ ts.add_server_key(host_key)
+ # Server & client setup
+ event = threading.Event()
+ server = StubServer()
+ ts.set_subsystem_handler('sftp', SFTPServer, StubSFTPServer)
+ ts.start_server(event, server)
+ tc.connect(username='slowdive', password='pygmalion')
+ event.wait(1.0)
+ client = SFTP.from_transport(tc)
+ # Work in 'remote' folder setup (as it wants to use the client)
+ # TODO: how cleanest to make this available to tests? Doing it this way is
+ # marginally less bad than the previous 'global'-using setup, but not by
+ # much?
+ client.FOLDER = make_sftp_folder(client)
+ # Yield client to caller
+ yield client
+ # Clean up
+ client.rmdir(client.FOLDER)