blob: 2cdf1741f4095bd9e80862140a391cab1fd9856f [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
Nam T. Nguyenf3816362014-06-13 09:26:27 -07007import os
Nam T. Nguyenf3816362014-06-13 09:26:27 -07008import unittest
9
Aviv Keshetb7519e12016-10-04 00:50:00 -070010from chromite.lib import constants
Nam T. Nguyenf3816362014-06-13 09:26:27 -070011from chromite.lib import commandline
Mike Nicholsa8e8f242019-09-09 14:10:29 -060012from chromite.lib import image_lib
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070013from chromite.lib import image_test_lib
Nam T. Nguyenf3816362014-06-13 09:26:27 -070014from chromite.lib import osutils
Gilad Arnold1c8eda52015-05-04 22:32:38 -070015from chromite.lib import path_util
Nam T. Nguyenf3816362014-06-13 09:26:27 -070016
17
18def ParseArgs(args):
19 """Return parsed commandline arguments."""
20
Mike Frysinger99b12fd2018-09-05 18:14:26 -040021 parser = commandline.ArgumentParser(description=__doc__)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070022 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...)')
Mike Frysinger99b12fd2018-09-05 18:14:26 -040025 parser.add_argument('image', type='path',
Nam T. Nguyenf3816362014-06-13 09:26:27 -070026 help='Image directory (or file) with mount_image.sh and '
27 'umount_image.sh')
Mike Frysinger6dd14f42017-10-18 18:33:35 -040028
29 parser.add_argument('-l', '--list', default=False, action='store_true',
30 help='List all the available tests')
31 parser.add_argument('tests', nargs='*', metavar='test',
32 help='Specific tests to run (default runs all)')
33
Nam T. Nguyenf3816362014-06-13 09:26:27 -070034 opts = parser.parse_args(args)
35 opts.Freeze()
36 return opts
37
38
39def FindImage(image_path):
40 """Return the path to the image file.
41
42 Args:
43 image_path: A path to the image file, or a directory containing the base
44 image.
45
46 Returns:
47 ImageFileAndMountScripts containing absolute paths to the image,
48 the mount and umount invocation commands
49 """
50
51 if os.path.isdir(image_path):
52 # Assume base image.
53 image_file = os.path.join(image_path, constants.BASE_IMAGE_NAME + '.bin')
54 if not os.path.exists(image_file):
Nam T. Nguyend5e4cfb2014-07-23 14:47:14 -070055 raise ValueError('Cannot find base image %s' % image_file)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070056 elif os.path.isfile(image_path):
57 image_file = image_path
58 else:
59 raise ValueError('%s is neither a directory nor a file' % image_path)
60
61 return image_file
62
63
64def main(args):
65 opts = ParseArgs(args)
66
67 # Build up test suites.
68 loader = unittest.TestLoader()
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070069 loader.suiteClass = image_test_lib.ImageTestSuite
Nam T. Nguyenf3816362014-06-13 09:26:27 -070070 # We use a different prefix here so that unittest DO NOT pick up the
71 # image tests automatically because they depend on a proper environment.
72 loader.testMethodPrefix = 'Test'
Mike Frysinger6dd14f42017-10-18 18:33:35 -040073 tests_namespace = 'chromite.cros.test.image_test'
74 if opts.tests:
75 tests = ['%s.%s' % (tests_namespace, x) for x in opts.tests]
76 else:
77 tests = (tests_namespace,)
78 all_tests = loader.loadTestsFromNames(tests)
79
80 # If they just want to see the lists of tests, show them now.
81 if opts.list:
82 def _WalkSuite(suite):
83 for test in suite:
84 if isinstance(test, unittest.BaseTestSuite):
85 for result in _WalkSuite(test):
86 yield result
87 else:
88 yield (test.id()[len(tests_namespace) + 1:],
89 test.shortDescription() or '')
90
91 test_list = list(_WalkSuite(all_tests))
92 maxlen = max(len(x[0]) for x in test_list)
93 for name, desc in test_list:
94 print('%-*s %s' % (maxlen, name, desc))
95 return
Nam T. Nguyenf3816362014-06-13 09:26:27 -070096
97 # Run them in the image directory.
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070098 runner = image_test_lib.ImageTestRunner()
Nam T. Nguyenf3816362014-06-13 09:26:27 -070099 runner.SetBoard(opts.board)
100 runner.SetResultDir(opts.test_results_root)
Mike Frysinger99b12fd2018-09-05 18:14:26 -0400101 image_file = FindImage(opts.image)
Gilad Arnold1c8eda52015-05-04 22:32:38 -0700102 tmp_in_chroot = path_util.FromChrootPath('/tmp')
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700103 with osutils.TempDir(base_dir=tmp_in_chroot) as temp_dir:
Mike Nicholsa8e8f242019-09-09 14:10:29 -0600104 with image_lib.LoopbackPartitions(image_file, temp_dir) as image:
105 # Due to the lack of mount context, we mount the partitions
106 # but do not reference directly. This will be removed with the
107 # submission of http://crrev/c/1795578
108 _ = image.Mount((constants.PART_ROOT_A,))[0]
109 _ = image.Mount((constants.PART_STATE,))[0]
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700110 with osutils.ChdirContext(temp_dir):
Mike Frysinger3e046612017-10-18 17:19:23 -0400111 result = runner.run(all_tests)
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700112
113 if result and not result.wasSuccessful():
114 return 1
115 return 0