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 | |
| 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') |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 70 | |
| 71 | # Run them in the image directory. |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame] | 72 | runner = image_test_lib.ImageTestRunner() |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 73 | runner.SetBoard(opts.board) |
| 74 | runner.SetResultDir(opts.test_results_root) |
| 75 | image_file = FindImage(opts.image_dir) |
Gilad Arnold | 1c8eda5 | 2015-05-04 22:32:38 -0700 | [diff] [blame] | 76 | tmp_in_chroot = path_util.FromChrootPath('/tmp') |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 77 | with osutils.TempDir(base_dir=tmp_in_chroot) as temp_dir: |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 78 | with osutils.MountImageContext(image_file, temp_dir): |
| 79 | with osutils.ChdirContext(temp_dir): |
Mike Frysinger | 3e04661 | 2017-10-18 17:19:23 -0400 | [diff] [blame^] | 80 | result = runner.run(all_tests) |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 81 | |
| 82 | if result and not result.wasSuccessful(): |
| 83 | return 1 |
| 84 | return 0 |