Tests: Fix: Use '.logfile' instead of '.log' for test app output
[lttng-tools.git] / tests / regression / ust / fork / test_fork.py
CommitLineData
37bd6c8e
JG
1#!/usr/bin/env python3
2#
9d16b343 3# Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
37bd6c8e 4#
9d16b343 5# SPDX-License-Identifier: GPL-2.0-only
37bd6c8e
JG
6
7import os
8import subprocess
9import re
10import shutil
11import sys
12
13test_path = os.path.dirname(os.path.abspath(__file__)) + "/"
14test_utils_path = test_path
15for i in range(4):
16 test_utils_path = os.path.dirname(test_utils_path)
17test_utils_path = test_utils_path + "/utils"
18sys.path.append(test_utils_path)
19from test_utils import *
20
21
22NR_TESTS = 6
23current_test = 1
24print("1..{0}".format(NR_TESTS))
25
26# Check if a sessiond is running... bail out if none found.
27if session_daemon_alive() == 0:
6a871bbe
KS
28 bail(
29 '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.'
30 )
37bd6c8e
JG
31
32session_info = create_session()
33enable_ust_tracepoint_event(session_info, "ust_tests_fork*")
34start_session(session_info)
35
6a871bbe
KS
36fork_process = subprocess.Popen(
37 [test_path + "fork", test_path + "fork2"],
38 stdout=subprocess.PIPE,
39 stderr=subprocess.PIPE,
40)
37bd6c8e
JG
41parent_pid = -1
42child_pid = -1
43for line in fork_process.stdout:
6a871bbe 44 line = line.decode("utf-8").replace("\n", "")
37bd6c8e
JG
45 match = re.search(r"child_pid (\d+)", line)
46 if match:
47 child_pid = match.group(1)
48 match = re.search(r"parent_pid (\d+)", line)
49 if match:
50 parent_pid = match.group(1)
51
3e198d90
JG
52fork_process.wait()
53
6a871bbe
KS
54print_test_result(
55 fork_process.returncode == 0, current_test, "Fork test application exited normally"
56)
37bd6c8e
JG
57current_test += 1
58
59stop_session(session_info)
60
61# Check both events (normal exit and suicide messages) are present in the resulting trace
62try:
6a871bbe
KS
63 babeltrace_process = subprocess.Popen(
64 [BABELTRACE_BIN, session_info.trace_path],
65 stdout=subprocess.PIPE,
66 stderr=subprocess.PIPE,
67 )
37bd6c8e 68except FileNotFoundError:
c125de8f 69 bail("Could not open {}. Please make sure it is installed.".format(BABELTRACE_BIN))
37bd6c8e
JG
70
71event_lines = []
72for event_line in babeltrace_process.stdout:
6a871bbe
KS
73 event_line = event_line.decode("utf-8").replace("\n", "")
74 if (
75 re.search(r"warning", event_line) is not None
76 or re.search(r"error", event_line) is not None
77 ):
78 print("# " + event_line)
37bd6c8e
JG
79 else:
80 event_lines.append(event_line)
81
82babeltrace_process.wait()
83
6a871bbe
KS
84print_test_result(
85 babeltrace_process.returncode == 0, current_test, "Resulting trace is readable"
86)
37bd6c8e
JG
87current_test += 1
88
89if babeltrace_process.returncode != 0:
90 bail("Unreadable trace; can't proceed with analysis.", session_info)
91
92event_before_fork = False
93event_after_fork_parent = False
94event_after_fork_child = False
95event_after_exec = False
96
97for event_line in event_lines:
98 match = re.search(r".*pid = (\d+)", event_line)
99 if match is not None:
100 event_pid = match.group(1)
101 else:
102 continue
103
104 if re.search(r"before_fork", event_line):
6a871bbe 105 event_before_fork = event_pid == parent_pid
37bd6c8e 106 if re.search(r"after_fork_parent", event_line):
6a871bbe 107 event_after_fork_parent = event_pid == parent_pid
37bd6c8e 108 if re.search(r"after_fork_child", event_line):
6a871bbe 109 event_after_fork_child = event_pid == child_pid
37bd6c8e 110 if re.search(r"after_exec", event_line):
6a871bbe 111 event_after_exec = event_pid == child_pid
37bd6c8e 112
6a871bbe
KS
113print_test_result(
114 event_before_fork,
115 current_test,
116 "before_fork event logged by parent process found in trace",
117)
37bd6c8e 118current_test += 1
6a871bbe
KS
119print_test_result(
120 event_after_fork_parent,
121 current_test,
122 "after_fork_parent event logged by parent process found in trace",
123)
37bd6c8e 124current_test += 1
6a871bbe
KS
125print_test_result(
126 event_after_fork_child,
127 current_test,
128 "after_fork_child event logged by child process found in trace",
129)
37bd6c8e 130current_test += 1
6a871bbe
KS
131print_test_result(
132 event_after_exec,
133 current_test,
134 "after_exec event logged by child process found in trace",
135)
37bd6c8e
JG
136current_test += 1
137
138shutil.rmtree(session_info.tmp_directory)
This page took 0.072839 seconds and 4 git commands to generate.