| 1 |
#!/usr/bin/python3 |
| 2 |
# This file is part of COVESA Project DLT - Diagnostic Log and Trace. |
| 3 |
# |
| 4 |
# This Source Code Form is subject to the terms of the |
| 5 |
# Mozilla Public License (MPL), v. 2.0. |
| 6 |
# If a copy of the MPL was not distributed with this file, |
| 7 |
# You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
# |
| 9 |
# For further information see http://www.covesa.org/. |
| 10 |
#******************************************************************************** |
| 11 |
#** Author Identity ** |
| 12 |
#******************************************************************************** |
| 13 |
#** ** |
| 14 |
#** Initials Name Company ** |
| 15 |
#** -------- ------------------------- ---------------------------------- ** |
| 16 |
#** aw LUU QUANG MINH BOSCH GLOBAL SOFTWARE TECHNOLOGY ** |
| 17 |
#******************************************************************************** |
| 18 |
# DLT actuator is constructed here for utesting |
| 19 |
# due to the fact that some components need |
| 20 |
# setting up environment in it own scripts. |
| 21 |
# Some scripts is not good handled with exit |
| 22 |
# condition or control, honestly no exit at all, |
| 23 |
# and this can lead to an infinite loop or |
| 24 |
# bash recursive calls making system hang. |
| 25 |
import os |
| 26 |
import subprocess |
| 27 |
import sys |
| 28 |
def run_command(command, logfile, mode='w'): |
| 29 |
command_name = os.path.basename(command[0]) |
| 30 |
print(f"----INFO: Executing {command_name}") |
| 31 |
print(f"----INFO: Logging to {logfile}") |
| 32 |
with open(logfile, mode) as log_file: |
| 33 |
return subprocess.call(command, stdout=log_file, stderr=log_file) |
| 34 |
|
| 35 |
def pre_test_cleanup(): |
| 36 |
# Kill PIDs listed in /tmp/dlt.pid and remove FIFO /tmp/dlt to ensure isolated test environment |
| 37 |
pidfile = "/tmp/dlt.pid" |
| 38 |
fifo = "/tmp/dlt" |
| 39 |
if os.path.isfile(pidfile): |
| 40 |
print(f"----INFO: Found pidfile {pidfile}, attempting to kill listed PIDs") |
| 41 |
try: |
| 42 |
with open(pidfile, 'r') as pf: |
| 43 |
for line in pf: |
| 44 |
line = line.strip() |
| 45 |
if not line: |
| 46 |
continue |
| 47 |
try: |
| 48 |
pid = int(line) |
| 49 |
print(f"----INFO: Killing PID: {pid}") |
| 50 |
try: |
| 51 |
os.kill(pid, 9) |
| 52 |
except Exception: |
| 53 |
pass |
| 54 |
except ValueError: |
| 55 |
continue |
| 56 |
try: |
| 57 |
os.remove(pidfile) |
| 58 |
print(f"----INFO: Removed pidfile {pidfile}") |
| 59 |
except Exception: |
| 60 |
pass |
| 61 |
except Exception as e: |
| 62 |
print(f"----WARNING: Failed to read/cleanup {pidfile}: {e}") |
| 63 |
|
| 64 |
if os.path.exists(fifo): |
| 65 |
try: |
| 66 |
os.remove(fifo) |
| 67 |
print(f"----INFO: Removed FIFO {fifo}") |
| 68 |
except Exception as e: |
| 69 |
print(f"----WARNING: Failed to remove FIFO {fifo}: {e}") |
| 70 |
def run_gtest_teardown(): |
| 71 |
# Try common locations: current working dir, ./test, ./build/test |
| 72 |
candidates = [ |
| 73 |
os.path.join(os.getcwd(), "gtest_dlt_teardown.sh"), |
| 74 |
os.path.join(os.getcwd(), "test", "gtest_dlt_teardown.sh"), |
| 75 |
os.path.join(os.getcwd(), "build", "test", "gtest_dlt_teardown.sh"), |
| 76 |
] |
| 77 |
|
| 78 |
bash_script = None |
| 79 |
for p in candidates: |
| 80 |
if os.path.isfile(p): |
| 81 |
bash_script = p |
| 82 |
break |
| 83 |
|
| 84 |
# Fallback: search recursively under cwd |
| 85 |
if bash_script is None: |
| 86 |
for root, dirs, files in os.walk(os.getcwd()): |
| 87 |
if "gtest_dlt_teardown.sh" in files: |
| 88 |
bash_script = os.path.join(root, "gtest_dlt_teardown.sh") |
| 89 |
break |
| 90 |
|
| 91 |
if bash_script is None: |
| 92 |
print(f"----ERROR: gtest teardown script not found under {os.getcwd()}") |
| 93 |
sys.exit(1) |
| 94 |
|
| 95 |
log_file = os.path.splitext(bash_script)[0] + ".log" |
| 96 |
ret_code = run_command([bash_script], log_file) |
| 97 |
if ret_code != 0: |
| 98 |
print(f"----ERROR: Teardown failed with exit code {ret_code}") |
| 99 |
with open(log_file, 'r') as log: |
| 100 |
print("----INFO: Teardown Log Output:") |
| 101 |
print("\n===== LOG OUTPUT START =====\n") |
| 102 |
print(log.read()) |
| 103 |
print("\n===== LOG OUTPUT END =====\n") |
| 104 |
sys.exit(ret_code) |
| 105 |
else: |
| 106 |
print(f"----INFO: Trigger gtest_dlt_teardown.......... SUCCESS") |
| 107 |
return ret_code |
| 108 |
def run_gtest_binary(gtest_binary): |
| 109 |
binary_name = os.path.basename(gtest_binary) |
| 110 |
gtest_binary_path = os.path.join(os.getcwd(), gtest_binary) |
| 111 |
if not os.path.isfile(gtest_binary_path): |
| 112 |
print(f"----ERROR: gtest binary {gtest_binary_path} does not exist") |
| 113 |
sys.exit(1) |
| 114 |
|
| 115 |
pre_test_cleanup() |
| 116 |
bash_script = gtest_binary_path + ".sh" |
| 117 |
log_file = gtest_binary_path + ".log" |
| 118 |
if os.path.isfile(bash_script): |
| 119 |
print(f"----INFO: Preparing environment") |
| 120 |
log_mode = 'a' |
| 121 |
ret_code = run_command([bash_script], log_file) |
| 122 |
if ret_code != 0: |
| 123 |
print(f"----ERROR: Setup failed with exit code {ret_code}") |
| 124 |
with open(log_file, 'r') as log: |
| 125 |
print("----INFO: Test Log Output:") |
| 126 |
print("\n===== LOG OUTPUT START =====\n") |
| 127 |
print(log.read()) |
| 128 |
print("\n===== LOG OUTPUT END =====\n") |
| 129 |
sys.exit(ret_code) |
| 130 |
else: |
| 131 |
print(f"----INFO: No environment setup, running test") |
| 132 |
log_mode = 'w' |
| 133 |
ret_code = run_command([gtest_binary_path], log_file, mode=log_mode) |
| 134 |
if ret_code != 0: |
| 135 |
print(f"----INFO: Test {binary_name} .......... FAILED") |
| 136 |
with open(log_file, 'r') as log: |
| 137 |
print("----INFO: Test Log Output:") |
| 138 |
print("\n===== LOG OUTPUT START =====\n") |
| 139 |
print(log.read()) |
| 140 |
print("\n===== LOG OUTPUT END =====\n") |
| 141 |
else: |
| 142 |
print(f"----INFO: Test {binary_name} .......... PASSED") |
| 143 |
run_gtest_teardown() |
| 144 |
return ret_code |
| 145 |
if __name__ == "__main__": |
| 146 |
if len(sys.argv) < 2: |
| 147 |
print("----INFO: Usage: python gtest_dlt_actuator.py <gtest_dlt_binary_path>") |
| 148 |
sys.exit(1) |
| 149 |
gtest_binary = sys.argv[1] |
| 150 |
if not os.path.isfile(gtest_binary): |
| 151 |
print(f"----ERROR: gtest binary {gtest_binary} does not exist") |
| 152 |
sys.exit(1) |
| 153 |
exit_code = run_gtest_binary(gtest_binary) |
| 154 |
sys.exit(exit_code) |