blob: a86abe8f6d0bf764af5b6632b0959b28cb557db4 [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
23 parser = commandline.ArgumentParser()
24 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...)')
27 parser.add_argument('image_dir', type='path',
28 help='Image directory (or file) with mount_image.sh and '
29 'umount_image.sh')
30 opts = parser.parse_args(args)
31 opts.Freeze()
32 return opts
33
34
35def FindImage(image_path):
36 """Return the path to the image file.
37
38 Args:
39 image_path: A path to the image file, or a directory containing the base
40 image.
41
42 Returns:
43 ImageFileAndMountScripts containing absolute paths to the image,
44 the mount and umount invocation commands
45 """
46
47 if os.path.isdir(image_path):
48 # Assume base image.
49 image_file = os.path.join(image_path, constants.BASE_IMAGE_NAME + '.bin')
50 if not os.path.exists(image_file):
Nam T. Nguyend5e4cfb2014-07-23 14:47:14 -070051 raise ValueError('Cannot find base image %s' % image_file)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070052 elif os.path.isfile(image_path):
53 image_file = image_path
54 else:
55 raise ValueError('%s is neither a directory nor a file' % image_path)
56
57 return image_file
58
59
60def main(args):
61 opts = ParseArgs(args)
62
63 # Build up test suites.
64 loader = unittest.TestLoader()
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070065 loader.suiteClass = image_test_lib.ImageTestSuite
Nam T. Nguyenf3816362014-06-13 09:26:27 -070066 # We use a different prefix here so that unittest DO NOT pick up the
67 # image tests automatically because they depend on a proper environment.
68 loader.testMethodPrefix = 'Test'
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070069 all_tests = loader.loadTestsFromName('chromite.cros.test.image_test')
Nam T. Nguyenf3816362014-06-13 09:26:27 -070070
71 # Run them in the image directory.
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070072 runner = image_test_lib.ImageTestRunner()
Nam T. Nguyenf3816362014-06-13 09:26:27 -070073 runner.SetBoard(opts.board)
74 runner.SetResultDir(opts.test_results_root)
75 image_file = FindImage(opts.image_dir)
Gilad Arnold1c8eda52015-05-04 22:32:38 -070076 tmp_in_chroot = path_util.FromChrootPath('/tmp')
Nam T. Nguyenf3816362014-06-13 09:26:27 -070077 with osutils.TempDir(base_dir=tmp_in_chroot) as temp_dir:
Nam T. Nguyenf3816362014-06-13 09:26:27 -070078 with osutils.MountImageContext(image_file, temp_dir):
79 with osutils.ChdirContext(temp_dir):
Mike Frysinger3e046612017-10-18 17:19:23 -040080 result = runner.run(all_tests)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070081
82 if result and not result.wasSuccessful():
83 return 1
84 return 0