#!/usr/bin/env python3 """ SP NETWORKING ACADEMY BGP REGRESSION TEST SUITE - VERSION 1.0 Executes BGP-001 through BGP-010 sequentially. Features: - Individual test start and end timestamps - Individual test execution duration - Live terminal output - PASS / FAIL / ERROR / SKIPPED status - Complete suite execution duration - Single consolidated timestamped report """ import sys import time import subprocess from pathlib import Path from datetime import datetime # --------------------------------------------------------- # PROJECT PATHS # --------------------------------------------------------- BASE_DIR = Path(__file__).resolve().parent TEST_DIR = BASE_DIR / "tests" REPORT_DIR = BASE_DIR / "REPORTS" REPORT_DIR.mkdir(parents=True, exist_ok=True) # --------------------------------------------------------- # TEST SEQUENCE # --------------------------------------------------------- TESTS = [ ("BGP-001", "test_bgp_001_identity.py"), ("BGP-002", "test_bgp_002_config.py"), ("BGP-003", "test_bgp_003_neighbors.py"), ("BGP-004", "test_bgp_004_routes.py"), ("BGP-005", "test_bgp_005_best_path.py"), ("BGP-006", "test_bgp_006_attributes.py"), ("BGP-007", "test_bgp_007_next_hop.py"), ("BGP-008", "test_bgp_008_failure_recovery.py"), ("BGP-009", "test_bgp_009_route_advertisement_withdrawal.py"), ("BGP-010", "test_bgp_010_prefix_list_filtering.py"), ] # --------------------------------------------------------- # REPORT HELPERS # --------------------------------------------------------- report_lines = [] def log(message=""): """Print and store a report line.""" print(message, flush=True) report_lines.append(str(message)) def timestamp(): return datetime.now().strftime("%Y-%m-%d %H:%M:%S") def duration_text(seconds): minutes = int(seconds // 60) remaining_seconds = seconds % 60 return f"{minutes} min {remaining_seconds:.2f} sec" def classify_result(return_code, output): """ Determine test status. Explicit FINAL STATUS takes priority. Otherwise use the process return code. """ upper_output = output.upper() if "FINAL STATUS: FAIL" in upper_output: return "FAIL" if "FINAL STATUS: PASS" in upper_output: return "PASS" if return_code != 0: return "FAIL" return "PASS" # --------------------------------------------------------- # MAIN REGRESSION EXECUTION # --------------------------------------------------------- def main(): suite_start = datetime.now() suite_start_monotonic = time.monotonic() timestamp_id = suite_start.strftime("%Y%m%d_%H%M%S") consolidated_report = ( REPORT_DIR / f"BGP-REGRESSION-{timestamp_id}.txt" ) results = [] passed = 0 failed = 0 errors = 0 skipped = 0 log("=" * 90) log("SP NETWORKING ACADEMY - BGP REGRESSION TEST SUITE") log("VERSION 1.0") log("=" * 90) log(f"Execution started : {suite_start.strftime('%Y-%m-%d %H:%M:%S')}") log(f"Project directory : {BASE_DIR}") log(f"Test directory : {TEST_DIR}") log("Test range : BGP-001 to BGP-010") log("Execution mode : Sequential") log(f"Total test cases : {len(TESTS)}") log("=" * 90) # ----------------------------------------------------- # EXECUTE EACH TEST # ----------------------------------------------------- for test_id, filename in TESTS: script_path = TEST_DIR / filename log("\n") log("#" * 90) log(f"{test_id} - STARTING TEST") log("#" * 90) test_start = datetime.now() test_start_monotonic = time.monotonic() log(f"Start time : {test_start.strftime('%Y-%m-%d %H:%M:%S')}") log(f"Script : {filename}") if not script_path.is_file(): test_end = datetime.now() elapsed = time.monotonic() - test_start_monotonic status = "SKIPPED" skipped += 1 log(f"[SKIPPED] Script not found: {script_path}") results.append({ "id": test_id, "filename": filename, "start": test_start, "end": test_end, "duration": elapsed, "status": status, "return_code": None, }) continue output_lines = [] status = "ERROR" return_code = None try: process = subprocess.Popen( [sys.executable, str(script_path)], cwd=str(TEST_DIR), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1, errors="replace", ) # Live output display and capture for line in process.stdout: print(line, end="", flush=True) output_lines.append(line) report_lines.append(line.rstrip("\n")) return_code = process.wait() complete_output = "".join(output_lines) status = classify_result( return_code, complete_output ) if status == "PASS": passed += 1 else: failed += 1 except Exception as exc: status = "ERROR" errors += 1 log(f"[ERROR] Execution exception: {exc}") test_end = datetime.now() elapsed = time.monotonic() - test_start_monotonic log("") log(f"{test_id} EXECUTION RESULT") log("-" * 90) log(f"Start time : {test_start.strftime('%Y-%m-%d %H:%M:%S')}") log(f"End time : {test_end.strftime('%Y-%m-%d %H:%M:%S')}") log(f"Duration : {duration_text(elapsed)}") log(f"Exit code : {return_code}") log(f"Status : {status}") results.append({ "id": test_id, "filename": filename, "start": test_start, "end": test_end, "duration": elapsed, "status": status, "return_code": return_code, }) # ----------------------------------------------------- # FINAL SUITE SUMMARY # ----------------------------------------------------- suite_end = datetime.now() total_duration = time.monotonic() - suite_start_monotonic log("\n") log("=" * 90) log("BGP REGRESSION TEST SUITE - FINAL SUMMARY") log("=" * 90) log("") log("INDIVIDUAL TEST RESULTS") log("-" * 90) for item in results: log( f"{item['id']} | " f"{item['status']:<8} | " f"Duration: {duration_text(item['duration'])} | " f"Exit code: {item['return_code']}" ) log("") log("=" * 90) log("REGRESSION EXECUTION SUMMARY") log("=" * 90) log(f"Total test cases : {len(TESTS)}") log(f"Tests executed : {len(results)}") log(f"Tests passed : {passed}") log(f"Tests failed : {failed}") log(f"Execution errors : {errors}") log(f"Tests skipped : {skipped}") log("") log(f"Suite start time : {suite_start.strftime('%Y-%m-%d %H:%M:%S')}") log(f"Suite end time : {suite_end.strftime('%Y-%m-%d %H:%M:%S')}") log(f"Total duration : {duration_text(total_duration)}") if ( len(results) == len(TESTS) and passed == len(TESTS) and failed == 0 and errors == 0 and skipped == 0 ): final_status = "PASS" elif errors > 0: final_status = "ERROR" elif skipped > 0: final_status = "INCOMPLETE" else: final_status = "FAIL" log("") log(f"FINAL REGRESSION STATUS: {final_status}") log("") log("=" * 90) log(f"Consolidated report: {consolidated_report}") log("=" * 90) # ----------------------------------------------------- # SAVE SINGLE CONSOLIDATED REPORT # ----------------------------------------------------- with consolidated_report.open( "w", encoding="utf-8" ) as report: report.write("\n".join(report_lines)) report.write("\n") print("\nRegression report successfully saved.") print(f"Report: {consolidated_report}") # Non-zero exit code if suite is not fully successful return 0 if final_status == "PASS" else 1 if __name__ == "__main__": sys.exit(main())