Skip to content

Commit

Permalink
support error for single shot upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Tulsishah committed Oct 27, 2024
1 parent 79cfcab commit 3307b5b
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
16 changes: 16 additions & 0 deletions testbench/rest_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,22 @@ def object_insert(bucket_name):
blob, projection = gcs_type.object.Object.init_media(flask.request, bucket)
elif upload_type == "multipart":
blob, projection = gcs_type.object.Object.init_multipart(flask.request, bucket)
testbench.common.extract_instruction(request, context=None)
(
error_code,
after_bytes,
test_id,
) = testbench.common.get_retry_uploads_error_after_bytes(db, request)
if (error_code and len(blob.media) >= after_bytes):
if test_id:
db.dequeue_next_instruction(test_id, "storage.objects.insert")
testbench.error.generic(
"Fault injected during a resumable upload",
rest_code=error_code,
grpc_code=None,
context=None,
)

# Handle stall for full uploads.
testbench.common.extract_instruction(request, context=None)
(
Expand Down
110 changes: 110 additions & 0 deletions tests/test_testbench_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,64 @@ def test_write_retry_test_stall_single_shot(self):
self.assertLess(elapsed_time, 1)
self.assertEqual(response.status_code, 200)

def test_write_retry_test_error_single_shot(self):
# Create a new bucket
response = self.client.post(
"/storage/v1/b", data=json.dumps({"name": "bucket-name"})
)
self.assertEqual(response.status_code, 200)

# Setup a stall for reading back the object.
response = self.client.post(
"/retry_test",
data=json.dumps(
{
"instructions": {
"storage.objects.insert": [
"return-503-after-250K",
]
}
}
),
content_type="application/json",
)
self.assertEqual(response.status_code, 200)
self.assertTrue(
response.headers.get("content-type").startswith("application/json")
)

create_rest = json.loads(response.data)
self.assertIn("id", create_rest)
test_id = create_rest.get("id")

# Upload the 256KiB of data and trigger the stall.
data = self._create_block(UPLOAD_QUANTUM)
self.assertEqual(len(data), UPLOAD_QUANTUM)

boundary, payload = format_multipart_upload({}, data)
response = self.client.post(
"/upload/storage/v1/b/bucket-name/o",
query_string={"uploadType": "multipart", "name": "stall"},
content_type="multipart/related; boundary=" + boundary,
headers={
"x-retry-test-id": test_id,
},
data=payload,
)
self.assertEqual(response.status_code, 503)

# Upload the data again and check that stall not happen.
response = self.client.post(
"/upload/storage/v1/b/bucket-name/o",
query_string={"uploadType": "multipart", "name": "stall"},
content_type="multipart/related; boundary=" + boundary,
headers={
"x-retry-test-id": test_id,
},
data=payload,
)
self.assertEqual(response.status_code, 200)

def test_write_retry_test_stall_single_shot_while_upload_size_less_than_stall_size(
self,
):
Expand Down Expand Up @@ -880,6 +938,58 @@ def test_write_retry_test_stall_single_shot_while_upload_size_less_than_stall_si
self.assertEqual(response.status_code, 200)
self.assertLess(elapsed_time, 1)

def test_write_retry_error_single_shot_while_upload_size_less_than_size(
self,
):
# Create a new bucket
response = self.client.post(
"/storage/v1/b", data=json.dumps({"name": "bucket-name"})
)
self.assertEqual(response.status_code, 200)

# Setup a error for reading back the object.
response = self.client.post(
"/retry_test",
data=json.dumps(
{
"instructions": {
"storage.objects.insert": [
"return-503-after-250K",
]
}
}
),
content_type="application/json",
)
self.assertEqual(response.status_code, 200)
self.assertTrue(
response.headers.get("content-type").startswith("application/json")
)

create_rest = json.loads(response.data)
self.assertIn("id", create_rest)
test_id = create_rest.get("id")

# Upload the 200KiB of data and check error not happen.
data = self._create_block(200 * 1024)
self.assertEqual(len(data), 200 * 1024)

start_time = time.perf_counter()
boundary, payload = format_multipart_upload({}, data)
response = self.client.post(
"/upload/storage/v1/b/bucket-name/o",
query_string={"uploadType": "multipart", "name": "error"},
content_type="multipart/related; boundary=" + boundary,
headers={
"x-retry-test-id": test_id,
},
data=payload,
)
end_time = time.perf_counter()
elapsed_time = end_time - start_time
self.assertEqual(response.status_code, 200)
self.assertLess(elapsed_time, 1)


class TestTestbenchRetryGrpc(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit 3307b5b

Please sign in to comment.