summaryrefslogtreecommitdiffstats
path: root/bin/test_torbrowser-launcher.sh
blob: f7113a0d89206fb8f45b11ae6e44cd1bbadc3f16 (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
#!/bin/bash

# Copyright 2015 Holger Levsen <holger@layer-acht.org>
# released under the GPLv=2

DEBUG=false
. /srv/jenkins/bin/common-functions.sh
common_init "$@"

set -e

cleanup_all() {
	set +e
	if [ "$1" = "quiet" ] ; then
		echo "$(date -u) - everything ran nicely, congrats."
	fi
	# kill xvfb and ffmpeg
	kill $XPID $FFMPEGPID 2>/dev/null|| true
	# preserve screenshots
	[ ! -f screenshot.png ] || mv screenshot.png $RESULTS/
	[ ! -f screenshot-thumb.png ] || mv screenshot-thumb.png $RESULTS/
	[ ! -f $VIDEO ] || mv $VIDEO $RESULTS/
	[ ! -f screenshot_from_git.png ] || mv screenshot_from_git.png screenshot.png
	# shutdown and end session if it still exists
	STATUS=$(schroot -l --all-sessions | grep $SESSION || true)
	if [ -n "$STATUS" ] ; then
		echo "$(date -u ) - stopping dbus service."
		schroot --run-session -c $SESSION --directory /tmp -u root -- service dbus stop || true
		sleep 1
		schroot --end-session -c $SESSION || true
		echo "$(date -u ) - schroot session $SESSION end."
	fi
	# delete main work dir
	cd
	rm $TMPDIR -r
	# end
	echo "$(date -u) - $TMPDIR deleted. Cleanup done."
}

update_screenshot() {
	TIMESTAMP=$(date +%Y%m%d%H%M%S)
	ffmpeg -y -f x11grab -s $SIZE -i :$SCREEN.0 -frames 1 screenshot.png > /dev/null 2>&1
	convert screenshot.png -adaptive-resize 128x96 screenshot-thumb.png
	# for publishing
	cp screenshot.png $RESULTS/screenshot_$TIMESTAMP.png
	echo "screenshot_$TIMESTAMP.png preserved."
	# for the live screenshot plugin
	mv screenshot.png screenshot-thumb.png $WORKSPACE/
}

begin_session() {
	schroot --begin-session --session-name=$SESSION -c jenkins-torbrowser-launcher-$SUITE
	echo "Starting schroot session, schroot --run-session -c $SESSION -- now availble."
	schroot --run-session -c $SESSION --directory /tmp -u root -- mkdir $HOME
	schroot --run-session -c $SESSION --directory /tmp -u root -- chown jenkins:jenkins $HOME
}

end_session() {
	schroot --end-session -c $SESSION
	echo "$(date -u ) - schroot session $SESSION end."
	sleep 1
}

upgrade_to_experimental_version() {
	echo
	echo "$(date -u ) - upgrading to torbrowser-launcher from experimental…"
	echo "deb $MIRROR experimental main contrib" | schroot --run-session -c $SESSION --directory /tmp -u root -- tee -a /etc/apt/sources.list
	schroot --run-session -c $SESSION --directory /tmp -u root -- apt-get update
	schroot --run-session -c $SESSION --directory /tmp -u root -- apt-get -y install -t experimental torbrowser-launcher
}

build_and_upgrade_to_git_version() {
	echo
	echo "$(date -u ) - building torbrowser-launcher from git, branch $BRANCH…"
	schroot --run-session -c $SESSION --directory $TMPDIR/git -- debuild -b
	DEB=$(cd $TMPDIR/git ; ls torbrowser-launcher_*deb)
	echo "$(date -u ) - installing $DEB…"
	schroot --run-session -c $SESSION --directory $TMPDIR/git -u root -- dpkg -i $DEB
	rm $TMPDIR/git -r
}

download_and_launch() {
	echo
	echo "$(date -u) - Test download_and_launch begins."
	echo "$(date -u ) - starting dbus service."
	schroot --run-session -c $SESSION --directory /tmp -u root -- service dbus start
	sleep 2
	echo "$(date -u) - starting Xfvb on :$SCREEN.0…"
	Xvfb -ac -br -screen 0 ${SIZE}x24 :$SCREEN &
	XPID=$!
	sleep 1
	export DISPLAY=":$SCREEN.0"
	echo export DISPLAY=":$SCREEN.0"
	unset http_proxy
	unset https_proxy
	echo "$(date -u) - starting awesome…"
	timeout -k 30m 29m schroot --run-session -c $SESSION --preserve-environment -- awesome &
	sleep 2
	DBUS_SESSION_FILE=$(mktemp)
	DBUS_SESSION_POINTER=$(schroot --run-session -c $SESSION --preserve-environment -- ls $HOME/.dbus/session-bus/ -t1 | head -1)
	schroot --run-session -c $SESSION --preserve-environment -- cat $HOME/.dbus/session-bus/$DBUS_SESSION_POINTER > $DBUS_SESSION_FILE
	. $DBUS_SESSION_FILE && export DBUS_SESSION_BUS_ADDRESS
	echo export DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS
	rm $DBUS_SESSION_FILE
	ffmpeg -f x11grab -s $SIZE -i :$SCREEN.0 $VIDEO > /dev/null 2>&1 &
	FFMPEGPID=$!
	echo "'$(date -u) - starting torbrowser tests'" | tee >( xargs schroot --run-session -c $SESSION --preserve-environment -- notify-send )
	update_screenshot
	echo "$(date -u) - starting torbrowser-launcher the first time…"
	( timeout -k 30m 29m schroot --run-session -c $SESSION --preserve-environment -- torbrowser-launcher --settings || true ) &
	sleep 10
	update_screenshot
	echo "$(date -u) - pressing, <tab>, <return>…"
	xvkbd -text "\t" > /dev/null 2>&1
	sleep 1
	update_screenshot
	xvkbd -text "\r" > /dev/null 2>&1
	for i in $(seq 1 60) ; do
		sleep 10
		update_screenshot
		# this directory only exist once torbrower has been successfully installed
		STATUS="$(schroot --run-session -c $SESSION -- [ ! -d $HOME/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US/Browser ] || echo '$(date -u ) - torbrowser downloaded and installed.')"
		if [ -n "$STATUS" ] ; then
			sleep 10
			echo "'$STATUS'" | tee >( xargs schroot --run-session -c $SESSION --preserve-environment -- notify-send )
			update_screenshot
			break
		fi
	done
	if [ ! -n "$STATUS" ] ; then
		echo "'$(date -u) - could not download torbrowser, please investigate.'" | tee >( xargs schroot --run-session -c $SESSION --preserve-environment -- notify-send -u critical )
		update_screenshot
		exit 1
	fi
	echo "$(date -u) - waiting for torbrowser to start…"
	for i in $(seq 1 6) ; do
		sleep 10
		# this directory only exist once torbrower has successfully started
		STATUS="$(schroot --run-session -c $SESSION -- [ ! -d $HOME/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US/Browser/TorBrowser/Data/Browser/profile.default ] || echo '$(date -u ) - torbrowser running.')"
		if [ -n "$STATUS" ] ; then
			sleep 10
			echo "'$STATUS'" | tee >( xargs schroot --run-session -c $SESSION --preserve-environment -- notify-send )
			update_screenshot
			break
		fi
	done
	if [ ! -n "$STATUS" ] ; then
		echo "'$(date -u) - could not start torbrowser, please investigate.'" | tee >( xargs schroot --run-session -c $SESSION --preserve-environment -- notify-send -u critical )
		update_screenshot
		exit 1
	fi
	echo "$(date -u) - pressing <return>, to connect directly via tor…"
	xvkbd -text "\r" > /dev/null 2>&1
	sleep 5
	for i in $(seq 1 2) ; do
		sleep 15
		update_screenshot
	done
	echo "$(date -u) - pressing <ctrl>-l - about to enter an URL…"
	xvkbd -text "\Cl" > /dev/null 2>&1
	sleep 3
	URL="https://www.debian.org"
	xvkbd -text "$URL" > /dev/null 2>&1
	update_screenshot
	sleep 0.5
	xvkbd -text "\r" > /dev/null 2>&1
	update_screenshot
	for i in $(seq 1 2) ; do
		sleep 15
		update_screenshot
	done
	sleep 1
	echo "'$(date -u) - torbrowser tests end.'" | tee >( xargs schroot --run-session -c $SESSION --preserve-environment -- notify-send )
	update_screenshot
	echo "$(date) - telling awesome to quit."
	echo 'awesome.quit()' | schroot --run-session -c $SESSION --preserve-environment -- awesome-client
	sleep 1
	schroot --run-session -c $SESSION --directory /tmp -u root -- service dbus stop
	sleep 1
	echo "$(date -u ) - killing Xfvb and ffmpeg."
	kill $XPID $FFMPEGPID || true
	sleep 1
	echo "$(date -u ) - Test ends."
	echo
}

#
# prepare
#
if [ -z "$1" ] ; then
	echo "call $0 with a suite as param."
	exit 1
fi
SUITE=$1
TMPDIR=$(mktemp -d)  # where everything actually happens
SESSION="tbb-launcher-$SUITE-$(basename $TMPDIR)"
STARTTIME=$(date +%Y%m%d%H%M)
VIDEO=test-torbrowser-${SUITE}_$STARTTIME.mpg
SIZE=1024x768
SCREEN=$EXECUTOR_NUMBER
if [ "$2" = "git" ] ; then
	if [ -z "$3"  ] ; then
		BRANCH=master
	else
		BRANCH=$3
	fi
	echo "$(date -u) - preserving git workspace."
	git branch -av
	mkdir $TMPDIR/git
	cp -r * $TMPDIR/git
elif [ "$SUITE" = "experimental" ] || [ "$2" = "experimental" ] ; then
	SUITE=unstable
	EXPERIMENTAL=yes
fi
WORKSPACE=$(pwd)
RESULTS=$WORKSPACE/results
[ ! -f screenshot.png ] || mv screenshot.png screenshot_from_git.png
mkdir -p $RESULTS
cd $TMPDIR
trap cleanup_all INT TERM EXIT

#
# main
#
echo "$(date -u) - testing torbrowser-launcher on $SUITE now."
begin_session
if [ "$2" = "git" ] ; then
	build_and_upgrade_to_git_version
elif [ "$EXPERIMENTAL" = "yes" ] ; then
	upgrade_to_experimental_version
fi
download_and_launch
end_session

# the end
trap - INT TERM EXIT
cleanup_all quiet
echo "$(date -u) - the end."