| 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | # |
| 5 | # SPDX-License-Identifier: GPL-2.0-only |
| 6 | |
| 7 | import os |
| 8 | import subprocess |
| 9 | import re |
| 10 | import shutil |
| 11 | import sys |
| 12 | |
| 13 | test_path = os.path.dirname(os.path.abspath(__file__)) + "/" |
| 14 | test_utils_path = test_path |
| 15 | for i in range(4): |
| 16 | test_utils_path = os.path.dirname(test_utils_path) |
| 17 | test_utils_path = test_utils_path + "/utils" |
| 18 | sys.path.append(test_utils_path) |
| 19 | from test_utils import * |
| 20 | |
| 21 | |
| 22 | def check_ust_test_demo2_event(event_line, expected_int_field_value): |
| 23 | match = re.search(r".*ust_tests_demo2:loop.*", event_line) |
| 24 | if match is None: |
| 25 | return False |
| 26 | match = re.search(r".*intfield = (\d+)", event_line) |
| 27 | if match is None or int(match.group(1)) != expected_int_field_value: |
| 28 | return False |
| 29 | match = re.search(r".*longfield = (\d+)", event_line) |
| 30 | if match is None or int(match.group(1)) != expected_int_field_value: |
| 31 | return False |
| 32 | match = re.search(r".*netintfield = (\d+)", event_line) |
| 33 | if match is None or int(match.group(1)) != expected_int_field_value: |
| 34 | return False |
| 35 | match = re.search(r".*intfield2 = 0x(\d+)", event_line) |
| 36 | if match is None or int(match.group(1)) != expected_int_field_value: |
| 37 | return False |
| 38 | match = re.search(r".*netintfieldhex = 0x(\d+)", event_line) |
| 39 | if match is None or int(match.group(1)) != expected_int_field_value: |
| 40 | return False |
| 41 | match = re.search(r".*floatfield = (\d+)", event_line) |
| 42 | if match is None or int(match.group(1)) != 2222: |
| 43 | return False |
| 44 | match = re.search(r".*doublefield = (\d+)", event_line) |
| 45 | if match is None or int(match.group(1)) != 2: |
| 46 | return False |
| 47 | match = re.search(r".*_seqfield1_length = (\d+)", event_line) |
| 48 | if match is None or int(match.group(1)) != 4: |
| 49 | return False |
| 50 | match = re.search( |
| 51 | r".*seqfield1 = \[ \[0\] = (\d+), \[1\] = (\d+), \[2\] = (\d+), \[3\] = (\d+) \]", |
| 52 | event_line, |
| 53 | ) |
| 54 | if ( |
| 55 | match is None |
| 56 | or int(match.group(1)) != 116 |
| 57 | or int(match.group(2)) != 101 |
| 58 | or int(match.group(3)) != 115 |
| 59 | or int(match.group(4)) != 116 |
| 60 | ): |
| 61 | return False |
| 62 | match = re.search( |
| 63 | r".*arrfield1 = \[ \[0\] = (\d), \[1\] = (\d), \[2\] = (\d) \]", event_line |
| 64 | ) |
| 65 | if ( |
| 66 | match is None |
| 67 | or int(match.group(1)) != 1 |
| 68 | or int(match.group(2)) != 2 |
| 69 | or int(match.group(3)) != 3 |
| 70 | ): |
| 71 | return False |
| 72 | match = re.search(r".*arrfield2 = \"([a-z]*)\"", event_line) |
| 73 | if match is None or match.group(1) != "test": |
| 74 | return False |
| 75 | match = re.search(r".*_seqfield2_length = (\d+)", event_line) |
| 76 | if match is None or int(match.group(1)) != 4: |
| 77 | return False |
| 78 | match = re.search(r".*seqfield2 = \"([a-z]*)\"", event_line) |
| 79 | if match is None or match.group(1) != "test": |
| 80 | return False |
| 81 | match = re.search(r".*stringfield = \"([a-z]*)\"", event_line) |
| 82 | if match is None or match.group(1) != "test": |
| 83 | return False |
| 84 | |
| 85 | return True |
| 86 | |
| 87 | |
| 88 | NR_TESTS = 0 |
| 89 | DYNAMIC_TEST_ENABLED = False |
| 90 | |
| 91 | test_executables = [test_path + "demo_static", test_path + "demo_builtin"] |
| 92 | if os.path.exists(test_path + "demo"): |
| 93 | test_executables.append(test_path + "demo_preload") |
| 94 | NR_TESTS = 2 |
| 95 | DYNAMIC_TEST_ENABLED = True |
| 96 | |
| 97 | # Only enable tests that were compiled successfully |
| 98 | test_executables = [ |
| 99 | executable for executable in test_executables if os.path.exists(executable) |
| 100 | ] |
| 101 | |
| 102 | NR_TESTS += len(test_executables) * 10 |
| 103 | |
| 104 | current_test = 1 |
| 105 | print("1..{0}".format(NR_TESTS)) |
| 106 | |
| 107 | if NR_TESTS == 0: |
| 108 | print("# No test binary found") |
| 109 | exit(-1) |
| 110 | |
| 111 | # Check if a sessiond is running... bail out if none found. |
| 112 | if session_daemon_alive() == 0: |
| 113 | bail( |
| 114 | 'No sessiond running. Please make sure you are running this test with the "run" shell script and verify that the lttng tools are properly installed.' |
| 115 | ) |
| 116 | |
| 117 | if DYNAMIC_TEST_ENABLED: |
| 118 | session_info = create_session() |
| 119 | enable_ust_tracepoint_event(session_info, "ust_tests_demo*") |
| 120 | start_session(session_info) |
| 121 | |
| 122 | # Dry run, no events should be logged |
| 123 | demo_process = subprocess.Popen( |
| 124 | test_path + "demo", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL |
| 125 | ) |
| 126 | demo_process.wait() |
| 127 | stop_session(session_info) |
| 128 | |
| 129 | print_test_result( |
| 130 | demo_process.returncode == 0, |
| 131 | current_test, |
| 132 | "Running application dynamically linked to providers, no preload", |
| 133 | ) |
| 134 | current_test += 1 |
| 135 | trace_path = os.path.join(session_info.trace_path, "ust", "uid") |
| 136 | print_test_result( |
| 137 | not os.path.exists(trace_path), |
| 138 | current_test, |
| 139 | "No events logged when running demo application without preloading providers", |
| 140 | ) |
| 141 | current_test += 1 |
| 142 | |
| 143 | shutil.rmtree(session_info.tmp_directory) |
| 144 | |
| 145 | for executable in test_executables: |
| 146 | executable_name = os.path.basename(executable) |
| 147 | session_info = create_session() |
| 148 | enable_ust_tracepoint_event(session_info, "ust_tests_demo*") |
| 149 | start_session(session_info) |
| 150 | |
| 151 | demo_process = subprocess.Popen( |
| 152 | executable, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL |
| 153 | ) |
| 154 | demo_process.wait() |
| 155 | stop_session(session_info) |
| 156 | |
| 157 | trace_found = os.path.exists(session_info.trace_path) |
| 158 | print_test_result( |
| 159 | trace_found, current_test, "{0}, resulting trace found".format(executable_name) |
| 160 | ) |
| 161 | current_test += 1 |
| 162 | |
| 163 | if not trace_found: |
| 164 | print("# Skipping " + executable_name + " trace verification tests") |
| 165 | continue |
| 166 | |
| 167 | try: |
| 168 | babeltrace_process = subprocess.Popen( |
| 169 | [BABELTRACE_BIN, session_info.trace_path], |
| 170 | stdout=subprocess.PIPE, |
| 171 | stderr=subprocess.PIPE, |
| 172 | ) |
| 173 | except FileNotFoundError: |
| 174 | bail( |
| 175 | "Could not open {}. Please make sure it is installed.".format( |
| 176 | BABELTRACE_BIN |
| 177 | ) |
| 178 | ) |
| 179 | |
| 180 | # We should find 8 events in the resulting trace |
| 181 | event_entries = [] |
| 182 | for event_line in babeltrace_process.stdout: |
| 183 | event_line = event_line.decode("utf-8").replace("\n", "") |
| 184 | event_entries.append(event_line) |
| 185 | |
| 186 | if len(event_entries) != 8: |
| 187 | bail( |
| 188 | "{0}, wrong number of events found in resulting trace.".format( |
| 189 | executable_name |
| 190 | ) |
| 191 | ) |
| 192 | |
| 193 | shutil.rmtree(session_info.tmp_directory) |
| 194 | |
| 195 | print_test_result( |
| 196 | len(event_entries) == 8, |
| 197 | current_test, |
| 198 | "{0}, total number of events logged is correct".format(executable_name), |
| 199 | ) |
| 200 | current_test += 1 |
| 201 | |
| 202 | # Check each loop event |
| 203 | match = re.search(r".*ust_tests_demo:starting.*value = (\d+) ", event_entries[0]) |
| 204 | print_test_result( |
| 205 | match is not None and (int(match.group(1)) == 123), |
| 206 | current_test, |
| 207 | "{0}, ust_tests_demo:starting event found in trace with a correct integer argument".format( |
| 208 | executable_name |
| 209 | ), |
| 210 | ) |
| 211 | current_test += 1 |
| 212 | |
| 213 | for i in range(5): |
| 214 | print_test_result( |
| 215 | check_ust_test_demo2_event(event_entries[i + 1], i), |
| 216 | current_test, |
| 217 | "{0}, ust_tests_demo2:loop event found in trace and arguments are correct, iteration ".format( |
| 218 | executable_name |
| 219 | ) |
| 220 | + str(i + 1), |
| 221 | ) |
| 222 | current_test += 1 |
| 223 | |
| 224 | match = re.search(r".*ust_tests_demo:done.*value = (\d+)", event_entries[6]) |
| 225 | print_test_result( |
| 226 | match is not None and (int(match.group(1)) == 456), |
| 227 | current_test, |
| 228 | "{0}, ust_tests_demo:done event found in resulting trace with a correct integer argument".format( |
| 229 | executable_name |
| 230 | ), |
| 231 | ) |
| 232 | current_test += 1 |
| 233 | |
| 234 | match = re.search(r".*ust_tests_demo3:done.*value = (\d+)", event_entries[7]) |
| 235 | print_test_result( |
| 236 | match is not None and (int(match.group(1)) == 42), |
| 237 | current_test, |
| 238 | "{0}, ust_tests_demo3:done event found in resulting trace with a correct integer argument".format( |
| 239 | executable_name |
| 240 | ), |
| 241 | ) |
| 242 | current_test += 1 |