| 1 |
###################################################################### |
| 2 |
# |
| 3 |
# File: test/unit/console_tool/test_upload_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 |
from test.helpers import skip_on_windows |
| 12 |
|
| 13 |
import b2 |
| 14 |
|
| 15 |
|
| 16 |
def test_upload_file__file_info_src_last_modified_millis_and_headers(b2_cli, bucket, tmpdir): |
| 17 |
"""Test upload_file supports manually specifying file info src_last_modified_millis""" |
| 18 |
filename = 'file1.txt' |
| 19 |
content = 'hello world' |
| 20 |
local_file1 = tmpdir.join('file1.txt') |
| 21 |
local_file1.write(content) |
| 22 |
|
| 23 |
expected_json = { |
| 24 |
"action": "upload", |
| 25 |
"contentSha1": "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed", |
| 26 |
"fileInfo": |
| 27 |
{ |
| 28 |
"b2-cache-control": "max-age=3600", |
| 29 |
"b2-expires": "Thu, 01 Dec 2050 16:00:00 GMT", |
| 30 |
"b2-content-language": "en", |
| 31 |
"b2-content-disposition": "attachment", |
| 32 |
"b2-content-encoding": "gzip", |
| 33 |
"src_last_modified_millis": "1" |
| 34 |
}, |
| 35 |
"fileName": filename, |
| 36 |
"size": len(content), |
| 37 |
} |
| 38 |
b2_cli.run( |
| 39 |
[ |
| 40 |
'upload-file', '--no-progress', '--info=src_last_modified_millis=1', 'my-bucket', |
| 41 |
'--cache-control', 'max-age=3600', '--expires', 'Thu, 01 Dec 2050 16:00:00 GMT', |
| 42 |
'--content-language', 'en', '--content-disposition', 'attachment', '--content-encoding', |
| 43 |
'gzip', |
| 44 |
str(local_file1), 'file1.txt' |
| 45 |
], |
| 46 |
expected_json_in_stdout=expected_json, |
| 47 |
remove_version=True, |
| 48 |
) |
| 49 |
|
| 50 |
|
| 51 |
@skip_on_windows |
| 52 |
def test_upload_file__named_pipe(b2_cli, bucket, tmpdir, bg_executor): |
| 53 |
"""Test upload_file supports named pipes""" |
| 54 |
filename = 'named_pipe.txt' |
| 55 |
content = 'hello world' |
| 56 |
local_file1 = tmpdir.join('file1.txt') |
| 57 |
os.mkfifo(str(local_file1)) |
| 58 |
writer = bg_executor.submit( |
| 59 |
local_file1.write, content |
| 60 |
) # writer will block until content is read |
| 61 |
|
| 62 |
expected_stdout = f'URL by file name: http://download.example.com/file/my-bucket/{filename}' |
| 63 |
expected_json = { |
| 64 |
"action": "upload", |
| 65 |
"contentSha1": "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed", |
| 66 |
"contentType": "b2/x-auto", |
| 67 |
"fileName": filename, |
| 68 |
"size": len(content), |
| 69 |
} |
| 70 |
b2_cli.run( |
| 71 |
['upload-file', '--no-progress', 'my-bucket', |
| 72 |
str(local_file1), filename], |
| 73 |
expected_json_in_stdout=expected_json, |
| 74 |
remove_version=True, |
| 75 |
expected_part_of_stdout=expected_stdout, |
| 76 |
) |
| 77 |
writer.result(timeout=1) |
| 78 |
|
| 79 |
|
| 80 |
def test_upload_file__hyphen_file_instead_of_stdin(b2_cli, bucket, tmpdir, monkeypatch): |
| 81 |
"""Test upload_file will upload file named `-` instead of stdin by default""" |
| 82 |
# TODO remove this in v4 |
| 83 |
assert b2.__version__ < '4', "`-` filename should not be supported in next major version of CLI" |
| 84 |
filename = 'stdin.txt' |
| 85 |
content = "I'm very rare creature, a file named '-'" |
| 86 |
monkeypatch.chdir(str(tmpdir)) |
| 87 |
source_file = tmpdir.join('-') |
| 88 |
source_file.write(content) |
| 89 |
|
| 90 |
expected_stdout = f'URL by file name: http://download.example.com/file/my-bucket/{filename}' |
| 91 |
expected_json = { |
| 92 |
"action": "upload", |
| 93 |
"contentSha1": "ab467567b98216a255f77aef08aa2c418073d974", |
| 94 |
"fileName": filename, |
| 95 |
"size": len(content), |
| 96 |
} |
| 97 |
b2_cli.run( |
| 98 |
['upload-file', '--no-progress', 'my-bucket', '-', filename], |
| 99 |
expected_json_in_stdout=expected_json, |
| 100 |
remove_version=True, |
| 101 |
expected_part_of_stdout=expected_stdout, |
| 102 |
expected_stderr= |
| 103 |
"WARNING: Filename `-` won't be supported in the future and will always be treated as stdin alias.\n", |
| 104 |
) |
| 105 |
|
| 106 |
|
| 107 |
def test_upload_file__stdin(b2_cli, bucket, tmpdir, mock_stdin): |
| 108 |
"""Test upload_file stdin alias support""" |
| 109 |
content = "stdin input" |
| 110 |
filename = 'stdin.txt' |
| 111 |
|
| 112 |
expected_stdout = f'URL by file name: http://download.example.com/file/my-bucket/{filename}' |
| 113 |
expected_json = { |
| 114 |
"action": "upload", |
| 115 |
"contentSha1": "2ce72aa159d1f190fddf295cc883f20c4787a751", |
| 116 |
"fileName": filename, |
| 117 |
"size": len(content), |
| 118 |
} |
| 119 |
mock_stdin.write(content) |
| 120 |
mock_stdin.close() |
| 121 |
|
| 122 |
b2_cli.run( |
| 123 |
['upload-file', '--no-progress', 'my-bucket', '-', filename], |
| 124 |
expected_json_in_stdout=expected_json, |
| 125 |
remove_version=True, |
| 126 |
expected_part_of_stdout=expected_stdout, |
| 127 |
) |
| 128 |
|
| 129 |
|
| 130 |
def test_upload_file__threads_setting(b2_cli, bucket, tmp_path): |
| 131 |
"""Test upload_file supports setting number of threads""" |
| 132 |
num_threads = 66 |
| 133 |
filename = 'file1.txt' |
| 134 |
content = 'hello world' |
| 135 |
local_file1 = tmp_path / 'file1.txt' |
| 136 |
local_file1.write_text(content) |
| 137 |
|
| 138 |
expected_json = { |
| 139 |
"action": "upload", |
| 140 |
"contentSha1": "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed", |
| 141 |
"fileInfo": { |
| 142 |
"src_last_modified_millis": f"{local_file1.stat().st_mtime_ns // 1000000}" |
| 143 |
}, |
| 144 |
"fileName": filename, |
| 145 |
"size": len(content), |
| 146 |
} |
| 147 |
|
| 148 |
b2_cli.run( |
| 149 |
[ |
| 150 |
'upload-file', '--no-progress', 'my-bucket', '--threads', |
| 151 |
str(num_threads), |
| 152 |
str(local_file1), 'file1.txt' |
| 153 |
], |
| 154 |
expected_json_in_stdout=expected_json, |
| 155 |
remove_version=True, |
| 156 |
) |
| 157 |
|
| 158 |
assert b2_cli.console_tool.api.services.upload_manager.get_thread_pool_size() == num_threads |