summaryrefslogtreecommitdiffstats
path: root/test/integration/run-tests
blob: 331adfcfa502602ee54213fbb37962bdf34dbb90 (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
#!/bin/sh
set -e

TESTTORUN=''
while [ -n "$1" ]; do
	if [ "$1" = "-q" ]; then
		export MSGLEVEL=2
	elif [ "$1" = "-qq" ]; then
		export MSGLEVEL=1
	elif [ "$1" = "-v" ]; then
		export MSGLEVEL=4
	elif [ "$1" = '--color=no' ]; then
		export MSGCOLOR='NO'
	elif [ "$1" = '--color=yes' ]; then
		export MSGCOLOR='YES'
	elif [ "$1" = '--color' ]; then
		export MSGCOLOR="$(echo "$2" | tr 'a-z' 'A-Z')"
		shift
	elif [ "$1" = '--level' ]; then
		export MSGLEVEL=$2
		shift
	elif [ "$1" = '-j' ]; then
		APT_TEST_JOBS=$2
		shift
	elif [ "$1" = '--solver' ]; then
		export APT_SOLVER="$2"
		shift
	elif [ "$1" = '--skip' ]; then
		export APT_SKIP_TEST_FILE="$2"
		shift
	elif [ "$1" = '--only' ]; then
		TESTLIST="$2"
		shift
	elif [ -e "$1" ]; then
		TESTTORUN="$1"
	else
		echo >&2 "WARNING: Unknown parameter »$1« will be ignored"
	fi
	shift
done
export MSGLEVEL="${MSGLEVEL:-3}"

if [ "${MSGCOLOR:-YES}" = 'YES' ]; then
	if [ ! -t 1 ]; then # but check that we output to a terminal
		export MSGCOLOR='NO'
	fi
fi
if [ "$MSGCOLOR" != 'NO' ]; then
	CTEST='\033[1;32m'
	CHIGH='\033[1;35m'
	CRESET='\033[0m'
else
	CTEST=''
	CHIGH=''
	CRESET=''
fi

if [ -n "$TESTTORUN" ]; then
	# collecting the output of one test to have it together
	OUTPUT="$(mktemp)"
	CURRENTTRAP="rm -f \"$OUTPUT\"; $CURRENTTRAP"
	trap "$CURRENTTRAP" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
	{
		if [ "$APT_SKIP_TEST_FILE" ] && grep -qFx "${TESTTORUN##*/}" "$APT_SKIP_TEST_FILE"; then
			if [ "$MSGLEVEL" -le 2 ]; then
				printf "${CTEST}Skip Testcase ${CHIGH}${TESTTORUN##*/}${CRESET}"
			else
				printf "${CTEST}Skip Testcase ${CHIGH}${TESTTORUN##*/}${CRESET}\n"
			fi
			SKIP='yes'
		elif [ "$MSGLEVEL" -le 1 ]; then
			printf "${TESTTORUN##*/}"
		elif [ "$MSGLEVEL" -le 2 ]; then
			printf "${CTEST}Testcase ${CHIGH}${TESTTORUN##*/}${CRESET}: "
		else
			printf "${CTEST}Run Testcase ${CHIGH}${TESTTORUN##*/}${CRESET}\n"
		fi

		if [ "$SKIP" != "yes" ] && ! "$TESTTORUN"; then
			FAIL='yes'
			if [ "$MSGLEVEL" -le 2 ]; then
				printf >&2 "\n${CHIGH}Running ${TESTTORUN##*/} -> FAILED${CRESET}\n"
			elif [ "$MSGLEVEL" -le 2 ]; then
				printf >&2 "\n${CHIGH}Running ${TESTTORUN##*/} -> FAILED${CRESET}"
			else
				echo >&2 "${CHIGH}Running ${TESTTORUN##*/} -> FAILED${CRESET}"
			fi
		else
			if [ "$MSGLEVEL" -le 1 ]; then
				printf " "
			fi
		fi
		if [ "$MSGLEVEL" -le 1 ]; then
			:
		elif [ "$MSGLEVEL" -le 2 ]; then
			echo
		fi
	} >"$OUTPUT" 2>&1
	# without we end up getting stepped output 'randomly'
	stty sane || true
	cat >&2 "$OUTPUT"
	stty sane || true
	if [ "$STATS_FILE" ]; then
		flock "$STATS_FILE" -c "echo $TESTTORUN skip=${SKIP:-no} fail=${FAIL:-no} >> \"$STATS_FILE\""
	fi
	if [ "$FAIL" = 'yes' ]; then
		exit 1
	else
		exit 0
	fi
fi

FAIL=0
PASS=0
ALL=0
FAILED_TESTS=""
DIR="$(readlink -f "$(dirname "$0")")"
cd "$DIR"
if [ -e "$TESTLIST" ]; then
	TESTLIST="$(sort < "$TESTLIST" | sed 's#^#./#')"
else
	TESTLIST="$(find . -mindepth 1 -maxdepth 1 -regex '^\./test-[^/]*$' | sort)"
fi
if [ -n "$APT_TEST_JOBS" ]; then
	export STATS_FILE="$(mktemp)"
	CURRENTTRAP="rm -f \"$STATS_FILE\"; $CURRENTTRAP"
	trap "$CURRENTTRAP" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
	if [ "$MSGCOLOR" != 'NO' ]; then
		export MSGCOLOR='ALWAYS'
	fi
	parallel=parallel
	if command -v moreutils-parallel >/dev/null 2>&1; then
		parallel=moreutils-parallel
	elif command -v parallel.moreutils >/dev/null 2>&1; then
		parallel=parallel.moreutils
	fi
	$parallel -j "$APT_TEST_JOBS" "./$(basename "$0")" -- $(echo "$TESTLIST") || true
	ALL=$(wc -l < "$STATS_FILE")
	SKIP=$(grep -c "skip=yes" "$STATS_FILE" || true)
	PASS=$(grep -c "skip=no fail=no" "$STATS_FILE" || true)
	FAIL=$(grep -c "fail=yes" "$STATS_FILE" || true)
	PASSED_TESTS=$(awk '! /fail=yes/ {print $1}' < "$STATS_FILE" | cut -f2 -d/ | sort | xargs)
	SKIPPED_TESTS=$(awk '/fail=skip/ {print $1}' < "$STATS_FILE" | cut -f2 -d/ | sort | xargs)
	FAILED_TESTS=$(awk '/fail=yes/ {print $1}' < "$STATS_FILE" | cut -f2 -d/ | sort | xargs)
	echo >&2 "Statistics: $ALL tests were run: $PASS successfully and $FAIL failed, $SKIP skipped"
	if [ -n "$FAILED_TESTS" ]; then
		if [ $PASS -lt $FAIL ]; then
			echo >&2 "Passed tests: $PASSED_TESTS"
		else
			echo >&2 "Failed tests: $FAILED_TESTS"
		fi
	else
		echo >&2 'All tests seem to have been run successfully. What could possibly go wrong?'
	fi
	# ensure we don't overflow
	exit $((FAIL <= 255 ? FAIL : 255))
fi

APT_TEST_SIGNINGHOME="$(mktemp --directory --tmpdir 'apt-key-signinghome.XXXXXXXXXX')"
removesigninghome() {
	if [ -z "$APT_TEST_SIGNINGHOME" ]; then return; fi
	GNUPGHOME="${APT_TEST_SIGNINGHOME}" gpgconf --kill gpg-agent >/dev/null 2>&1 || true
	rm -rf -- "$APT_TEST_SIGNINGHOME"
}
trap "exit 1" 0 HUP INT ILL ABRT FPE SEGV PIPE TERM
trap "removesigninghome" 0 QUIT
export APT_TEST_SIGNINGHOME

TOTAL="$(echo "$TESTLIST" | wc -l)"
if [ "$MSGLEVEL" -le 1 ]; then
	printf "${CTEST}Running testcases${CRESET}: "
fi
for testcase in $TESTLIST; do
	if [ "$APT_SKIP_TEST_FILE" ] && grep -qFx "${TESTTORUN##*/}" "$APT_SKIP_TEST_FILE"; then
		printf "${CTEST}Skipping Testcase ${CHIGH}${TESTTORUN##*/}${CRESET}\n"
		continue
	fi
	if [ "$MSGLEVEL" -le 1 ]; then
		printf "${testcase##*/}"
	elif [ "$MSGLEVEL" -le 2 ]; then
		printf "($(($ALL+1))/${TOTAL}) ${CTEST}Testcase ${CHIGH}${testcase##*/}${CRESET}: "
	else
		printf "${CTEST}Run Testcase ($(($ALL+1))/${TOTAL}) ${CHIGH}${testcase##*/}${CRESET}\n"
	fi
	if ! ${testcase}; then
		FAIL=$((FAIL+1))
		FAILED_TESTS="$FAILED_TESTS ${testcase##*/}"
		if [ "$MSGLEVEL" -le 1 ]; then
			printf >&2 "\n${CHIGH}Running ${testcase##*/} -> FAILED${CRESET}\n"
		elif [ "$MSGLEVEL" -le 2 ]; then
			printf >&2 "\n${CHIGH}Running ${testcase##*/} -> FAILED${CRESET}"
		else
			echo >&2 "${CHIGH}Running ${testcase##*/} -> FAILED${CRESET}"
		fi
	else
		PASS=$((PASS+1))
		if [ "$MSGLEVEL" -le 1 ]; then
			printf " "
		fi
	fi
	ALL=$((ALL+1))
	if [ "$MSGLEVEL" -le 1 ]; then
		:
	elif [ "$MSGLEVEL" -le 2 ]; then
		echo
	fi
done

echo >&2 "Statistics: $ALL tests were run: $PASS successfully and $FAIL failed"
if [ -n "$FAILED_TESTS" ]; then
	echo >&2 "Failed tests: $FAILED_TESTS"
else
	echo >&2 'All tests seem to have been run successfully. What could possibly go wrong?'
fi
# ensure we don't overflow
exit $((FAIL <= 255 ? FAIL : 255))