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