2021-08-25 17:08:54 +00:00
|
|
|
#!/usr/bin/python3.8
|
2021-04-26 10:30:40 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
import tarfile
|
|
|
|
import uuid
|
2022-05-19 11:10:00 +00:00
|
|
|
import hashlib
|
2022-01-10 11:02:57 +00:00
|
|
|
import docker
|
2021-04-26 10:30:40 +00:00
|
|
|
|
2022-02-07 11:41:34 +00:00
|
|
|
from common import SIMPLE_OBJ_SIZE, ASSETS_DIR
|
2022-05-19 11:10:00 +00:00
|
|
|
from cli_helpers import _cmd_run
|
2021-04-26 10:30:40 +00:00
|
|
|
from robot.api.deco import keyword
|
|
|
|
from robot.api import logger
|
2021-06-25 13:44:42 +00:00
|
|
|
from robot.libraries.BuiltIn import BuiltIn
|
2022-01-10 11:02:57 +00:00
|
|
|
|
2021-04-26 10:30:40 +00:00
|
|
|
|
|
|
|
ROBOT_AUTO_KEYWORDS = False
|
|
|
|
|
|
|
|
@keyword('Generate file of bytes')
|
|
|
|
def generate_file_of_bytes(size: str) -> str:
|
|
|
|
"""
|
|
|
|
Function generates big binary file with the specified size in bytes.
|
|
|
|
:param size: the size in bytes, can be declared as 6e+6 for example
|
|
|
|
"""
|
|
|
|
size = int(float(size))
|
2022-05-19 11:10:00 +00:00
|
|
|
filename = f"{os.getcwd()}/{ASSETS_DIR}/{uuid.uuid4()}"
|
2021-04-26 10:30:40 +00:00
|
|
|
with open(filename, 'wb') as fout:
|
|
|
|
fout.write(os.urandom(size))
|
2021-06-01 12:07:31 +00:00
|
|
|
logger.info(f"file with size {size} bytes has been generated: {filename}")
|
|
|
|
return filename
|
2021-04-26 10:30:40 +00:00
|
|
|
|
2022-05-19 11:10:00 +00:00
|
|
|
@keyword('Generate file')
|
|
|
|
def generate_file_and_file_hash(size: str) -> str:
|
|
|
|
"""
|
|
|
|
Function generates a big binary file with the specified size in bytes and its hash.
|
|
|
|
Args:
|
|
|
|
size (str): the size in bytes, can be declared as 6e+6 for example
|
|
|
|
Returns:
|
|
|
|
(str): the path to the generated file
|
|
|
|
(str): the hash of the generated file
|
|
|
|
"""
|
|
|
|
size = int(float(size))
|
|
|
|
filename = f"{os.getcwd()}/{ASSETS_DIR}/{str(uuid.uuid4())}"
|
|
|
|
with open(filename, 'wb') as fout:
|
|
|
|
fout.write(os.urandom(size))
|
|
|
|
logger.info(f"file with size {size} bytes has been generated: {filename}")
|
|
|
|
|
|
|
|
file_hash = _get_file_hash(filename)
|
|
|
|
|
|
|
|
return filename, file_hash
|
|
|
|
|
2021-04-26 10:30:40 +00:00
|
|
|
@keyword('Get Docker Logs')
|
|
|
|
def get_container_logs(testcase_name: str) -> None:
|
|
|
|
client = docker.APIClient(base_url='unix://var/run/docker.sock')
|
2021-06-25 13:44:42 +00:00
|
|
|
logs_dir = BuiltIn().get_variable_value("${OUTPUT_DIR}")
|
|
|
|
tar_name = f"{logs_dir}/dockerlogs({testcase_name}).tar.gz"
|
2021-04-26 10:30:40 +00:00
|
|
|
tar = tarfile.open(tar_name, "w:gz")
|
|
|
|
for container in client.containers():
|
|
|
|
container_name = container['Names'][0][1:]
|
|
|
|
if client.inspect_container(container_name)['Config']['Domainname'] == "neofs.devenv":
|
2021-06-25 13:44:42 +00:00
|
|
|
file_name = f"{logs_dir}/docker_log_{container_name}"
|
2021-04-26 10:30:40 +00:00
|
|
|
with open(file_name,'wb') as out:
|
|
|
|
out.write(client.logs(container_name))
|
|
|
|
logger.info(f"Collected logs from container {container_name}")
|
|
|
|
tar.add(file_name)
|
|
|
|
os.remove(file_name)
|
|
|
|
tar.close()
|
2021-09-09 13:36:34 +00:00
|
|
|
|
2022-01-10 11:02:57 +00:00
|
|
|
@keyword('Make Up')
|
2021-12-28 07:08:16 +00:00
|
|
|
def make_up(services: list=[], config_dict: dict={}):
|
2022-01-10 11:02:57 +00:00
|
|
|
test_path = os.getcwd()
|
|
|
|
dev_path = os.getenv('DEVENV_PATH', '../neofs-dev-env')
|
|
|
|
os.chdir(dev_path)
|
|
|
|
|
2022-03-11 16:08:14 +00:00
|
|
|
if len(services) > 0:
|
2022-01-10 11:02:57 +00:00
|
|
|
for service in services:
|
2021-12-28 07:08:16 +00:00
|
|
|
if config_dict != {}:
|
|
|
|
with open(f"{dev_path}/.int_test.env", "a") as out:
|
|
|
|
for key, value in config_dict.items():
|
|
|
|
out.write(f'{key}={value}')
|
2022-01-10 11:02:57 +00:00
|
|
|
cmd = f'make up/{service}'
|
2022-02-07 11:41:34 +00:00
|
|
|
_cmd_run(cmd)
|
2022-01-10 11:02:57 +00:00
|
|
|
else:
|
|
|
|
cmd = f'make up/basic; make update.max_object_size val={SIMPLE_OBJ_SIZE}'
|
2022-03-09 16:02:59 +00:00
|
|
|
_cmd_run(cmd, timeout=120)
|
2022-01-10 11:02:57 +00:00
|
|
|
|
|
|
|
os.chdir(test_path)
|
|
|
|
|
|
|
|
@keyword('Make Down')
|
2021-12-28 07:08:16 +00:00
|
|
|
def make_down(services: list=[]):
|
2022-01-10 11:02:57 +00:00
|
|
|
test_path = os.getcwd()
|
|
|
|
dev_path = os.getenv('DEVENV_PATH', '../neofs-dev-env')
|
|
|
|
os.chdir(dev_path)
|
|
|
|
|
2021-12-28 07:08:16 +00:00
|
|
|
if len(services) > 0:
|
|
|
|
for service in services:
|
|
|
|
cmd = f'make down/{service}'
|
|
|
|
_cmd_run(cmd)
|
|
|
|
with open(f"{dev_path}/.int_test.env", "w"):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
cmd = 'make down; make clean'
|
|
|
|
_cmd_run(cmd, timeout=60)
|
2022-05-19 11:10:00 +00:00
|
|
|
|
2022-01-10 11:02:57 +00:00
|
|
|
os.chdir(test_path)
|
2022-05-19 11:10:00 +00:00
|
|
|
|
|
|
|
def _get_file_hash(filename: str):
|
|
|
|
blocksize = 65536
|
|
|
|
file_hash = hashlib.md5()
|
|
|
|
with open(filename, "rb") as out:
|
|
|
|
for block in iter(lambda: out.read(blocksize), b""):
|
|
|
|
file_hash.update(block)
|
|
|
|
logger.info(f"Hash: {file_hash.hexdigest()}")
|
|
|
|
return file_hash.hexdigest()
|