blob: f775c7dd06bacf34ed4b24f0779e5323ca8cf31b [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
Ralph Nathan91874ca2015-03-19 13:29:41 -070015from chromite.lib import cros_logging as logging
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070016from chromite.lib import image_test_lib
Nam T. Nguyenf3816362014-06-13 09:26:27 -070017from chromite.lib import osutils
Gilad Arnold1c8eda52015-05-04 22:32:38 -070018from chromite.lib import path_util
Nam T. Nguyenf3816362014-06-13 09:26:27 -070019
20
21def ParseArgs(args):
22 """Return parsed commandline arguments."""
23
24 parser = commandline.ArgumentParser()
25 parser.add_argument('--test_results_root', type='path',
26 help='Directory to store test results')
27 parser.add_argument('--board', type=str, help='Board (wolf, beaglebone...)')
28 parser.add_argument('image_dir', type='path',
29 help='Image directory (or file) with mount_image.sh and '
30 'umount_image.sh')
31 opts = parser.parse_args(args)
32 opts.Freeze()
33 return opts
34
35
36def FindImage(image_path):
37 """Return the path to the image file.
38
39 Args:
40 image_path: A path to the image file, or a directory containing the base
41 image.
42
43 Returns:
44 ImageFileAndMountScripts containing absolute paths to the image,
45 the mount and umount invocation commands
46 """
47
48 if os.path.isdir(image_path):
49 # Assume base image.
50 image_file = os.path.join(image_path, constants.BASE_IMAGE_NAME + '.bin')
51 if not os.path.exists(image_file):
Nam T. Nguyend5e4cfb2014-07-23 14:47:14 -070052 raise ValueError('Cannot find base image %s' % image_file)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070053 elif os.path.isfile(image_path):
54 image_file = image_path
55 else:
56 raise ValueError('%s is neither a directory nor a file' % image_path)
57
58 return image_file
59
60
61def main(args):
62 opts = ParseArgs(args)
63
64 # Build up test suites.
65 loader = unittest.TestLoader()
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070066 loader.suiteClass = image_test_lib.ImageTestSuite
Nam T. Nguyenf3816362014-06-13 09:26:27 -070067 # We use a different prefix here so that unittest DO NOT pick up the
68 # image tests automatically because they depend on a proper environment.
69 loader.testMethodPrefix = 'Test'
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070070 all_tests = loader.loadTestsFromName('chromite.cros.test.image_test')
71 forgiving = image_test_lib.ImageTestSuite()
72 non_forgiving = image_test_lib.ImageTestSuite()
Nam T. Nguyenf3816362014-06-13 09:26:27 -070073 for suite in all_tests:
74 for test in suite.GetTests():
75 if test.IsForgiving():
76 forgiving.addTest(test)
77 else:
78 non_forgiving.addTest(test)
79
80 # Run them in the image directory.
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070081 runner = image_test_lib.ImageTestRunner()
Nam T. Nguyenf3816362014-06-13 09:26:27 -070082 runner.SetBoard(opts.board)
83 runner.SetResultDir(opts.test_results_root)
84 image_file = FindImage(opts.image_dir)
Gilad Arnold1c8eda52015-05-04 22:32:38 -070085 tmp_in_chroot = path_util.FromChrootPath('/tmp')
Nam T. Nguyenf3816362014-06-13 09:26:27 -070086 with osutils.TempDir(base_dir=tmp_in_chroot) as temp_dir:
Nam T. Nguyenf3816362014-06-13 09:26:27 -070087 with osutils.MountImageContext(image_file, temp_dir):
88 with osutils.ChdirContext(temp_dir):
89 # Run non-forgiving tests first so that exceptions in forgiving tests
90 # do not skip any required tests.
91 logging.info('Running NON-forgiving tests.')
92 result = runner.run(non_forgiving)
93 logging.info('Running forgiving tests.')
94 runner.run(forgiving)
95
96 if result and not result.wasSuccessful():
97 return 1
98 return 0