File: test_trace_format_ust.py

package info (click to toggle)
ltt-control 2.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,860 kB
  • sloc: cpp: 192,012; sh: 28,777; ansic: 10,960; python: 7,108; makefile: 3,520; java: 109; xml: 46
file content (213 lines) | stat: -rwxr-xr-x 7,110 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
#
# SPDX-FileCopyrightText: 2025 Jérémie Galarneau <jeremie.galarneau@efficios.com>
#
# SPDX-License-Identifier: GPL-2.0-only

import pathlib
import sys
import os
import json

# Import in-tree test utils
test_utils_import_path = pathlib.Path(__file__).absolute().parents[3] / "utils"
sys.path.append(str(test_utils_import_path))

import lttngtest
import bt2

from trace_format_helpers import (
    check_ctf2_trace_smoketest,
    check_trace_event_counts,
    test_local_trace_all_formats,
)


def capture_local_ust_trace(environment):
    # type: (lttngtest._Environment) -> pathlib.Path
    session_output_location = lttngtest.LocalSessionOutputLocation(
        environment.create_temporary_directory("ctf2-trace")
    )
    client = lttngtest.LTTngClient(environment, log=tap.diagnostic)

    session = client.create_session(output=session_output_location)
    tap.diagnostic("Created session `{session_name}`".format(session_name=session.name))

    channel = session.add_channel(lttngtest.TracingDomain.User)
    tap.diagnostic("Created channel `{channel_name}`".format(channel_name=channel.name))

    test_app = environment.launch_wait_trace_test_application(10)

    # Only track the test application
    session.user_vpid_process_attribute_tracker.track(test_app.vpid)

    channel.add_recording_rule(lttngtest.UserTracepointEventRule("tp:tptest"))

    session.start()
    test_app.trace()
    test_app.wait_for_exit()
    session.stop()
    session.destroy()

    return session_output_location.path


def test_snapshot_trace_valid_ctf2():
    with lttngtest.test_environment(
        with_sessiond=True,
        log=tap.diagnostic,
        extra_env_vars={"LTTNG_EXPERIMENTAL_FORCE_CTF_2": "1"},
    ) as test_env:
        session_output_location = lttngtest.LocalSessionOutputLocation(
            test_env.create_temporary_directory("ctf2-trace-snapshot")
        )

        with tap.case("Capture local snapshot trace in CTF2 format"):
            client = lttngtest.LTTngClient(test_env, log=tap.diagnostic)

            session = client.create_session(snapshot=True)
            tap.diagnostic(
                "Created snapshot session `{session_name}`".format(
                    session_name=session.name
                )
            )

            channel = session.add_channel(lttngtest.TracingDomain.User)
            tap.diagnostic(
                "Created channel `{channel_name}`".format(channel_name=channel.name)
            )

            test_app = test_env.launch_wait_trace_test_application(10)

            # Only track the test application
            session.user_vpid_process_attribute_tracker.track(test_app.vpid)

            channel.add_recording_rule(lttngtest.UserTracepointEventRule("tp:tptest"))

            session.start()
            test_app.trace()
            test_app.wait_for_exit()
            session.stop()

            session.record_snapshot(session_output_location)
            session.destroy()

        check_ctf2_trace_smoketest(session_output_location.path, tap)
        with tap.case("Decode trace and count events by name"):
            check_trace_event_counts(session_output_location.path, {"tp:tptest": 10})


def test_live_tracing_is_disallowed_for_ctf2():
    with lttngtest.test_environment(
        with_sessiond=True,
        log=tap.diagnostic,
        extra_env_vars={"LTTNG_EXPERIMENTAL_FORCE_CTF_2": "1"},
    ) as test_env:
        network_output = lttngtest.NetworkSessionOutputLocation(
            "net://localhost:{}:{}/".format(
                test_env.lttng_relayd_control_port, test_env.lttng_relayd_data_port
            )
        )

        with tap.case_raises(
            "Live tracing is disallowed with CTF2", lttngtest.lttng.LTTngClientError
        ):
            client = lttngtest.LTTngClient(test_env, log=tap.diagnostic)
            client.create_session(live=True, output=network_output)


def test_streaming_is_disallowed_for_ctf2():
    with lttngtest.test_environment(
        with_sessiond=True,
        log=tap.diagnostic,
        extra_env_vars={"LTTNG_EXPERIMENTAL_FORCE_CTF_2": "1"},
    ) as test_env:

        network_output = lttngtest.NetworkSessionOutputLocation(
            "net://localhost:{}:{}/".format(
                test_env.lttng_relayd_control_port, test_env.lttng_relayd_data_port
            )
        )

        with tap.case_raises(
            "Trace streaming is disallowed with CTF2", lttngtest.lttng.LTTngClientError
        ):

            client = lttngtest.LTTngClient(test_env, log=tap.diagnostic)
            client.create_session(output=network_output)


def test_snapshot_network_output_disallowed_for_ctf2():
    with lttngtest.test_environment(
        with_sessiond=True,
        log=tap.diagnostic,
        with_relayd=True,
        extra_env_vars={"LTTNG_EXPERIMENTAL_FORCE_CTF_2": "1"},
    ) as test_env:
        network_output = lttngtest.NetworkSessionOutputLocation(
            "net://localhost:{}:{}/".format(
                test_env.lttng_relayd_control_port, test_env.lttng_relayd_data_port
            )
        )

        client = lttngtest.LTTngClient(test_env, log=tap.diagnostic)

        session = client.create_session(snapshot=True)
        tap.diagnostic(
            "Created snapshot session `{session_name}`".format(
                session_name=session.name
            )
        )

        channel = session.add_channel(lttngtest.TracingDomain.User)
        tap.diagnostic(
            "Created channel `{channel_name}`".format(channel_name=channel.name)
        )

        test_app = test_env.launch_wait_trace_test_application(10)

        # Only track the test application
        session.user_vpid_process_attribute_tracker.track(test_app.vpid)

        # Enable all user space events, the default for a user tracepoint event rule.
        channel.add_recording_rule(lttngtest.UserTracepointEventRule())

        session.start()
        test_app.trace()
        test_app.wait_for_exit()
        session.stop()

        with tap.case_raises(
            "Capturing a snapshot to a network output is disallowed with CTF2",
            lttngtest.lttng.LTTngClientError,
        ):
            session.record_snapshot(network_output)


tap = lttngtest.TapGenerator(17)
tap.diagnostic("Test trace format generation (user space)")

version_parts = tuple(map(int, bt2.__version__.split(".")[:2]))
if version_parts < (2, 1):
    tap.skip_all_remaining(
        "Skipping test: Babeltrace 2.1.0 or later is required to run the CTF2 trace format test"
    )
    sys.exit(0)

pretty_expect_path = pretty_expect_path = (
    pathlib.Path(__file__).absolute().parents[0] / "ust-local-trace-pretty.expect"
)

test_local_trace_all_formats(
    tap=tap,
    capture_local_trace=capture_local_ust_trace,
    pretty_expect_path=pretty_expect_path,
    enable_kernel_domain=False,
    expected_events={"tp:tptest": 10},
)
test_snapshot_trace_valid_ctf2()
test_live_tracing_is_disallowed_for_ctf2()
test_streaming_is_disallowed_for_ctf2()
test_snapshot_network_output_disallowed_for_ctf2()

sys.exit(0 if tap.is_successful else 1)