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