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