Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions bzt/bza.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,19 +559,25 @@ def upload_files(self, taurus_config, resource_files):
chunk_size = 50
failed_files = []

for chunk_idx in range(0, len(resource_files), chunk_size):
chunk = resource_files[chunk_idx:chunk_idx + chunk_size]
if not resource_files:
body = MultiPartForm()
if chunk_idx == 0:
body.add_file_as_string('script', 'taurus.yml', taurus_config)
for idx, rfile in enumerate(chunk):
body.add_file(f'files[{idx}]', rfile)
body.add_file_as_string('script', 'taurus.yml', taurus_config)
hdr = {"Content-Type": str(body.get_content_type())}
try:
self._request(url, body.form_as_bytes(), headers=hdr)
except Exception as e:
self.log.error(f"Error uploading chunk {chunk_idx}: {e}")
failed_files.extend(chunk)
self._request(url, body.form_as_bytes(), headers=hdr)
else:
for chunk_idx in range(0, len(resource_files), chunk_size):
chunk = resource_files[chunk_idx:chunk_idx + chunk_size]
body = MultiPartForm()
if chunk_idx == 0:
body.add_file_as_string('script', 'taurus.yml', taurus_config)
for idx, rfile in enumerate(chunk):
body.add_file(f'files[{idx}]', rfile)
hdr = {"Content-Type": str(body.get_content_type())}
try:
self._request(url, body.form_as_bytes(), headers=hdr)
except Exception as e:
self.log.error(f"Error uploading chunk {chunk_idx}: {e}")
failed_files.extend(chunk)

if failed_files:
error_msg = f"Failed to upload files: {failed_files}"
Expand Down
22 changes: 21 additions & 1 deletion tests/unit/test_bza.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,24 @@ def test_upload_files_logs_error_and_returns_failed_files(self, MockMultiPartFor
test_obj.log.error.assert_any_call("Error uploading chunk 0: Upload error")
test_obj.log.error.assert_any_call("Error uploading chunk 50: Upload error")
self.assertIn("Failed to upload files:", error_msg)
self.assertTrue(all(f'file_{i}' in error_msg for i in range(60)))
self.assertTrue(all(f'file_{i}' in error_msg for i in range(60)))

@patch('bzt.bza.MultiPartForm')
def test_upload_files_uploads_taurus_yml_when_no_resource_files(self, MockMultiPartForm):
mock_form = MagicMock()
MockMultiPartForm.return_value = mock_form
mock_form.get_content_type.return_value = 'multipart/form-data'
mock_form.form_as_bytes.return_value = b'data'

test_obj = Test()
test_obj['id'] = '123'
test_obj.address = 'http://fake'
test_obj.log = MagicMock()
test_obj._request = MagicMock()

taurus_config = 'config'
resource_files = []
test_obj.upload_files(taurus_config, resource_files)

mock_form.add_file_as_string.assert_called_once_with('script', 'taurus.yml', 'config')
test_obj._request.assert_called_once()