[#133] Change reporter usage

Signed-off-by: Andrey Berezin <a.berezin@yadro.com>
This commit is contained in:
Andrey Berezin 2023-11-29 15:27:17 +03:00
parent 39a17f3634
commit dc6b0e407f
37 changed files with 478 additions and 678 deletions

View file

@ -6,7 +6,7 @@ from datetime import datetime
from time import sleep
from typing import Literal, Optional, Union
from frostfs_testlib.reporter import get_reporter
from frostfs_testlib import reporter
from frostfs_testlib.resources.common import ASSETS_DIR, MAX_REQUEST_ATTEMPTS, RETRY_MODE, S3_SYNC_WAIT_TIME
from frostfs_testlib.s3.interfaces import S3ClientWrapper, VersioningStatus, _make_objs_dict
from frostfs_testlib.shell import CommandOptions
@ -15,7 +15,6 @@ from frostfs_testlib.shell.local_shell import LocalShell
# TODO: Refactor this code to use shell instead of _cmd_run
from frostfs_testlib.utils.cli_utils import _configure_aws_cli
reporter = get_reporter()
logger = logging.getLogger("NeoLogger")
command_options = CommandOptions(timeout=480)
@ -28,8 +27,10 @@ class AwsCliClient(S3ClientWrapper):
common_flags = "--no-verify-ssl --no-paginate"
s3gate_endpoint: str
@reporter.step_deco("Configure S3 client (aws cli)")
def __init__(self, access_key_id: str, secret_access_key: str, s3gate_endpoint: str, profile: str='default') -> None:
@reporter.step("Configure S3 client (aws cli)")
def __init__(
self, access_key_id: str, secret_access_key: str, s3gate_endpoint: str, profile: str = "default"
) -> None:
self.s3gate_endpoint = s3gate_endpoint
self.profile = profile
self.local_shell = LocalShell()
@ -42,11 +43,11 @@ class AwsCliClient(S3ClientWrapper):
except Exception as err:
raise RuntimeError("Error while configuring AwsCliClient") from err
@reporter.step_deco("Set endpoint S3 to {s3gate_endpoint}")
@reporter.step("Set endpoint S3 to {s3gate_endpoint}")
def set_endpoint(self, s3gate_endpoint: str):
self.s3gate_endpoint = s3gate_endpoint
@reporter.step_deco("Create bucket S3")
@reporter.step("Create bucket S3")
def create_bucket(
self,
bucket: Optional[str] = None,
@ -85,25 +86,25 @@ class AwsCliClient(S3ClientWrapper):
return bucket
@reporter.step_deco("List buckets S3")
@reporter.step("List buckets S3")
def list_buckets(self) -> list[str]:
cmd = f"aws {self.common_flags} s3api list-buckets --endpoint {self.s3gate_endpoint} --profile {self.profile}"
output = self.local_shell.exec(cmd).stdout
buckets_json = self._to_json(output)
return [bucket["Name"] for bucket in buckets_json["Buckets"]]
@reporter.step_deco("Delete bucket S3")
@reporter.step("Delete bucket S3")
def delete_bucket(self, bucket: str) -> None:
cmd = f"aws {self.common_flags} s3api delete-bucket --bucket {bucket} --endpoint {self.s3gate_endpoint} --profile {self.profile}"
self.local_shell.exec(cmd, command_options)
sleep(S3_SYNC_WAIT_TIME)
@reporter.step_deco("Head bucket S3")
@reporter.step("Head bucket S3")
def head_bucket(self, bucket: str) -> None:
cmd = f"aws {self.common_flags} s3api head-bucket --bucket {bucket} --endpoint {self.s3gate_endpoint} --profile {self.profile}"
self.local_shell.exec(cmd)
@reporter.step_deco("Put bucket versioning status")
@reporter.step("Put bucket versioning status")
def put_bucket_versioning(self, bucket: str, status: VersioningStatus) -> None:
cmd = (
f"aws {self.common_flags} s3api put-bucket-versioning --bucket {bucket} "
@ -112,7 +113,7 @@ class AwsCliClient(S3ClientWrapper):
)
self.local_shell.exec(cmd)
@reporter.step_deco("Get bucket versioning status")
@reporter.step("Get bucket versioning status")
def get_bucket_versioning_status(self, bucket: str) -> Literal["Enabled", "Suspended"]:
cmd = (
f"aws {self.common_flags} s3api get-bucket-versioning --bucket {bucket} "
@ -122,7 +123,7 @@ class AwsCliClient(S3ClientWrapper):
response = self._to_json(output)
return response.get("Status")
@reporter.step_deco("Put bucket tagging")
@reporter.step("Put bucket tagging")
def put_bucket_tagging(self, bucket: str, tags: list) -> None:
tags_json = {"TagSet": [{"Key": tag_key, "Value": tag_value} for tag_key, tag_value in tags]}
cmd = (
@ -131,34 +132,42 @@ class AwsCliClient(S3ClientWrapper):
)
self.local_shell.exec(cmd)
@reporter.step_deco("Get bucket tagging")
@reporter.step("Get bucket tagging")
def get_bucket_tagging(self, bucket: str) -> list:
cmd = (
f"aws {self.common_flags} s3api get-bucket-tagging --bucket {bucket} " f"--endpoint {self.s3gate_endpoint} --profile {self.profile}"
f"aws {self.common_flags} s3api get-bucket-tagging --bucket {bucket} "
f"--endpoint {self.s3gate_endpoint} --profile {self.profile}"
)
output = self.local_shell.exec(cmd).stdout
response = self._to_json(output)
return response.get("TagSet")
@reporter.step_deco("Get bucket acl")
@reporter.step("Get bucket acl")
def get_bucket_acl(self, bucket: str) -> list:
cmd = f"aws {self.common_flags} s3api get-bucket-acl --bucket {bucket} " f"--endpoint {self.s3gate_endpoint} --profile {self.profile}"
cmd = (
f"aws {self.common_flags} s3api get-bucket-acl --bucket {bucket} "
f"--endpoint {self.s3gate_endpoint} --profile {self.profile}"
)
output = self.local_shell.exec(cmd).stdout
response = self._to_json(output)
return response.get("Grants")
@reporter.step_deco("Get bucket location")
@reporter.step("Get bucket location")
def get_bucket_location(self, bucket: str) -> dict:
cmd = (
f"aws {self.common_flags} s3api get-bucket-location --bucket {bucket} " f"--endpoint {self.s3gate_endpoint} --profile {self.profile}"
f"aws {self.common_flags} s3api get-bucket-location --bucket {bucket} "
f"--endpoint {self.s3gate_endpoint} --profile {self.profile}"
)
output = self.local_shell.exec(cmd).stdout
response = self._to_json(output)
return response.get("LocationConstraint")
@reporter.step_deco("List objects S3")
@reporter.step("List objects S3")
def list_objects(self, bucket: str, full_output: bool = False) -> Union[dict, list[str]]:
cmd = f"aws {self.common_flags} s3api list-objects --bucket {bucket} " f"--endpoint {self.s3gate_endpoint} --profile {self.profile}"
cmd = (
f"aws {self.common_flags} s3api list-objects --bucket {bucket} "
f"--endpoint {self.s3gate_endpoint} --profile {self.profile}"
)
output = self.local_shell.exec(cmd).stdout
response = self._to_json(output)
@ -167,9 +176,12 @@ class AwsCliClient(S3ClientWrapper):
return response if full_output else obj_list
@reporter.step_deco("List objects S3 v2")
@reporter.step("List objects S3 v2")
def list_objects_v2(self, bucket: str, full_output: bool = False) -> Union[dict, list[str]]:
cmd = f"aws {self.common_flags} s3api list-objects-v2 --bucket {bucket} " f"--endpoint {self.s3gate_endpoint} --profile {self.profile}"
cmd = (
f"aws {self.common_flags} s3api list-objects-v2 --bucket {bucket} "
f"--endpoint {self.s3gate_endpoint} --profile {self.profile}"
)
output = self.local_shell.exec(cmd).stdout
response = self._to_json(output)
@ -178,7 +190,7 @@ class AwsCliClient(S3ClientWrapper):
return response if full_output else obj_list
@reporter.step_deco("List objects versions S3")
@reporter.step("List objects versions S3")
def list_objects_versions(self, bucket: str, full_output: bool = False) -> dict:
cmd = (
f"aws {self.common_flags} s3api list-object-versions --bucket {bucket} "
@ -188,7 +200,7 @@ class AwsCliClient(S3ClientWrapper):
response = self._to_json(output)
return response if full_output else response.get("Versions", [])
@reporter.step_deco("List objects delete markers S3")
@reporter.step("List objects delete markers S3")
def list_delete_markers(self, bucket: str, full_output: bool = False) -> list:
cmd = (
f"aws {self.common_flags} s3api list-object-versions --bucket {bucket} "
@ -198,7 +210,7 @@ class AwsCliClient(S3ClientWrapper):
response = self._to_json(output)
return response if full_output else response.get("DeleteMarkers", [])
@reporter.step_deco("Copy object S3")
@reporter.step("Copy object S3")
def copy_object(
self,
source_bucket: str,
@ -236,7 +248,7 @@ class AwsCliClient(S3ClientWrapper):
self.local_shell.exec(cmd, command_options)
return key
@reporter.step_deco("Put object S3")
@reporter.step("Put object S3")
def put_object(
self,
bucket: str,
@ -280,7 +292,7 @@ class AwsCliClient(S3ClientWrapper):
response = self._to_json(output)
return response.get("VersionId")
@reporter.step_deco("Head object S3")
@reporter.step("Head object S3")
def head_object(self, bucket: str, key: str, version_id: Optional[str] = None) -> dict:
version = f" --version-id {version_id}" if version_id else ""
cmd = (
@ -291,7 +303,7 @@ class AwsCliClient(S3ClientWrapper):
response = self._to_json(output)
return response
@reporter.step_deco("Get object S3")
@reporter.step("Get object S3")
def get_object(
self,
bucket: str,
@ -312,7 +324,7 @@ class AwsCliClient(S3ClientWrapper):
response = self._to_json(output)
return response if full_output else file_path
@reporter.step_deco("Get object ACL")
@reporter.step("Get object ACL")
def get_object_acl(self, bucket: str, key: str, version_id: Optional[str] = None) -> list:
version = f" --version-id {version_id}" if version_id else ""
cmd = (
@ -323,7 +335,7 @@ class AwsCliClient(S3ClientWrapper):
response = self._to_json(output)
return response.get("Grants")
@reporter.step_deco("Put object ACL")
@reporter.step("Put object ACL")
def put_object_acl(
self,
bucket: str,
@ -346,7 +358,7 @@ class AwsCliClient(S3ClientWrapper):
response = self._to_json(output)
return response.get("Grants")
@reporter.step_deco("Put bucket ACL")
@reporter.step("Put bucket ACL")
def put_bucket_acl(
self,
bucket: str,
@ -354,7 +366,10 @@ class AwsCliClient(S3ClientWrapper):
grant_write: Optional[str] = None,
grant_read: Optional[str] = None,
) -> None:
cmd = f"aws {self.common_flags} s3api put-bucket-acl --bucket {bucket} " f" --endpoint {self.s3gate_endpoint} --profile {self.profile}"
cmd = (
f"aws {self.common_flags} s3api put-bucket-acl --bucket {bucket} "
f" --endpoint {self.s3gate_endpoint} --profile {self.profile}"
)
if acl:
cmd += f" --acl {acl}"
if grant_write:
@ -363,7 +378,7 @@ class AwsCliClient(S3ClientWrapper):
cmd += f" --grant-read {grant_read}"
self.local_shell.exec(cmd)
@reporter.step_deco("Delete objects S3")
@reporter.step("Delete objects S3")
def delete_objects(self, bucket: str, keys: list[str]) -> dict:
file_path = os.path.join(os.getcwd(), ASSETS_DIR, "delete.json")
delete_structure = json.dumps(_make_objs_dict(keys))
@ -380,7 +395,7 @@ class AwsCliClient(S3ClientWrapper):
sleep(S3_SYNC_WAIT_TIME)
return response
@reporter.step_deco("Delete object S3")
@reporter.step("Delete object S3")
def delete_object(self, bucket: str, key: str, version_id: Optional[str] = None) -> dict:
version = f" --version-id {version_id}" if version_id else ""
cmd = (
@ -391,7 +406,7 @@ class AwsCliClient(S3ClientWrapper):
sleep(S3_SYNC_WAIT_TIME)
return self._to_json(output)
@reporter.step_deco("Delete object versions S3")
@reporter.step("Delete object versions S3")
def delete_object_versions(self, bucket: str, object_versions: list) -> dict:
# Build deletion list in S3 format
delete_list = {
@ -418,13 +433,13 @@ class AwsCliClient(S3ClientWrapper):
sleep(S3_SYNC_WAIT_TIME)
return self._to_json(output)
@reporter.step_deco("Delete object versions S3 without delete markers")
@reporter.step("Delete object versions S3 without delete markers")
def delete_object_versions_without_dm(self, bucket: str, object_versions: list) -> None:
# Delete objects without creating delete markers
for object_version in object_versions:
self.delete_object(bucket=bucket, key=object_version["Key"], version_id=object_version["VersionId"])
@reporter.step_deco("Get object attributes")
@reporter.step("Get object attributes")
def get_object_attributes(
self,
bucket: str,
@ -456,14 +471,17 @@ class AwsCliClient(S3ClientWrapper):
else:
return response.get(attributes[0])
@reporter.step_deco("Get bucket policy")
@reporter.step("Get bucket policy")
def get_bucket_policy(self, bucket: str) -> dict:
cmd = f"aws {self.common_flags} s3api get-bucket-policy --bucket {bucket} " f"--endpoint {self.s3gate_endpoint} --profile {self.profile}"
cmd = (
f"aws {self.common_flags} s3api get-bucket-policy --bucket {bucket} "
f"--endpoint {self.s3gate_endpoint} --profile {self.profile}"
)
output = self.local_shell.exec(cmd).stdout
response = self._to_json(output)
return response.get("Policy")
@reporter.step_deco("Put bucket policy")
@reporter.step("Put bucket policy")
def put_bucket_policy(self, bucket: str, policy: dict) -> None:
# Leaving it as is was in test repo. Double dumps to escape resulting string
# Example:
@ -478,14 +496,17 @@ class AwsCliClient(S3ClientWrapper):
)
self.local_shell.exec(cmd)
@reporter.step_deco("Get bucket cors")
@reporter.step("Get bucket cors")
def get_bucket_cors(self, bucket: str) -> dict:
cmd = f"aws {self.common_flags} s3api get-bucket-cors --bucket {bucket} " f"--endpoint {self.s3gate_endpoint} --profile {self.profile}"
cmd = (
f"aws {self.common_flags} s3api get-bucket-cors --bucket {bucket} "
f"--endpoint {self.s3gate_endpoint} --profile {self.profile}"
)
output = self.local_shell.exec(cmd).stdout
response = self._to_json(output)
return response.get("CORSRules")
@reporter.step_deco("Put bucket cors")
@reporter.step("Put bucket cors")
def put_bucket_cors(self, bucket: str, cors_configuration: dict) -> None:
cmd = (
f"aws {self.common_flags} s3api put-bucket-cors --bucket {bucket} "
@ -493,14 +514,15 @@ class AwsCliClient(S3ClientWrapper):
)
self.local_shell.exec(cmd)
@reporter.step_deco("Delete bucket cors")
@reporter.step("Delete bucket cors")
def delete_bucket_cors(self, bucket: str) -> None:
cmd = (
f"aws {self.common_flags} s3api delete-bucket-cors --bucket {bucket} " f"--endpoint {self.s3gate_endpoint} --profile {self.profile}"
f"aws {self.common_flags} s3api delete-bucket-cors --bucket {bucket} "
f"--endpoint {self.s3gate_endpoint} --profile {self.profile}"
)
self.local_shell.exec(cmd)
@reporter.step_deco("Delete bucket tagging")
@reporter.step("Delete bucket tagging")
def delete_bucket_tagging(self, bucket: str) -> None:
cmd = (
f"aws {self.common_flags} s3api delete-bucket-tagging --bucket {bucket} "
@ -508,7 +530,7 @@ class AwsCliClient(S3ClientWrapper):
)
self.local_shell.exec(cmd)
@reporter.step_deco("Put object retention")
@reporter.step("Put object retention")
def put_object_retention(
self,
bucket: str,
@ -526,7 +548,7 @@ class AwsCliClient(S3ClientWrapper):
cmd += " --bypass-governance-retention"
self.local_shell.exec(cmd)
@reporter.step_deco("Put object legal hold")
@reporter.step("Put object legal hold")
def put_object_legal_hold(
self,
bucket: str,
@ -542,7 +564,7 @@ class AwsCliClient(S3ClientWrapper):
)
self.local_shell.exec(cmd)
@reporter.step_deco("Put object tagging")
@reporter.step("Put object tagging")
def put_object_tagging(self, bucket: str, key: str, tags: list) -> None:
tags = [{"Key": tag_key, "Value": tag_value} for tag_key, tag_value in tags]
tagging = {"TagSet": tags}
@ -552,7 +574,7 @@ class AwsCliClient(S3ClientWrapper):
)
self.local_shell.exec(cmd)
@reporter.step_deco("Get object tagging")
@reporter.step("Get object tagging")
def get_object_tagging(self, bucket: str, key: str, version_id: Optional[str] = None) -> list:
version = f" --version-id {version_id}" if version_id else ""
cmd = (
@ -563,7 +585,7 @@ class AwsCliClient(S3ClientWrapper):
response = self._to_json(output)
return response.get("TagSet")
@reporter.step_deco("Delete object tagging")
@reporter.step("Delete object tagging")
def delete_object_tagging(self, bucket: str, key: str) -> None:
cmd = (
f"aws {self.common_flags} s3api delete-object-tagging --bucket {bucket} "
@ -571,7 +593,7 @@ class AwsCliClient(S3ClientWrapper):
)
self.local_shell.exec(cmd)
@reporter.step_deco("Sync directory S3")
@reporter.step("Sync directory S3")
def sync(
self,
bucket: str,
@ -579,7 +601,10 @@ class AwsCliClient(S3ClientWrapper):
acl: Optional[str] = None,
metadata: Optional[dict] = None,
) -> dict:
cmd = f"aws {self.common_flags} s3 sync {dir_path} s3://{bucket} " f"--endpoint-url {self.s3gate_endpoint} --profile {self.profile}"
cmd = (
f"aws {self.common_flags} s3 sync {dir_path} s3://{bucket} "
f"--endpoint-url {self.s3gate_endpoint} --profile {self.profile}"
)
if metadata:
cmd += " --metadata"
for key, value in metadata.items():
@ -589,7 +614,7 @@ class AwsCliClient(S3ClientWrapper):
output = self.local_shell.exec(cmd, command_options).stdout
return self._to_json(output)
@reporter.step_deco("CP directory S3")
@reporter.step("CP directory S3")
def cp(
self,
bucket: str,
@ -610,7 +635,7 @@ class AwsCliClient(S3ClientWrapper):
output = self.local_shell.exec(cmd, command_options).stdout
return self._to_json(output)
@reporter.step_deco("Create multipart upload S3")
@reporter.step("Create multipart upload S3")
def create_multipart_upload(self, bucket: str, key: str) -> str:
cmd = (
f"aws {self.common_flags} s3api create-multipart-upload --bucket {bucket} "
@ -623,7 +648,7 @@ class AwsCliClient(S3ClientWrapper):
return response["UploadId"]
@reporter.step_deco("List multipart uploads S3")
@reporter.step("List multipart uploads S3")
def list_multipart_uploads(self, bucket: str) -> Optional[list[dict]]:
cmd = (
f"aws {self.common_flags} s3api list-multipart-uploads --bucket {bucket} "
@ -633,7 +658,7 @@ class AwsCliClient(S3ClientWrapper):
response = self._to_json(output)
return response.get("Uploads")
@reporter.step_deco("Abort multipart upload S3")
@reporter.step("Abort multipart upload S3")
def abort_multipart_upload(self, bucket: str, key: str, upload_id: str) -> None:
cmd = (
f"aws {self.common_flags} s3api abort-multipart-upload --bucket {bucket} "
@ -641,7 +666,7 @@ class AwsCliClient(S3ClientWrapper):
)
self.local_shell.exec(cmd)
@reporter.step_deco("Upload part S3")
@reporter.step("Upload part S3")
def upload_part(self, bucket: str, key: str, upload_id: str, part_num: int, filepath: str) -> str:
cmd = (
f"aws {self.common_flags} s3api upload-part --bucket {bucket} --key {key} "
@ -653,7 +678,7 @@ class AwsCliClient(S3ClientWrapper):
assert response.get("ETag"), f"Expected ETag in response:\n{response}"
return response["ETag"]
@reporter.step_deco("Upload copy part S3")
@reporter.step("Upload copy part S3")
def upload_part_copy(self, bucket: str, key: str, upload_id: str, part_num: int, copy_source: str) -> str:
cmd = (
f"aws {self.common_flags} s3api upload-part-copy --bucket {bucket} --key {key} "
@ -666,7 +691,7 @@ class AwsCliClient(S3ClientWrapper):
return response["CopyPartResult"]["ETag"]
@reporter.step_deco("List parts S3")
@reporter.step("List parts S3")
def list_parts(self, bucket: str, key: str, upload_id: str) -> list[dict]:
cmd = (
f"aws {self.common_flags} s3api list-parts --bucket {bucket} --key {key} "
@ -679,7 +704,7 @@ class AwsCliClient(S3ClientWrapper):
return response["Parts"]
@reporter.step_deco("Complete multipart upload S3")
@reporter.step("Complete multipart upload S3")
def complete_multipart_upload(self, bucket: str, key: str, upload_id: str, parts: list) -> None:
file_path = os.path.join(os.getcwd(), ASSETS_DIR, "parts.json")
parts_dict = {"Parts": [{"ETag": etag, "PartNumber": part_num} for part_num, etag in parts]}
@ -696,7 +721,7 @@ class AwsCliClient(S3ClientWrapper):
)
self.local_shell.exec(cmd)
@reporter.step_deco("Put object lock configuration")
@reporter.step("Put object lock configuration")
def put_object_lock_configuration(self, bucket: str, configuration: dict) -> dict:
cmd = (
f"aws {self.common_flags} s3api put-object-lock-configuration --bucket {bucket} "
@ -705,7 +730,7 @@ class AwsCliClient(S3ClientWrapper):
output = self.local_shell.exec(cmd).stdout
return self._to_json(output)
@reporter.step_deco("Get object lock configuration")
@reporter.step("Get object lock configuration")
def get_object_lock_configuration(self, bucket: str):
cmd = (
f"aws {self.common_flags} s3api get-object-lock-configuration --bucket {bucket} "