| 1 |
###################################################################### |
| 2 |
# |
| 3 |
# File: test/unit/console_tool/test_download_file.py |
| 4 |
# |
| 5 |
# Copyright 2023 Backblaze Inc. All Rights Reserved. |
| 6 |
# |
| 7 |
# License https://www.backblaze.com/using_b2_code.html |
| 8 |
# |
| 9 |
###################################################################### |
| 10 |
import os |
| 11 |
import pathlib |
| 12 |
from test.helpers import skip_on_windows |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
EXPECTED_STDOUT_DOWNLOAD = ''' |
| 17 |
File name: file1.txt |
| 18 |
File id: 9999 |
| 19 |
Output file path: {output_path} |
| 20 |
File size: 11 |
| 21 |
Content type: b2/x-auto |
| 22 |
Content sha1: 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed |
| 23 |
Encryption: none |
| 24 |
Retention: none |
| 25 |
Legal hold: <unset> |
| 26 |
INFO src_last_modified_millis: 1500111222000 |
| 27 |
Checksum matches |
| 28 |
Download finished |
| 29 |
''' |
| 30 |
|
| 31 |
|
| 32 |
@pytest.mark.parametrize( |
| 33 |
'flag,expected_stdout', [ |
| 34 |
('--no-progress', EXPECTED_STDOUT_DOWNLOAD), |
| 35 |
('-q', ''), |
| 36 |
('--quiet', ''), |
| 37 |
] |
| 38 |
) |
| 39 |
def test_download_file_by_uri__flag_support(b2_cli, uploaded_file, tmp_path, flag, expected_stdout): |
| 40 |
output_path = tmp_path / 'output.txt' |
| 41 |
|
| 42 |
b2_cli.run( |
| 43 |
['download-file', flag, 'b2id://9999', |
| 44 |
str(output_path)], |
| 45 |
expected_stdout=expected_stdout.format(output_path=pathlib.Path(output_path).resolve()) |
| 46 |
) |
| 47 |
assert output_path.read_text() == uploaded_file['content'] |
| 48 |
|
| 49 |
|
| 50 |
@pytest.mark.parametrize('b2_uri', [ |
| 51 |
'b2://my-bucket/file1.txt', |
| 52 |
'b2id://9999', |
| 53 |
]) |
| 54 |
def test_download_file_by_uri__b2_uri_support(b2_cli, uploaded_file, tmp_path, b2_uri): |
| 55 |
output_path = tmp_path / 'output.txt' |
| 56 |
|
| 57 |
b2_cli.run( |
| 58 |
['download-file', b2_uri, str(output_path)], |
| 59 |
expected_stdout=EXPECTED_STDOUT_DOWNLOAD.format( |
| 60 |
output_path=pathlib.Path(output_path).resolve() |
| 61 |
) |
| 62 |
) |
| 63 |
assert output_path.read_text() == uploaded_file['content'] |
| 64 |
|
| 65 |
|
| 66 |
@pytest.mark.parametrize( |
| 67 |
'flag,expected_stdout', [ |
| 68 |
('--no-progress', EXPECTED_STDOUT_DOWNLOAD), |
| 69 |
('-q', ''), |
| 70 |
('--quiet', ''), |
| 71 |
] |
| 72 |
) |
| 73 |
def test_download_file_by_name(b2_cli, local_file, uploaded_file, tmp_path, flag, expected_stdout): |
| 74 |
output_path = tmp_path / 'output.txt' |
| 75 |
|
| 76 |
b2_cli.run( |
| 77 |
[ |
| 78 |
'download-file-by-name', uploaded_file['bucket'], uploaded_file['fileName'], |
| 79 |
str(output_path) |
| 80 |
], |
| 81 |
expected_stdout=EXPECTED_STDOUT_DOWNLOAD.format( |
| 82 |
output_path=pathlib.Path(output_path).resolve() |
| 83 |
), |
| 84 |
expected_stderr= |
| 85 |
'WARNING: download-file-by-name command is deprecated. Use download-file instead.\n', |
| 86 |
) |
| 87 |
assert output_path.read_text() == uploaded_file['content'] |
| 88 |
|
| 89 |
|
| 90 |
@pytest.mark.parametrize( |
| 91 |
'flag,expected_stdout', [ |
| 92 |
('--no-progress', EXPECTED_STDOUT_DOWNLOAD), |
| 93 |
('-q', ''), |
| 94 |
('--quiet', ''), |
| 95 |
] |
| 96 |
) |
| 97 |
def test_download_file_by_id(b2_cli, uploaded_file, tmp_path, flag, expected_stdout): |
| 98 |
output_path = tmp_path / 'output.txt' |
| 99 |
|
| 100 |
b2_cli.run( |
| 101 |
['download-file-by-id', flag, '9999', str(output_path)], |
| 102 |
expected_stdout=expected_stdout.format(output_path=pathlib.Path(output_path).resolve()), |
| 103 |
expected_stderr= |
| 104 |
'WARNING: download-file-by-id command is deprecated. Use download-file instead.\n', |
| 105 |
) |
| 106 |
assert output_path.read_text() == uploaded_file['content'] |
| 107 |
|
| 108 |
|
| 109 |
@skip_on_windows(reason='os.mkfifo is not supported on Windows') |
| 110 |
def test_download_file_by_name__named_pipe( |
| 111 |
b2_cli, local_file, uploaded_file, tmp_path, bg_executor |
| 112 |
): |
| 113 |
output_path = tmp_path / 'output.txt' |
| 114 |
os.mkfifo(output_path) |
| 115 |
|
| 116 |
output_string = None |
| 117 |
|
| 118 |
def reader(): |
| 119 |
nonlocal output_string |
| 120 |
output_string = output_path.read_text() |
| 121 |
|
| 122 |
reader_future = bg_executor.submit(reader) |
| 123 |
|
| 124 |
b2_cli.run( |
| 125 |
[ |
| 126 |
'download-file-by-name', '--no-progress', uploaded_file['bucket'], |
| 127 |
uploaded_file['fileName'], |
| 128 |
str(output_path) |
| 129 |
], |
| 130 |
expected_stdout=EXPECTED_STDOUT_DOWNLOAD.format( |
| 131 |
output_path=pathlib.Path(output_path).resolve() |
| 132 |
), |
| 133 |
expected_stderr= |
| 134 |
'WARNING: download-file-by-name command is deprecated. Use download-file instead.\n', |
| 135 |
) |
| 136 |
reader_future.result(timeout=1) |
| 137 |
assert output_string == uploaded_file['content'] |
| 138 |
|
| 139 |
|
| 140 |
@pytest.fixture |
| 141 |
def uploaded_stdout_txt(b2_cli, bucket, local_file, tmp_path): |
| 142 |
local_file.write_text('non-mocked /dev/stdout test ignore me') |
| 143 |
b2_cli.run(['upload-file', bucket, str(local_file), 'stdout.txt']) |
| 144 |
return { |
| 145 |
'bucket': bucket, |
| 146 |
'fileName': 'stdout.txt', |
| 147 |
'content': local_file.read_text(), |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
def test_download_file_by_name__to_stdout_by_alias( |
| 152 |
b2_cli, bucket, uploaded_stdout_txt, tmp_path, capfd |
| 153 |
): |
| 154 |
"""Test download_file_by_name stdout alias support""" |
| 155 |
b2_cli.run( |
| 156 |
['download-file-by-name', '--no-progress', bucket, uploaded_stdout_txt['fileName'], '-'], |
| 157 |
expected_stderr= |
| 158 |
'WARNING: download-file-by-name command is deprecated. Use download-file instead.\n', |
| 159 |
) |
| 160 |
assert capfd.readouterr().out == uploaded_stdout_txt['content'] |
| 161 |
assert not pathlib.Path('-').exists() |
| 162 |
|
| 163 |
|
| 164 |
def test_cat__b2_uri(b2_cli, bucket, uploaded_stdout_txt, tmp_path, capfd): |
| 165 |
"""Test download_file_by_name stdout alias support""" |
| 166 |
b2_cli.run(['cat', '--no-progress', f"b2://{bucket}/{uploaded_stdout_txt['fileName']}"],) |
| 167 |
assert capfd.readouterr().out == uploaded_stdout_txt['content'] |
| 168 |
|
| 169 |
|
| 170 |
def test_cat__b2_uri__invalid(b2_cli, capfd): |
| 171 |
b2_cli.run( |
| 172 |
['cat', "nothing/meaningful"], |
| 173 |
expected_stderr=None, |
| 174 |
expected_status=2, |
| 175 |
) |
| 176 |
assert "argument B2_URI: Unsupported URI scheme: ''" in capfd.readouterr().err |
| 177 |
|
| 178 |
|
| 179 |
def test_cat__b2_uri__not_a_file(b2_cli, bucket, capfd): |
| 180 |
b2_cli.run( |
| 181 |
['cat', "b2://bucket/dir/subdir/"], |
| 182 |
expected_stderr=None, |
| 183 |
expected_status=2, |
| 184 |
) |
| 185 |
assert "argument B2_URI: B2 URI pointing to a file-like object is required" in capfd.readouterr( |
| 186 |
).err |
| 187 |
|
| 188 |
|
| 189 |
def test_cat__b2id_uri(b2_cli, bucket, uploaded_stdout_txt, tmp_path, capfd): |
| 190 |
"""Test download_file_by_name stdout alias support""" |
| 191 |
b2_cli.run(['cat', '--no-progress', "b2id://9999"],) |
| 192 |
assert capfd.readouterr().out == uploaded_stdout_txt['content'] |
| 193 |
|
| 194 |
|
| 195 |
def test__download_file__threads(b2_cli, local_file, uploaded_file, tmp_path): |
| 196 |
num_threads = 13 |
| 197 |
output_path = tmp_path / 'output.txt' |
| 198 |
|
| 199 |
b2_cli.run( |
| 200 |
[ |
| 201 |
'download-file', '--no-progress', '--threads', |
| 202 |
str(num_threads), 'b2://my-bucket/file1.txt', |
| 203 |
str(output_path) |
| 204 |
] |
| 205 |
) |
| 206 |
|
| 207 |
assert output_path.read_text() == uploaded_file['content'] |
| 208 |
assert b2_cli.console_tool.api.services.download_manager.get_thread_pool_size() == num_threads |