|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import os.path |
| 4 | +import requests |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +StrPath = str | Path |
| 8 | + |
| 9 | + |
| 10 | +def check_status(response: requests.models.Response): |
| 11 | + if response.status_code != 200: |
| 12 | + print(f"Error {response.status_code}: {response.json()['error']}") |
| 13 | + return True |
| 14 | + return False |
| 15 | + |
| 16 | + |
| 17 | +def test_runs_list(args): |
| 18 | + r = requests.get(f"{args.url}/api/v1/test-runs") |
| 19 | + if check_status(r): |
| 20 | + return |
| 21 | + print("Tests runs on the server:") |
| 22 | + print(json.dumps(r.json(), indent=4)) |
| 23 | + |
| 24 | + |
| 25 | +def test_run_trigger(args): |
| 26 | + params = { |
| 27 | + "config_name": args.test_config, |
| 28 | + "test_suite_name": args.test_suite or "" |
| 29 | + } |
| 30 | + r = requests.post(f"{args.url}/api/v1/test-runs", json=params) |
| 31 | + if check_status(r): |
| 32 | + return |
| 33 | + print("Triggered a test run:") |
| 34 | + print(json.dumps(r.json(), indent=4)) |
| 35 | + |
| 36 | + |
| 37 | +def test_run_info(args): |
| 38 | + r = requests.get(f"{args.url}/api/v1/test-runs/{args.id}") |
| 39 | + if check_status(r): |
| 40 | + return |
| 41 | + print(f"Test info fetched from the server:") |
| 42 | + print(json.dumps(r.json(), indent=4)) |
| 43 | + |
| 44 | + |
| 45 | +def test_run_abort(args): |
| 46 | + r = requests.delete(f"{args.url}/api/v1/test-runs/{args.id}") |
| 47 | + if check_status(r): |
| 48 | + return |
| 49 | + print(f"Test deleted from the server:") |
| 50 | + print(json.dumps(r.json(), indent=4)) |
| 51 | + |
| 52 | + |
| 53 | +def test_run_report(args): |
| 54 | + r = requests.get(f"{args.url}/api/v1/test-runs/{args.id}/report") |
| 55 | + if check_status(r): |
| 56 | + return |
| 57 | + save_path = os.path.join(args.report_dir, f"{args.id}.csv") |
| 58 | + try: |
| 59 | + with open(save_path, "wb") as file: |
| 60 | + file.write(r.content) |
| 61 | + print(f"Report written to {save_path}") |
| 62 | + except Exception as e: |
| 63 | + print(f"Failed to save the report: {str(e)}") |
| 64 | + |
| 65 | + |
| 66 | +def test_run_artifact(args): |
| 67 | + r = requests.get( |
| 68 | + f"{args.url}/api/v1/test-runs/{args.id}/artifacts/{args.name}") |
| 69 | + if check_status(r): |
| 70 | + return |
| 71 | + save_path = os.path.join(args.artifact_dir, args.name) |
| 72 | + try: |
| 73 | + with open(save_path, "wb") as file: |
| 74 | + file.write(r.content) |
| 75 | + print(f"Artifact written to {save_path}") |
| 76 | + except Exception as e: |
| 77 | + print(f"Failed to save the artifact: {str(e)}") |
| 78 | + |
| 79 | + |
| 80 | +def add_runs_parser(parser: argparse._SubParsersAction): |
| 81 | + |
| 82 | + runs = parser.add_parser("runs", help="Test runs management") |
| 83 | + sub = runs.add_subparsers(required=True, title="Test runs commands") |
| 84 | + |
| 85 | + list = sub.add_parser("list", help="List test runs") |
| 86 | + list.set_defaults(func=test_runs_list) |
| 87 | + |
| 88 | + run = sub.add_parser("run", help="Trigger a test run") |
| 89 | + run.set_defaults(func=test_run_trigger) |
| 90 | + run.add_argument( |
| 91 | + "--test-config", |
| 92 | + required=True, |
| 93 | + help="Test config name for this test run", |
| 94 | + ) |
| 95 | + run.add_argument( |
| 96 | + "--test-suite", |
| 97 | + help="Test suite name for this test run", |
| 98 | + ) |
| 99 | + |
| 100 | + info = sub.add_parser("info", help="Information about a test run") |
| 101 | + info.set_defaults(func=test_run_info) |
| 102 | + info.add_argument("--id", type=str, required=True, help="Test run ID") |
| 103 | + |
| 104 | + abort = sub.add_parser("abort", help="Abort a pending test run") |
| 105 | + abort.set_defaults(func=test_run_abort) |
| 106 | + abort.add_argument("--id", type=str, required=True, help="Test run ID") |
| 107 | + |
| 108 | + report = sub.add_parser("report", help="Fetch test run report") |
| 109 | + report.set_defaults(func=test_run_report) |
| 110 | + report.add_argument("--id", type=str, required=True, help="Test run ID") |
| 111 | + |
| 112 | + artifact = sub.add_parser("artifact", help="Fetch test run artifact") |
| 113 | + artifact.set_defaults(func=test_run_artifact) |
| 114 | + artifact.add_argument("--id", type=str, required=True, help="Test run ID") |
| 115 | + artifact.add_argument("--name", |
| 116 | + type=str, |
| 117 | + required=True, |
| 118 | + help="Artifact name") |
0 commit comments