Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add mutation test case #598

Merged
merged 2 commits into from
Feb 27, 2024
Merged
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
7 changes: 5 additions & 2 deletions gcs/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,9 @@ def init_write_object_grpc(cls, db, request_iterator, context):
upload.complete = True
continue
else:
print("WARNING unexpected data field %s\n" % data)
continue
testbench.error.invalid("Invalid data field in upload", context)
return None, False

content = checksummed_data.content
crc32c_hash = (
checksummed_data.crc32c if checksummed_data.HasField("crc32c") else None
Expand Down Expand Up @@ -330,6 +331,8 @@ def process_bidi_write_object_grpc(cls, db, request_iterator, context):
# Handles final message with no data to insert.
upload.complete = True
continue
else:
return testbench.error.invalid("Invalid data field in upload", context)

content = checksummed_data.content
crc32c_hash = (
Expand Down
62 changes: 62 additions & 0 deletions tests/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,37 @@ def test_init_object_write_grpc_final_message_empty_data(self):
self.assertEqual(upload.metadata.name, "object")
self.assertEqual(upload.metadata.bucket, "projects/_/buckets/bucket-name")

def test_init_object_write_grpc_message_invalid_data(self):
request = testbench.common.FakeRequest(
args={}, data=json.dumps({"name": "bucket-name"})
)
bucket, _ = gcs.bucket.Bucket.init(request, None)
request = storage_pb2.StartResumableWriteRequest(
write_object_spec=storage_pb2.WriteObjectSpec(
resource={"name": "object", "bucket": "projects/_/buckets/bucket-name"}
)
)
context = self.mock_context()
upload = gcs.upload.Upload.init_resumable_grpc(
request, bucket.metadata, context
)

r1 = storage_pb2.WriteObjectRequest(
upload_id=upload.upload_id,
write_offset=0,
finish_write=False,
)

context = self.mock_context()
db = unittest.mock.Mock()
db.get_bucket = unittest.mock.MagicMock(return_value=bucket)
db.get_upload = unittest.mock.MagicMock(return_value=upload)
upload, _ = gcs.upload.Upload.init_write_object_grpc(db, [r1], context)
self.assertIsNone(upload)
context.abort.assert_called_once_with(
grpc.StatusCode.INVALID_ARGUMENT, unittest.mock.ANY
)

def test_process_bidi_write_grpc_resumable(self):
request = testbench.common.FakeRequest(
args={}, data=json.dumps({"name": "bucket-name"})
Expand Down Expand Up @@ -989,6 +1020,37 @@ def test_process_bidi_write_grpc_final_message_empty_data(self):
blob = responses[0].resource
self.assertEqual(blob.name, "object")
self.assertEqual(blob.bucket, "projects/_/buckets/bucket-name")
self.assertTrue(upload.complete)

def test_process_bidi_write_grpc_message_invalid_data(self):
request = testbench.common.FakeRequest(
args={}, data=json.dumps({"name": "bucket-name"})
)
bucket, _ = gcs.bucket.Bucket.init(request, None)
request = storage_pb2.StartResumableWriteRequest(
write_object_spec=storage_pb2.WriteObjectSpec(
resource={"name": "object", "bucket": "projects/_/buckets/bucket-name"}
)
)
context = self.mock_context()
upload = gcs.upload.Upload.init_resumable_grpc(
request, bucket.metadata, context
)

r1 = storage_pb2.BidiWriteObjectRequest(
upload_id=upload.upload_id,
write_offset=0,
finish_write=False,
)
context = self.mock_context()
db = unittest.mock.Mock()
db.get_bucket = unittest.mock.MagicMock(return_value=bucket)
db.get_upload = unittest.mock.MagicMock(return_value=upload)
list(gcs.upload.Upload.process_bidi_write_object_grpc(db, [r1], context))
context.abort.assert_called_once_with(
grpc.StatusCode.INVALID_ARGUMENT, unittest.mock.ANY
)
self.assertFalse(upload.complete)


if __name__ == "__main__":
Expand Down