tests: Split test_ust_constructor into several tests
[lttng-tools.git] / tests / regression / ust / ust-constructor / ust_constructor_common.py
CommitLineData
da1e97c9
MD
1#!/usr/bin/env python3
2#
f506900f
KS
3# SPDX-FileCopyrightText: 2024 Kienan Stewart <kstewart@efficios.com>
4# SPDX-License-Identifer: GPL-2.0-only
da1e97c9 5#
da1e97c9 6
09a872ef 7import copy
da1e97c9
MD
8import pathlib
9import sys
10import os
b8e79f3f 11import subprocess
da1e97c9
MD
12from typing import Any, Callable, Type
13
da1e97c9
MD
14# Import in-tree test utils
15test_utils_import_path = pathlib.Path(__file__).absolute().parents[3] / "utils"
16sys.path.append(str(test_utils_import_path))
17
18import lttngtest
19import bt2
20
b8e79f3f
KS
21# Determine if LTTNG_UST_ALLOCATE_COMPOUND_LITERAL_ON_HEAP is set. This will
22# affect if certain events may or may not be expected when compiling with
23# C++.
24# @see https://github.com/lttng/lttng-ust/blob/47fa3e4ed7ab43e034dc61fc1480f919f4ee51d0/include/lttng/ust-compiler.h#L51
25#
26compound_literal_on_heap = False
27process = subprocess.Popen(
28 [
29 os.path.join(
30 str(test_utils_import_path),
31 "testapp",
32 "gen-ust-events-constructor",
33 "uses_heap",
34 )
35 ]
36)
37process.wait()
38if process.returncode == 0:
39 compound_literal_on_heap = True
40
21b65d7f 41expected_events_common_cpp = [
da1e97c9
MD
42 {
43 "name": "tp:constructor_cplusplus",
44 "msg": "global - across units before define",
45 "count": 0,
b8e79f3f
KS
46 "may_fail": compound_literal_on_heap,
47 },
da1e97c9
MD
48 {
49 "name": "tp:constructor_cplusplus",
50 "msg": "global - same unit before define",
51 "count": 0,
b8e79f3f 52 "may_fail": compound_literal_on_heap,
da1e97c9
MD
53 },
54 {
55 "name": "tp:constructor_cplusplus",
56 "msg": "global - same unit after define",
57 "count": 0,
b8e79f3f
KS
58 "may_fail": compound_literal_on_heap,
59 },
da1e97c9
MD
60 {
61 "name": "tp:constructor_cplusplus",
62 "msg": "global - across units after define",
63 "count": 0,
b8e79f3f
KS
64 "may_fail": compound_literal_on_heap,
65 },
da1e97c9
MD
66 {
67 "name": "tp:constructor_cplusplus",
68 "msg": "global - same unit before provider",
69 "count": 0,
b8e79f3f 70 "may_fail": compound_literal_on_heap,
da1e97c9
MD
71 },
72 {
73 "name": "tp:constructor_cplusplus",
74 "msg": "global - same unit after provider",
75 "count": 0,
76 },
da1e97c9
MD
77 {
78 "name": "tp:constructor_cplusplus",
79 "msg": "global - across units after provider",
80 "count": 0,
81 },
82 {"name": "tp:constructor_cplusplus", "msg": "main() local", "count": 0},
da1e97c9
MD
83 {"name": "tp:destructor_cplusplus", "msg": "main() local", "count": 0},
84 {
85 "name": "tp:destructor_cplusplus",
86 "msg": "global - across units after provider",
87 "count": 0,
88 },
89 {
90 "name": "tp:destructor_cplusplus",
91 "msg": "global - same unit after provider",
92 "count": 0,
93 },
94 {
95 "name": "tp:destructor_cplusplus",
96 "msg": "global - same unit before provider",
97 "count": 0,
b8e79f3f 98 "may_fail": compound_literal_on_heap,
da1e97c9
MD
99 },
100 {
101 "name": "tp:destructor_cplusplus",
102 "msg": "global - across units after define",
103 "count": 0,
b8e79f3f 104 "may_fail": compound_literal_on_heap,
da1e97c9
MD
105 },
106 {
107 "name": "tp:destructor_cplusplus",
108 "msg": "global - same unit after define",
109 "count": 0,
b8e79f3f 110 "may_fail": compound_literal_on_heap,
da1e97c9
MD
111 },
112 {
113 "name": "tp:destructor_cplusplus",
114 "msg": "global - same unit before define",
115 "count": 0,
b8e79f3f 116 "may_fail": compound_literal_on_heap,
da1e97c9
MD
117 },
118 {
119 "name": "tp:destructor_cplusplus",
120 "msg": "global - across units before define",
121 "count": 0,
b8e79f3f 122 "may_fail": compound_literal_on_heap,
da1e97c9 123 },
21b65d7f
KS
124]
125
126expected_events_common = [
127 {
128 "name": "tp:constructor_c_across_units_before_define",
129 "msg": None,
130 "count": 0,
131 "may_fail": compound_literal_on_heap,
132 },
133 {
134 "name": "tp:constructor_c_same_unit_before_define",
135 "msg": None,
136 "count": 0,
137 "may_fail": compound_literal_on_heap,
138 },
139 {
140 "name": "tp:constructor_c_same_unit_after_define",
141 "msg": None,
142 "count": 0,
143 "may_fail": compound_literal_on_heap,
144 },
145 {
146 "name": "tp:constructor_c_across_units_after_define",
147 "msg": None,
148 "count": 0,
149 "may_fail": compound_literal_on_heap,
150 },
151 {
152 "name": "tp:constructor_c_same_unit_before_provider",
153 "msg": None,
154 "count": 0,
155 "may_fail": compound_literal_on_heap,
156 },
157 {
158 "name": "tp:constructor_c_same_unit_after_provider",
159 "msg": None,
160 "count": 0,
161 "may_fail": compound_literal_on_heap,
162 },
163 {"name": "tp:constructor_c_across_units_after_provider", "msg": None, "count": 0},
164 {"name": "tp:main", "msg": None, "count": 0},
b8e79f3f
KS
165 {
166 "name": "tp:destructor_c_across_units_after_provider",
167 "msg": None,
168 "count": 0,
169 "may_fail": compound_literal_on_heap,
170 },
171 {
172 "name": "tp:destructor_c_same_unit_after_provider",
173 "msg": None,
174 "count": 0,
175 "may_fail": compound_literal_on_heap,
176 },
177 {
178 "name": "tp:destructor_c_same_unit_before_provider",
179 "msg": None,
180 "count": 0,
181 "may_fail": compound_literal_on_heap,
182 },
183 {
184 "name": "tp:destructor_c_across_units_after_define",
185 "msg": None,
186 "count": 0,
187 "may_fail": compound_literal_on_heap,
188 },
189 {
190 "name": "tp:destructor_c_same_unit_after_define",
191 "msg": None,
192 "count": 0,
193 "may_fail": compound_literal_on_heap,
194 },
195 {
196 "name": "tp:destructor_c_same_unit_before_define",
197 "msg": None,
198 "count": 0,
199 "may_fail": compound_literal_on_heap,
200 },
201 {
202 "name": "tp:destructor_c_across_units_before_define",
203 "msg": None,
204 "count": 0,
205 "may_fail": compound_literal_on_heap,
206 },
09a872ef 207]
21b65d7f
KS
208
209expected_events_tp_so_cpp = [
b8e79f3f 210 {
09a872ef
KS
211 "name": "tp_so:constructor_cplusplus_provider_shared_library",
212 "msg": "global - shared library define and provider",
213 "count": 0,
214 },
215 {
216 "name": "tp_so:constructor_cplusplus_provider_shared_library",
217 "msg": "main() local - shared library define and provider",
218 "count": 0,
219 },
220 {
221 "name": "tp_so:destructor_cplusplus_provider_shared_library",
222 "msg": "main() local - shared library define and provider",
223 "count": 0,
224 },
225 {
226 "name": "tp_so:destructor_cplusplus_provider_shared_library",
227 "msg": "global - shared library define and provider",
b8e79f3f 228 "count": 0,
b8e79f3f 229 },
21b65d7f
KS
230]
231
232expected_events_tp_so = [
233 {"name": "tp_so_c:constructor_c_provider_shared_library", "msg": None, "count": 0},
ed1b6b66 234 {"name": "tp_so_c:destructor_c_provider_shared_library", "msg": None, "count": 0},
da1e97c9 235]
21b65d7f
KS
236
237expected_events_tp_a_cpp = [
09a872ef
KS
238 {
239 "name": "tp_a:constructor_cplusplus_provider_static_archive",
240 "msg": "global - static archive define and provider",
241 "count": 0,
242 "may_fail": compound_literal_on_heap,
243 },
244 {
245 "name": "tp_a:constructor_cplusplus_provider_static_archive",
246 "msg": "main() local - static archive define and provider",
247 "count": 0,
248 },
249 {
250 "name": "tp_a:destructor_cplusplus_provider_static_archive",
251 "msg": "main() local - static archive define and provider",
252 "count": 0,
253 },
254 {
255 "name": "tp_a:destructor_cplusplus_provider_static_archive",
256 "msg": "global - static archive define and provider",
257 "count": 0,
258 "may_fail": compound_literal_on_heap,
259 },
21b65d7f
KS
260]
261
262expected_events_tp_a = [
263 {"name": "tp_a_c:constructor_c_provider_static_archive", "msg": None, "count": 0},
09a872ef
KS
264 {"name": "tp_a_c:destructor_c_provider_static_archive", "msg": None, "count": 0},
265]
d096be91 266
da1e97c9 267
09a872ef 268def capture_trace(tap, test_env, application, description):
d2455527 269 # type: (lttngtest.TapGenerator, lttngtest._Environment) -> lttngtest.LocalSessionOutputLocation
09a872ef 270 tap.diagnostic(description)
da1e97c9
MD
271
272 session_output_location = lttngtest.LocalSessionOutputLocation(
273 test_env.create_temporary_directory("trace")
274 )
275
aae4cdd1 276 client = lttngtest.LTTngClient(test_env, log=tap.diagnostic)
da1e97c9
MD
277
278 with tap.case("Create a session") as test_case:
279 session = client.create_session(output=session_output_location)
280 tap.diagnostic("Created session `{session_name}`".format(session_name=session.name))
281
282 with tap.case(
283 "Add a channel to session `{session_name}`".format(session_name=session.name)
284 ) as test_case:
285 channel = session.add_channel(lttngtest.TracingDomain.User)
286 tap.diagnostic("Created channel `{channel_name}`".format(channel_name=channel.name))
287
288 # Enable all user space events, the default for a user tracepoint event rule.
289 channel.add_recording_rule(lttngtest.UserTracepointEventRule("tp*"))
290
d096be91
MJ
291 with tap.case(
292 "Start session `{session_name}`".format(session_name=session.name)
293 ) as test_case:
294 session.start()
295
09a872ef
KS
296 test_app = test_env.launch_test_application(application)
297 with tap.case(
298 "Run test app '{}'".format(application, session_name=session.name)
299 ) as test_case:
d096be91
MJ
300 test_app.wait_for_exit()
301
302 with tap.case(
303 "Stop session `{session_name}`".format(session_name=session.name)
304 ) as test_case:
305 session.stop()
306
307 with tap.case(
308 "Destroy session `{session_name}`".format(session_name=session.name)
309 ) as test_case:
310 session.destroy()
311
da1e97c9
MD
312 return session_output_location
313
314
09a872ef 315def validate_trace(trace_location, tap, expected_events):
d096be91 316 # type: (pathlib.Path, lttngtest.TapGenerator)
da1e97c9
MD
317 unknown_event_count = 0
318
319 for msg in bt2.TraceCollectionMessageIterator(str(trace_location)):
320 if type(msg) is not bt2._EventMessageConst:
321 continue
322
323 found = False
324 for event in expected_events:
325 if event["name"] == msg.event.name and event["msg"] is None:
326 found = True
327 event["count"] = event["count"] + 1
328 break
329 elif (
330 event["name"] == msg.event.name
331 and event["msg"] is not None
332 and event["msg"] == msg.event["msg"]
333 ):
334 found = True
335 event["count"] = event["count"] + 1
336 break
d096be91 337
da1e97c9
MD
338 if found == False:
339 unknown_event_count = unknown_event_count + 1
340 printmsg = None
341 if "msg" in msg.event:
342 printmsg = msg.event["msg"]
343 tap.diagnostic(
344 'Unexpected event name="{}" msg="{}" encountered'.format(
345 msg.event.name, str(printmsg)
346 )
347 )
348
349 for event in expected_events:
b8e79f3f
KS
350 may_fail = "may_fail" in event.keys() and event["may_fail"]
351 if not may_fail:
352 tap.test(
353 event["count"] == 1,
354 'Found expected event name="{}" msg="{}"'.format(
355 event["name"], str(event["msg"])
356 ),
f506900f 357 )
b8e79f3f
KS
358 else:
359 tap.skip("Event '{}' may or may not be recorded".format(event["name"]))
d096be91
MJ
360
361 tap.test(unknown_event_count == 0, "Found no unexpected events")
This page took 0.043723 seconds and 4 git commands to generate.