diff --git a/testgres/operations/local_ops.py b/testgres/operations/local_ops.py index c88c16ca..8bdb22cd 100644 --- a/testgres/operations/local_ops.py +++ b/testgres/operations/local_ops.py @@ -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 diff --git a/testgres/operations/os_ops.py b/testgres/operations/os_ops.py index d644509a..35525b3c 100644 --- a/testgres/operations/os_ops.py +++ b/testgres/operations/os_ops.py @@ -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): diff --git a/testgres/operations/remote_ops.py b/testgres/operations/remote_ops.py index c0307195..2f34ecec 100644 --- a/testgres/operations/remote_ops.py +++ b/testgres/operations/remote_ops.py @@ -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 diff --git a/tests/test_local.py b/tests/test_local.py index 47a63994..d7adce17 100644 --- a/tests/test_local.py +++ b/tests/test_local.py @@ -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. diff --git a/tests/test_remote.py b/tests/test_remote.py index 7421ca3a..7071a9d9 100755 --- a/tests/test_remote.py +++ b/tests/test_remote.py @@ -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.