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