File: labpretest.sh

package info (click to toggle)
android-platform-development 8.1.0%2Br23-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 37,780 kB
  • sloc: ansic: 166,133; xml: 75,737; java: 19,329; python: 11,511; cpp: 8,221; sh: 2,075; lisp: 261; ruby: 183; asm: 132; perl: 129; makefile: 18
file content (582 lines) | stat: -rwxr-xr-x 15,075 bytes parent folder | download | duplicates (10)
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
#!/bin/bash
#
# Copyright 2010 Google Inc. All Rights Reserved.
# Author: bgay@google.com (Bruce Gay)
#
# The labpretest.sh script is designed to emulate a typical automated test lab
# session.  It puts a device into bootloader mode, reboots into bootloader mode,
# determines device type, erases user cache, flashes a generic userdata image,
# updates the bootloader image, updates the radio image, updates the system
# image and reboots, sets up for a monkey run and finally runs a random monkey
# test. It will repeat this based on an optional parameter(-i) or default to 100
# times. It will detect if it is in a low battery situation and wait for it to
# charge again.


COUNT=100
ROOT=$(cd `dirname $0` && pwd)
ADB="$ROOT/tools/adb"
FASTBOOT="$ROOT/tools/fastboot"
MEVENTS=200
NOMONKEY=0

buildfile=''
device=''
product=''
bootpart=''
bootfile=''

while getopts "d:i::m:xh" optionName; do
  case "$optionName" in
    d) device="$OPTARG";;
    i) COUNT=$OPTARG;;
    m) MEVENTS=$OPTARG;;
    x) NOMONKEY=1;;
    h) echo "options: [-d <device ID>, -i <loop count>, -m <monkey events> -x (skips monkey)]"; exit;;
    *) echo "invalid parameter -$optionName"; exit -1;;
  esac
done

declare -r COUNT
declare -r MEVENTS
declare -r NOMONKEY


################################################
# Prints output to console with time stamp
# Arguments:
#   None
# Returns:
#   None
################################################
log_print()
{
  if [ -z "$1" ]; then
    echo "# $(date +'%D %T')"
  else
    echo "# $(date +'%D %T'): $1"
  fi
}

################################################
# Blocks until battery level is at least
# above TARGET if below LIMIT
# Globals:
#   ADB
#   device
# Arguments:
#   None
# Returns:
#   None
################################################
wait_for_battery()
{
  TARGET=80
  LIMIT=20
  local battery
  local tick
  log_print "checking battery level"
  while [ "$battery" = "" ]; do
    battery=`$ADB -s $device shell dumpsys battery | tr -d '\r' | awk '/level:/ {print $2}'`
    sleep 2
  done
  if [ $battery -lt $LIMIT ]; then
    log_print "Battery is low, waiting for charge"
    while true; do
      battery=`$ADB -s $device shell dumpsys battery | tr -d '\r' | awk '/level:/ {print $2}'`
      if (( $battery >= $TARGET )); then break; fi
      tick=$[$TARGET - $battery]
      echo "battery charge level is $battery, sleeping for $tick seconds"
      sleep $[$TARGET - $battery * 10]
    done
    log_print "resuming test run with battery level at $battery%"
  else
    log_print "resuming test run with battery level at $battery%"
  fi
}

################################################
# Blocks until device is in fastboot mode or
# time out is reached
# Globals:
#   loop
#   device
# Arguments:
#   None
# Returns:
#   None
################################################
fastboot_wait_for_device()
{
  local fdevice=""
  local n=0
  while [ "$device" != "$fdevice" -a $n -le 30 ]; do
    sleep 6
    fdevice=`$FASTBOOT devices | sed -n "s/\($device\).*/\1/ p"`
    let n+=1
  done
  if [ $n -gt 30 ]; then
    log_print "device time out after $loop iterations"
    exit
  else
    log_print "device returned and available"
  fi
}

################################################
# reboots device into fastboot mode or
# time out is reached
# Globals:
#   device
#   ADB
# Arguments:
#   None
# Returns:
#   None
################################################
reboot_into_fastboot_from_adb()
{
  log_print "rebooting into bootloader and waiting for availability via fastboot"
  $ADB -s $device reboot bootloader
  fastboot_wait_for_device
}

################################################
# reboots device into fastboot mode or
# times out
# Globals:
#   device
#   FASTBOOT
# Arguments:
#   None
# Returns:
#   None
################################################
reboot_into_fastboot_from_fastboot()
{
  log_print "rebooting into bootloader and waiting for availability via fastboot"
  $FASTBOOT -s $device reboot-bootloader
  fastboot_wait_for_device
}

################################################
# reboots device from fastboot to adb or
# times out
# Globals:
#   device
#   FASTBOOT
#   ADB
# Arguments:
#   None
# Returns:
#   None
################################################
reboot_into_adb_from_fastboot()
{
  log_print "rebooting and waiting for availability via adb"
  $FASTBOOT -s $device reboot
  $ADB -s $device wait-for-device
}

################################################
# reboots device from fastboot to adb or
# times out
# Globals:
#   device
#   ADB
# Arguments:
#   None
# Returns:
#   None
################################################
wait_for_boot_complete()
{
  log_print "waiting for device to finish booting"
  local result=$($ADB -s $device shell getprop dev.bootcomplete)
  local result_test=${result:1:1}
  echo -n "."
  while [ -z $result_test ]; do
    sleep 1
    echo -n "."
    result=$($ADB -s $device shell getprop dev.bootcomplete)
    result_test=${result:0:1}
  done
  log_print "finished booting"
}

################################################
# fastboot flashes partition
#
# Globals:
#   device
#   FASTBOOT
# Arguments:
#   command_name
#   command_parameters
# Returns:
#   None
################################################
fastboot_command()
{
  $FASTBOOT -s $device $1 $2 $3
  sleep 5
}

################################################
# fastboot command wrapper
#
# Globals:
#   device
#   FASTBOOT
# Arguments:
#   partition_name
#   file_name
# Returns:
#   None
################################################
flash_partition()
{
  $FASTBOOT -s $device flash $1 $2
  sleep 5
}

################################################
# adb command wrapper
#
# Globals:
#   device
#   ADB
# Arguments:
#   command_name
#   command_parameters
# Returns:
#   None
################################################
adb_command()
{
  $ADB -s $device $1 $2 $3 $4 $5
  sleep 5
}

################################################
# sets the name of the boot partition and
# bootfile, then flashes device
#
# Globals:
#   product
#   ROOT
#   bootloaderfile
#   bootpart
#   device
# Arguments:
#   None
# Returns:
#   None
################################################
flash_bootloader_image()
{
  if [ "$bootpart" == '' ]; then
    log_print "bootpart not defined"
    exit
  fi
  if [ "$bootloaderfile" == '' ]; then
    log_print "getting bootloader file for $product"
    bootloaderfile=`ls -1 $ROOT/$product | sed -n 's/\(.*boot[0-9._]\+img\)/\1/ p'`
    if [ "$bootloaderfile" == '' ]; then
      log_print "bootloader file empty: $bootloaderfile"
      exit
    fi
    if [ ! -e "$ROOT/$product/$bootloaderfile" ]; then
      log_print "bootloader file not found: ./$product/$bootloaderfile"
      exit
    fi
    log_print "using $ROOT/$product/$bootloaderfile as the bootloader image file"
  fi
  log_print "downloading bootloader image to $device"
  flash_partition $bootpart $ROOT/$product/$bootloaderfile
  reboot_into_fastboot_from_fastboot
}

################################################
# sets the name of the radio partition and
# radiofile and flashes device
#
# Globals:
#   product
#   ROOT
#   radiofile
#   radiopart
#   device
# Arguments:
#   None
# Returns:
#   None
################################################
flash_radio_image()
{
  if [ "$radiopart" == '' ]; then
    log_print "setting radio partion to 'radio'"
    radiopart='radio'
  fi
  if [ "$radiofile" == "" ]; then
    log_print "getting radio file for $product"
    radiofile=`ls -1 $ROOT/$product | sed -n 's/\(radio[0-9._A-Za-z]\+img\)/\1/ p'`
    if [ "$radiofile" == "" ]; then
      log_print "radio file empty: $radiofile"
      exit
    fi
    if [ ! -e "$ROOT/$product/$radiofile" ]; then
      log_print "radio file not found: ./$product/$radiofile"
      exit
    fi
    log_print "using $ROOT/$product/$radiofile as the radio image file"
  fi
  log_print "downloading radio image to $device"
  flash_partition $radiopart  $ROOT/$product/$radiofile
  reboot_into_fastboot_from_fastboot
}

################################################
# sets the name of the boot partition and
# bootfile
#
# Globals:
#   product
#   ROOT
#   buildfile
#   device
# Arguments:
#   None
# Returns:
#   None
################################################
flash_system_image()
{
  if [ "$buildfile" == "" ]; then
    log_print "getting build file for $product"
    buildfile=`\ls -1 $ROOT/$product 2>&1 | sed -n 's/\([a-z]\+-img-[0-9]\+.zip\)/\1/ p'`
    if [ "$buildfile" == "" ]; then
      log_print "build file empty: $buildfile"
      exit
    fi
    if [ ! -e "$ROOT/$product/$buildfile" ]; then
      log_print "build file not found: ./$product/$buildfile"
      exit
    fi
    log_print "using $ROOT/$product/$buildfile as the system image file"
  fi
  log_print "downloading system image to $device"
  fastboot_command update $ROOT/$product/$buildfile

}
################################################
# flashes the userdata partition
#
# Globals:
#   product
#   ROOT
# Arguments:
#   None
# Returns:
#   None
################################################
flash_userdata_image()
{
  log_print "flashing userdata..."
  if [ -e $ROOT/$product/userdata.img ];then
    flash_partition userdata $ROOT/$product/userdata.img
  else
    log_print "userdata.img file not found: $ROOT/$product/userdata.img"
    exit
  fi
}


################################################
# flashes the device
#
# Globals:
#   product
#   ROOT
#   FASTBOOT
#   bootfile
#   bootpart
#   radiofile
# Arguments:
#   None
# Returns:
#   None
################################################
flash_device()
{
  log_print "erasing cache..."
  fastboot_command erase cache
  flash_userdata_image
  flash_bootloader_image
  flash_radio_image
  flash_system_image
  #device has been rebooted
  adb_command wait-for-device
}

################################################
# gets the device product type and sets product
#
# Globals:
#   product
#   ROOT
#   FASTBOOT
#   device
# Arguments:
#   None
# Returns:
#   None
################################################
set_product_type()
{
  if [ "$product" == "" ]; then
    log_print "getting device product type"
    product=`$FASTBOOT -s $device getvar product 2>&1 | sed -n 's/product: \([a-z]*\)\n*/\1/ p'`
    if [ ! -e "$ROOT/$product" ]; then
      log_print "device product id not supported: $product"
      exit
    fi
  fi
  log_print "using $product as device product id"
}



#start of script
#test for dependencies
if [ ! -e $ADB ]; then
  echo "Error: adb not in path! Please correct this."
  exit
fi
if [ ! -e $FASTBOOT ]; then
  echo "Error: fastboot not in path! Please correct this."
  exit
fi
#checks to see if the called device is available
if [ "$device" != "" ]; then
  tmpdevice=`$ADB devices | sed -n "s/\($device\).*/\1/ p"`
  if [ "$device" != "$tmpdevice" ]; then
      tmpdevice=`$FASTBOOT devices | sed -n "s/\($device\).*/\1/ p"`
    if [ "$device" != "$tmpdevice" ]; then
      echo "Warning: device not found... $device"
      exit
    else
      echo "'Device '$device' found!'"
      reboot_into_adb_from_fastboot
      wait_for_boot_complete
    fi
  fi
else
  device=`$ADB devices | sed -n 's/.*\(^[0-9A-Z]\{2\}[0-9A-Z]*\).*/\1/ p'`
  if [ `echo $device | wc -w` -ne 1 ]; then
    echo 'There is more than one device found,'
    echo 'please pass the correct device ID in as a parameter.'
    exit
  fi
fi
if [ "$device" == "" ]; then
  echo 'Device not found via adb'
  device=`$FASTBOOT devices | sed -n 's/.*\(^[0-9A-Z]\{2\}[0-9A-Z]*\).*/\1/ p'`
  if [ `echo $device | wc -w` -ne 1 ]; then
    echo "There is more than one device available,"
    echo "please pass the correct device ID in as a parameter."
    exit
  fi
  if [ "$device" == "" ]; then
    echo 'Device not found via fastboot, please investigate'
    exit
  else
    echo 'Device '$device' found!'
    reboot_into_adb_from_fastboot
    wait_for_boot_complete
    echo 'Hammering on '$device
  fi
else
  echo 'Hammering on '$device
fi
reboot_into_fastboot_from_adb
set_product_type
reboot_into_adb_from_fastboot
wait_for_boot_complete

#check for availability of a custom flash info file and retreive it
if [ -e "$ROOT/$product/custom_flash.sh" ]; then
  . $ROOT/$product/custom_flash.sh
fi
echo $'\n\n'

#start of looping
for ((loop=1 ; loop <= $COUNT ; loop++ )) ; do
  echo ""
  echo ""
  echo ________________ $(date +'%D %T') - $loop - $device ______________________

  log_print "setting adb root and sleeping for 7 seconds"
  adb_command root
  wait_for_battery
  log_print "rebooting into bootloader and waiting for availability via fastboot"
  reboot_into_fastboot_from_adb
  # not necessary, but useful in testing
  log_print "using fastboot to reboot to bootloader for test purposes"
  reboot_into_fastboot_from_fastboot

  #flashing the device
  flash_device

  #preping device for monkey run
  log_print "setting adb root"
  adb_command root
  log_print "setting ro.test_harness property"
  adb_command shell setprop ro.test_harness 1

  log_print "waiting for device to finish booting"
  result=$($ADB -s $device shell getprop dev.bootcomplete)
  result_test=${result:1:1}
  echo -n "."
  while [ -z $result_test ]; do
    sleep 1
    echo -n "."
    result=$($ADB -s $device shell getprop dev.bootcomplete)
    result_test=${result:0:1}
  done

  log_print "finished booting"
  log_print "waiting for the Package Manager"
  result=$($ADB -s $device shell pm path android)
  result_test=${result:0:7}
  echo -n "."
  while [ $result_test != "package" ]; do
    sleep 1
    echo -n "."
    result=$($ADB -s $device shell pm path android)
    result_test=${result:0:7}
  done
  echo "Package Manager available"

  #lets you see what's going on
  log_print "setting shell svc power stayon true"
  adb_command shell svc power stayon true

  #calls the monkey run if not skipped
  if [ $NOMONKEY == 0 ]; then
    seed=$(($(date +%s) % 99))
    log_print "running short monkey run..."
    $ADB -s $device shell monkey -p com.android.alarmclock -p com.android.browser -p com.android.calculator2 -p com.android.calendar -p com.android.camera -p com.android.contacts -p com.google.android.gm -p com.android.im -p com.android.launcher -p com.google.android.apps.maps -p com.android.mms -p com.android.music -p com.android.phone -p com.android.settings -p com.google.android.street -p com.android.vending -p com.google.android.youtube -p com.android.email -p com.google.android.voicesearch  -c android.intent.category.LAUNCHER  --ignore-security-exceptions  -s $seed $MEVENTS
    log_print "finished running monkey, rinse, repeat..."
  else
    log_print "-x parameter used, skipping the monkey run"
  fi

  if [ $loop -eq $COUNT ]; then
    log_print "device $device has returned, testing completed, count = $loop"
    echo `echo "Device $device has returned, testing completed, count = $loop." > $ROOT/$device.log`
  else
    log_print "device $device has returned, rinse and repeat count = $loop"
    echo `echo "Device $device has returned, rinse and repeat count = $loop." > $ROOT/$device.log`
  fi
done