tahoe-lafs/integration/test_get_put.py

132 lines
3.9 KiB
Python
Raw Permalink Normal View History

2021-05-21 15:57:32 +00:00
"""
Integration tests for getting and putting files, including reading from stdin
and stdout.
"""
from subprocess import Popen, PIPE, check_output, check_call
2021-05-21 15:57:32 +00:00
import pytest
2023-02-07 14:44:51 +00:00
from twisted.internet import reactor
2023-05-04 20:15:30 +00:00
from twisted.internet.threads import blockingCallFromThread
2021-05-21 15:57:32 +00:00
from .util import run_in_thread, cli
2021-05-21 15:57:32 +00:00
DATA = b"abc123 this is not utf-8 decodable \xff\x00\x33 \x11"
try:
DATA.decode("utf-8")
except UnicodeDecodeError:
pass # great, what we want
else:
raise ValueError("BUG, the DATA string was decoded from UTF-8")
@pytest.fixture(scope="session")
def get_put_alias(alice):
cli(alice.process, "create-alias", "getput")
2021-05-21 15:57:32 +00:00
def read_bytes(path):
with open(path, "rb") as f:
return f.read()
@run_in_thread
def test_put_from_stdin(alice, get_put_alias, tmpdir):
"""
It's possible to upload a file via `tahoe put`'s STDIN, and then download
it to a file.
"""
tempfile = str(tmpdir.join("file"))
p = Popen(
["tahoe", "--node-directory", alice.process.node_dir, "put", "-", "getput:fromstdin"],
2021-05-21 15:57:32 +00:00
stdin=PIPE
)
p.stdin.write(DATA)
p.stdin.close()
assert p.wait() == 0
cli(alice.process, "get", "getput:fromstdin", tempfile)
2021-05-21 15:57:32 +00:00
assert read_bytes(tempfile) == DATA
2023-03-20 19:08:22 +00:00
@run_in_thread
2021-05-21 16:02:20 +00:00
def test_get_to_stdout(alice, get_put_alias, tmpdir):
"""
It's possible to upload a file, and then download it to stdout.
"""
tempfile = tmpdir.join("file")
with tempfile.open("wb") as f:
f.write(DATA)
cli(alice.process, "put", str(tempfile), "getput:tostdout")
2021-05-21 16:02:20 +00:00
p = Popen(
["tahoe", "--node-directory", alice.process.node_dir, "get", "getput:tostdout", "-"],
2021-05-21 16:02:20 +00:00
stdout=PIPE
)
assert p.stdout.read() == DATA
2021-05-21 16:02:20 +00:00
assert p.wait() == 0
2023-02-07 14:44:51 +00:00
2023-03-20 19:08:22 +00:00
@run_in_thread
def test_large_file(alice, get_put_alias, tmp_path):
"""
It's possible to upload and download a larger file.
We avoid stdin/stdout since that's flaky on Windows.
"""
tempfile = tmp_path / "file"
with tempfile.open("wb") as f:
f.write(DATA * 1_000_000)
cli(alice.process, "put", str(tempfile), "getput:largefile")
outfile = tmp_path / "out"
check_call(
["tahoe", "--node-directory", alice.process.node_dir, "get", "getput:largefile", str(outfile)],
)
assert outfile.read_bytes() == tempfile.read_bytes()
2023-05-04 20:15:30 +00:00
@run_in_thread
def test_upload_download_immutable_different_default_max_segment_size(alice, get_put_alias, tmpdir, request):
2023-02-07 14:44:51 +00:00
"""
Tahoe-LAFS used to have a default max segment size of 128KB, and is now
1MB. Test that an upload created when 128KB was the default can be
downloaded with 1MB as the default (i.e. old uploader, new downloader), and
vice versa, (new uploader, old downloader).
"""
tempfile = tmpdir.join("file")
large_data = DATA * 100_000
assert len(large_data) > 2 * 1024 * 1024
with tempfile.open("wb") as f:
f.write(large_data)
2023-05-04 20:15:30 +00:00
def set_segment_size(segment_size):
return blockingCallFromThread(
2023-02-07 14:44:51 +00:00
reactor,
lambda: alice.reconfigure_zfec(
2023-05-04 20:15:30 +00:00
reactor,
(1, 1, 1),
None,
max_segment_size=segment_size
)
2023-05-04 20:15:30 +00:00
)
2023-02-07 14:44:51 +00:00
# 1. Upload file 1 with default segment size set to 1MB
2023-05-04 20:15:30 +00:00
set_segment_size(1024 * 1024)
cli(alice.process, "put", str(tempfile), "getput:seg1024kb")
2023-02-07 14:44:51 +00:00
# 2. Download file 1 with default segment size set to 128KB
2023-05-04 20:15:30 +00:00
set_segment_size(128 * 1024)
2023-02-07 14:44:51 +00:00
assert large_data == check_output(
["tahoe", "--node-directory", alice.process.node_dir, "get", "getput:seg1024kb", "-"]
2023-02-07 14:44:51 +00:00
)
# 3. Upload file 2 with default segment size set to 128KB
cli(alice.process, "put", str(tempfile), "getput:seg128kb")
2023-02-07 14:44:51 +00:00
# 4. Download file 2 with default segment size set to 1MB
2023-05-04 20:15:30 +00:00
set_segment_size(1024 * 1024)
2023-02-07 14:44:51 +00:00
assert large_data == check_output(
["tahoe", "--node-directory", alice.process.node_dir, "get", "getput:seg128kb", "-"]
2023-02-07 14:44:51 +00:00
)