github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/python/tests/s3compat/minio_helpers.py (about) 1 # 2 # The functions in this file are modified under the Apache 2.0 license as provided by minio 3 # The original code can be found at https://github.com/minio/minio-py 4 # 5 # MinIO Python Library for Amazon S3 Compatible Cloud Storage, 6 # (C) 2015, 2016, 2017, 2018 MinIO, Inc. 7 # 8 # Licensed under the Apache License, Version 2.0 (the "License"); 9 # you may not use this file except in compliance with the License. 10 # You may obtain a copy of the License at 11 # 12 # http://www.apache.org/licenses/LICENSE-2.0 13 # 14 # Unless required by applicable law or agreed to in writing, software 15 # distributed under the License is distributed on an "AS IS" BASIS, 16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 # See the License for the specific language governing permissions and 18 # limitations under the License. 19 20 import json 21 import shutil 22 import time 23 import traceback 24 from inspect import getfullargspec 25 from minio import Minio 26 from minio.error import S3Error 27 from tests import LimitedRandomReader, MB 28 import tests 29 30 31 class TestFailed(Exception): 32 """Indicate test failed error.""" 33 34 35 # pylint: disable=unused-variable,protected-access 36 def init_tests(server_endpoint, access_key, secret_key): 37 tests._CLIENT = Minio(server_endpoint, access_key, secret_key, secure=False) 38 test_file = "datafile-1-MB" 39 large_file = "datafile-11-MB" 40 with open(test_file, "wb") as file_data: 41 shutil.copyfileobj(LimitedRandomReader(1 * MB), file_data) 42 with open(large_file, "wb") as file_data: 43 shutil.copyfileobj(LimitedRandomReader(11 * MB), file_data) 44 tests._TEST_FILE = test_file 45 tests._LARGE_FILE = large_file 46 47 48 def call_test(func, strict): 49 """Execute given test function.""" 50 51 log_entry = { 52 "name": func.__name__, 53 "status": "PASS", 54 } 55 56 start_time = time.time() 57 try: 58 func(log_entry) 59 except S3Error as exc: 60 if exc.code == "NotImplemented": 61 log_entry["alert"] = "Not Implemented" 62 log_entry["status"] = "NA" 63 else: 64 log_entry["message"] = f"{exc}" 65 log_entry["error"] = traceback.format_exc() 66 log_entry["status"] = "FAIL" 67 except Exception as exc: # pylint: disable=broad-except 68 log_entry["message"] = f"{exc}" 69 log_entry["error"] = traceback.format_exc() 70 log_entry["status"] = "FAIL" 71 72 if log_entry.get("method"): 73 # pylint: disable=deprecated-method 74 args_string = ", ".join(getfullargspec(log_entry["method"]).args[1:]) 75 log_entry["function"] = f"{log_entry['method'].__name__}({args_string})" 76 log_entry["args"] = {k: v for k, v in log_entry.get("args", {}).items() if v} 77 log_entry["duration"] = int(round((time.time() - start_time) * 1000)) 78 log_entry["name"] = "minio-py:" + log_entry["name"] 79 log_entry["method"] = None 80 print(json.dumps({k: v for k, v in log_entry.items() if v})) 81 print() 82 if strict and log_entry["status"] == "FAIL": 83 raise TestFailed()