~linaro-validation/lava-android-test/trunk

« back to all changes in this revision

Viewing changes to lava_android_test/commands.py

  • Committer: Yongqin Liu
  • Date: 2012-03-13 02:08:24 UTC
  • mfrom: (140.2.4 lava-android-test)
  • Revision ID: yongqin.liu@linaro.org-20120313020824-t3ni2llkwhyq53be
merge with the branch that support custom android command 

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
from lava_android_test.adb import ADB
28
28
from lava_android_test.config import get_config
29
 
from lava_android_test.testdef import testloader
 
29
from lava_android_test.testdef import testloader, AndroidTest
 
30
from lava_android_test.testdef import AndroidTestRunner, \
 
31
                                      AndroidTestInstaller, \
 
32
                                      AndroidTestParser
30
33
 
31
34
 
32
35
class Command(LAVACommand):
277
280
    @classmethod
278
281
    def register_arguments(cls, parser):
279
282
        super(run, cls).register_arguments(parser)
 
283
        parser.add_argument('-O', '--run-option',
 
284
                    help=("Specified in the job file for using in "
 
285
                          "the real test action, so that we can customize"
 
286
                          " some test when need"))
280
287
        group = parser.add_argument_group("specify the bundle output file")
281
288
        group.add_argument("-o", "--output",
282
289
                            default=None,
299
306
                    % self.args.test_id)
300
307
 
301
308
        try:
 
309
            result_id = test.run(quiet=self.args.quiet,
 
310
                                  run_options=self.args.run_option)
 
311
            if self.args.output:
 
312
                output_dir = os.path.dirname(self.args.output)
 
313
                if not os.path.exists(output_dir):
 
314
                    os.makedirs(output_dir)
 
315
                bundle = generate_bundle(self.args.serial, result_id)
 
316
                with open(self.args.output, "wt") as stream:
 
317
                    DocumentIO.dump(stream, bundle)
 
318
 
 
319
        except Exception as strerror:
 
320
            raise LavaCommandError("Test execution error: %s" % strerror)
 
321
 
 
322
        self.say_end(tip_msg)
 
323
 
 
324
 
 
325
class run_custom(AndroidCommand):
 
326
    """
 
327
    Run the command(s) that specified by the -c option in the command line
 
328
    program:: lava-android-test run-custom -c 'command1' -c 'command2'
 
329
     -p 'parse-regex1'
 
330
    program:: lava-android-test run test-id -s device_serial
 
331
    program:: lava-android-test run test-id -s device_serial -o outputfile
 
332
    """
 
333
 
 
334
    @classmethod
 
335
    def register_arguments(cls, parser):
 
336
        super(run_custom, cls).register_arguments(parser)
 
337
        parser.add_argument('-c', '--android-command', action='append',
 
338
                            help=("Specified in the job file for using"
 
339
                                  " in the real test action, so that "
 
340
                                  "we can customize some test when need"))
 
341
        parser.add_argument('-p', '--parse-regex',
 
342
                            help=("Specified the regular expression used"
 
343
                                  " for analyzing command output"))
 
344
        group = parser.add_argument_group("specify the bundle output file")
 
345
        group.add_argument("-o", "--output",
 
346
                            default=None,
 
347
                            metavar="FILE",
 
348
                           help=("After running the test parse the result"
 
349
                                 " artefacts, fuse them with the initial"
 
350
                                 " bundle and finally save the complete bundle"
 
351
                                 " to the  specified FILE."))
 
352
 
 
353
    def invoke(self):
 
354
        self.adb = ADB(self.args.serial)
 
355
        test_name = 'custom'
 
356
        ADB_SHELL_STEPS = []
 
357
        if self.args.android_command:
 
358
            ADB_SHELL_STEPS = self.args.android_command
 
359
        PATTERN = None
 
360
        if self.args.parse_regex:
 
361
            PATTERN = self.args.parse_regex
 
362
 
 
363
        tip_msg = ''
 
364
        if self.args.serial:
 
365
            tip_msg = "Run following custom test(s) on device(%s):\n\t\t%s" % (
 
366
                           self.args.serial, '\n\t\t'.join(ADB_SHELL_STEPS))
 
367
        else:
 
368
            tip_msg = "Run following custom test(s):\n\t\t%s" % (
 
369
                                             '\n\t\t'.join(ADB_SHELL_STEPS))
 
370
        self.say_begin(tip_msg)
 
371
 
 
372
        inst = AndroidTestInstaller()
 
373
        run = AndroidTestRunner(adbshell_steps=ADB_SHELL_STEPS)
 
374
        parser = AndroidTestParser(pattern=PATTERN)
 
375
        test = AndroidTest(testname=test_name, installer=inst,
 
376
                                runner=run, parser=parser)
 
377
        test.parser.results = {'test_results': []}
 
378
        test.setadb(self.adb)
 
379
 
 
380
        if not self.test_installed(test.testname):
 
381
            test.install()
 
382
 
 
383
        try:
302
384
            result_id = test.run(quiet=self.args.quiet)
303
385
            if self.args.output:
304
386
                output_dir = os.path.dirname(self.args.output)
305
387
                if not os.path.exists(output_dir):
306
388
                    os.makedirs(output_dir)
307
 
                bundle = generate_bundle(self.args.serial, result_id)
 
389
                bundle = generate_bundle(self.args.serial,
 
390
                                          result_id, test=test)
308
391
                with open(self.args.output, "wt") as stream:
309
392
                    DocumentIO.dump(stream, bundle)
310
393
 
311
394
        except Exception as strerror:
312
395
            raise LavaCommandError("Test execution error: %s" % strerror)
313
 
 
314
396
        self.say_end(tip_msg)
315
397
 
316
398
 
328
410
            pass
329
411
 
330
412
 
331
 
def generate_combined_bundle(serial=None, result_ids=None):
 
413
class parse_custom(AndroidResultsCommand):
 
414
    """
 
415
    Parse the results of previous test that run with run-custom command
 
416
    on the specified device
 
417
    program:: lava-android-test parse-custom test-result-id -P
 
418
    """
 
419
    @classmethod
 
420
    def register_arguments(cls, parser):
 
421
        super(parse_custom, cls).register_arguments(parser)
 
422
        parser.add_argument('-p', '--parse-regex',
 
423
                            help=("Specified the regular expression used"
 
424
                                  " for analyzing command output"))
 
425
 
 
426
    def invoke(self):
 
427
        PATTERN = None
 
428
        if self.args.parse_regex:
 
429
            PATTERN = self.args.parse_regex
 
430
        test_name = 'custom'
 
431
        inst = AndroidTestInstaller()
 
432
        run = AndroidTestRunner()
 
433
        parser = AndroidTestParser(pattern=PATTERN)
 
434
        test = AndroidTest(testname=test_name, installer=inst,
 
435
                                runner=run, parser=parser)
 
436
        test.parser.results = {'test_results': []}
 
437
        test.setadb(ADB(self.args.serial))
 
438
 
 
439
        bundle = generate_combined_bundle(self.args.serial,
 
440
                                          self.args.result_id, test=test)
 
441
        try:
 
442
            print DocumentIO.dumps(bundle)
 
443
        except IOError:
 
444
            pass
 
445
 
 
446
 
 
447
def generate_combined_bundle(serial=None, result_ids=None, test=None):
332
448
    if result_ids is None:
333
449
        return {}
334
450
 
335
451
    bundle = None
336
452
 
337
453
    for rid in result_ids:
338
 
        b = generate_bundle(serial, rid)
 
454
        b = generate_bundle(serial, rid, test=None)
339
455
        if rid == result_ids[0]:
340
456
            bundle = b
341
457
        else:
344
460
    return bundle
345
461
 
346
462
 
347
 
def generate_bundle(serial=None, result_id=None):
 
463
def generate_bundle(serial=None, result_id=None, test=None):
348
464
    if result_id is None:
349
465
        return {}
350
466
    config = get_config()
355
471
 
356
472
    bundle_text = adb.read_file(os.path.join(resultdir, "testdata.json"))
357
473
    bundle = DocumentIO.loads(bundle_text)[1]
358
 
    test = testloader(bundle['test_runs'][0]['test_id'], serial)
 
474
    test_tmp = None
 
475
    if bundle['test_runs'][0]['test_id'] == 'custom':
 
476
        test_tmp = test
 
477
    else:
 
478
        test_tmp = testloader(bundle['test_runs'][0]['test_id'], serial)
359
479
 
360
 
    test.parse(result_id)
 
480
    test_tmp.parse(result_id)
361
481
    stdout_text = adb.read_file(os.path.join(resultdir,
362
482
                                  os.path.basename(test.org_ouput_file)))
363
483
    if stdout_text is None: