blob: 2cc7125c97458bcaa460e859d9a712ef36181b53 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Nam T. Nguyenf3816362014-06-13 09:26:27 -07002# 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 Frysinger383367e2014-09-16 15:06:17 -04008from __future__ import print_function
9
Nam T. Nguyenf3816362014-06-13 09:26:27 -070010import os
Nam T. Nguyenf3816362014-06-13 09:26:27 -070011import unittest
12
Aviv Keshetb7519e12016-10-04 00:50:00 -070013from chromite.lib import constants
Nam T. Nguyenf3816362014-06-13 09:26:27 -070014from chromite.lib import commandline
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070015from chromite.lib import image_test_lib
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
Mike Frysinger99b12fd2018-09-05 18:14:26 -040023 parser = commandline.ArgumentParser(description=__doc__)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070024 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...)')
Mike Frysinger99b12fd2018-09-05 18:14:26 -040027 parser.add_argument('image', type='path',
Nam T. Nguyenf3816362014-06-13 09:26:27 -070028 help='Image directory (or file) with mount_image.sh and '
29 'umount_image.sh')
Mike Frysinger6dd14f42017-10-18 18:33:35 -040030
31 parser.add_argument('-l', '--list', default=False, action='store_true',
32 help='List all the available tests')
33 parser.add_argument('tests', nargs='*', metavar='test',
34 help='Specific tests to run (default runs all)')
35
Nam T. Nguyenf3816362014-06-13 09:26:27 -070036 opts = parser.parse_args(args)
37 opts.Freeze()
38 return opts
39
40
41def FindImage(image_path):
42 """Return the path to the image file.
43
44 Args:
45 image_path: A path to the image file, or a directory containing the base
46 image.
47
48 Returns:
49 ImageFileAndMountScripts containing absolute paths to the image,
50 the mount and umount invocation commands
51 """
52
53 if os.path.isdir(image_path):
54 # Assume base image.
55 image_file = os.path.join(image_path, constants.BASE_IMAGE_NAME + '.bin')
56 if not os.path.exists(image_file):
Nam T. Nguyend5e4cfb2014-07-23 14:47:14 -070057 raise ValueError('Cannot find base image %s' % image_file)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070058 elif os.path.isfile(image_path):
59 image_file = image_path
60 else:
61 raise ValueError('%s is neither a directory nor a file' % image_path)
62
63 return image_file
64
65
66def main(args):
67 opts = ParseArgs(args)
68
69 # Build up test suites.
70 loader = unittest.TestLoader()
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070071 loader.suiteClass = image_test_lib.ImageTestSuite
Nam T. Nguyenf3816362014-06-13 09:26:27 -070072 # We use a different prefix here so that unittest DO NOT pick up the
73 # image tests automatically because they depend on a proper environment.
74 loader.testMethodPrefix = 'Test'
Mike Frysinger6dd14f42017-10-18 18:33:35 -040075 tests_namespace = 'chromite.cros.test.image_test'
76 if opts.tests:
77 tests = ['%s.%s' % (tests_namespace, x) for x in opts.tests]
78 else:
79 tests = (tests_namespace,)
80 all_tests = loader.loadTestsFromNames(tests)
81
82 # If they just want to see the lists of tests, show them now.
83 if opts.list:
84 def _WalkSuite(suite):
85 for test in suite:
86 if isinstance(test, unittest.BaseTestSuite):
87 for result in _WalkSuite(test):
88 yield result
89 else:
90 yield (test.id()[len(tests_namespace) + 1:],
91 test.shortDescription() or '')
92
93 test_list = list(_WalkSuite(all_tests))
94 maxlen = max(len(x[0]) for x in test_list)
95 for name, desc in test_list:
96 print('%-*s %s' % (maxlen, name, desc))
97 return
Nam T. Nguyenf3816362014-06-13 09:26:27 -070098
99 # Run them in the image directory.
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -0700100 runner = image_test_lib.ImageTestRunner()
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700101 runner.SetBoard(opts.board)
102 runner.SetResultDir(opts.test_results_root)
Mike Frysinger99b12fd2018-09-05 18:14:26 -0400103 image_file = FindImage(opts.image)
Gilad Arnold1c8eda52015-05-04 22:32:38 -0700104 tmp_in_chroot = path_util.FromChrootPath('/tmp')
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700105 with osutils.TempDir(base_dir=tmp_in_chroot) as temp_dir:
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700106 with osutils.MountImageContext(image_file, temp_dir):
107 with osutils.ChdirContext(temp_dir):
Mike Frysinger3e046612017-10-18 17:19:23 -0400108 result = runner.run(all_tests)
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700109
110 if result and not result.wasSuccessful():
111 return 1
112 return 0