aboutsummaryrefslogtreecommitdiffstats
path: root/src/onaptests/steps/reports_collection.py
blob: 4b7f79bb2ccda49855fdcbe6b8c7fcdd9bcdd726 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from dataclasses import dataclass
from enum import Enum
from typing import List
from jinja2 import Environment, FileSystemLoader, select_autoescape
from onapsdk.configuration import settings
from onapsdk.exceptions import SettingsError
from onaptests.utils.resources import get_resource_location


class ReportStepStatus(Enum):
    """Enum which stores steps execution statuses."""
    PASS = "PASS"
    FAIL = "FAIL"
    NOT_EXECUTED = "NOT EXECUTED"


@dataclass
class Report:
    """Step execution report."""
    step_description: str
    step_execution_status: ReportStepStatus
    step_execution_duration: float


class ReportsCollection:
    """Collection to store steps execution statuses."""

    def __init__(self) -> None:
        """Initialize collection."""
        self._collection: list = []

    def put(self, item: Report) -> None:
        """Put execution status dictionary.

        Args:
            item (Report): Step report

        """
        self._collection.insert(0, item)

    @property
    def report(self) -> List[Report]:
        """Get report.

        Build a dictionary with execution statuses.

        Returns:
            List[str, str]: Steps name with status dictionary

        """
        return self._collection

    @property
    def failed_steps_num(self) -> int:
        """Number of failed steps in report.

        Returns:
            int: How many steps failed

        """
        return sum((1 for step_report in self.report if
                    step_report.step_execution_status == ReportStepStatus.FAIL))

    def generate_report(self) -> None:
        usecase = settings.SERVICE_NAME
        try:
            details = settings.SERVICE_DETAILS
        except (KeyError, AttributeError, SettingsError):
            details = ""

        try:
            components = settings.SERVICE_COMPONENTS
        except (KeyError, AttributeError, SettingsError):
            components = ""

        jinja_env = Environment(
            autoescape=select_autoescape(['html']),
            loader=FileSystemLoader(get_resource_location('templates/reporting')))

        jinja_env.get_template('reporting.html.j2').stream(
            report=self,
            usecase=usecase,
            details=details,
            components=components,
            log_path="./pythonsdk.debug.log").dump(
            settings.REPORTING_FILE_PATH)