Fix delete all objects in bucket

Signed-off-by: anikeev-yadro <a.anikeev@yadro.com>
This commit is contained in:
anikeev-yadro 2022-12-12 13:34:52 +03:00 committed by anikeev-yadro
parent 3497f3b23a
commit 2f04775fce
2 changed files with 55 additions and 2 deletions

View file

@ -88,15 +88,32 @@ class TestS3GateBase(ClusterTestBase):
def delete_all_object_in_bucket(self, bucket): def delete_all_object_in_bucket(self, bucket):
versioning_status = s3_gate_bucket.get_bucket_versioning_status(self.s3_client, bucket) versioning_status = s3_gate_bucket.get_bucket_versioning_status(self.s3_client, bucket)
if versioning_status == s3_gate_bucket.VersioningStatus.ENABLED.value: if versioning_status == s3_gate_bucket.VersioningStatus.ENABLED.value:
# From versioned bucket we should delete all versions of all objects # From versioned bucket we should delete all versions and delete markers of all objects
objects_versions = s3_gate_object.list_objects_versions_s3(self.s3_client, bucket) objects_versions = s3_gate_object.list_objects_versions_s3(self.s3_client, bucket)
if objects_versions: if objects_versions:
s3_gate_object.delete_object_versions_s3(self.s3_client, bucket, objects_versions) s3_gate_object.delete_object_versions_s3_without_dm(
self.s3_client, bucket, objects_versions
)
objects_delete_markers = s3_gate_object.list_objects_delete_markers_s3(
self.s3_client, bucket
)
if objects_delete_markers:
s3_gate_object.delete_object_versions_s3_without_dm(
self.s3_client, bucket, objects_delete_markers
)
else: else:
# From non-versioned bucket it's sufficient to delete objects by key # From non-versioned bucket it's sufficient to delete objects by key
objects = s3_gate_object.list_objects_s3(self.s3_client, bucket) objects = s3_gate_object.list_objects_s3(self.s3_client, bucket)
if objects: if objects:
s3_gate_object.delete_objects_s3(self.s3_client, bucket, objects) s3_gate_object.delete_objects_s3(self.s3_client, bucket, objects)
objects_delete_markers = s3_gate_object.list_objects_delete_markers_s3(
self.s3_client, bucket
)
if objects_delete_markers:
s3_gate_object.delete_object_versions_s3_without_dm(
self.s3_client, bucket, objects_delete_markers
)
# Delete the bucket itself # Delete the bucket itself
s3_gate_bucket.delete_bucket_s3(self.s3_client, bucket) s3_gate_bucket.delete_bucket_s3(self.s3_client, bucket)

View file

@ -85,6 +85,21 @@ def list_objects_versions_s3(s3_client, bucket: str, full_output: bool = False)
) from err ) from err
@allure.step("List objects delete markers S3")
def list_objects_delete_markers_s3(s3_client, bucket: str, full_output: bool = False) -> list:
try:
response = s3_client.list_object_versions(Bucket=bucket)
delete_markers = response.get("DeleteMarkers", [])
log_command_execution("S3 List objects delete markers result", response)
return response if full_output else delete_markers
except ClientError as err:
raise Exception(
f'Error Message: {err.response["Error"]["Message"]}\n'
f'Http status code: {err.response["ResponseMetadata"]["HTTPStatusCode"]}'
) from err
@allure.step("Put object S3") @allure.step("Put object S3")
def put_object_s3(s3_client, bucket: str, filepath: str, **kwargs): def put_object_s3(s3_client, bucket: str, filepath: str, **kwargs):
filename = os.path.basename(filepath) filename = os.path.basename(filepath)
@ -185,6 +200,27 @@ def delete_object_versions_s3(s3_client, bucket: str, object_versions: list):
) from err ) from err
@allure.step("Delete object versions S3 without delete markers")
def delete_object_versions_s3_without_dm(s3_client, bucket: str, object_versions: list):
try:
# Delete objects without creating delete markers
for object_version in object_versions:
params = {
"Bucket": bucket,
"Key": object_version["Key"],
"VersionId": object_version["VersionId"],
}
response = s3_client.delete_object(**params)
log_command_execution("S3 Delete object result", response)
return response
except ClientError as err:
raise Exception(
f'Error Message: {err.response["Error"]["Message"]}\n'
f'Http status code: {err.response["ResponseMetadata"]["HTTPStatusCode"]}'
) from err
@allure.step("Put object ACL") @allure.step("Put object ACL")
def put_object_acl_s3( def put_object_acl_s3(
s3_client, s3_client,