summaryrefslogtreecommitdiff
path: root/common/report
blob: 9bfa09eccea8ced59a3b3a4febeb25907555946a (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
#
# Reports generator funcitons lives here
#

# List of xfstests's enviroment variables to include reports
## TODO automate list population inside common/conf
REPORT_ENV_LIST=("SECTION" "FSTYP" "PLATFORM" "MKFS_OPTIONS" "MOUNT_OPTIONS" \
		 "HOST_OPTIONS" "CHECK_OPTIONS" "XFS_MKFS_OPTIONS" \
		 "TIME_FACTOR" "LOAD_FACTOR" "TEST_DIR" "TEST_DEV" \
		 "SCRATCH_DEV" "SCRATCH_MNT" "OVL_UPPER" "OVL_LOWER" "OVL_WORK")

# Variables that are captured in the report /if/ they are set.
REPORT_ENV_LIST_OPT=("TAPE_DEV" "RMT_TAPE_DEV" "FSSTRESS_AVOID" "FSX_AVOID"
		     "KCONFIG_PATH" "PERF_CONFIGNAME" "MIN_FSSIZE"
		     "IDMAPPED_MOUNTS")

encode_xml()
{
	cat -v | \
	    sed -e 's/&/\&/g' \
		-e 's/>/\>/g' \
		-e 's/</\&lt;/g' \
		-e "s/'/\&apos;/g" \
		-e 's/"/\&quot;/g'
}

encode_cdata()
{
	cat -v | sed -e 's/]]>/]]]]><![CDATA[>/g'
}

# Fill out REPORT_VARS with information about the block device referred to by
# the passed-in bash variable.
__generate_blockdev_report_vars() {
	local bdev_var="$1"
	local bdev="${!bdev_var}"

	test -z "$bdev" && return
	test -b "$bdev" || return
	local sysfs_bdev="$(_sysfs_dev "$bdev")"

	REPORT_VARS["${bdev_var}_SIZE_KB"]="$(( "$(blockdev --getsz "$bdev")" / 2 ))"
	REPORT_VARS["${bdev_var}_ROTATIONAL"]="$(cat "$sysfs_bdev/queue/rotational" 2>/dev/null)"
	REPORT_VARS["${bdev_var}_DAX"]="$(cat "$sysfs_bdev/queue/dax" 2>/dev/null)"
	REPORT_VARS["${bdev_var}_DISCARD"]="$(sed -e 's/[1-9][0-9]*/1/g' "$sysfs_bdev/queue/discard_max_bytes" 2>/dev/null)"
	REPORT_VARS["${bdev_var}_WRITE_ZEROES"]="$(sed -e 's/[1-9][0-9]*/1/g' "$sysfs_bdev/queue/write_zeroes_max_bytes" 2>/dev/null)"
	REPORT_VARS["${bdev_var}_PHYS_BLOCK_BYTES"]="$(cat "$sysfs_bdev/queue/physical_block_size" 2>/dev/null)"
	REPORT_VARS["${bdev_var}_LBA_BYTES"]="$(cat "$sysfs_bdev/queue/logical_block_size" 2>/dev/null)"
	REPORT_VARS["${bdev_var}_ZONES"]="$(cat "$sysfs_bdev/queue/nr_zones" 2>/dev/null)"
}

__import_report_vars() {
	local fname="$1"

	while IFS=':' read key value; do
		REPORT_VARS["${key%% }"]="${value## }"
	done < "$1"
}

# Fill out REPORT_VARS with tidbits about our test runner configuration.
# Caller is required to declare REPORT_VARS to be an associative array.
__generate_report_vars() {
	test "$REPORT_VARS_FILE" && __import_report_vars "$REPORT_VARS_FILE"

	REPORT_VARS["ARCH"]="$(uname -m)"
	REPORT_VARS["KERNEL"]="$(uname -r)"
	REPORT_VARS["CPUS"]="$(getconf _NPROCESSORS_ONLN 2>/dev/null)"
	REPORT_VARS["MEM_KB"]="$(grep MemTotal: /proc/meminfo | awk '{print $2}')"
	REPORT_VARS["SWAP_KB"]="$(grep SwapTotal: /proc/meminfo | awk '{print $2}')"
	test -n "$SOAK_DURATION" && REPORT_VARS["SOAK_DURATION"]="$SOAK_DURATION"

	test -e /sys/devices/system/node/possible && \
		REPORT_VARS["NUMA_NODES"]="$(cat /sys/devices/system/node/possible 2>/dev/null)"

	__generate_blockdev_report_vars "TEST_DEV"
	__generate_blockdev_report_vars "SCRATCH_DEV"

	# Add per-filesystem variables to the report variable list
	test "$FSTYP" = "xfs" && __generate_xfs_report_vars
	[[ "$FSTYP" == ext[0-9]* ]] && __generate_ext4_report_vars

	# Optional environmental variables
	for varname in "${REPORT_ENV_LIST_OPT[@]}"; do
		test -n "${!varname}" && REPORT_VARS["${varname}"]="${!varname}"
	done
}

#
# Xunit format report functions
_xunit_add_property()
{
	local name="$1"
	local value="$2"

	test -z "$value" && return

	local xname="$(echo "$name" | encode_xml)"
	local xvalue="$(echo "$value" | encode_xml)"

	echo -e "\t\t<property name=\"$xname\" value=\"$xvalue\"/>"
}

_xunit_make_section_report()
{
	# xfstest:section ==> xunit:testsuite
	local sect_name="$1"
	local tests_count="$2"
	local bad_count="$3"
	local notrun_count="$4"
	local sect_time="$5"
	local timestamp

	if [ $sect_name == '-no-sections-' ]; then
		sect_name='global'
	fi
	local report=$tmp.report.xunit.$sect_name.xml
	# Header
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > $REPORT_DIR/result.xml
	if [ -n "$test_start_time" ]; then
		timestamp="$(date -Iseconds --date="$test_start_time")"
	else
		timestamp="$(date -Iseconds)"
	fi

	local fstests_ns="https://git.kernel.org/pub/scm/fs/xfs/xfstests-dev.git"
	cat >> $REPORT_DIR/result.xml << ENDL
<testsuite
 xmlns="$fstests_ns"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="$fstests_ns $fstests_ns/tree/doc/xunit.xsd"

 name="xfstests"
 failures="$bad_count" skipped="$notrun_count" tests="$tests_count" time="$sect_time"
 hostname="$HOST"
  start_timestamp="$(date -Iseconds --date="$fstests_start_time")"
        timestamp="$timestamp"
 report_timestamp="$(date -Iseconds)"
>
ENDL

	declare -A REPORT_VARS
	__generate_report_vars

	# Properties
	echo -e "\t<properties>" >> $REPORT_DIR/result.xml
	(for key in "${!REPORT_VARS[@]}"; do
		_xunit_add_property "$key" "${REPORT_VARS["$key"]}"
	done;
	for p in "${REPORT_ENV_LIST[@]}"; do
		_xunit_add_property "$p" "${!p}"
	done) | sort >> $REPORT_DIR/result.xml
	echo -e "\t</properties>" >> $REPORT_DIR/result.xml
	if [ -f $report ]; then
		cat $report >> $REPORT_DIR/result.xml
	fi
	echo "</testsuite>" >> $REPORT_DIR/result.xml
	echo "Xunit report: $REPORT_DIR/result.xml"
}

_xunit_make_testcase_report()
{
	local sect_name="$1"
	local test_name="$2"
	local test_status="$3"
	local test_time="$4"
	local report_format="$5"
	local quiet

	if [ "$report_format" = xunit-quiet ]; then
		quiet=yes
	fi

	# TODO: other places may also win if no-section mode will be named like 'default/global'
	if [ $sect_name == '-no-sections-' ]; then
		sect_name='global'
	fi
	local report=$tmp.report.xunit.$sect_name.xml

	echo -e "\t<testcase classname=\"xfstests.$sect_name\" name=\"$test_name\" time=\"$test_time\">" >> $report
	case $test_status in
	"pass")
		;;
	"notrun")
		local notrun_file="${REPORT_DIR}/${test_name}.notrun"
		if [ -f "$notrun_file" ]; then
			local msg=`cat "$notrun_file" | encode_xml`
			echo -e "\t\t<skipped message=\"$msg\" />" >> $report
		else
			echo -e "\t\t<skipped/>" >> $report
		fi
		;;
	"list")
		echo -e "\t\t<skipped/>" >> $report
		;;
	"fail")
		local out_src="${SRC_DIR}/${test_name}.out"
		local full_file="${REPORT_DIR}/${test_name}.full"
		local dmesg_file="${REPORT_DIR}/${test_name}.dmesg"
		local outbad_file="${REPORT_DIR}/${test_name}.out.bad"
		if [ -z "$_err_msg" ]; then
			_err_msg="Test $test_name failed, reason unknown"
		fi
		echo -e "\t\t<failure message=\"$_err_msg\" type=\"TestFail\" />" >> $report
		if [ -z "$quiet" -a -s "$full_file" ]; then
			echo -e "\t\t<system-out>" >> $report
			printf	'<![CDATA[\n' >>$report
			cat "$full_file" | tr -dc '[:print:][:space:]' | encode_cdata >>$report
			printf ']]>\n'	>>$report
			echo -e "\t\t</system-out>" >> $report
		fi
		if [ -z "$quiet" -a -f "$dmesg_file" ]; then
			echo -e "\t\t<kernel-log>" >> $report
			printf	'<![CDATA[\n' >>$report
			cat "$dmesg_file" | tr -dc '[:print:][:space:]' | encode_cdata >>$report
			printf ']]>\n'	>>$report
			echo -e "\t\t</kernel-log>" >> $report
		fi
		if [ -z "$quiet" -a -s "$outbad_file" ]; then
			echo -e "\t\t<system-err>" >> $report
			printf	'<![CDATA[\n' >>$report
			$diff "$out_src" "$outbad_file" | encode_cdata >>$report
			printf ']]>\n'	>>$report
			echo -e "\t\t</system-err>" >> $report
		fi
		;;
	*)
		echo -e "\t\t<failure message=\"Unknown test_status=$test_status\" type=\"TestFail\"/>" >> $report
		;;
	esac
	echo -e "\t</testcase>" >> $report
}


#
#  Common report generator entry points
_make_section_report()
{
	local sect_name="$1"
	local tests_count="$2"
	local bad_count="$3"
	local notrun_count="$4"
	local sect_time="$5"
	for report in $REPORT_LIST; do
		case "$report" in
		"xunit"|"xunit-quiet")
			_xunit_make_section_report "$sect_name" "$tests_count" \
						   "$bad_count" "$notrun_count" \
						   "$sect_time"
			;;
		*)
			_dump_err "format '$report' is not supported"
			;;
		esac
	done
}

_make_testcase_report()
{
	local sect_name="$1"
	local test_seq="$2"
	local test_status="$3"
	local test_time="$4"
	for report in $REPORT_LIST; do
		case "$report" in
		"xunit"|"xunit-quiet")
			_xunit_make_testcase_report "$sect_name" "$test_seq" \
						    "$test_status" "$test_time" "$report"
			;;
		*)
			_dump_err "report format '$report' is not supported"
			;;
		esac
	done
}

_assert_report_list() {
	for report in $REPORT_LIST; do
		case "$report" in
		"xunit"|"xunit-quiet")
			;;
		*)
			_fatal "report format '$report' is not supported"
			;;
		esac
	done
}