blob: 12ac5010aac00c88fff5a000ade7ebe2bd3eeb12 [file] [log] [blame]
Nam T. Nguyenf3816362014-06-13 09:26:27 -07001# 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 Frysinger383367e2014-09-16 15:06:17 -04007from __future__ import print_function
8
Nam T. Nguyenf3816362014-06-13 09:26:27 -07009import os
Nam T. Nguyenf3816362014-06-13 09:26:27 -070010import unittest
11
12from chromite.cbuildbot import constants
Nam T. Nguyenf3816362014-06-13 09:26:27 -070013from chromite.lib import commandline
Ralph Nathan91874ca2015-03-19 13:29:41 -070014from chromite.lib import cros_logging as logging
David Pursellcfd58872015-03-19 09:15:48 -070015from chromite.lib import image_test
Nam T. Nguyenf3816362014-06-13 09:26:27 -070016from chromite.lib import osutils
Gilad Arnold1c8eda52015-05-04 22:32:38 -070017from chromite.lib import path_util
Nam T. Nguyenf3816362014-06-13 09:26:27 -070018
19
20def 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
35def 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. Nguyend5e4cfb2014-07-23 14:47:14 -070051 raise ValueError('Cannot find base image %s' % image_file)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070052 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
60def main(args):
61 opts = ParseArgs(args)
62
63 # Build up test suites.
64 loader = unittest.TestLoader()
65 loader.suiteClass = image_test.ImageTestSuite
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'
David Pursellcfd58872015-03-19 09:15:48 -070069 all_tests = loader.loadTestsFromName('chromite.lib.image_test')
Nam T. Nguyenf3816362014-06-13 09:26:27 -070070 forgiving = image_test.ImageTestSuite()
71 non_forgiving = image_test.ImageTestSuite()
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.
80 runner = image_test.ImageTestRunner()
81 runner.SetBoard(opts.board)
82 runner.SetResultDir(opts.test_results_root)
83 image_file = FindImage(opts.image_dir)
Gilad Arnold1c8eda52015-05-04 22:32:38 -070084 tmp_in_chroot = path_util.FromChrootPath('/tmp')
Nam T. Nguyenf3816362014-06-13 09:26:27 -070085 with osutils.TempDir(base_dir=tmp_in_chroot) as temp_dir:
Nam T. Nguyenf3816362014-06-13 09:26:27 -070086 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