File: test_ns_contexts_change

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 (123 lines) | stat: -rwxr-xr-x 3,382 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
#!/bin/bash
#
# SPDX-FileCopyrightText: 2019 Michael Jeanson <mjeanson@efficios.com>
#
# SPDX-License-Identifier: LGPL-2.1-only

TEST_DESC="UST - Namespace contexts change"

CURDIR=$(dirname "$0")/
TESTDIR=$CURDIR/../../..

TESTAPP_PATH="$TESTDIR/utils/testapp"
TESTAPP_NAME="gen-ust-events-ns"
TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
NUM_EVENT=1000
EVENT_NAME="tp:tptest"

TESTS_PER_NS=16

NUM_TESTS=$((TESTS_PER_NS * 5))

source "$TESTDIR/utils/utils.sh"

# MUST set TESTDIR before calling those functions

function test_ns()
{
	local ns=$1

	local session_name="${ns}_ns"
	local chan_name="${ns}_ns"
	local context_name="${ns}_ns"

	local trace_path
	local ns_inode
	local file_sync_before_last
	local file_sync_after_unshare

	# Check if the kernel has support for this ns type
	if [ ! -f "/proc/$$/ns/$ns" ]; then
		skip 0 "System has no $ns namespace support" $TESTS_PER_NS
		return
	fi

	# Get the current ns inode number
	ns_inode=$(stat -c '%i' -L "/proc/$$/ns/$ns")
	ok $? "Get current $ns namespace inode: $ns_inode" || ns_inode="invalid"

	trace_path=$(mktemp -d -t "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")
	file_sync_before_last=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_last.XXXXXX")
	file_sync_after_unshare=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_after_unshare.XXXXXX")

	start_lttng_sessiond

	create_lttng_session_ok "$session_name" "$trace_path"
	enable_ust_lttng_channel_ok "$session_name" "$chan_name"
	add_context_ust_ok "$session_name" "$chan_name" "$context_name"
	enable_ust_lttng_event_ok "$session_name" "$EVENT_NAME" "$chan_name"
	start_lttng_tracing_ok "$session_name"

	$TESTAPP_BIN -n "$ns" -i $NUM_EVENT -a "$file_sync_after_unshare" -b "$file_sync_before_last" &
	app_pid=$!

	while [ ! -f "$file_sync_after_unshare" ]; do
		# Break if the app failed / died
		if ! kill -0 "$app_pid" ; then
			echo "# App failed"
			break
		fi
		echo "# Waiting for app..."
		sleep 0.5
	done

	app_ns_inode=$(stat -c '%i' -L "/proc/$app_pid/ns/$ns")
	ok $? "Get current $ns namespace inode: $app_ns_inode" || app_ns_inode="invalid"

	test "$ns_inode" != "invalid" && test "$app_ns_inode" != "invalid" && test "$ns_inode" != "$app_ns_inode"
	ok $? "Reported namespace inode changed after unshare"

	touch "$file_sync_before_last"

	# Wait for the test app to generate all expected events and exit.
	wait $app_pid

	# stop and destroy
	stop_lttng_tracing_ok "$session_name"
	destroy_lttng_session_ok "$session_name"
	stop_lttng_sessiond

	# Check that the events contain the right namespace inode number
	validate_trace_count "${ns}_ns = $ns_inode" "$trace_path" $NUM_EVENT
	validate_trace_count "${ns}_ns = $app_ns_inode" "$trace_path" $NUM_EVENT

	rm -rf "$trace_path"
	rm -f "$file_sync_before_last"
	rm -f "$file_sync_after_unshare"
}


plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

bail_out_if_no_babeltrace

check_skip_kernel_test "$NUM_TESTS" "Skipping all tests." && exit 0

system_has_ns=0
if [ -d "/proc/$$/ns" ]; then
	system_has_ns=1
fi

skip $system_has_ns "System has no namespaces support" $NUM_TESTS && exit 0


test_ns cgroup
test_ns ipc
test_ns mnt
test_ns net
#test_ns pid # pid_ns is special, can't be changed that way
#test_ns time # time_ns is special, can't be changed that way
#test_ns user # user_ns can only be change when the app is single threaded, this is always false for an ust instrumented app
test_ns uts