From f47a9d09ecd5ba23b14b56796ca5794ed978fa04 Mon Sep 17 00:00:00 2001 From: anikeev-yadro Date: Tue, 25 Oct 2022 10:34:03 +0300 Subject: [PATCH] Fix for object range content tests Signed-off-by: anikeev-yadro --- pytest_tests/helpers/file_helper.py | 16 ++++++++++++++-- .../testsuites/object/test_object_api.py | 10 ++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/pytest_tests/helpers/file_helper.py b/pytest_tests/helpers/file_helper.py index 5cce4602..c036b7f4 100644 --- a/pytest_tests/helpers/file_helper.py +++ b/pytest_tests/helpers/file_helper.py @@ -78,6 +78,9 @@ def get_file_hash(file_path: str, len: Optional[int] = None, offset: Optional[in elif len and offset: out.seek(offset, 0) file_hash.update(out.read(len)) + elif offset and not len: + out.seek(offset, 0) + file_hash.update(out.read()) else: file_hash.update(out.read()) return file_hash.hexdigest() @@ -133,7 +136,9 @@ def split_file(file_path: str, parts: int) -> list[str]: return part_file_paths -def get_file_content(file_path: str, content_len: Optional[int] = None, mode: str = "r") -> Any: +def get_file_content( + file_path: str, content_len: Optional[int] = None, mode: str = "r", offset: Optional[int] = None +) -> Any: """Returns content of specified file. Args: @@ -141,13 +146,20 @@ def get_file_content(file_path: str, content_len: Optional[int] = None, mode: st content_len: Limit of content length. If None, then entire file content is returned; otherwise only the first content_len bytes of the content are returned. mode: Mode of opening the file. + offset: Position to start reading from. Returns: Content of the specified file. """ with open(file_path, mode) as file: - if content_len: + if content_len and not offset: content = file.read(content_len) + elif content_len and offset: + file.seek(offset, 0) + content = file.read(content_len) + elif offset and not content_len: + file.seek(offset, 0) + content = file.read() else: content = file.read() diff --git a/pytest_tests/testsuites/object/test_object_api.py b/pytest_tests/testsuites/object/test_object_api.py index 163b6e02..c2251793 100644 --- a/pytest_tests/testsuites/object/test_object_api.py +++ b/pytest_tests/testsuites/object/test_object_api.py @@ -45,8 +45,9 @@ def test_object_api(prepare_wallet_and_deposit, client_shell, request, object_si file_usr_header = {"key1": 1, "key2": "abc", "common_key": "common_value"} file_usr_header_oth = {"key1": 2, "common_key": "common_value"} common_header = {"common_key": "common_value"} + range_offset = 0 range_len = 10 - range_cut = f"0:{range_len}" + range_cut = f"{range_offset}:{range_len}" file_path = generate_file(object_size) file_hash = get_file_hash(file_path) @@ -98,21 +99,22 @@ def test_object_api(prepare_wallet_and_deposit, client_shell, request, object_si **wallet_cid, oid=oids[0], shell=client_shell, range_cut=range_cut ) assert ( - get_file_hash(file_path, range_len) == range_hash + get_file_hash(file_path, range_len, range_offset) == range_hash ), f"Expected range hash to match {range_cut} slice of file payload" range_hash = get_range_hash( **wallet_cid, oid=oids[1], shell=client_shell, range_cut=range_cut ) assert ( - get_file_hash(file_path, range_len) == range_hash + get_file_hash(file_path, range_len, range_offset) == range_hash ), f"Expected range hash to match {range_cut} slice of file payload" _, range_content = get_range( **wallet_cid, oid=oids[1], shell=client_shell, range_cut=range_cut ) assert ( - get_file_content(file_path, content_len=range_len, mode="rb") == range_content + get_file_content(file_path, content_len=range_len, mode="rb", offset=range_offset) + == range_content ), f"Expected range content to match {range_cut} slice of file payload" with allure.step("Search objects"):