Skip to content

Commit

Permalink
tests: Reproducer for ansible#341
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
  • Loading branch information
Jakuje authored and webknjaz committed Sep 12, 2024
1 parent c5d3248 commit 5361f6b
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tests/unit/sftp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

"""Tests suite for sftp."""

import random
import string
import uuid

import pytest
Expand Down Expand Up @@ -63,3 +65,41 @@ def test_get(dst_path, src_path, sftp_session, transmit_payload):
"""Check that SFTP file download works."""
sftp_session.get(str(src_path), str(dst_path))
assert dst_path.read_bytes() == transmit_payload


@pytest.fixture
def large_payload():
"""
Generate a large 255 * 1024 + 1 B test payload.
The OpenSSH SFTP server supports maximum reads and writes of 256 * 1024 - 1024 B per request.
"""
random_char_kilobyte = [ord(random.choice(string.printable)) for _ in range(1024)]
full_bytes_number = 255
a_255kB_chunk = bytes(random_char_kilobyte * full_bytes_number)
the_last_byte = random.choice(random_char_kilobyte).to_bytes(length=1, byteorder='big')
return a_255kB_chunk + the_last_byte


@pytest.fixture
def src_path_large(tmp_path, large_payload):
"""Return a remote path to a 255kB + 1B sized file.
The openssh max read/write chunk size is 255kB so the test needs
a file that would execute at least two loops.
"""
path = tmp_path / 'large.txt'
path.write_bytes(large_payload)
return path


def test_put_large(dst_path, src_path_large, sftp_session, large_payload):
"""Check that SFTP can upload large file."""
sftp_session.put(str(src_path_large), str(dst_path))
assert dst_path.read_bytes() == large_payload


def test_get_large(dst_path, src_path_large, sftp_session, large_payload):
"""Check that SFTP can download large file."""
sftp_session.get(str(src_path_large), str(dst_path))
assert dst_path.read_bytes() == large_payload

0 comments on commit 5361f6b

Please sign in to comment.