tests: Move to kernel style SPDX license identifiers
[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:
28 bail("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.")
29
30session_info = create_session()
31enable_ust_tracepoint_event(session_info, "ust_tests_fork*")
32start_session(session_info)
33
34fork_process = subprocess.Popen([test_path + "fork", test_path + "fork2"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
37bd6c8e
JG
35parent_pid = -1
36child_pid = -1
37for line in fork_process.stdout:
38 line = line.decode('utf-8').replace("\n", "")
39 match = re.search(r"child_pid (\d+)", line)
40 if match:
41 child_pid = match.group(1)
42 match = re.search(r"parent_pid (\d+)", line)
43 if match:
44 parent_pid = match.group(1)
45
3e198d90
JG
46fork_process.wait()
47
37bd6c8e
JG
48print_test_result(fork_process.returncode == 0, current_test, "Fork test application exited normally")
49current_test += 1
50
51stop_session(session_info)
52
53# Check both events (normal exit and suicide messages) are present in the resulting trace
54try:
55 babeltrace_process = subprocess.Popen(["babeltrace", session_info.trace_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
56except FileNotFoundError:
57 bail("Could not open babeltrace. Please make sure it is installed.", session_info)
58
59event_lines = []
60for event_line in babeltrace_process.stdout:
61 event_line = event_line.decode('utf-8').replace("\n", "")
62 if re.search(r"warning", event_line) is not None or re.search(r"error", event_line) is not None:
63 print( "# " + event_line )
64 else:
65 event_lines.append(event_line)
66
67babeltrace_process.wait()
68
69print_test_result(babeltrace_process.returncode == 0, current_test, "Resulting trace is readable")
70current_test += 1
71
72if babeltrace_process.returncode != 0:
73 bail("Unreadable trace; can't proceed with analysis.", session_info)
74
75event_before_fork = False
76event_after_fork_parent = False
77event_after_fork_child = False
78event_after_exec = False
79
80for event_line in event_lines:
81 match = re.search(r".*pid = (\d+)", event_line)
82 if match is not None:
83 event_pid = match.group(1)
84 else:
85 continue
86
87 if re.search(r"before_fork", event_line):
88 event_before_fork = (event_pid == parent_pid)
89 if re.search(r"after_fork_parent", event_line):
90 event_after_fork_parent = (event_pid == parent_pid)
91 if re.search(r"after_fork_child", event_line):
92 event_after_fork_child = (event_pid == child_pid)
93 if re.search(r"after_exec", event_line):
94 event_after_exec = (event_pid == child_pid)
95
96print_test_result(event_before_fork, current_test, "before_fork event logged by parent process found in trace")
97current_test += 1
98print_test_result(event_after_fork_parent, current_test, "after_fork_parent event logged by parent process found in trace")
99current_test += 1
100print_test_result(event_after_fork_child, current_test, "after_fork_child event logged by child process found in trace")
101current_test += 1
102print_test_result(event_after_exec, current_test, "after_exec event logged by child process found in trace")
103current_test += 1
104
105shutil.rmtree(session_info.tmp_directory)
This page took 0.039942 seconds and 4 git commands to generate.