~zeller-benjamin/ubuntu-ui-toolkit/trusty-builds

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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#!/bin/bash
# Copyright 2014 Canonical Ltd.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; version 2.1.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author: Zoltán Balogh <zoltan.baloghn@canonical.com>

LAZY=true
SERIALNUMBER=086e443edf51b915
RESET=false
COMISSION=false
COMISSION_SILO=false
COMISSION_FLASH=false
COMISSION_BOOTSTRAP=false
DONOTRUNTESTS=false
PPA="ubuntu-sdk-team/staging"
TIMESTAMP=`date +"%Y_%m_%d-%H_%M_%S"`
DATESTAMP=`date +"%Y_%m_%d"`
LOGFILENAME="ap-${TIMESTAMP}"
OUTPUTDIR=$HOME
FILTER=.*
RTM=true
REVISION=105
DISTRO="ubuntu" 
SERIES="vivid"
CHANNEL="ubuntu-touch/rc-proposed/${DISTRO}"
PASSWORD="0000"
BOOTTIME=250
ONLYCOMPARE=false
DISTUPGRADE=false
UNLOCK_ONLY=false
NORMAL_USE=false
WIPE="--wipe"
VERBOSE=true

declare -a TEST_SUITE=(
    " -p mediaplayer-app-autopilot mediaplayer_app"
    " -p dialer-app-autopilot dialer_app"
    " -p reminders-app-autopilot reminders"
    " -p ubuntu-ui-toolkit-autopilot ubuntuuitoolkit"
    " -p webbrowser-app-autopilot webbrowser_app"
    " -p address-book-app-autopilot address_book_app" 
    " sudoku_app"
    " ubuntu_calculator_app"
    " dropping_letters_app"
    " ubuntu_weather_app"
    " -p ubuntu-system-settings-autopilot ubuntu_system_settings"
    " music_app"
    " gallery_app"
    " -p messaging-app-autopilot messaging_app"
    " camera_app"
#   " filemanager"
#   " ubuntu_terminal_app"
    " -n unity8"
#   " ubuntu_clock_app"
#   " shorts_app"
#   " online_accounts_ui"
)


UITK_PACKAGES="qtdeclarative5-ubuntu-ui-toolkit-plugin \
               ubuntu-ui-toolkit-autopilot \
               ubuntu-ui-toolkit-theme"
#UITK_PACKAGES="unity8 unity8-common unity8-private"
#UITK_PACKAGES="qtmir-android qtdeclarative5-qtmir-plugin"


AP_PACKAGES="address-book-service-dummy \
             python3-lxml \
             ubuntu-ui-toolkit-autopilot \
             gallery-app-autopilot \
             reminders-app-autopilot \
             address-book-app-autopilot \
#             messaging-app-autopilot \
#             unity8-autopilot \
             dialer-app-autopilot \
             camera-app-autopilot \
             webbrowser-app-autopilot \
             mediaplayer-app-autopilot \
             unity-webapps-qml-autopilot \
             ubuntu-system-settings-autopilot\
             ubuntu-html5-ui-toolkit-autopilot \
             ubuntu-system-settings-online-accounts-autopilot \
             messaging-app-autopilot \
             unity8"

declare -a UNREGISTERED_APPS=(
	"com.ubuntu.terminal"
	"com.ubuntu.calculator"
	"com.ubuntu.shorts"
)

function echo(){
        if [[ $VERBOSE == true ]]; then
                builtin echo $@;
        fi
}

wait_for_shell()
{
	# Waiting for device on ADB
	set -e
	adb -s ${SERIALNUMBER} wait-for-device
	# Start waiting for Unity8"
	until PIDS=$(adb -s ${SERIALNUMBER} shell pidof unity8 2>/dev/null|egrep -v "^$"); 
	do
        	sleep 0.1;
	done;
	echo "Unity8 is up with PID: ${PIDS}"
	set +e
}

# some apps needs special permission to access system service.
fix_permissions () {
	APP_ID=$1
	SERVICE=$2
	NUMBER_OF_ROWS=`adb shell  "echo 'SELECT Count(*) FROM requests;' | sqlite3 /home/phablet/.local/share/${SERVICE}/trust.db"`
	NUMBER_OF_ROWS="${NUMBER_OF_ROWS%"${NUMBER_OF_ROWS##*[![:space:]]}"}"
	NUMBER_OF_ROWS=$(( $NUMBER_OF_ROWS + 1 ))
	EPOCH_DATE=`date +%s%N | cut -b1-19`
	APP_ID=$1
	adb shell "echo \"INSERT INTO requests VALUES(${NUMBER_OF_ROWS}, '${APP_ID}', 0, ${EPOCH_DATE}, 1);\"|sqlite3 /home/phablet/.local/share/${SERVICE}/trust.db"
}

fatal_failure () {
    echo -e "\e[31mFailed operation:\e[0m $1"
    exit
}

sleep_indicator () {
    if [ -z "$1" ]; then
	i=1
	while [  $(adb get-state  -s ${SERIALNUMBER}) == "unknown" ]
	do
		echo -ne "Wait for adb: $i seconds\r"
		sleep 1
		i=$[$i+1]
	done
	echo -ne '\n'
        until pids=$(adb -s ${SERIALNUMBER} shell pidof unity-system-compositor|egrep -v "^$|initctl:"); 
        do
          echo -ne "Wait for compositor: $i seconds\r"
          sleep 1;
	  i=$[$i+1]
        done;
        echo -ne '\n'
	return
    fi
    SLEEP=$1
    for (( LOOPVAR=1; LOOPVAR<= ${SLEEP}; LOOPVAR++ ))
    do
        echo -ne "Wait: $SLEEP/$LOOPVAR seconds\r"
        sleep 1
    done
    echo -ne '\n'
    return 0
}

function network {
        if phablet-network -s ${SERIALNUMBER} 2>&1|grep -q Error; then
                echo "Reset and try again."
                reset -f
                phablet-network -s ${SERIALNUMBER} 2>&1
        fi
}

function reset {
     if [ ${RESET} == true  -o  x"$1" == x-f ]; then
	if [ ${UNLOCK_ONLY} == false ]; then
             adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S reboot 2>&1|grep -v password"
             sleep_indicator 120
             wait_for_shell 
             sleep_indicator 10
             network
        fi
	if [ ${NORMAL_USE} == false ]; then
	        phablet-config -s ${SERIALNUMBER} autopilot --dbus-probe enable 2>&1 > /dev/null
	        adb -s ${SERIALNUMBER} shell powerd-cli display on |egrep -v "Display State requested, cookie is|Press ctrl-c to exit|not fully supported." &
	        adb -s ${SERIALNUMBER} shell powerd-cli active |egrep -v "requested, cookie is|Press ctrl-c to exit|not fully supported." &
	        #sleep_indicator 10
	        adb -s ${SERIALNUMBER} shell amixer -D pulse set Master 1+ mute 2>&1 > /dev/null
	        adb -s ${SERIALNUMBER} shell "gdbus call --session --dest com.canonical.UnityGreeter --object-path / --method com.canonical.UnityGreeter.HideGreeter|grep -v '\(\)'"
	        adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S dbus-send   --system --print-reply \
	                                                 --dest=org.freedesktop.Accounts \
        	                                         /org/freedesktop/Accounts/User32011 \
                	                                 org.freedesktop.DBus.Properties.Set \
                        	                         string:com.canonical.unity.AccountsService \
                                	                 string:demo-edges variant:boolean:false 2>&1|grep -v password|egrep -v '\(\)|method return'"
	fi
    fi
}

function device_comission {
    if [ ${COMISSION_BOOTSTRAP} == true ]; then
        # bootstrap the device with the latest image
	ubuntu-device-flash touch --serial=${SERIALNUMBER} --channel=${CHANNEL} --wipe --bootstrap --developer-mode --password=0000
    elif [ ${COMISSION_FLASH} == true ]; then
        adb -s ${SERIALNUMBER} wait-for-device
        # Avoid https://bugs.launchpad.net/gallery-app/+bug/1363190
        adb -s ${SERIALNUMBER} shell "echo ${PASSWORD} |sudo -S rm -rf /userdata/user-data/phablet/.cache/com.ubuntu.gallery 2>&1|grep -v password"
        # flash the latest image
        echo -e "Flashing \e[31m${CHANNEL}\e[0m"
        ubuntu-device-flash touch --serial=${SERIALNUMBER} --channel=${CHANNEL} ${WIPE} --developer-mode --password=${PASSWORD}
    fi
    if [[ ${COMISSION_BOOTSTRAP} == true || ${COMISSION_FLASH} == true ]]; then
	    sleep_indicator ${BOOTTIME}
	    reset -f
	    echo -e "Disable the intro wizard"
	    phablet-config -s ${SERIALNUMBER}  welcome-wizard --disable
	    echo -e "Disable the edge swiping lecture "
	    phablet-config -s ${SERIALNUMBER} edges-intro --disable
    fi
    echo -e "Clone the network "
    network
    sleep_indicator 10
    reset 
    if [ ${NORMAL_USE} == false ]; then
        # skip the click setup and app configuration if teh device is provisioned for normal use
        echo -e "Unregister few apps"
	# Unregister few apps as they break phablet-click-test-setup or something else
        for APP_TO_UNREGISTER in "${UNREGISTERED_APPS[@]}"
        do
	    echo -e "\e[31m${APP_TO_UNREGISTER}\e[0m"
            adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S click unregister ${APP_TO_UNREGISTER} 2>&1|grep -v password"
        done
	echo -e "Configure permissions for applications"
	fix_permissions com.ubuntu.camera_camera PulseAudio
	fix_permissions com.ubuntu.camera_camera CameraService
	fix_permissions com.ubuntu.clock_clock UbuntuLocationService
	echo -e "phablet-click-test-setup  \e[31m${DISTRO} ${SERIES}\e[0m"
	phablet-click-test-setup -s ${SERIALNUMBER} --distribution=${DISTRO} --series=${SERIES} 2>&1 || fatal_failure "phablet-click-test-setup has failed" 
	echo "Sleep after phablet-click-test-setup";
	sleep_indicator 120
    fi
    if [ ${PPA} == "archive"  ]; then
        echo "Set up with the archive image"
        adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S touch /userdata/.writable_image 2>&1|grep -v password > /dev/null"
        adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S touch /userdata/.adb_onlock 2>&1|grep -v password > /dev/null"
	reset -f
        echo "Sleep after turning the device writable";
        sleep_indicator 120
        network
    else
        if [[ "$PPA" =~ ^[0-9]{3}$ ]]; then
            echo -e "Set up with the silo \e[31m${PPA}\e[0m"
            adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S touch /userdata/.writable_image 2>&1|grep -v password > /dev/null"
            adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S touch /userdata/.adb_onlock 2>&1|grep -v password > /dev/null"
            reset -f
	    echo "Sleep after turning the device writable";
	    sleep_indicator 120
            network
            # TODO: hide the sudo output
            adb -s ${SERIALNUMBER} shell "test -e /usr/sbin/policy-rc.d && cp /usr/sbin/policy-rc.d /tmp/policy-rc.d"
            adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S  bash -c 'echo \"exit 101\" > /usr/sbin/policy-rc.d'  2>&1|grep -v password > /dev/null"
            adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S  bash -c 'chmod +x /usr/sbin/policy-rc.d'  2>&1|grep -v password > /dev/null"
            adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S  bash -c 'echo \"deb http://ppa.launchpad.net/ci-train-ppa-service/landing-${PPA}/${DISTRO} ${SERIES} main\" > /etc/apt/sources.list.d/silo-${PPA}.list'  2>&1|grep -v password > /dev/null "
            # pin up the silo
            adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S  bash -c 'echo -e \"Package: *\nPin: release o=LP-PPA-ci-train-ppa-service-landing-${PPA}\nPin-Priority: 1100\" > /etc/apt/preferences.d/silo.pref' 2>&1|grep -v password > /dev/null "
            # Resynchronize the package index files from their sources.
            adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S apt-get update 2>&1|grep -v password > /dev/null"
        else
            echo  -e "Set up with the PPA \e[31m${PPA}\e[0m"
            adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S touch /userdata/.writable_image 2>&1|grep -v password > /dev/null"
            adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S touch /userdata/.adb_onlock 2>&1|grep -v password > /dev/null"
	    reset -f
	    echo "Sleep after turning the device writable";
	    sleep_indicator 120
            network
            adb -s ${SERIALNUMBER} shell "test -e /usr/sbin/policy-rc.d && cp /usr/sbin/policy-rc.d /tmp/policy-rc.d"
            adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S  bash -c 'echo \"exit 101\" > /usr/sbin/policy-rc.d'  2>&1|grep -v password > /dev/null"
            adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S  bash -c 'chmod +x /usr/sbin/policy-rc.d'  2>&1|grep -v password > /dev/null"
            adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S  bash -c 'echo \"deb http://ppa.launchpad.net/${PPA}/${DISTRO} ${SERIES} main\" > /etc/apt/sources.list.d/ppa.list' 2>&1|grep -v password > /dev/null"
           #pin up the PPA
           PIN_UP_PPA=${PPA/\//-}
           adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S  bash -c 'echo -e \"Package: *\nPin: release o=LP-PPA-${PIN_UP_PPA}\nPin-Priority: 1100\" > /etc/apt/preferences.d/ppa.pref' 2>&1|grep -v password > /dev/null "
           adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S apt-get update 2>&1|grep -v password > /dev/null"
        fi
    fi
    adb -s ${SERIALNUMBER} shell rm -rf /home/phablet/autopilot/ubuntuuitoolkit
    UITK_VERSION=`adb -s ${SERIALNUMBER} shell "stty cols 250; dpkg -l"|grep qtdeclarative5-ubuntu-ui-toolkit-plugin|awk '{print $3}'`
    echo -e "Original UITK version:\t\e[31m${UITK_VERSION}\e[0m"
    echo "Updating APT";
    adb -s ${SERIALNUMBER} shell "echo ${PASSWORD} |sudo -S apt-get update  2>&1|grep -v password > /dev/null"
    if [ ${DISTUPGRADE} == true ]; then
        echo "Install all the upgraded packages with dist-upgrade"
        adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S apt-get dist-upgrade --yes --force-yes 2>&1 |grep -v password > /dev/null"
        adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S dpkg --configure -a 2>&1 |grep -v password > /dev/null"
    else
        echo "Install the UITK packages"
        adb -s ${SERIALNUMBER} shell "echo ${PASSWORD}|sudo -S apt-get install --yes --force-yes ${UITK_PACKAGES} 2>&1 |grep -v password > /dev/null"
    fi
    UITK_VERSION=`adb -s ${SERIALNUMBER} shell "stty cols 250; dpkg -l"|grep qtdeclarative5-ubuntu-ui-toolkit-plugin|awk '{print $3}'`
    echo -e "New UITK version:\t\e[31m${UITK_VERSION}\e[0m"
    # Update APT
    adb -s ${SERIALNUMBER} shell "echo ${PASSWORD} |sudo -S apt-get update 2>&1|grep -v password > /dev/null"
    # Install the autopilot tests for the apps
    adb -s ${SERIALNUMBER} shell "echo ${PASSWORD} |sudo -S apt-get install --yes --force-yes ${AP_PACKAGES} 2>&1 | grep -v password > /dev/null"
    echo "Cleaning QML cache for the next boot"
    adb -s ${SERIALNUMBER} shell "rm -rf ~/.cache/QML/Apps/"
}

function compare_results {
    sed -i 's/\r//g' *tests
    rm ${MAINFILE} 2> /dev/null
    for RESULT_FILE in *${PPA}.tests;
    do
        echo "$RESULT_FILE" >> ${MAINFILE};
        LAST_LINE=`tail -1 $RESULT_FILE`
        if [[ "$LAST_LINE" =~ OK || "$LAST_LINE" =~ FAILED ]]; then
	    echo -e  "\t"`tail -2 $RESULT_FILE` >> ${MAINFILE}
	else
	    echo -e "\tBroken tests"  >> ${MAINFILE}
        fi
        egrep -v "NO TAGS DATABASE" $RESULT_FILE |egrep "^ERROR:|^FAIL:" | while read -r FAILED ; 
        do
	    FAILED=${FAILED/ERROR:/FAIL:}
            echo -e "\tFailed with ${PPA} - $FAILED"  >> ${MAINFILE}
            FAILED_TEST_CASE=${FAILED/FAIL: /}
            #echo ${FAILED_TEST_CASE}
	    if grep --quiet "$FAILED_TEST_CASE" *archive.tests; then
                echo -e "\tSame on archive"  >> ${MAINFILE}
            else
                echo -e "\tPossible regression"  >> ${MAINFILE}
            fi
        done
    done
}

while getopts ":hrintduc:slqwbvi:o:p:e:f:a:" opt; do
    case $opt in
        c)
		COMISSION=true
		case $OPTARG in
			"flash")
				COMISSION_FLASH=true
				;;
			"bootstrap")
				COMISSION_BOOTSTRAP=true
				;;
			"silo")
				COMISSION_SILO=true
				;;
			*)
				echo "Invalid parameter for for -c [silo|flash|bootstrap]"
        	esac
        	;;
        r)
            RESET=true
            ;;
        s)
            SERIALNUMBER=$OPTARG
            LAZY=false
            ;;
        e)
	    PASSWORD=$OPTARG
	    ;;
        o)
            if [ -d "$OPTARG" ]; then
                OUTPUTDIR=$OPTARG
            else
                echo "$OPTARG does not exist. Using default $OUTPUTDIR"
            fi
            ;;
        p)
            PPA=$OPTARG
            ;;
	a)
	    START_FROM=$OPTARG
            ;;
        f)
            FILTER=$OPTARG
            ;;
        i)
            REVISION=$OPTARG
            ;;
        d)
            ONLYCOMPARE=true
            ;;
        n)
            DONOTRUNTESTS=true
            ;;
        u)
            RTM=false
            CHANNEL="ubuntu-touch/devel-proposed/ubuntu"
            DISTRO="ubuntu"
            SERIES="xenial"
            ;;
        w)
            DISTUPGRADE=true
            ;;
        l)
           UNLOCK_ONLY=true
	   VERBOSE=false
           ;;
	q)
	   NORMAL_USE=true
	   DONOTRUNTESTS=true
           COMISSION=true
           WIPE=""
	   ;;
        v)
	   VERBOSE=true
	   ;;
        h)
            echo "Usage: uitk_test_plan.sh -s [serial number] -m -c [mode]"
            echo -e "\t-v : Enable verbose output. Default: ${VERBOSE}"
            echo -e "\t-r : Reset after each tests. Default: ${RESET}"
            echo -e "\t-s : Use the device with the given serial number. Default: ${SERIALNUMBER}"
            echo -e "\t-c : Comission the device with the ${PPA} enabled in [silo|flash|bootstrap]"
            echo -e "\t-d : Only compare the -p ${PPA} results with the archive test results. Default: ${ONLYCOMPARE}"
            echo -e "\t-n : Do not run the test set. Default ${DONOTRUNTESTS}"
            echo -e "\t-o : Output directory. Default $OUTPUTDIR"
            echo -e "\t-p : Source PPA for the UITK. Default $PPA. Use -p archive to test stock image or -p [0-9]* to set a silo."
            echo -e "\t-f : Filter for the test suite. Default $FILTER"
            echo -e "\t-a : Start the test suite from the given test."
            echo -e "\t-u : Provision the Development release of Ubuntu, Xenial. Default is vivid-overlay (formerly RTM)."
            echo -e "\t-w : dist-upgrade to the whole PPA instead of just Ubuntu UI Toolkit. Default is only UITK."
            echo -e "\t-q : Provision the device for normal use with the ${PPA} enabled"
            echo ""
            echo "By default tihe uitk_test_plan.sh flashes the latest vivid-overlay image on the device, installs the click application"
            echo "tests, configures the ppa:ubuntu-sdk-team/staging PPA, installs the UITK from the PPA and executes the test plan."
            echo ""
	    echo "Provision the device for normal use without wipeing the userdata"
	    echo -e "\t$ ./uitk_test_plan.sh -q"
            echo ""
            echo "Validate the staging branch of the UITK against the vivid-overlay image"
            echo -e "\t$ ./uitk_test_plan.sh -c"
            echo ""
            echo "Validate the UITK from a CI silo on an vivid-overlay image"
            echo -e "\t$ ./uitk_test_plan.sh -c -p 001"
            echo ""
            echo "Validate the UITK from teh archive on an vivid-overlay image"
            echo -e "\t$ ./uitk_test_plan.sh -c -p archive"
            echo ""
            echo "Validate the UITK from a specific CI silo on an Ubuntu Xenial image"
            echo -e "\t$ ./uitk_test_plan.sh -c -p 001 -u"
            echo ""
            echo "Provision the device for manual testing with the latest vivid-overlay image"
            echo -e "\t$ ./uitk_test_plan.sh -c -p archive -n"
            echo ""
            echo "Provision the device for manual testing with the latest Ubuntu Xenial image"
            echo -e "\t$ ./uitk_test_plan.sh -c -p archive -u -n"
            echo ""
            echo "Run the test plan on an already provisioned device"
            echo -e "\t$ ./uitk_test_plan.sh"
            echo ""
            echo "Run only the UITK tests on an already provisioned device"
            echo -e "\t$ ./uitk_test_plan.sh -f ubuntuuitoolkit"
            echo ""
            echo "Compare the archive test results with the silo 001 results"
            echo -e "\t$ ./uitk_test_plan.sh -d -p 001"
	    exit
            ;;
        :)
            echo "Option -$OPTARG requires an argument." >&2
            exit
    esac
done

MAINFILE=MAIN-ap-${DATESTAMP}-${SERIES}-${PPA}
MAINFILE=${MAINFILE/\//_}

if [ ${ONLYCOMPARE} == true ]; then
   echo "Comparing results with the archive tests" > $MAINFILE
   PPA=${PPA/\//_}
   compare_results
   rm ${MAINFILE}-regressing_cases 2> /dev/null
   egrep "regression" -B1 ${MAINFILE} |grep Failed|sort|uniq -c|grep " 2"|sed 's/2//g' >${MAINFILE}-regressing_cases
   rm ${MAINFILE}-regression_backtraces 2> /dev/null
   for i in $(cat ${MAINFILE}|egrep "regression" -B1   |grep Failed|sort|uniq -c|grep " 2"|awk '{print $7}');    
   do       
      sed -n "/$i/,/=====/p" *.tests|sed -e '/Traceback/,/=====/p'|uniq -c|egrep "FAIL\:|ERROR\:|    2"|perl -p -e "s/^\s+\d //g;s/^ERROR:/\nERROR:/;s/^FAIL:/\nFAIL:/" >> ${MAINFILE}-regression_backtraces
   done
   exit
fi

if [ ${COMISSION_BOOTSTRAP} == false ]; then
    # Use the first available device for testing
    if [ ${LAZY} == true ]; then
        echo "Waiting for a device"
        adb wait-for-device
        SERIALNUMBER=`adb devices -l | grep -Ev "List of devices attached" | grep -Ev "emulator-" | sed "/^$/d"|sed "s/ .*//g"`
    else
        echo "Waiting for the device with ${SERIALNUMBER} serial number."
        adb -s ${SERIALNUMBER} wait-for-device
    fi
fi

echo "=== Settings ==="
echo ""
echo "Serial number: ${SERIALNUMBER}"
echo "Output directory: ${OUTPUTDIR}"
echo "PPA: $PPA"
echo "Filter: ${FILTER}"
echo "Start with: ${START_FROM}"
echo ""
echo "Unlock only: ${UNLOCK_ONLY}"
echo "Reset: ${RESET}"
echo "Commission: ${COMISSION}"
echo "Only compare: ${ONLYCOMPARE}"
echo "Do not run tests: ${DONOTRUNTESTS}"
echo "Provision for normal use: ${NORMAL_USE}"
echo "RTM: ${RTM}"
echo "Dist-upgrade: ${DISTUPGRADE}"
echo "Main logs: ${MAINFILE}"
echo "=== Starting ==="
echo ""

if [ ${UNLOCK_ONLY} == true ]; then
	adb -s ${SERIALNUMBER} shell powerd-cli display on |egrep -v "Display State requested, cookie is|Press ctrl-c to exit|not fully supported." &
	adb -s ${SERIALNUMBER} shell powerd-cli active |egrep -v "requested, cookie is|Press ctrl-c to exit|not fully supported." &
	adb -s ${SERIALNUMBER} shell "gdbus call --session --dest com.canonical.UnityGreeter --object-path / --method com.canonical.UnityGreeter.HideGreeter|grep -v '\(\)'"
	exit
fi
# Check if the device need to be flashed and set up for testing
if [ ${COMISSION} == true ]; then
    device_comission
    if [[ -f ${MAINFILE} ]]; then
	    egrep "regression" -B1 ${MAINFILE} |grep Failed|sort|uniq -c|grep " 2"
    fi
fi

# Fix the PPA string as it is used in log file names
PPA=${PPA/\//_}

# Check if the job is only comissioning the device
if [ ${DONOTRUNTESTS} != true ]; then
    # Reset the device for testing
    reset -f
	
    # Run the test suite
    for TEST_SET in "${TEST_SUITE[@]}"
    do
	if [[ ! -z $START_FROM ]]; then
            if [[ ! ${TEST_SET} =~ ${START_FROM} ]]; then
        	continue
	     else
		unset START_FROM
	     fi
	fi
        if [[ ${TEST_SET} =~ ${FILTER} ]]; then
            APPNAME=${TEST_SET##* }
            LOGFILE="$OUTPUTDIR/${LOGFILENAME}-${APPNAME}-1-${PPA}.tests"
            COMMAND="phablet-test-run -r ${PASSWORD} -s ${SERIALNUMBER} $TEST_SET >> ${LOGFILE}"
            echo "<<<=== ${APPNAME} 1 ===>>>" >> ${LOGFILE}
            reset
            eval ${COMMAND}
             egrep "<<<===|Ran|OK|FAILED" ${LOGFILE}
            # check if the tests were successful and re-run after a reset
            if (tail -1 ${LOGFILE}|grep -q OK) ;then 
		echo -e "OK - ${LOGFILE}";
          else 
                echo -e "\e[31mFailed\e[0m - ${LOGFILE}";
                reset -f
                LOGFILE="$OUTPUTDIR/${LOGFILENAME}-${APPNAME}-2-${PPA}.tests"
                COMMAND="phablet-test-run -r ${PASSWORD} -s ${SERIALNUMBER} $TEST_SET >> ${LOGFILE}"
                echo "<<<=== ${APPNAME} 2 ===>>>" >> ${LOGFILE}
                eval ${COMMAND}
                egrep "<<<===|Ran|OK|FAILED" ${LOGFILE}
                if (tail -1 ${LOGFILE}|grep -q OK) ;then
                    echo -e "OK - ${LOGFILE}";
                else 
                    echo -e "\e[31mFailed\e[0m - ${LOGFILE}";
                fi
            fi
        fi
    done
fi

if [[ ${NORMAL_USE} != true  && ${PPA} != "archive" && ${DONOTRUNTESTS} != true ]]; then
   echo "Comparing results with the archive tests"  > $MAINFILE
   compare_results
fi