Skip to content

Commit

Permalink
tests: Simplify generating payload
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Jelen <[email protected]>
  • Loading branch information
Jakuje committed Feb 11, 2025
1 parent 9d105eb commit 50d87ee
Showing 1 changed file with 15 additions and 37 deletions.
52 changes: 15 additions & 37 deletions tests/unit/sftp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,21 @@ def sftp_session(ssh_client_session):
del sftp_sess # noqa: WPS420


@pytest.fixture
def transmit_payload():
"""Generate a binary test payload."""
uuid_name = uuid.uuid4()
return 'Hello, {name!s}'.format(name=uuid_name).encode()
@pytest.fixture(
params=(32, 1024 + 1),
ids=('small-payload', 'large-payload'),
)
def transmit_payload(request: pytest.FixtureRequest):
"""Generate binary test payloads of assorted sizes.
The choice 32 is arbitrary small value.
The choice 1024 + 1 is meant to be 1B larger than the chunk size used in
sftp.pyx to make sure we excercise at least two rounds of reading/writing.
"""
payload_len = request.param
random_bytes = [ord(random.choice(string.printable)) for _ in range(payload_len)] #NOSONAR
return bytes(random_bytes)


@pytest.fixture
Expand Down Expand Up @@ -93,35 +103,3 @@ def test_put_existing(dst_exists_path, src_path, sftp_session, transmit_payload)
"""Check that SFTP file upload works when target file exists."""
sftp_session.put(str(src_path), str(dst_exists_path))
assert dst_exists_path.read_bytes() == transmit_payload


@pytest.fixture
def large_payload():
"""Generate a large 1025 byte (1024 + 1B) test payload."""
payload_len = 1024 + 1
random_bytes = [ord(random.choice(string.printable)) for _ in range(payload_len)]
return bytes(random_bytes)


@pytest.fixture
def src_path_large(tmp_path, large_payload):
"""Return a remote path to a 1025 byte-sized file.
The pylibssh chunk size is 1024 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 50d87ee

Please sign in to comment.