2022-09-20 15:03:52 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2022-07-05 07:18:37 +00:00
|
|
|
import re
|
2022-03-11 16:08:14 +00:00
|
|
|
import shutil
|
2022-07-05 07:18:37 +00:00
|
|
|
import uuid
|
|
|
|
import zipfile
|
|
|
|
from urllib.parse import quote_plus
|
2022-03-11 16:08:14 +00:00
|
|
|
|
2022-07-05 07:18:37 +00:00
|
|
|
import allure
|
2022-03-11 16:08:14 +00:00
|
|
|
import requests
|
2022-07-05 07:18:37 +00:00
|
|
|
from cli_helpers import _cmd_run
|
2022-03-11 16:08:14 +00:00
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
logger = logging.getLogger("NeoLogger")
|
2022-07-05 07:18:37 +00:00
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
ASSETS_DIR = os.getenv("ASSETS_DIR", "TemporaryDir/")
|
2022-03-11 16:08:14 +00:00
|
|
|
|
2022-06-09 13:08:11 +00:00
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
@allure.step("Get via HTTP Gate")
|
2022-12-05 22:31:45 +00:00
|
|
|
def get_via_http_gate(cid: str, oid: str, endpoint: str):
|
2022-03-11 16:08:14 +00:00
|
|
|
"""
|
|
|
|
This function gets given object from HTTP gate
|
2022-12-05 22:31:45 +00:00
|
|
|
cid: container id to get object from
|
|
|
|
oid: object ID
|
|
|
|
endpoint: http gate endpoint
|
2022-03-11 16:08:14 +00:00
|
|
|
"""
|
2022-12-05 22:31:45 +00:00
|
|
|
request = f"{endpoint}/get/{cid}/{oid}"
|
2022-03-11 16:08:14 +00:00
|
|
|
resp = requests.get(request, stream=True)
|
|
|
|
|
|
|
|
if not resp.ok:
|
2022-09-20 15:03:52 +00:00
|
|
|
raise Exception(
|
|
|
|
f"""Failed to get object via HTTP gate:
|
2022-03-11 16:08:14 +00:00
|
|
|
request: {resp.request.path_url},
|
|
|
|
response: {resp.text},
|
2022-09-20 15:03:52 +00:00
|
|
|
status code: {resp.status_code} {resp.reason}"""
|
|
|
|
)
|
2022-03-11 16:08:14 +00:00
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
logger.info(f"Request: {request}")
|
2022-07-05 07:18:37 +00:00
|
|
|
_attach_allure_step(request, resp.status_code)
|
|
|
|
|
2022-10-18 07:11:57 +00:00
|
|
|
file_path = os.path.join(os.getcwd(), ASSETS_DIR, f"{cid}_{oid}")
|
|
|
|
with open(file_path, "wb") as file:
|
|
|
|
shutil.copyfileobj(resp.raw, file)
|
|
|
|
return file_path
|
2022-07-05 07:18:37 +00:00
|
|
|
|
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
@allure.step("Get via Zip HTTP Gate")
|
2022-12-05 22:31:45 +00:00
|
|
|
def get_via_zip_http_gate(cid: str, prefix: str, endpoint: str):
|
2022-07-05 07:18:37 +00:00
|
|
|
"""
|
|
|
|
This function gets given object from HTTP gate
|
2022-12-05 22:31:45 +00:00
|
|
|
cid: container id to get object from
|
|
|
|
prefix: common prefix
|
|
|
|
endpoint: http gate endpoint
|
2022-07-05 07:18:37 +00:00
|
|
|
"""
|
2022-12-05 22:31:45 +00:00
|
|
|
request = f"{endpoint}/zip/{cid}/{prefix}"
|
2022-07-05 07:18:37 +00:00
|
|
|
resp = requests.get(request, stream=True)
|
|
|
|
|
|
|
|
if not resp.ok:
|
2022-09-20 15:03:52 +00:00
|
|
|
raise Exception(
|
|
|
|
f"""Failed to get object via HTTP gate:
|
2022-07-05 07:18:37 +00:00
|
|
|
request: {resp.request.path_url},
|
|
|
|
response: {resp.text},
|
2022-09-20 15:03:52 +00:00
|
|
|
status code: {resp.status_code} {resp.reason}"""
|
|
|
|
)
|
2022-07-05 07:18:37 +00:00
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
logger.info(f"Request: {request}")
|
2022-07-05 07:18:37 +00:00
|
|
|
_attach_allure_step(request, resp.status_code)
|
|
|
|
|
2022-10-18 07:11:57 +00:00
|
|
|
file_path = os.path.join(os.getcwd(), ASSETS_DIR, f"{cid}_archive.zip")
|
|
|
|
with open(file_path, "wb") as file:
|
|
|
|
shutil.copyfileobj(resp.raw, file)
|
2022-07-05 07:18:37 +00:00
|
|
|
|
2022-10-18 07:11:57 +00:00
|
|
|
with zipfile.ZipFile(file_path, "r") as zip_ref:
|
2022-07-05 07:18:37 +00:00
|
|
|
zip_ref.extractall(ASSETS_DIR)
|
|
|
|
|
2022-10-18 07:11:57 +00:00
|
|
|
return os.path.join(os.getcwd(), ASSETS_DIR, prefix)
|
2022-07-05 07:18:37 +00:00
|
|
|
|
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
@allure.step("Get via HTTP Gate by attribute")
|
2022-12-05 22:31:45 +00:00
|
|
|
def get_via_http_gate_by_attribute(cid: str, attribute: dict, endpoint: str):
|
2022-07-05 07:18:37 +00:00
|
|
|
"""
|
|
|
|
This function gets given object from HTTP gate
|
2022-12-05 22:31:45 +00:00
|
|
|
cid: CID to get object from
|
|
|
|
attribute: attribute {name: attribute} value pair
|
|
|
|
endpoint: http gate endpoint
|
2022-07-05 07:18:37 +00:00
|
|
|
"""
|
|
|
|
attr_name = list(attribute.keys())[0]
|
|
|
|
attr_value = quote_plus(str(attribute.get(attr_name)))
|
2022-12-05 22:31:45 +00:00
|
|
|
request = f"{endpoint}/get_by_attribute/{cid}/{quote_plus(str(attr_name))}/{attr_value}"
|
2022-07-05 07:18:37 +00:00
|
|
|
resp = requests.get(request, stream=True)
|
|
|
|
|
|
|
|
if not resp.ok:
|
2022-09-20 15:03:52 +00:00
|
|
|
raise Exception(
|
|
|
|
f"""Failed to get object via HTTP gate:
|
2022-07-05 07:18:37 +00:00
|
|
|
request: {resp.request.path_url},
|
|
|
|
response: {resp.text},
|
2022-09-20 15:03:52 +00:00
|
|
|
status code: {resp.status_code} {resp.reason}"""
|
|
|
|
)
|
2022-07-05 07:18:37 +00:00
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
logger.info(f"Request: {request}")
|
2022-07-05 07:18:37 +00:00
|
|
|
_attach_allure_step(request, resp.status_code)
|
|
|
|
|
2022-10-18 07:11:57 +00:00
|
|
|
file_path = os.path.join(os.getcwd(), ASSETS_DIR, f"{cid}_{str(uuid.uuid4())}")
|
|
|
|
with open(file_path, "wb") as file:
|
|
|
|
shutil.copyfileobj(resp.raw, file)
|
|
|
|
return file_path
|
2022-07-05 07:18:37 +00:00
|
|
|
|
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
@allure.step("Upload via HTTP Gate")
|
2022-12-05 22:31:45 +00:00
|
|
|
def upload_via_http_gate(cid: str, path: str, endpoint: str, headers: dict = None) -> str:
|
2022-07-05 07:18:37 +00:00
|
|
|
"""
|
|
|
|
This function upload given object through HTTP gate
|
2022-12-05 22:31:45 +00:00
|
|
|
cid: CID to get object from
|
|
|
|
path: File path to upload
|
|
|
|
endpoint: http gate endpoint
|
|
|
|
headers: Object header
|
2022-07-05 07:18:37 +00:00
|
|
|
"""
|
2022-12-05 22:31:45 +00:00
|
|
|
request = f"{endpoint}/upload/{cid}"
|
2022-09-20 15:03:52 +00:00
|
|
|
files = {"upload_file": open(path, "rb")}
|
|
|
|
body = {"filename": path}
|
2022-07-05 07:18:37 +00:00
|
|
|
resp = requests.post(request, files=files, data=body, headers=headers)
|
|
|
|
|
|
|
|
if not resp.ok:
|
2022-09-20 15:03:52 +00:00
|
|
|
raise Exception(
|
|
|
|
f"""Failed to get object via HTTP gate:
|
2022-07-05 07:18:37 +00:00
|
|
|
request: {resp.request.path_url},
|
|
|
|
response: {resp.text},
|
2022-09-20 15:03:52 +00:00
|
|
|
status code: {resp.status_code} {resp.reason}"""
|
|
|
|
)
|
2022-07-05 07:18:37 +00:00
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
logger.info(f"Request: {request}")
|
|
|
|
_attach_allure_step(request, resp.json(), req_type="POST")
|
2022-07-05 07:18:37 +00:00
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
assert resp.json().get("object_id"), f"OID found in response {resp}"
|
2022-07-05 07:18:37 +00:00
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
return resp.json().get("object_id")
|
2022-07-05 07:18:37 +00:00
|
|
|
|
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
@allure.step("Upload via HTTP Gate using Curl")
|
|
|
|
def upload_via_http_gate_curl(
|
2022-12-05 22:31:45 +00:00
|
|
|
cid: str, filepath: str, endpoint: str, large_object=False, headers: dict = None
|
2022-09-20 15:03:52 +00:00
|
|
|
) -> str:
|
2022-07-05 07:18:37 +00:00
|
|
|
"""
|
|
|
|
This function upload given object through HTTP gate using curl utility.
|
2022-12-05 22:31:45 +00:00
|
|
|
cid: CID to get object from
|
|
|
|
filepath: File path to upload
|
|
|
|
headers: Object header
|
|
|
|
endpoint: http gate endpoint
|
2022-07-05 07:18:37 +00:00
|
|
|
"""
|
2022-12-05 22:31:45 +00:00
|
|
|
request = f"{endpoint}/upload/{cid}"
|
2022-09-20 15:03:52 +00:00
|
|
|
files = f"file=@{filepath};filename={os.path.basename(filepath)}"
|
|
|
|
cmd = f"curl -F '{files}' {request}"
|
2022-07-05 07:18:37 +00:00
|
|
|
if large_object:
|
2022-09-20 15:03:52 +00:00
|
|
|
files = f"file=@pipe;filename={os.path.basename(filepath)}"
|
|
|
|
cmd = f"mkfifo pipe;cat {filepath} > pipe & curl --no-buffer -F '{files}' {request}"
|
2022-07-05 07:18:37 +00:00
|
|
|
output = _cmd_run(cmd)
|
|
|
|
oid_re = re.search(r'"object_id": "(.*)"', output)
|
|
|
|
if not oid_re:
|
|
|
|
raise AssertionError(f'Could not find "object_id" in {output}')
|
|
|
|
return oid_re.group(1)
|
|
|
|
|
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
@allure.step("Get via HTTP Gate using Curl")
|
2022-12-05 22:31:45 +00:00
|
|
|
def get_via_http_curl(cid: str, oid: str, endpoint: str) -> str:
|
2022-07-05 07:18:37 +00:00
|
|
|
"""
|
|
|
|
This function gets given object from HTTP gate using curl utility.
|
2022-12-05 22:31:45 +00:00
|
|
|
cid: CID to get object from
|
|
|
|
oid: object OID
|
|
|
|
endpoint: http gate endpoint
|
2022-07-05 07:18:37 +00:00
|
|
|
"""
|
2022-12-05 22:31:45 +00:00
|
|
|
request = f"{endpoint}/get/{cid}/{oid}"
|
2022-10-18 07:11:57 +00:00
|
|
|
file_path = os.path.join(os.getcwd(), ASSETS_DIR, f"{cid}_{oid}_{str(uuid.uuid4())}")
|
2022-07-05 07:18:37 +00:00
|
|
|
|
2022-10-18 07:11:57 +00:00
|
|
|
cmd = f"curl {request} > {file_path}"
|
2022-07-05 07:18:37 +00:00
|
|
|
_cmd_run(cmd)
|
|
|
|
|
2022-10-18 07:11:57 +00:00
|
|
|
return file_path
|
2022-07-05 07:18:37 +00:00
|
|
|
|
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
def _attach_allure_step(request: str, status_code: int, req_type="GET"):
|
|
|
|
command_attachment = f"REQUEST: '{request}'\n" f"RESPONSE:\n {status_code}\n"
|
|
|
|
with allure.step(f"{req_type} Request"):
|
|
|
|
allure.attach(command_attachment, f"{req_type} Request", allure.attachment_type.TEXT)
|