Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 2 | # Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Script to mount a built image and run tests on it.""" |
| 7 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 10 | import os |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 11 | import unittest |
| 12 | |
Aviv Keshet | b7519e1 | 2016-10-04 00:50:00 -0700 | [diff] [blame] | 13 | from chromite.lib import constants |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 14 | from chromite.lib import commandline |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame] | 15 | from chromite.lib import image_test_lib |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 16 | from chromite.lib import osutils |
Gilad Arnold | 1c8eda5 | 2015-05-04 22:32:38 -0700 | [diff] [blame] | 17 | from chromite.lib import path_util |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 18 | |
| 19 | |
| 20 | def ParseArgs(args): |
| 21 | """Return parsed commandline arguments.""" |
| 22 | |
Mike Frysinger | 99b12fd | 2018-09-05 18:14:26 -0400 | [diff] [blame] | 23 | parser = commandline.ArgumentParser(description=__doc__) |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 24 | parser.add_argument('--test_results_root', type='path', |
| 25 | help='Directory to store test results') |
| 26 | parser.add_argument('--board', type=str, help='Board (wolf, beaglebone...)') |
Mike Frysinger | 99b12fd | 2018-09-05 18:14:26 -0400 | [diff] [blame] | 27 | parser.add_argument('image', type='path', |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 28 | help='Image directory (or file) with mount_image.sh and ' |
| 29 | 'umount_image.sh') |
Mike Frysinger | 6dd14f4 | 2017-10-18 18:33:35 -0400 | [diff] [blame] | 30 | |
| 31 | parser.add_argument('-l', '--list', default=False, action='store_true', |
| 32 | help='List all the available tests') |
| 33 | parser.add_argument('tests', nargs='*', metavar='test', |
| 34 | help='Specific tests to run (default runs all)') |
| 35 | |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 36 | opts = parser.parse_args(args) |
| 37 | opts.Freeze() |
| 38 | return opts |
| 39 | |
| 40 | |
| 41 | def FindImage(image_path): |
| 42 | """Return the path to the image file. |
| 43 | |
| 44 | Args: |
| 45 | image_path: A path to the image file, or a directory containing the base |
| 46 | image. |
| 47 | |
| 48 | Returns: |
| 49 | ImageFileAndMountScripts containing absolute paths to the image, |
| 50 | the mount and umount invocation commands |
| 51 | """ |
| 52 | |
| 53 | if os.path.isdir(image_path): |
| 54 | # Assume base image. |
| 55 | image_file = os.path.join(image_path, constants.BASE_IMAGE_NAME + '.bin') |
| 56 | if not os.path.exists(image_file): |
Nam T. Nguyen | d5e4cfb | 2014-07-23 14:47:14 -0700 | [diff] [blame] | 57 | raise ValueError('Cannot find base image %s' % image_file) |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 58 | elif os.path.isfile(image_path): |
| 59 | image_file = image_path |
| 60 | else: |
| 61 | raise ValueError('%s is neither a directory nor a file' % image_path) |
| 62 | |
| 63 | return image_file |
| 64 | |
| 65 | |
| 66 | def main(args): |
| 67 | opts = ParseArgs(args) |
| 68 | |
| 69 | # Build up test suites. |
| 70 | loader = unittest.TestLoader() |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame] | 71 | loader.suiteClass = image_test_lib.ImageTestSuite |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 72 | # We use a different prefix here so that unittest DO NOT pick up the |
| 73 | # image tests automatically because they depend on a proper environment. |
| 74 | loader.testMethodPrefix = 'Test' |
Mike Frysinger | 6dd14f4 | 2017-10-18 18:33:35 -0400 | [diff] [blame] | 75 | tests_namespace = 'chromite.cros.test.image_test' |
| 76 | if opts.tests: |
| 77 | tests = ['%s.%s' % (tests_namespace, x) for x in opts.tests] |
| 78 | else: |
| 79 | tests = (tests_namespace,) |
| 80 | all_tests = loader.loadTestsFromNames(tests) |
| 81 | |
| 82 | # If they just want to see the lists of tests, show them now. |
| 83 | if opts.list: |
| 84 | def _WalkSuite(suite): |
| 85 | for test in suite: |
| 86 | if isinstance(test, unittest.BaseTestSuite): |
| 87 | for result in _WalkSuite(test): |
| 88 | yield result |
| 89 | else: |
| 90 | yield (test.id()[len(tests_namespace) + 1:], |
| 91 | test.shortDescription() or '') |
| 92 | |
| 93 | test_list = list(_WalkSuite(all_tests)) |
| 94 | maxlen = max(len(x[0]) for x in test_list) |
| 95 | for name, desc in test_list: |
| 96 | print('%-*s %s' % (maxlen, name, desc)) |
| 97 | return |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 98 | |
| 99 | # Run them in the image directory. |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame] | 100 | runner = image_test_lib.ImageTestRunner() |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 101 | runner.SetBoard(opts.board) |
| 102 | runner.SetResultDir(opts.test_results_root) |
Mike Frysinger | 99b12fd | 2018-09-05 18:14:26 -0400 | [diff] [blame] | 103 | image_file = FindImage(opts.image) |
Gilad Arnold | 1c8eda5 | 2015-05-04 22:32:38 -0700 | [diff] [blame] | 104 | tmp_in_chroot = path_util.FromChrootPath('/tmp') |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 105 | with osutils.TempDir(base_dir=tmp_in_chroot) as temp_dir: |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 106 | with osutils.MountImageContext(image_file, temp_dir): |
| 107 | with osutils.ChdirContext(temp_dir): |
Mike Frysinger | 3e04661 | 2017-10-18 17:19:23 -0400 | [diff] [blame] | 108 | result = runner.run(all_tests) |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 109 | |
| 110 | if result and not result.wasSuccessful(): |
| 111 | return 1 |
| 112 | return 0 |