Skip to content

OsOps::read_binary is updated (offset) #173

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions testgres/operations/local_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,15 @@ def readlines(self, filename, num_lines=0, binary=False, encoding=None):
buffers * max(2, int(num_lines / max(cur_lines, 1)))
) # Adjust buffer size

def read_binary(self, filename, start_pos):
def read_binary(self, filename, offset):
assert type(filename) == str # noqa: E721
assert type(start_pos) == int # noqa: E721
assert start_pos >= 0
assert type(offset) == int # noqa: E721

if offset < 0:
raise ValueError("Negative 'offset' is not supported.")

with open(filename, 'rb') as file: # open in a binary mode
file.seek(start_pos, os.SEEK_SET)
file.seek(offset, os.SEEK_SET)
r = file.read()
assert type(r) == bytes # noqa: E721
return r
Expand Down
6 changes: 3 additions & 3 deletions testgres/operations/os_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ def read(self, filename, encoding, binary):
def readlines(self, filename):
raise NotImplementedError()

def read_binary(self, filename, start_pos):
def read_binary(self, filename, offset):
assert type(filename) == str # noqa: E721
assert type(start_pos) == int # noqa: E721
assert start_pos >= 0
assert type(offset) == int # noqa: E721
assert offset >= 0
raise NotImplementedError()

def isfile(self, remote_file):
Expand Down
10 changes: 6 additions & 4 deletions testgres/operations/remote_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,14 @@ def readlines(self, filename, num_lines=0, binary=False, encoding=None):

return lines

def read_binary(self, filename, start_pos):
def read_binary(self, filename, offset):
assert type(filename) == str # noqa: E721
assert type(start_pos) == int # noqa: E721
assert start_pos >= 0
assert type(offset) == int # noqa: E721

cmd = ["tail", "-c", "+{}".format(start_pos + 1), filename]
if offset < 0:
raise ValueError("Negative 'offset' is not supported.")

cmd = ["tail", "-c", "+{}".format(offset + 1), filename]
r = self.exec_command(cmd)
assert type(r) == bytes # noqa: E721
return r
Expand Down
10 changes: 10 additions & 0 deletions tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ def test_read_binary__spec__unk_file(self):
match=re.escape("[Errno 2] No such file or directory: '/dummy'")):
self.operations.read_binary("/dummy", 0)

def test_read_binary__spec__negative_offset(self):
"""
Test LocalOperations::read_binary with negative offset.
"""

with pytest.raises(
ValueError,
match=re.escape("Negative 'offset' is not supported.")):
self.operations.read_binary(__file__, -1)

def test_get_file_size(self):
"""
Test LocalOperations::get_file_size.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,16 @@ def test_read_binary__spec__unk_file(self):
with pytest.raises(ExecUtilException, match=re.escape("tail: cannot open '/dummy' for reading: No such file or directory")):
self.operations.read_binary("/dummy", 0)

def test_read_binary__spec__negative_offset(self):
"""
Test RemoteOperations::read_binary with negative offset.
"""

with pytest.raises(
ValueError,
match=re.escape("Negative 'offset' is not supported.")):
self.operations.read_binary(__file__, -1)

def test_get_file_size(self):
"""
Test RemoteOperations::get_file_size.
Expand Down