Skip to content

Commit

Permalink
Merge pull request #24 from vanatteveldt/remove_bucket
Browse files Browse the repository at this point in the history
Changes to (mostly) the bucket interface
  • Loading branch information
oussjarrousse authored Apr 26, 2024
2 parents b7d5f15 + 8b33d20 commit 1eb20b6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
43 changes: 37 additions & 6 deletions pytest_minio_mock/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import validators
from minio import Minio
from minio.commonconfig import ENABLED
from minio.datatypes import Object
from minio.datatypes import Object, Bucket
from minio.error import S3Error
from minio.versioningconfig import OFF
from minio.versioningconfig import SUSPENDED
Expand Down Expand Up @@ -386,6 +386,7 @@ def __init__(
self._objects = {}
self._location = location
self._object_lock = object_lock
self._creation_date = datetime.datetime.now()

@property
def bucket_name(self):
Expand Down Expand Up @@ -511,7 +512,8 @@ def list_objects(
if prefix is None:
prefix = ""

for object_name, obj in self.objects.items():
# Note: Wrapped items() in list(.) to allow modification during iteration
for object_name, obj in list(self.objects.items()):
if object_name.startswith(prefix) and (
not start_after or object_name > start_after
):
Expand Down Expand Up @@ -1073,10 +1075,7 @@ def list_buckets(self):
"""
try:
self._health_check()
buckets_list = []
for bucket_name in self.buckets.keys():
buckets_list.append(bucket_name)
return buckets_list
return [Bucket(name, bucket._creation_date) for (name, bucket) in list(self.buckets.items())]
except Exception as e:
logging.error(e)
raise e
Expand Down Expand Up @@ -1126,6 +1125,38 @@ def make_bucket(self, bucket_name, location=None, object_lock=False):
)
return True

def remove_bucket(self, bucket_name: str):
"""
Remove an empty bucket.
:param bucket_name: Name of the bucket.
Example::
client.remove_bucket("my-bucket")
"""
self._health_check()
if bucket_name not in self.buckets:
raise S3Error(
code="NoSuchBucket",
message="The specified bucket does not exist",
resource=f"/{bucket_name}",
request_id=None,
host_id=None,
response="mocked_response",
bucket_name=bucket_name,
)
if self.buckets[bucket_name].objects:
raise S3Error(
code="BucketNotEmpty",
message="The bucket you tried to delete is not empty",
resource=f"/{bucket_name}",
request_id=None,
host_id=None,
response="mocked_response",
bucket_name=bucket_name,
)
del self.buckets[bucket_name]

def set_bucket_versioning(self, bucket_name: str, config: VersioningConfig):
"""Bucket versioning can be set to ENABLED or SUSPENDED, but not to
OFF (filtered out by VersioningConfig itself)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_minio_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from minio.versioningconfig import OFF
from minio.versioningconfig import SUSPENDED
from minio.versioningconfig import VersioningConfig
from minio.datatypes import Bucket

from pytest_minio_mock.plugin import MockMinioBucket
from pytest_minio_mock.plugin import MockMinioObject
Expand Down Expand Up @@ -63,6 +64,21 @@ def test_make_bucket(minio_mock):
assert client.bucket_exists(bucket_name), "Bucket should exist after creation"


@pytest.mark.UNIT
@pytest.mark.API
def test_remove_bucket(minio_mock):
client = Minio("http://local.host:9000")
original_buckets = client.list_buckets()
n = len(original_buckets)
bucket_name = "new-bucket"
client.make_bucket(bucket_name)
buckets = client.list_buckets()
assert len(buckets) == n + 1
client.remove_bucket(bucket_name)
buckets = client.list_buckets()
assert buckets == original_buckets


@pytest.mark.API
@pytest.mark.FUNC
def test_putting_and_removing_objects_no_versionning(minio_mock):
Expand Down Expand Up @@ -369,6 +385,8 @@ def test_list_buckets(minio_mock):
client.make_bucket(bucket_name)
buckets = client.list_buckets()
assert len(buckets) == n + 1
assert "new-bucket" in {b.name for b in buckets}
assert all(isinstance(b, Bucket) for b in buckets)


@pytest.mark.REGRESSION
Expand Down

0 comments on commit 1eb20b6

Please sign in to comment.