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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
from dataclasses import dataclass
from enum import Enum
import json
from pathlib import Path
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
step_component: str
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(
str(Path(settings.REPORTING_FILE_DIRECTORY).joinpath(settings.HTML_REPORTING_FILE_NAME)))
report_dict = {
'usecase': usecase,
'details': details,
'components': components,
'steps': [
{
'description': step_report.step_description,
'status': step_report.step_execution_status.value,
'duration': step_report.step_execution_duration,
'component': step_report.step_component
}
for step_report in reversed(self.report)
]
}
with (Path(settings.REPORTING_FILE_DIRECTORY).joinpath(settings.JSON_REPORTING_FILE_NAME)).open('w') as file:
json.dump(report_dict, file, indent=4)
|