summaryrefslogtreecommitdiff
path: root/test/lib/utils.sh
blob: ca9f5ec6117ff6890303a20e1ae23eebb58a59c7 (plain)
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
#!/usr/bin/env bash
# Copyright (C) 2011-2012 Red Hat, Inc. All rights reserved.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions
# of the GNU General Public License v.2.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

set -e
MAX_TRIES=4
IFS_NL='
'

die() {
	rm -f debug.log*
	echo -e "$@" >&2
	return 1
}

rand_bytes() {
	n=$1

	chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

	dev_rand="/dev/urandom"
	if test -r "$dev_rand"; then
		# Note: 256-length($chars) == 194; 3 copies of $chars is 186 + 8 = 194.
		head -c"$n" "$dev_rand" | tr -c "$chars" "01234567$chars$chars$chars"
		return
	fi

	cmds='date; date +%N; free; who -a; w; ps auxww; ps ef; netstat -n'
	data=$( (eval "$cmds") 2>&1 | gzip )

	n_plus_50=$(( n + 50 ))

	# Ensure that $data has length at least 50+$n
	while :; do
		len=${#data} # number of chars in $data
		test "$n_plus_50" -le "$len" && break;
		data=$( (echo "$data"; eval "$cmds") 2>&1 | gzip )
	done

	echo "$data" | dd bs=1 skip=50 count="$n" 2>/dev/null \
		| tr -c "$chars" "01234567$chars$chars$chars"
}

mkdtemp() {
	case $# in
		2) ;;
		*) die "Usage: mkdtemp DIR TEMPLATE";;
	esac

	destdir=$1
	template=$2

        test -d "$destdir" || die "DIR ('$destdir') does not exist."

	case "$template" in
		*XXXX) ;;
		*) die "Invalid template: $template (must have a suffix of at least 4 X's)";;
	esac

	fail=0

	# First, try to use mktemp.
	d=$(env -u TMPDIR mktemp -d -t -p "$destdir" "$template" 2>/dev/null) || fail=1

	# The resulting name must be in the specified directory.
	case "$d" in "${destdir}"*);; *) fail=1;; esac

	# It must have created the directory.
	test -d "$d" || fail=1

	# It must have 0700 permissions.
	perms=$(ls -dgo "$d" 2>/dev/null) || fail=1
	case "$perms" in drwx------*) ;; *) fail=1;; esac

	test $fail = 0 && { echo "$d"; return; }

	# If we reach this point, we'll have to create a directory manually.

	# Get a copy of the template without its suffix of X's.
	base_template=$(echo "$template" | sed 's/XX*$//')

	# Calculate how many X's we've just removed.
	nx=$(( ${#template} - ${#base_template} ))

	err=
	i=1
	while :; do
		X=$(rand_bytes "$nx")
		candidate_dir="$destdir/$base_template$X"
		err=$(mkdir -m 0700 "$candidate_dir" 2>&1) && \
			{ echo "$candidate_dir"; return; }
		test $MAX_TRIES -le $i && break;
		i=$(( i + 1 ))
	done
	die "$err"
}

# Like grep, just always print 1st. line
grep1_() {
	awk -v pattern="${1}" 'NR==1 || $0~pattern' "${@:2}"
}

stacktrace() {
	trap - ERR
	# i=1 - ignoring innermost frame - it is always stacktrace function
	local i=1 n=${#BASH_LINENO[*]}
	# n-=1 - ignoring last frame as well - it is not interesting
	n=$(( n - 1 ))

	echo "## - $0:${BASH_LINENO[$((n-1))]}"
	while [[ $i -lt $n ]]; do
		echo "## $i ${FUNCNAME[$i]}() called from ${BASH_SOURCE[$((i+1))]}:${BASH_LINENO[$i]}"
		i=$(( i + 1 ))
	done
}

STACKTRACE() {
	trap - ERR
	local i

	stacktrace

	test "${LVM_TEST_PARALLEL:-0}" -eq 0 && test -z "$RUNNING_DMEVENTD" && \
		test ! -f LOCAL_DMEVENTD && pgrep dmeventd >DPID 2>/dev/null  && {
			echo "## ERROR: The test started dmeventd ($(< DPID)) unexpectedly."
			kill "$(< DPID)"
		}

	# Get backtraces from coredumps
	if which gdb &>/dev/null; then
		# Check for all cores newer then TESTNAME file
		# Assume users keep prefix 'core'
		# TODO: possibly better integrate with coredumpctl & systemd
		while IFS= read -r i; do
			bin=$(gdb -batch -c "$i" 2>&1 | grep "generated by" | \
			sed -e "s,.*generated by \`\([^ ']*\).*,\1,") || continue
			{
				echo bt full
				echo l
				echo quit
			} > gdb_commands.txt || rm -f gdb_commands.txt

			if test ! -s gdb_commands.txt ; then
				echo "Out of disk space, can't check coredump $i generated by $bin."
				continue
			fi

			echo "## Checking coredump: $i generated by $bin."
			gdb -batch -c "$i" -x gdb_commands.txt "$(which "$bin")" 2>/dev/null | \
			sed -e "s,^,## GDB:	," || continue
		done < <(find . "$(dirname "$(sysctl -n kernel.core_pattern)")" \
			"/var/lib/systemd/coredump/" -name 'core*' -newer TESTNAME 2>/dev/null || true)
	fi

	test -f SKIP_THIS_TEST && exit 200

	test -z "$LVM_TEST_NODEBUG" && test -f TESTNAME && {
		local name
		local idx=0
		for i in debug.log* ; do
			test -f "$i" || break  # nothing is found (expands to debug.log*)
			name=${i##debug.log_}
			name=${name%%_*}
			test "$name" = "DEBUG" && { name="$name$idx" ; idx=$(( idx + 1 )) ; }
			echo "<======== Debug log $i ========>"
			sed -e "s,^,## $name: ," "$i"
			mv -f "$i" "debug_${i#debug.}"
		done
		if test -e strace.log ; then
			echo "<======== Strace debug log ========>"
			sed -e "s,^,## STRACE: ," strace.log
		fi
		if dmsetup info -c | grep -q "$PREFIX" ; then
			echo "<======== Info ========>"
			dmsetup info -c | grep1_ "$PREFIX"| sed -e "s,^,## DMINFO:   ,"
			echo "<======== Active table ========>"
			dmsetup table | grep "$PREFIX" | sed -e "s,^,## DMTABLE:  ,"
			echo "<======== Inactive table ========>"
			dmsetup table --inactive  | grep "$PREFIX" | sed -e "s,^,## DMITABLE: ,"
			echo "<======== Status ========>"
			dmsetup status --noflush | grep "$PREFIX" | sed -e "s,^,## DMSTATUS: ,"
			echo "<======== Tree ========>"
			dmsetup ls --tree | sed -e "s,^,## DMTREE:   ,"
			echo "<======== Recursive list of $DM_DEV_DIR ========>"
			ls -Rl --hide=shm --hide=bus --hide=snd --hide=input --hide=dri \
			       --hide=net --hide=hugepages --hide=mqueue --hide=pts \
			       "$DM_DEV_DIR" | sed -e "s,^,## LSLR:	,"
			echo "<======== Udev DB content ========>"
			for i in /sys/block/dm-* /sys/block/loop* ; do
				udevadm info --query=all --path "$i" 2>/dev/null || true
			done | sed -e "s,^,## UDEV:	,"
		fi
		echo "<======== Script file \"$(< TESTNAME)\" ========>"
		local script=$0
		test -f "$script" || script="$TESTOLDPWD/$0"
		awk '{print "## Line:", NR, "\t", $0}' "$script"
	}
}

init_udev_transaction() {
	if test "$DM_UDEV_SYNCHRONISATION" = 1; then
		local cookie
		cookie=$(dmsetup udevcreatecookie)
		# Cookie is not generated if udev is not running!
		test -z "$cookie" || export DM_UDEV_COOKIE=$cookie
	fi
}

finish_udev_transaction() {
	if test "$DM_UDEV_SYNCHRONISATION" = 1 && test -n "${DM_UDEV_COOKIE-}" ; then
		dmsetup udevreleasecookie || true
		unset DM_UDEV_COOKIE
	fi
}

teardown_udev_cookies() {
	if test "$DM_UDEV_SYNCHRONISATION" = 1; then
		# Delete any cookies created more than 10 minutes ago
		# and not used in the last 10 minutes.
		# Log only non-zero semaphores count
		(dmsetup udevcomplete_all -y 10 | grep -v "^0 ") || true
	fi
}

dm_info() {
	should dmsetup info --noheadings -c -o "$@"
}

dm_status() {
	should dmsetup status --noheadings "$@"
}

dm_table() {
	should dmsetup table "$@"
}

skip() {
	set +vx # debug off
	if test "$#" -eq 0; then
		stacktrace
	else
		echo -e "TEST SKIPPED:" "$@"
	fi
	touch SKIP_THIS_TEST
	exit 200
}

get_real_devs() {
	REAL_DEVICES=( $(<REAL_DEVICES) )
	export REAL_DEVICES
}

get_devs() {
	local IFS=$IFS_NL
	DEVICES=( $(<DEVICES) )
	export DEVICES
#	local DEVS=( $(<DEVICES) )
#	eval "$1"'=("${DEVS[@]}")'
}

prepare_test_vars() {
	vg="${PREFIX}vg"
	lv="LV"

	for i in {1..16}; do
		eval "lv$i=\"LV$i\""
		eval "vg$i=\"${PREFIX}vg$i\""
	done

	if test -n "$LVM_TEST_DEVICE_LIST"; then
		local count=0
		while read path; do
			count=$((  count + 1 ))
			eval "dev$count=\"$path\""
		done < "$LVM_TEST_DEVICE_LIST"
	else
		for i in {1..16}; do
			eval "dev$i=\"$DM_DEV_DIR/mapper/${PREFIX}pv$i\""
		done
	fi
}

if test -z "${abs_top_builddir+varset}" && test -z "${installed_testsuite+varset}"; then
    . lib/paths || die "something went wrong -- lib/paths is missing?"
fi

if test -z "${installed_testsuite+varset}"; then
    case "$PATH" in
    *"$abs_top_builddir/test/lib"*) ;;
    *)
	PATH="$abs_top_builddir/test/lib:$abs_top_builddir/test/api:$PATH"
	LVM_BINARY=$(which lvm)
	LD_LIBRARY_PATH=$(find -L "$abs_top_builddir/libdm/" "$abs_top_builddir/tools/"\
		"$abs_top_builddir/daemons/" \
		-name "*.so" -printf "%h:")"$LD_LIBRARY_PATH"
	export PATH LD_LIBRARY_PATH LVM_BINARY ;;
    esac
fi

test -z "$PREFIX" || prepare_test_vars