|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-License-Identifier: BSD-3-Clause |
| 3 | +# Copyright 2024-2025 Intel Corporation |
| 4 | +# Media Communications Mesh |
| 5 | + |
| 6 | +import csv |
| 7 | +import logging |
| 8 | +import os |
| 9 | +from datetime import datetime |
| 10 | +from pathlib import Path |
| 11 | +from typing import Dict, List, Optional |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | +# Constants |
| 16 | +CSV_REPORT_FILE = "test_report.csv" |
| 17 | +COMPLIANCE_REPORT_FILE = "compliance_report.csv" |
| 18 | +TEST_RESULT_COLUMN = "Test Result" |
| 19 | +TEST_NAME_COLUMN = "Test Name" |
| 20 | +TEST_DESC_COLUMN = "Test Description" |
| 21 | +TEST_TIME_COLUMN = "Test Time" |
| 22 | +TEST_DURATION_COLUMN = "Test Duration" |
| 23 | +TEST_ERROR_COLUMN = "Error Message" |
| 24 | +COMPONENT_COLUMN = "Component" |
| 25 | + |
| 26 | +# Components |
| 27 | +COMPONENT_FFMPEG = "FFmpeg" |
| 28 | +COMPONENT_MEDIA_PROXY = "Media Proxy" |
| 29 | +COMPONENT_MESH_AGENT = "Mesh Agent" |
| 30 | +COMPONENT_RXTX_APP = "RxTx App" |
| 31 | + |
| 32 | +# Standard column headers |
| 33 | +CSV_HEADERS = [ |
| 34 | + TEST_NAME_COLUMN, |
| 35 | + TEST_DESC_COLUMN, |
| 36 | + COMPONENT_COLUMN, |
| 37 | + TEST_RESULT_COLUMN, |
| 38 | + TEST_TIME_COLUMN, |
| 39 | + TEST_DURATION_COLUMN, |
| 40 | + TEST_ERROR_COLUMN, |
| 41 | +] |
| 42 | + |
| 43 | +# Global storage for test results |
| 44 | +_test_results = [] |
| 45 | +_compliance_results = {} |
| 46 | + |
| 47 | +def initialize_csv_report(log_dir: Optional[str] = None) -> None: |
| 48 | + """ |
| 49 | + Initialize CSV report files. |
| 50 | + |
| 51 | + Args: |
| 52 | + log_dir: Directory where report files will be stored |
| 53 | + """ |
| 54 | + global _test_results, _compliance_results |
| 55 | + _test_results = [] |
| 56 | + _compliance_results = {} |
| 57 | + |
| 58 | + # Create log directory if it doesn't exist |
| 59 | + if log_dir: |
| 60 | + Path(log_dir).mkdir(parents=True, exist_ok=True) |
| 61 | + |
| 62 | + |
| 63 | +def csv_add_test( |
| 64 | + test_name: str, |
| 65 | + test_description: str, |
| 66 | + component: str, |
| 67 | + result: bool, |
| 68 | + duration: float, |
| 69 | + error_message: str = "", |
| 70 | +) -> None: |
| 71 | + """ |
| 72 | + Add a test result to the CSV report. |
| 73 | + |
| 74 | + Args: |
| 75 | + test_name: Name of the test |
| 76 | + test_description: Description of the test |
| 77 | + component: Component being tested (FFmpeg, Media Proxy, Mesh Agent, RxTx App) |
| 78 | + result: True if test passed, False if test failed |
| 79 | + duration: Test duration in seconds |
| 80 | + error_message: Error message if test failed |
| 81 | + """ |
| 82 | + global _test_results |
| 83 | + |
| 84 | + test_result = "PASS" if result else "FAIL" |
| 85 | + test_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 86 | + |
| 87 | + _test_results.append({ |
| 88 | + TEST_NAME_COLUMN: test_name, |
| 89 | + TEST_DESC_COLUMN: test_description, |
| 90 | + COMPONENT_COLUMN: component, |
| 91 | + TEST_RESULT_COLUMN: test_result, |
| 92 | + TEST_TIME_COLUMN: test_time, |
| 93 | + TEST_DURATION_COLUMN: f"{duration:.2f}s", |
| 94 | + TEST_ERROR_COLUMN: error_message |
| 95 | + }) |
| 96 | + |
| 97 | + logger.info(f"Added test result: {test_name} - {test_result}") |
| 98 | + |
| 99 | + |
| 100 | +def update_compliance_result(requirement_id: str, is_compliant: bool) -> None: |
| 101 | + """ |
| 102 | + Update compliance result for a specific requirement. |
| 103 | + |
| 104 | + Args: |
| 105 | + requirement_id: ID of the requirement |
| 106 | + is_compliant: True if compliant, False if not |
| 107 | + """ |
| 108 | + global _compliance_results |
| 109 | + |
| 110 | + _compliance_results[requirement_id] = is_compliant |
| 111 | + logger.info(f"Updated compliance for {requirement_id}: {'COMPLIANT' if is_compliant else 'NON-COMPLIANT'}") |
| 112 | + |
| 113 | + |
| 114 | +def csv_write_report(log_dir: str = ".") -> None: |
| 115 | + """ |
| 116 | + Write test results and compliance results to CSV files. |
| 117 | + |
| 118 | + Args: |
| 119 | + log_dir: Directory where report files will be stored |
| 120 | + """ |
| 121 | + report_path = Path(log_dir) / CSV_REPORT_FILE |
| 122 | + compliance_path = Path(log_dir) / COMPLIANCE_REPORT_FILE |
| 123 | + |
| 124 | + # Write test results |
| 125 | + if _test_results: |
| 126 | + try: |
| 127 | + with open(report_path, 'w', newline='') as csvfile: |
| 128 | + writer = csv.DictWriter(csvfile, fieldnames=CSV_HEADERS) |
| 129 | + writer.writeheader() |
| 130 | + for result in _test_results: |
| 131 | + writer.writerow(result) |
| 132 | + logger.info(f"Test report written to {report_path}") |
| 133 | + except Exception as e: |
| 134 | + logger.error(f"Failed to write test report: {e}") |
| 135 | + |
| 136 | + # Write compliance results |
| 137 | + if _compliance_results: |
| 138 | + try: |
| 139 | + with open(compliance_path, 'w', newline='') as csvfile: |
| 140 | + writer = csv.DictWriter(csvfile, fieldnames=['Requirement ID', 'Status']) |
| 141 | + writer.writeheader() |
| 142 | + for req_id, is_compliant in _compliance_results.items(): |
| 143 | + writer.writerow({ |
| 144 | + 'Requirement ID': req_id, |
| 145 | + 'Status': 'COMPLIANT' if is_compliant else 'NON-COMPLIANT' |
| 146 | + }) |
| 147 | + logger.info(f"Compliance report written to {compliance_path}") |
| 148 | + except Exception as e: |
| 149 | + logger.error(f"Failed to write compliance report: {e}") |
| 150 | + |
| 151 | + |
| 152 | +def get_test_summary() -> Dict[str, int | str]: |
| 153 | + """ |
| 154 | + Get a summary of test results. |
| 155 | + |
| 156 | + Returns: |
| 157 | + Dictionary with total, passed, and failed test counts and pass rate |
| 158 | + """ |
| 159 | + pass_count = sum(1 for r in _test_results if r[TEST_RESULT_COLUMN] == "PASS") |
| 160 | + fail_count = sum(1 for r in _test_results if r[TEST_RESULT_COLUMN] == "FAIL") |
| 161 | + total_count = len(_test_results) |
| 162 | + |
| 163 | + return { |
| 164 | + "total": total_count, |
| 165 | + "passed": pass_count, |
| 166 | + "failed": fail_count, |
| 167 | + "pass_rate": f"{(pass_count / total_count * 100):.1f}%" if total_count > 0 else "N/A" |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | +def get_component_summary() -> Dict[str, Dict[str, int | str]]: |
| 172 | + """ |
| 173 | + Get a summary of test results by component. |
| 174 | + |
| 175 | + Returns: |
| 176 | + Dictionary with component-wise test summaries |
| 177 | + """ |
| 178 | + components = {} |
| 179 | + |
| 180 | + for result in _test_results: |
| 181 | + component = result[COMPONENT_COLUMN] |
| 182 | + if component not in components: |
| 183 | + components[component] = {"total": 0, "passed": 0, "failed": 0} |
| 184 | + |
| 185 | + components[component]["total"] += 1 |
| 186 | + if result[TEST_RESULT_COLUMN] == "PASS": |
| 187 | + components[component]["passed"] += 1 |
| 188 | + else: |
| 189 | + components[component]["failed"] += 1 |
| 190 | + |
| 191 | + # Add pass rate |
| 192 | + for component in components: |
| 193 | + total = components[component]["total"] |
| 194 | + passed = components[component]["passed"] |
| 195 | + components[component]["pass_rate"] = f"{(passed / total * 100):.1f}%" if total > 0 else "N/A" |
| 196 | + |
| 197 | + return components |
| 198 | + |
| 199 | + |
| 200 | +def get_compliance_summary() -> Dict[str, int | str]: |
| 201 | + """ |
| 202 | + Get a summary of compliance results. |
| 203 | + |
| 204 | + Returns: |
| 205 | + Dictionary with total, compliant, and non-compliant counts and compliance rate |
| 206 | + """ |
| 207 | + compliant_count = sum(1 for v in _compliance_results.values() if v) |
| 208 | + non_compliant_count = sum(1 for v in _compliance_results.values() if not v) |
| 209 | + total_count = len(_compliance_results) |
| 210 | + |
| 211 | + return { |
| 212 | + "total": total_count, |
| 213 | + "compliant": compliant_count, |
| 214 | + "non_compliant": non_compliant_count, |
| 215 | + "compliance_rate": f"{(compliant_count / total_count * 100):.1f}%" if total_count > 0 else "N/A" |
| 216 | + } |
0 commit comments