Tests: add kernel test into the `make check` test suite.
[lttng-tools.git] / tests / regression / kernel / test_ns_contexts_change
CommitLineData
5f0d4e78
MJ
1#!/bin/bash
2#
3# Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
4#
9d16b343 5# SPDX-License-Identifier: LGPL-2.1-only
5f0d4e78
MJ
6
7TEST_DESC="Kernel tracer - Namespace contexts change"
8
9CURDIR=$(dirname "$0")/
10TESTDIR=$CURDIR/../..
11
12TESTAPP_PATH="$TESTDIR/utils/testapp"
13TESTAPP_NAME="gen-ns-events"
14TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
15
16TESTS_PER_NS=19
17
18NUM_TESTS=$((TESTS_PER_NS * 6))
19
20source "$TESTDIR/utils/utils.sh"
21
22# MUST set TESTDIR before calling those functions
23
24function add_context_kernel_skip_ok()
25{
26 local session_name=$1
27 local channel_name=$2
28 local context_name=$3
29 local skip_num=$4
30
31 local ret
32
33 "$TESTDIR/../src/bin/lttng/$LTTNG_BIN" add-context -k \
34 -s "$session_name" -c "$channel_name" \
35 -t "$context_name" 1>"$OUTPUT_DEST" 2>"$ERROR_OUTPUT_DEST"
36 ret=$?
37
38 if [ "$ret" == "4" ]; then
39 skip 0 "Current kernel doesn't implement '$context_name' context" $((skip_num + 1))
40 else
41 ok $ret "Add context command for type: $context_name"
42 fi
43
44 return $ret
45}
46
47function enable_kernel_lttng_event_filter_ok()
48{
49 local session_name=$1
50 local syscall_name=$2
51 local channel_name=$3
52 local filter=$4
53
54 "$TESTDIR/../src/bin/lttng/$LTTNG_BIN" enable-event -k \
55 -c "$channel_name" -s "$session_name" \
56 --syscall "$syscall_name" \
57 -f "$filter" \
58 1>"$OUTPUT_DEST" 2>"$ERROR_OUTPUT_DEST"
59
60 ok $? "Add syscall with filter"
61}
62
63function test_ns()
64{
65 local ns=$1
66
67 local session_name="${ns}_ns"
68 local chan_name="${ns}_ns"
69 local context_name="${ns}_ns"
70
71 local trace_path
72 local ns_inode
73 local file_sync_wait_before_unshare
74 local file_sync_wait_after_unshare
75 local file_sync_signal_after_unshare
76
77 # Check if the kernel has support for this ns type
78 if [ ! -f "/proc/$$/ns/$ns" ]; then
79 skip 0 "System has no $ns namespace support" $TESTS_PER_NS
80 return
81 fi
82
33e55711
FD
83 trace_path=$(mktemp --tmpdir -d "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")
84 file_sync_wait_before_unshare=$(mktemp --tmpdir -u "tmp.${FUNCNAME[0]}_sync_before_unshare.XXXXXX")
85 file_sync_wait_after_unshare=$(mktemp --tmpdir -u "tmp.${FUNCNAME[0]}_sync_after_unshare.XXXXXX")
86 file_sync_signal_after_unshare=$(mktemp --tmpdir -u "tmp.${FUNCNAME[0]}_sync_signal_after_unshare.XXXXXX")
5f0d4e78
MJ
87
88 # Get the current ns inode number
89 ns_inode=$(stat -c '%i' -L "/proc/$$/ns/$ns")
90 ok $? "Get current $ns namespace inode: $ns_inode" || ns_inode="invalid"
91
92 $TESTAPP_BIN -n "$ns" -a "$file_sync_wait_after_unshare" -b "$file_sync_wait_before_unshare" -s "$file_sync_signal_after_unshare" &
93 ok $? "Launch test app."
94 app_pid=$!
95
96 app_ns_inode=$(stat -c '%i' -L "/proc/$app_pid/ns/$ns")
97 ok $? "Get app current $ns namespace inode: $app_ns_inode" || app_ns_inode="invalid"
98
99 start_lttng_sessiond
100
101 create_lttng_session_ok "$session_name" "$trace_path"
102 enable_kernel_lttng_channel_ok "$session_name" "$chan_name"
103 add_context_kernel_skip_ok "$session_name" "$chan_name" "$context_name" 10
104 if [ "$?" != "4" ]; then
105 lttng_enable_kernel_syscall_ok "$session_name" "unshare" "$chan_name"
106 lttng_track_pid_ok "$app_pid"
107 start_lttng_tracing_ok "$session_name"
108
109 touch "$file_sync_wait_before_unshare"
110
5f0d4e78
MJ
111 while [ ! -f "$file_sync_signal_after_unshare" ]; do
112 # Break if the app failed / died
70a7f9f7 113 if ! kill -0 "$app_pid" ; then
5f0d4e78 114 break
70a7f9f7 115 echo "# App failed"
5f0d4e78
MJ
116 fi
117 echo "# Waiting for app..."
118 sleep 0.5
119 done
120
121 app_unshare_ns_inode=$(stat -c '%i' -L "/proc/$app_pid/ns/$ns")
122 ok $? "Get app current $ns namespace inode: $app_unshare_ns_inode" || app_unshare_ns_inode="invalid"
123
124 test "$app_ns_inode" != "invalid" && test "$app_unshare_ns_inode" != "invalid" && test "$app_ns_inode" != "$app_unshare_ns_inode"
125 ok $? "Reported namespace inode changed after unshare"
126
127 touch "$file_sync_wait_after_unshare"
128
129 stop_lttng_tracing_ok "$session_name"
130
131 # Check that the events contain the right namespace inode number
132 validate_trace_count "${ns}_ns = $app_ns_inode" "$trace_path" 1
133 validate_trace_count "${ns}_ns = $app_unshare_ns_inode" "$trace_path" 1
0159fa50
JR
134 else
135 touch "$file_sync_wait_before_unshare"
136
137 while [ ! -f "$file_sync_signal_after_unshare" ]; do
138 # Break if the app failed / died
139 if ! kill -0 "$app_pid" ; then
140 break
141 echo "# App failed"
142 fi
143 echo "# Waiting for app..."
144 sleep 0.5
145 done
146 touch "$file_sync_wait_after_unshare"
5f0d4e78
MJ
147 fi
148
0159fa50 149
5f0d4e78
MJ
150 # stop and destroy
151 destroy_lttng_session_ok "$session_name"
152 stop_lttng_sessiond
153
154 rm -rf "$trace_path"
155 rm -f "$file_sync_wait_after_unshare"
156 rm -f "$file_sync_wait_before_unshare"
157 rm -f "$file_sync_signal_after_unshare"
158}
159
160
161plan_tests $NUM_TESTS
162
163print_test_banner "$TEST_DESC"
164
165
166isroot=0
167if [ "$(id -u)" == "0" ]; then
168 isroot=1
169fi
170
171skip $isroot "Root access is needed. Skipping all tests." "$NUM_TESTS" && exit 0
172
173
174system_has_ns=0
175if [ -d "/proc/$$/ns" ]; then
176 system_has_ns=1
177fi
178
179skip $system_has_ns "System has no namespaces support" $NUM_TESTS && exit 0
180
181
182validate_lttng_modules_present
183
184test_ns cgroup
185test_ns ipc
186test_ns mnt
187test_ns net
188#test_ns pid # pid_ns is special, can't be changed that way
d37ac3cd 189#test_ns time # time_ns is special, can't be changed that way
5f0d4e78
MJ
190test_ns user
191test_ns uts
0159fa50 192set +x
This page took 0.036912 seconds and 4 git commands to generate.