Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cmd/install
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ source venv/bin/activate

pip3 install -r framework/requirements.txt

deactivate
# Dependency for printing reports to pdf
# required by python package weasyprint
sudo apt-get install libpangocairo-1.0-0

deactivate
90 changes: 90 additions & 0 deletions framework/python/src/common/testreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"""Store previous test run information."""

from datetime import datetime
from weasyprint import HTML
from io import BytesIO

DATE_TIME_FORMAT = '%Y-%m-%d %H:%M:%S'

Expand Down Expand Up @@ -82,3 +84,91 @@ def from_json(self, json_file):
self.add_test(test_result)

return self

# Create a pdf file in memory and return the bytes
def to_pdf(self):
# Resolve the data as html first
report_html = self.to_html()

# Convert HTML to PDF in memory using weasyprint
pdf_bytes = BytesIO()
HTML(string=report_html).write_pdf(pdf_bytes)
return pdf_bytes

def to_html(self):
json_data = self.to_json()
return f'''
<!DOCTYPE html>
<html lang="en">
{self.generate_header()}
<body>
<h1>Test Results Summary</h1>

<div class="summary">
<h2>Device Information</h2>
<p><strong>MAC Address:</strong> {json_data["device"]["mac_addr"]}</p>
<p><strong>Manufacturer:</strong> {json_data["device"]["manufacturer"] or "Unknown"}</p>
<p><strong>Model:</strong> {json_data["device"]["model"]}</p>
</div>

<h2>Test Results</h2>
{self.generate_test_sections(json_data)}
</body>
</html>
'''

def generate_test_sections(self,json_data):
results = json_data["tests"]["results"]
sections = ""
for result in results:
sections += self.generate_test_section(result)
return sections

def generate_test_section(self, result):
section_content = '<section class="test-section">\n'
for key, value in result.items():
if value is not None: # Check if the value is not None
formatted_key = key.replace('_', ' ').title() # Replace underscores and capitalize
section_content += f'<p><strong>{formatted_key}:</strong> {value}</p>\n'
section_content += '</section>\n<div style="margin-bottom: 40px;"></div>\n'
return section_content

def generate_header(self):
return f'''
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Results Summary</title>
<style>
{self.generate_css()}
</style>
</head>
'''

def generate_css(self):
return '''
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
margin-bottom: 10px;
}
.summary {
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 20px;
background-color: #f5f5f5;
}
.test-list {
list-style: none;
padding: 0;
}
.test-item {
margin-bottom: 10px;
}
.test-link {
text-decoration: none;
color: #007bff;
}
'''
29 changes: 21 additions & 8 deletions framework/python/src/test_orc/test_orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def run_test_modules(self):
report = TestReport().from_json(self._generate_report())
device.add_report(report)

self._write_reports(report)

self._test_in_progress = False
self._timestamp_results(device)

Expand All @@ -95,6 +97,25 @@ def run_test_modules(self):
LOGGER.debug("Old test results cleaned")
self._test_in_progress = False

def _write_reports(self, test_report):
out_dir = os.path.join(
self._root_path, RUNTIME_DIR,
self._session.get_target_device().mac_addr.replace(":", ""))

# Write the json report
with open(os.path.join(out_dir,"report.json"),"w", encoding="utf-8") as f:
json.dump(test_report.to_json(), f, indent=2)

# Write the html report
with open(os.path.join(out_dir,"report.html"),"w", encoding="utf-8") as f:
f.write(test_report.to_html())

# Write the pdf report
with open(os.path.join(out_dir,"report.pdf"),"wb") as f:
f.write(test_report.to_pdf().getvalue())

util.run_command(f"chown -R {self._host_user} {out_dir}")

def _generate_report(self):

report = {}
Expand All @@ -105,14 +126,6 @@ def _generate_report(self):
"%Y-%m-%d %H:%M:%S")
report["status"] = self._calculate_result()
report["tests"] = self._session.get_report_tests()
out_file = os.path.join(
self._root_path, RUNTIME_DIR,
self._session.get_target_device().mac_addr.replace(":", ""),
"report.json")

with open(out_file, "w", encoding="utf-8") as f:
json.dump(report, f, indent=2)
util.run_command(f"chown -R {self._host_user} {out_file}")
return report

def _calculate_result(self):
Expand Down
3 changes: 3 additions & 0 deletions framework/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ ipaddress
netifaces
scapy

# Requirments for the test_orc module
weasyprint

# Requirements for the API
fastapi==0.99.1
psutil
Expand Down