Files
scylladb/test/cluster/dtest/tools/files.py
Cezar Moise 492b4cf71c test: fix test_one_big_mutation_corrupted_on_startup
The commitlog in the tests with big mutations were corrupted by
overwriting 10 chunks of 1KB with random data, which could not be enough
due to randomness and the big size of the commitlog (~65MB).

Change `corrupt_file` to overwrite based on a percentage
of the file's size instead of fixed number of chunks.

refs: #25627
2025-09-16 15:32:44 +03:00

26 lines
949 B
Python

#
# Copyright (C) 2025-present ScyllaDB
#
# SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
#
import os
import random
def corrupt_file(file_path: str, chunk_size: int = 1024, percentage: float = 0.2) -> None:
"""
Corrupts a file by overwriting random chunks of its content with random data.
Args:
file_path (str): The path to the file to be corrupted.
chunk_size (int, optional): The size of each random data chunk to write, in bytes.
percentage (float, optional): The percentage of the file to corrupt, represented as a float between 0 and 1.
"""
with open(file_path, "r+b") as f:
file_size = os.path.getsize(file_path)
chunks = int(file_size * percentage / chunk_size) or 1
for _ in range(chunks):
random_position = random.randint(0, max(file_size - chunk_size, 0))
f.seek(random_position)
f.write(os.urandom(chunk_size))