File: test_session_commands.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 (484 lines) | stat: -rwxr-xr-x 16,099 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#!/usr/bin/env python3
#
# SPDX-FileCopyrightText: 2023 Jérémie Galarneau <jeremie.galarneau@efficios.com>
#
# SPDX-License-Identifier: GPL-2.0-only

import pathlib
import sys
import os
from typing import Any, Callable, Type, Dict, Iterator
import random
import string
from collections.abc import Mapping

"""
Test the session commands of the `lttng` CLI client.
"""

# 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


class SessionSet(Mapping):
    def __init__(self, client, name_prefixes):
        self._sessions = {}  # type dict[str, lttngtest.Session]
        for prefix in name_prefixes:
            new_session = client.create_session(
                name=self._generate_session_name_from_prefix(prefix),
                output=lttngtest.LocalSessionOutputLocation(
                    test_env.create_temporary_directory("trace")
                ),
            )
            # Add a channel to all sessions to ensure the sessions can be started.
            new_session.add_channel(lttngtest.TracingDomain.User)
            self._sessions[prefix] = new_session

    @staticmethod
    def _generate_session_name_from_prefix(prefix):
        # type: (str) -> str
        return (
            prefix
            + "_"
            + "".join(
                random.choice(string.ascii_lowercase + string.digits) for _ in range(8)
            )
        )

    def __getitem__(self, __key):
        # type: (str) -> lttngtest.Session
        return self._sessions[__key]

    def __len__(self):
        # type: () -> int
        return len(self._sessions)

    def __iter__(self):
        # type: () -> Iterator[str]
        return iter(self._sessions)


def test_start_globbing(tap, test_env):
    # type: (lttngtest.TapGenerator, lttngtest._Environment) -> None
    tap.diagnostic("Test --glob match of start command")
    name_prefixes = ["abba", "alakazou", "alakazam"]

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

    tap.diagnostic("Create a set of sessions to test globbing")
    sessions = None
    with tap.case(
        "Create sessions with prefixes [{}]".format(", ".join(name_prefixes))
    ) as test_case:
        sessions = SessionSet(client, name_prefixes)

    tap.test(
        all(not session.is_active for prefix, session in sessions.items()),
        "All sessions created are in the inactive state",
    )

    start_pattern = "alak*"
    with tap.case("Start sessions with --glob={}".format(start_pattern)) as test_case:
        client.start_session_by_glob_pattern(start_pattern)

    tap.test(
        sessions["alakazou"].is_active
        and sessions["alakazam"].is_active
        and not sessions["abba"].is_active,
        "Only sessions 'alakazou' and 'alakazam' are active",
    )

    with tap.case(
        "Starting already started sessions with --glob={} doesn't produce an error".format(
            start_pattern
        )
    ) as test_case:
        client.start_session_by_glob_pattern(start_pattern)

    start_pattern = "tintina*"
    with tap.case(
        "Starting with --glob={} that doesn't match any session doesn't produce an error".format(
            start_pattern
        )
    ) as test_case:
        client.start_session_by_glob_pattern(start_pattern)

    for name, session in sessions.items():
        session.destroy()

    with tap.case(
        "Starting with --glob={} when no sessions exist doesn't produce an error".format(
            start_pattern
        )
    ) as test_case:
        client.start_session_by_glob_pattern(start_pattern)


def test_start_single(tap, test_env):
    # type: (lttngtest.TapGenerator, lttngtest._Environment) -> None
    tap.diagnostic("Test match of start command targeting a single session")
    name_prefixes = ["un", "deux", "patate", "pouel"]

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

    tap.diagnostic("Create a set of sessions to test single session start")
    sessions = None
    with tap.case(
        "Create sessions with prefixes [{}]".format(", ".join(name_prefixes))
    ) as test_case:
        sessions = SessionSet(client, name_prefixes)

    tap.test(
        all(not session.is_active for prefix, session in sessions.items()),
        "All sessions created are in the inactive state",
    )

    session_to_start_prefix = "patate"
    full_session_name = sessions[session_to_start_prefix].name
    with tap.case("Start session '{}'".format(session_to_start_prefix)) as test_case:
        client.start_session_by_name(full_session_name)

    tap.test(
        any(
            session.is_active and prefix != session_to_start_prefix
            for prefix, session in sessions.items()
        )
        is False,
        "Only session '{}' is active".format(session_to_start_prefix),
    )

    with tap.case(
        "Starting already started session '{}' doesn't produce an error".format(
            session_to_start_prefix
        )
    ) as test_case:
        client.start_session_by_name(full_session_name)

    for name, session in sessions.items():
        session.destroy()


def test_start_all(tap, test_env):
    # type: (lttngtest.TapGenerator, lttngtest._Environment) -> None
    tap.diagnostic("Test start command with the --all option")
    name_prefixes = ["a", "b", "c", "d"]

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

    tap.diagnostic("Create a set of sessions to test starting all sessions")
    sessions = None
    with tap.case(
        "Create sessions with prefixes [{}]".format(", ".join(name_prefixes))
    ) as test_case:
        sessions = SessionSet(client, name_prefixes)

    tap.test(
        all(not session.is_active for prefix, session in sessions.items()),
        "All sessions created are in the inactive state",
    )

    with tap.case("Start all sessions") as test_case:
        client.start_sessions_all()

    tap.test(
        all(session.is_active for prefix, session in sessions.items()),
        "All sessions are active",
    )

    with tap.case("Starting already started sessions") as test_case:
        client.start_sessions_all()

    for name, session in sessions.items():
        session.destroy()

    with tap.case(
        "Starting all sessions when none exist doesn't produce an error"
    ) as test_case:
        client.start_sessions_all()


def test_stop_globbing(tap, test_env):
    # type: (lttngtest.TapGenerator, lttngtest._Environment) -> None
    tap.diagnostic("Test --glob match of stop command")
    name_prefixes = ["East Farnham", "Amqui", "Amos"]

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

    tap.diagnostic("Create a set of sessions to test globbing")
    sessions = None
    with tap.case(
        "Create sessions with prefixes [{}]".format(", ".join(name_prefixes))
    ) as test_case:
        sessions = SessionSet(client, name_prefixes)

    client.start_sessions_all()
    tap.test(
        all(session.is_active for prefix, session in sessions.items()),
        "All sessions are in the active state",
    )

    stop_pattern = "Am??i*"
    with tap.case("Stop sessions with --glob={}".format(stop_pattern)) as test_case:
        client.stop_session_by_glob_pattern(stop_pattern)

    tap.test(
        (
            sessions["East Farnham"].is_active
            and sessions["Amos"].is_active
            and (not sessions["Amqui"].is_active)
        ),
        "Only session 'Amqui' is inactive",
    )

    stop_pattern = "Am*"
    with tap.case(
        "Stopping more sessions, including a stopped session, with --glob={} doesn't produce an error".format(
            stop_pattern
        )
    ) as test_case:
        client.stop_session_by_glob_pattern(stop_pattern)

    tap.test(
        sessions["East Farnham"].is_active
        and (not sessions["Amqui"].is_active)
        and (not sessions["Amos"].is_active),
        "Only session 'East Farnham' is active",
    )

    stop_pattern = "Notre-Dame*"
    with tap.case(
        "Stopping with --glob={} that doesn't match any session doesn't produce an error".format(
            stop_pattern
        )
    ) as test_case:
        client.stop_session_by_glob_pattern(stop_pattern)

    for name, session in sessions.items():
        session.destroy()

    with tap.case(
        "Stopping with --glob={} when no sessions exist doesn't produce an error".format(
            stop_pattern
        )
    ) as test_case:
        client.stop_session_by_glob_pattern(stop_pattern)


def test_stop_single(tap, test_env):
    # type: (lttngtest.TapGenerator, lttngtest._Environment) -> None
    tap.diagnostic("Test match of stop command targeting a single session")
    name_prefixes = ["Grosses-Roches", "Kazabazua", "Laval", "Magog"]

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

    tap.diagnostic("Create a set of sessions to test single session stop")
    sessions = None
    with tap.case(
        "Create sessions with prefixes [{}]".format(", ".join(name_prefixes))
    ) as test_case:
        sessions = SessionSet(client, name_prefixes)

    client.start_sessions_all()
    tap.test(
        all(session.is_active for prefix, session in sessions.items()),
        "All sessions are in the active state",
    )

    session_to_stop_prefix = "Kazabazua"
    full_session_name = sessions[session_to_stop_prefix].name
    with tap.case("Stop session '{}'".format(session_to_stop_prefix)) as test_case:
        client.stop_session_by_name(full_session_name)

    inactive_session_prefixes = [
        prefix for prefix, session in sessions.items() if not session.is_active
    ]
    tap.test(
        len(inactive_session_prefixes) == 1
        and inactive_session_prefixes[0] == session_to_stop_prefix,
        "Only session '{}' is inactive".format(session_to_stop_prefix),
    )

    with tap.case(
        "Stopping already stopped session '{}' doesn't produce an error".format(
            session_to_stop_prefix
        )
    ) as test_case:
        client.stop_session_by_name(full_session_name)

    for name, session in sessions.items():
        session.destroy()


def test_stop_all(tap, test_env):
    # type: (lttngtest.TapGenerator, lttngtest._Environment) -> None
    tap.diagnostic("Test stop command with the --all option")
    name_prefixes = ["a", "b", "c", "d"]

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

    tap.diagnostic("Create a set of sessions to test stopping all sessions")
    sessions = None
    with tap.case(
        "Create sessions with prefixes [{}]".format(", ".join(name_prefixes))
    ) as test_case:
        sessions = SessionSet(client, name_prefixes)

    client.start_sessions_all()
    tap.test(
        all(session.is_active for prefix, session in sessions.items()),
        "All sessions are in the active state",
    )

    with tap.case("Stop all sessions") as test_case:
        client.stop_sessions_all()

    tap.test(
        all(not session.is_active for prefix, session in sessions.items()),
        "All sessions are inactive",
    )

    with tap.case("Stopping already stopped sessions") as test_case:
        client.stop_sessions_all()

    for name, session in sessions.items():
        session.destroy()

    with tap.case(
        "Stopping all sessions when none exist doesn't produce an error"
    ) as test_case:
        client.stop_sessions_all()


def test_destroy_globbing(tap, test_env):
    # type: (lttngtest.TapGenerator, lttngtest._Environment) -> None
    tap.diagnostic("Test --glob match of destroy command")
    name_prefixes = ["Mont-Laurier", "Montreal", "Montmagny", "Neuville"]

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

    tap.diagnostic("Create a set of sessions to test globbing")
    sessions = None
    with tap.case(
        "Create sessions with prefixes [{}]".format(", ".join(name_prefixes))
    ) as test_case:
        sessions = SessionSet(client, name_prefixes)

    destroy_pattern = "Mont*"
    with tap.case(
        "Destroy sessions with --glob={}".format(destroy_pattern)
    ) as test_case:
        client.destroy_session_by_glob_pattern(destroy_pattern)

    listed_sessions = client.list_sessions()
    tap.test(
        len(listed_sessions) == 1
        and listed_sessions[0].name == sessions["Neuville"].name,
        "Neuville is the only remaining session",
    )

    for session in listed_sessions:
        session.destroy()

    with tap.case(
        "Destroying with --glob={} when no sessions exist doesn't produce an error".format(
            destroy_pattern
        )
    ) as test_case:
        client.destroy_session_by_glob_pattern(destroy_pattern)


def test_destroy_single(tap, test_env):
    # type: (lttngtest.TapGenerator, lttngtest._Environment) -> None
    tap.diagnostic("Test match of destroy command targeting a single session")
    name_prefixes = ["Natashquan", "Normetal", "Notre-Dame-des-Sept-Douleurs"]

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

    tap.diagnostic("Create a set of sessions to test single session destruction")
    sessions = None
    with tap.case(
        "Create sessions with prefixes [{}]".format(", ".join(name_prefixes))
    ) as test_case:
        sessions = SessionSet(client, name_prefixes)

    session_to_destroy_prefix = "Normetal"
    full_session_name = sessions[session_to_destroy_prefix].name
    with tap.case(
        "Destroy session '{}'".format(session_to_destroy_prefix)
    ) as test_case:
        client.destroy_session_by_name(full_session_name)

    listed_sessions = client.list_sessions()
    tap.test(
        len(listed_sessions) == 2
        and full_session_name not in [session.name for session in listed_sessions],
        "Session '{}' no longer exists".format(session_to_destroy_prefix),
    )

    for session in listed_sessions:
        session.destroy()


def test_destroy_all(tap, test_env):
    # type: (lttngtest.TapGenerator, lttngtest._Environment) -> None
    tap.diagnostic("Test destroy command with the --all option")
    name_prefixes = ["a", "b", "c", "d"]

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

    tap.diagnostic("Create a set of sessions to test destroying all sessions")
    sessions = None
    with tap.case(
        "Create sessions with prefixes [{}]".format(", ".join(name_prefixes))
    ) as test_case:
        sessions = SessionSet(client, name_prefixes)

    with tap.case("Destroy all sessions") as test_case:
        client.destroy_sessions_all()

    tap.test(
        len(client.list_sessions()) == 0,
        "No sessions exist after destroying all sessions",
    )

    with tap.case(
        "Destroy all sessions when none exist doesn't produce an error"
    ) as test_case:
        client.destroy_sessions_all()


tap = lttngtest.TapGenerator(48)
tap.diagnostic("Test client session command --glob and --all options")

with lttngtest.test_environment(with_sessiond=True, log=tap.diagnostic) as test_env:
    test_start_globbing(tap, test_env)

with lttngtest.test_environment(with_sessiond=True, log=tap.diagnostic) as test_env:
    test_start_single(tap, test_env)

with lttngtest.test_environment(with_sessiond=True, log=tap.diagnostic) as test_env:
    test_start_all(tap, test_env)

with lttngtest.test_environment(with_sessiond=True, log=tap.diagnostic) as test_env:
    test_stop_globbing(tap, test_env)

with lttngtest.test_environment(with_sessiond=True, log=tap.diagnostic) as test_env:
    test_stop_single(tap, test_env)

with lttngtest.test_environment(with_sessiond=True, log=tap.diagnostic) as test_env:
    test_stop_all(tap, test_env)

with lttngtest.test_environment(with_sessiond=True, log=tap.diagnostic) as test_env:
    test_destroy_globbing(tap, test_env)

with lttngtest.test_environment(with_sessiond=True, log=tap.diagnostic) as test_env:
    test_destroy_single(tap, test_env)

with lttngtest.test_environment(with_sessiond=True, log=tap.diagnostic) as test_env:
    test_destroy_all(tap, test_env)

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