From 028371756ffebf12a6e54ad0512d3da906710756 Mon Sep 17 00:00:00 2001 From: Oussama Jarrousse Date: Wed, 15 May 2024 18:27:52 +0200 Subject: [PATCH 1/2] fixed fget_object behaviour --- pytest_minio_mock/plugin.py | 15 +++++++++++++++ tests/test_minio_mock.py | 23 +++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/pytest_minio_mock/plugin.py b/pytest_minio_mock/plugin.py index 39810ff..5eab6b5 100644 --- a/pytest_minio_mock/plugin.py +++ b/pytest_minio_mock/plugin.py @@ -23,6 +23,7 @@ """ import copy import datetime +import errno import io import logging import os @@ -860,6 +861,20 @@ def fget_object( request_headers=request_headers, extra_query_params=extra_query_params, ) + + if os.path.isdir(file_path): + raise ValueError(f"file {file_path} is a directory") + + # Create top level directory if needed. + + dirname = os.path.dirname(file_path) + if dirname: + try: + os.makedirs(dirname) + except OSError as exc: # Python >2.5 + if exc.errno != errno.EEXIST: + raise + with open(file_path, "wb") as f: f.write(the_object.data) diff --git a/tests/test_minio_mock.py b/tests/test_minio_mock.py index 0395781..6ed7f65 100644 --- a/tests/test_minio_mock.py +++ b/tests/test_minio_mock.py @@ -1,3 +1,4 @@ +import os import sys import pytest @@ -493,3 +494,25 @@ def test_stat_object(minio_mock): version_id=objects[1].version_id, ) assert object_stat.version_id is None + + +@pytest.mark.FOCUS +@pytest.mark.REGRESSION +@pytest.mark.UNIT +def test_fget_object(minio_mock, tmp_path): + client = Minio("http://local.host:9000") + + bucket_name = "new-bucket" + client.make_bucket(bucket_name) + objects = client.list_objects(bucket_name) + assert len(list(objects)) == 0 + + client.put_object(bucket_name, "object1", data=b"object1 data", length=12) + + file_path = os.path.join(tmp_path, "object1.dat") + client.fget_object(bucket_name, "object1", file_path) + + # folder objects does not exist, fget_object should create it + + file_path = os.path.join(tmp_path, "another_folder", "object1.dat") + client.fget_object(bucket_name, "object1", file_path) From 55a99a71992205f3c82da21411278df630262be9 Mon Sep 17 00:00:00 2001 From: Oussama Jarrousse Date: Thu, 16 May 2024 13:39:03 +0200 Subject: [PATCH 2/2] fixed issue 31 --- pytest_minio_mock/plugin.py | 17 ++++++++++++++--- tests/test_minio_mock.py | 16 +++++++++++++--- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/pytest_minio_mock/plugin.py b/pytest_minio_mock/plugin.py index 5eab6b5..0dcfb4a 100644 --- a/pytest_minio_mock/plugin.py +++ b/pytest_minio_mock/plugin.py @@ -822,7 +822,7 @@ def fget_object( object_name, file_path, request_headers=None, - sse=None, + ssec=None, version_id=None, extra_query_params=None, ): @@ -852,13 +852,22 @@ def fget_object( IOError: If there's an issue writing to the specified file path. Returns: - None: The method writes the object's data to a file and has no return value. + Object: Stat of the object as Object. """ + + stat = self.stat_object( + bucket_name, + object_name, + ssec, + version_id, + ) + the_object = self.get_object( bucket_name, object_name, - version_id=version_id, request_headers=request_headers, + ssec=ssec, + version_id=version_id, extra_query_params=extra_query_params, ) @@ -878,6 +887,8 @@ def fget_object( with open(file_path, "wb") as f: f.write(the_object.data) + return stat + def get_object( self, bucket_name, diff --git a/tests/test_minio_mock.py b/tests/test_minio_mock.py index 6ed7f65..b6f6034 100644 --- a/tests/test_minio_mock.py +++ b/tests/test_minio_mock.py @@ -6,6 +6,7 @@ from minio import Minio from minio.commonconfig import ENABLED from minio.datatypes import Bucket +from minio.datatypes import Object from minio.error import S3Error from minio.versioningconfig import OFF from minio.versioningconfig import SUSPENDED @@ -496,7 +497,6 @@ def test_stat_object(minio_mock): assert object_stat.version_id is None -@pytest.mark.FOCUS @pytest.mark.REGRESSION @pytest.mark.UNIT def test_fget_object(minio_mock, tmp_path): @@ -509,10 +509,20 @@ def test_fget_object(minio_mock, tmp_path): client.put_object(bucket_name, "object1", data=b"object1 data", length=12) + file_path = tmp_path # should raise a Value error + with pytest.raises(ValueError): + _ = client.fget_object(bucket_name, "object1", file_path) + file_path = os.path.join(tmp_path, "object1.dat") - client.fget_object(bucket_name, "object1", file_path) + stat = client.fget_object(bucket_name, "object1", file_path) + assert isinstance(stat, Object) # folder objects does not exist, fget_object should create it + file_path = os.path.join(tmp_path, "another_folder", "object1.dat") + stat = client.fget_object(bucket_name, "object1", file_path) + assert isinstance(stat, Object) + # folder objects does not exist, fget_object should create it file_path = os.path.join(tmp_path, "another_folder", "object1.dat") - client.fget_object(bucket_name, "object1", file_path) + stat = client.fget_object(bucket_name, "object1", file_path) + assert isinstance(stat, Object)