2023-11-01 12:02:29 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-11-01 14:44:42 +00:00
|
|
|
import glob
|
2023-11-17 06:47:22 +00:00
|
|
|
from src.libs.custom_logger import get_custom_logger
|
2023-11-01 14:44:42 +00:00
|
|
|
import os
|
2023-11-01 12:02:29 +00:00
|
|
|
import pytest
|
2023-11-01 14:44:42 +00:00
|
|
|
from datetime import datetime
|
2023-11-17 06:47:22 +00:00
|
|
|
from time import time
|
2023-11-01 14:44:42 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
from src.libs.common import attach_allure_file
|
|
|
|
import src.env_vars as env_vars
|
2023-11-01 12:02:29 +00:00
|
|
|
from src.data_storage import DS
|
|
|
|
|
2023-11-17 06:47:22 +00:00
|
|
|
logger = get_custom_logger(__name__)
|
2023-11-01 12:02:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
# See https://docs.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures
|
|
|
|
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
|
|
|
|
def pytest_runtest_makereport(item):
|
|
|
|
outcome = yield
|
|
|
|
rep = outcome.get_result()
|
|
|
|
if rep.when == "call":
|
|
|
|
setattr(item, "rep_call", rep)
|
|
|
|
return rep
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2023-11-01 14:44:42 +00:00
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
|
|
def set_allure_env_variables():
|
|
|
|
yield
|
|
|
|
if os.path.isdir("allure-results") and not os.path.isfile(os.path.join("allure-results", "environment.properties")):
|
|
|
|
with open(os.path.join("allure-results", "environment.properties"), "w") as outfile:
|
|
|
|
for attribute_name in dir(env_vars):
|
|
|
|
if attribute_name.isupper():
|
|
|
|
attribute_value = getattr(env_vars, attribute_name)
|
|
|
|
outfile.write(f"{attribute_name}={attribute_value}\n")
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function", autouse=True)
|
|
|
|
def test_id(request):
|
|
|
|
# setting up an unique test id to be used where needed
|
|
|
|
request.cls.test_id = f"{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}__{str(uuid4())}"
|
|
|
|
|
|
|
|
|
2023-11-01 12:02:29 +00:00
|
|
|
@pytest.fixture(scope="function", autouse=True)
|
2023-11-01 14:44:42 +00:00
|
|
|
def test_setup(request, test_id):
|
2023-11-17 06:47:22 +00:00
|
|
|
logger.debug(f"Running test: {request.node.name} with id: {request.cls.test_id}")
|
|
|
|
yield
|
|
|
|
for file in glob.glob(os.path.join(env_vars.DOCKER_LOG_DIR, "*")):
|
|
|
|
if os.path.getmtime(file) < time() - 3600:
|
|
|
|
logger.debug(f"Deleting old log file: {file}")
|
|
|
|
try:
|
|
|
|
os.remove(file)
|
|
|
|
except:
|
|
|
|
logger.error("Could not delete file")
|
2023-11-01 14:44:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function", autouse=True)
|
|
|
|
def attach_logs_on_fail(request):
|
|
|
|
yield
|
2023-11-17 06:47:22 +00:00
|
|
|
if env_vars.RUNNING_IN_CI and hasattr(request.node, "rep_call") and request.node.rep_call.failed:
|
2023-11-01 14:44:42 +00:00
|
|
|
logger.debug("Test failed, attempting to attach logs to the allure reports")
|
2023-11-17 06:47:22 +00:00
|
|
|
for file in glob.glob(os.path.join(env_vars.DOCKER_LOG_DIR, "*" + request.cls.test_id + "*")):
|
2023-11-01 14:44:42 +00:00
|
|
|
attach_allure_file(file)
|
2023-11-01 12:02:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function", autouse=True)
|
2023-11-17 06:47:22 +00:00
|
|
|
def close_open_nodes(attach_logs_on_fail):
|
2023-11-01 12:02:29 +00:00
|
|
|
DS.waku_nodes = []
|
|
|
|
yield
|
2023-11-17 06:47:22 +00:00
|
|
|
crashed_containers = []
|
2023-11-01 12:02:29 +00:00
|
|
|
for node in DS.waku_nodes:
|
2023-11-17 06:47:22 +00:00
|
|
|
try:
|
|
|
|
node.stop()
|
|
|
|
except Exception as ex:
|
|
|
|
if "No such container" in str(ex):
|
|
|
|
crashed_containers.append(node.image)
|
|
|
|
logger.error(f"Failed to stop container because of error {ex}")
|
|
|
|
assert not crashed_containers, f"Containers {crashed_containers} crashed during the test!!!"
|