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 Jan 23, 2025
1 parent 225dfa3 commit 1c5bdcd
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions tests/unit/sftp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,19 @@ def dst_path(file_paths_pair):
return path


@pytest.fixture
def other_payload():
"""Generate a binary test payload."""
uuid_name = uuid.uuid4()
return 'Original content: {name!s}'.format(name=uuid_name).encode()
@pytest.fixture(params=[32, 1025])
def sized_payload():
"""Generate a binary test payload of size given as a parameter."""
payload_len = 1024 + 1
random_bytes = [ord(random.choice(string.printable)) for _ in range(payload_len)]
return bytes(random_bytes)


@pytest.fixture
def dst_exists_path(file_paths_pair, other_payload):
def dst_exists_path(file_paths_pair, sized_payload):
"""Return a data destination path."""
path = file_paths_pair[1]
path.write_bytes(other_payload)
path.write_bytes(sized_payload)
assert path.exists()
return path

Expand Down Expand Up @@ -96,32 +97,24 @@ def test_put_existing(dst_exists_path, src_path, sftp_session, 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):
def src_path_large(tmp_path, sized_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)
path.write_bytes(sized_payload)
return path


def test_put_large(dst_path, src_path_large, sftp_session, large_payload):
def test_put_large(dst_path, src_path_large, sftp_session, sized_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
assert dst_path.read_bytes() == sized_payload


def test_get_large(dst_path, src_path_large, sftp_session, large_payload):
def test_get_large(dst_path, src_path_large, sftp_session, sized_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
assert dst_path.read_bytes() == sized_payload

0 comments on commit 1c5bdcd

Please sign in to comment.