| 1 |
###################################################################### |
| 2 |
# |
| 3 |
# File: test/unit/console_tool/test_upload_unbound_stream.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 |
from b2sdk.v2 import DEFAULT_MIN_PART_SIZE |
| 14 |
|
| 15 |
|
| 16 |
@skip_on_windows |
| 17 |
def test_upload_unbound_stream__named_pipe(b2_cli, bucket, tmpdir, bg_executor): |
| 18 |
"""Test upload_unbound_stream supports named pipes""" |
| 19 |
filename = 'named_pipe.txt' |
| 20 |
content = 'hello world' |
| 21 |
fifo_file = tmpdir.join('fifo_file.txt') |
| 22 |
os.mkfifo(str(fifo_file)) |
| 23 |
writer = bg_executor.submit(fifo_file.write, content) # writer will block until content is read |
| 24 |
|
| 25 |
expected_stdout = f'URL by file name: http://download.example.com/file/my-bucket/{filename}' |
| 26 |
expected_json = { |
| 27 |
"action": "upload", |
| 28 |
"contentSha1": "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed", |
| 29 |
"fileName": filename, |
| 30 |
"size": len(content), |
| 31 |
} |
| 32 |
b2_cli.run( |
| 33 |
['upload-unbound-stream', '--no-progress', 'my-bucket', |
| 34 |
str(fifo_file), filename], |
| 35 |
expected_json_in_stdout=expected_json, |
| 36 |
remove_version=True, |
| 37 |
expected_part_of_stdout=expected_stdout, |
| 38 |
) |
| 39 |
writer.result(timeout=1) |
| 40 |
|
| 41 |
|
| 42 |
def test_upload_unbound_stream__stdin(b2_cli, bucket, tmpdir, mock_stdin): |
| 43 |
"""Test upload_unbound_stream stdin alias support""" |
| 44 |
content = "stdin input" |
| 45 |
filename = 'stdin.txt' |
| 46 |
|
| 47 |
expected_stdout = f'URL by file name: http://download.example.com/file/my-bucket/{filename}' |
| 48 |
expected_json = { |
| 49 |
"action": "upload", |
| 50 |
"contentSha1": "2ce72aa159d1f190fddf295cc883f20c4787a751", |
| 51 |
"fileName": filename, |
| 52 |
"size": len(content), |
| 53 |
} |
| 54 |
mock_stdin.write(content) |
| 55 |
mock_stdin.close() |
| 56 |
|
| 57 |
b2_cli.run( |
| 58 |
['upload-unbound-stream', '--no-progress', 'my-bucket', '-', filename], |
| 59 |
expected_json_in_stdout=expected_json, |
| 60 |
remove_version=True, |
| 61 |
expected_part_of_stdout=expected_stdout, |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
@skip_on_windows |
| 66 |
def test_upload_unbound_stream__with_part_size_options( |
| 67 |
b2_cli, bucket, tmpdir, mock_stdin, bg_executor |
| 68 |
): |
| 69 |
"""Test upload_unbound_stream with part size options""" |
| 70 |
part_size = DEFAULT_MIN_PART_SIZE |
| 71 |
expected_size = part_size + 500 # has to be bigger to force multipart upload |
| 72 |
|
| 73 |
filename = 'named_pipe.txt' |
| 74 |
fifo_file = tmpdir.join('fifo_file.txt') |
| 75 |
os.mkfifo(str(fifo_file)) |
| 76 |
writer = bg_executor.submit( |
| 77 |
lambda: fifo_file.write("x" * expected_size) |
| 78 |
) # writer will block until content is read |
| 79 |
|
| 80 |
expected_stdout = f'URL by file name: http://download.example.com/file/my-bucket/{filename}' |
| 81 |
expected_json = { |
| 82 |
"action": "upload", |
| 83 |
"fileName": filename, |
| 84 |
"size": expected_size, |
| 85 |
} |
| 86 |
|
| 87 |
b2_cli.run( |
| 88 |
[ |
| 89 |
'upload-unbound-stream', |
| 90 |
'--min-part-size', |
| 91 |
str(DEFAULT_MIN_PART_SIZE), |
| 92 |
'--part-size', |
| 93 |
str(part_size), |
| 94 |
'--no-progress', |
| 95 |
'my-bucket', |
| 96 |
str(fifo_file), |
| 97 |
filename, |
| 98 |
], |
| 99 |
expected_json_in_stdout=expected_json, |
| 100 |
remove_version=True, |
| 101 |
expected_part_of_stdout=expected_stdout, |
| 102 |
) |
| 103 |
writer.result(timeout=1) |
| 104 |
|
| 105 |
|
| 106 |
def test_upload_unbound_stream__regular_file(b2_cli, bucket, tmpdir): |
| 107 |
"""Test upload_unbound_stream regular file support""" |
| 108 |
content = "stdin input" |
| 109 |
filename = 'file.txt' |
| 110 |
filepath = tmpdir.join(filename) |
| 111 |
filepath.write(content) |
| 112 |
|
| 113 |
expected_stdout = f'URL by file name: http://download.example.com/file/my-bucket/{filename}' |
| 114 |
expected_json = { |
| 115 |
"action": "upload", |
| 116 |
"contentSha1": "2ce72aa159d1f190fddf295cc883f20c4787a751", |
| 117 |
"fileName": filename, |
| 118 |
"size": len(content), |
| 119 |
} |
| 120 |
|
| 121 |
b2_cli.run( |
| 122 |
['upload-unbound-stream', '--no-progress', 'my-bucket', |
| 123 |
str(filepath), filename], |
| 124 |
expected_json_in_stdout=expected_json, |
| 125 |
remove_version=True, |
| 126 |
expected_part_of_stdout=expected_stdout, |
| 127 |
expected_stderr= |
| 128 |
"WARNING: You are using a stream upload command to upload a regular file. While it will work, it is inefficient. Use of upload-file command is recommended.\n", |
| 129 |
) |