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 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 9 | import os |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 10 | import unittest |
| 11 | |
Aviv Keshet | b7519e1 | 2016-10-04 00:50:00 -0700 | [diff] [blame^] | 12 | from chromite.lib import constants |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 13 | from chromite.lib import commandline |
Ralph Nathan | 91874ca | 2015-03-19 13:29:41 -0700 | [diff] [blame] | 14 | from chromite.lib import cros_logging as logging |
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 | |
| 23 | parser = commandline.ArgumentParser() |
| 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...)') |
| 27 | parser.add_argument('image_dir', type='path', |
| 28 | help='Image directory (or file) with mount_image.sh and ' |
| 29 | 'umount_image.sh') |
| 30 | opts = parser.parse_args(args) |
| 31 | opts.Freeze() |
| 32 | return opts |
| 33 | |
| 34 | |
| 35 | def FindImage(image_path): |
| 36 | """Return the path to the image file. |
| 37 | |
| 38 | Args: |
| 39 | image_path: A path to the image file, or a directory containing the base |
| 40 | image. |
| 41 | |
| 42 | Returns: |
| 43 | ImageFileAndMountScripts containing absolute paths to the image, |
| 44 | the mount and umount invocation commands |
| 45 | """ |
| 46 | |
| 47 | if os.path.isdir(image_path): |
| 48 | # Assume base image. |
| 49 | image_file = os.path.join(image_path, constants.BASE_IMAGE_NAME + '.bin') |
| 50 | if not os.path.exists(image_file): |
Nam T. Nguyen | d5e4cfb | 2014-07-23 14:47:14 -0700 | [diff] [blame] | 51 | raise ValueError('Cannot find base image %s' % image_file) |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 52 | elif os.path.isfile(image_path): |
| 53 | image_file = image_path |
| 54 | else: |
| 55 | raise ValueError('%s is neither a directory nor a file' % image_path) |
| 56 | |
| 57 | return image_file |
| 58 | |
| 59 | |
| 60 | def main(args): |
| 61 | opts = ParseArgs(args) |
| 62 | |
| 63 | # Build up test suites. |
| 64 | loader = unittest.TestLoader() |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame] | 65 | loader.suiteClass = image_test_lib.ImageTestSuite |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 66 | # We use a different prefix here so that unittest DO NOT pick up the |
| 67 | # image tests automatically because they depend on a proper environment. |
| 68 | loader.testMethodPrefix = 'Test' |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame] | 69 | all_tests = loader.loadTestsFromName('chromite.cros.test.image_test') |
| 70 | forgiving = image_test_lib.ImageTestSuite() |
| 71 | non_forgiving = image_test_lib.ImageTestSuite() |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 72 | for suite in all_tests: |
| 73 | for test in suite.GetTests(): |
| 74 | if test.IsForgiving(): |
| 75 | forgiving.addTest(test) |
| 76 | else: |
| 77 | non_forgiving.addTest(test) |
| 78 | |
| 79 | # Run them in the image directory. |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame] | 80 | runner = image_test_lib.ImageTestRunner() |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 81 | runner.SetBoard(opts.board) |
| 82 | runner.SetResultDir(opts.test_results_root) |
| 83 | image_file = FindImage(opts.image_dir) |
Gilad Arnold | 1c8eda5 | 2015-05-04 22:32:38 -0700 | [diff] [blame] | 84 | tmp_in_chroot = path_util.FromChrootPath('/tmp') |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 85 | with osutils.TempDir(base_dir=tmp_in_chroot) as temp_dir: |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 86 | with osutils.MountImageContext(image_file, temp_dir): |
| 87 | with osutils.ChdirContext(temp_dir): |
| 88 | # Run non-forgiving tests first so that exceptions in forgiving tests |
| 89 | # do not skip any required tests. |
| 90 | logging.info('Running NON-forgiving tests.') |
| 91 | result = runner.run(non_forgiving) |
| 92 | logging.info('Running forgiving tests.') |
| 93 | runner.run(forgiving) |
| 94 | |
| 95 | if result and not result.wasSuccessful(): |
| 96 | return 1 |
| 97 | return 0 |