- class SFTPContainer(image: str = 'atmoz/sftp:alpine', port: int = 22, *, users: list[SFTPUser] | None = None, **kwargs: Any)¶
Test container for an SFTP server.
Default configuration creates two users,
basic:passwordandkeypairwhich has no password but should use the private key accessible atmy_container.users[1].private_key.Users can only download from their root user folder, but can upload & download from any subfolder (
upload/by default).Options:
users = [SFTPUser("jane", password="secret"), SFTPUser.with_keypair("ron")]createsjane:secretorronwho uses the private key accessible atusers[1].private_key.
Simple example with basic auth:
>>> import paramiko >>> from testcontainers.sftp import SFTPContainer >>> with SFTPContainer() as sftp_container: ... host_ip = sftp_container.get_container_host_ip() ... host_port = sftp_container.get_exposed_sftp_port() ... ssh = paramiko.SSHClient() ... ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ... ssh.connect(host_ip, host_port, "basic", "password") ... # ssh.get(...) ... # ssh.listdir() ... # ssh.chdir("upload") ... # ssh.put(...)
Example with keypair auth:
>>> import paramiko >>> from testcontainers.sftp import SFTPContainer >>> with SFTPContainer() as sftp_container: ... host_ip = sftp_container.get_container_host_ip() ... host_port = sftp_container.get_exposed_sftp_port() ... ssh = paramiko.SSHClient() ... ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ... private_key_file = sftp_container.users[1].private_key_file ... ssh.connect(host_ip, host_port, "keypair", key_filename=private_key_file) ... # ssh.listdir() ... # ssh.get(...) ... # ssh.chdir("upload") ... # ssh.put(...)
- class SFTPUser(name: str, *, public_key: bytes | None = None, private_key: bytes | None = None, password: str | None = None, uid: str | None = None, gid: str | None = None, folders: list[str] | None = None, mount_dir: str | None = None)¶
Helper class to define a user for SFTPContainer authentication.
Constructor args/kwargs:
name: (req.) usernamepublic_key: (opt.) bytes of publickeyprivate_key: (opt.) bytes of privatekey (useful if you want to access them later in test code)password: (opt.) passworduid: (opt.) user IDgid: (opt.) group IDfolders: (opt.) folders to create inside the user’s directory (e.g. upload/)mount_dir: (opt.) a local folder to mount to the user’s root directory
Properties:
public_key_file: str path of public key tempfile (gets mounted to SFTPContainer as a volume)private_key_file: str path of private key tempfile (useful to pass to paramiko when connecting to the sftp server using ssh
Methods:
with_keypair: classmethod to create a new user with an auto-generated RSA keypairconf: str configuration string to register user on server
Example
>>> from testcontainers.sftp import SFTPUser >>> users = [ ... SFTPUser("jane", password="secret"), ... SFTPUser.with_keypair("ron", folders=["stuff"]), ... ] >>> for user in users: ... print(user.name, user.folders[0]) ... jane upload ron stuff >>> assert users[0].password == "secret" >>> assert users[1].public_key is not None >>> assert users[1].public_key.decode().startswith("ssh-rsa ") >>> assert users[1].private_key is not None >>> assert users[1].private_key.decode().startswith("-----BEGIN RSA PRIVATE KEY-----")