Debian Codegraph @

crt/aws-c-common/scripts/appverifier_ctest.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 #
3 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 # SPDX-License-Identifier: Apache-2.0.
5
6 # Built-in
7 import argparse
8 import subprocess
9 import os
10 import json
11 import tempfile
12 import sys
13 import appverifier_xml
14
15 def add_app_verifier_settings(app_verified_executables, app_verifier_tests):
16 for test_executable in app_verified_executables:
17 arguments = ["appverif", "-enable"] + app_verifier_tests + ["-for", test_executable]
18 print (f'Calling AppVerifier with: {subprocess.list2cmdline(arguments)}')
19 # NOTE: Needs elevated permissions. We need this for the XML dump below so I figured we might as well
20 # also set AppVerifier here too then, simplifying the setup and running process
21 subprocess.run(args=arguments)
22
23 def remove_app_verifier_settings(app_verified_executables):
24 for test_executable in app_verified_executables:
25 arguments = ["appverif", "-delete", "settings", "-for", test_executable]
26 print (f'Calling AppVerifier with: {subprocess.list2cmdline(arguments)}')
27 # NOTE: Needs elevated permissions. We need this for the XML dump below so I figured we might as well
28 # also set AppVerifier here too then, simplifying the setup and running process
29 subprocess.run(args=arguments)
30
31 def main():
32 argument_parser = argparse.ArgumentParser(
33 description="AppVerifier Ctest runner util")
34 argument_parser.add_argument("--build_directory", metavar="<Path to CTest project to run>",
35 required=True, default="../aws-c-common-build", help="Path to CMake build folder to run CTest in")
36 parsed_commands = argument_parser.parse_args()
37
38 ctest_execute_directory = parsed_commands.build_directory
39 print (f"CTest execute directory: {ctest_execute_directory}")
40 os.chdir(ctest_execute_directory)
41 print (f"Current working directory {os.getcwd()}")
42 tmp_xml_file_path = os.path.join(tempfile.gettempdir(), "tmp.xml")
43
44 launch_arguments = ["ctest", "--show-only=json-v1"]
45 print (f"Launching CTest with arguments: {subprocess.list2cmdline(launch_arguments)}")
46 ctest_json_output = subprocess.run(args=launch_arguments, capture_output=True, encoding="utf8", check=True)
47
48 output_json = json.loads(ctest_json_output.stdout)
49 test_names = []
50 test_executables = []
51
52 # NOTE: Needs elevated permissions. We need this for the XML dump below so I figured we might as well
53 # also set AppVerifier here too then, simplifying the setup and running process
54 app_verified_executables = []
55 app_verifier_tests = ["Exceptions", "Handles", "Heaps", "Leak", "Locks", "Memory", "SRWLock", "Threadpool", "TLS"]
56
57 json_tests_list = output_json["tests"]
58 for test_data in json_tests_list:
59 test_names.append(test_data["name"])
60 tmp_path = os.path.basename(test_data["command"][0])
61 test_executables.append(tmp_path)
62 if not (tmp_path in app_verified_executables):
63 app_verified_executables.append(tmp_path)
64 if (len(test_names) <= 0):
65 sys.exit("ERROR: No tests found via CTest")
66
67 # Register with AppVerifier
68 add_app_verifier_settings(app_verified_executables, app_verifier_tests)
69
70 # Run all the tests!
71 for i in range(0, len(test_names)):
72 try:
73 print (f"Running test {test_names[i]} ({i}/{len(test_names)})")
74 ctest_args = ["ctest", "-R", "^" + test_names[i] + "$"]
75 print (f"With arguments: {subprocess.list2cmdline(ctest_args)}")
76 subprocess.run(args=ctest_args)
77
78 appverif_xml_dump_args = ["appverif", "-export", "log", "-for", test_executables[i], "-with", "to="+ tmp_xml_file_path]
79 print (f'Calling AppVerifier with: {subprocess.list2cmdline(appverif_xml_dump_args)}')
80 # NOTE: Needs elevated permissions
81 subprocess.run(args=appverif_xml_dump_args)
82
83 xml_result = appverifier_xml.parseXML(tmp_xml_file_path, True)
84 if (xml_result != 0):
85 print (f"ERROR: Test {test_names[i]} - failed!")
86 remove_app_verifier_settings(app_verified_executables)
87 sys.exit(xml_result)
88 finally:
89 # Delete the temporary XML file AppVerifier made on each run, ensuring we have a new one each time.
90 # We cannot use tempfile directly and just pass the path to it, because
91 # AppVerifier freaks out - so we just have to make files in a temporary directory
92 # and delete them when we're finished
93 os.remove(tmp_xml_file_path)
94
95 # Delete AppVerifier settings
96 remove_app_verifier_settings(app_verified_executables)
97
98 print ("SUCCESS: Finished running all tests!")
99
100 if __name__ == "__main__":
101 main()