OLD | NEW |
| (Empty) |
1 #!/usr/bin/python | |
2 # | |
3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 """Tests for build_image shell script. | |
8 | |
9 Note: | |
10 This script must be run from INSIDE chroot. | |
11 | |
12 Sample usage: | |
13 # (inside chroot) pushd ~/trunk/src/scripts/ | |
14 # run all test cases in this script | |
15 python chromite/tests/build_image_test.py | |
16 | |
17 # run all test cases in a test suite | |
18 python chromite/tests/build_image_test.py BuildImageTest | |
19 | |
20 # run a specific test | |
21 python chromite/tests/build_image_test.py BuildImageTest.testWithoutBoardExit | |
22 """ | |
23 | |
24 import os | |
25 import re | |
26 import sys | |
27 import unittest | |
28 sys.path.append(os.path.join(os.path.dirname(__file__), '../lib')) | |
29 from cros_build_lib import (RunCommand, IsInsideChroot, GetChromeosVersion, | |
30 GetOutputImageDir) | |
31 | |
32 | |
33 class BuildImageTest(unittest.TestCase): | |
34 """Test suite for build_image script.""" | |
35 | |
36 def setUp(self): | |
37 if not IsInsideChroot(): | |
38 raise RuntimeError('This script must be run from inside chroot.') | |
39 | |
40 def _CheckStringPresent(self, query_list, check_stdout=False): | |
41 """Check for presence of specific queries. | |
42 | |
43 Args: | |
44 query_list: a list of strings to look for. | |
45 check_stdout: a boolean. True == use stdout from child process. | |
46 Otherwise use its stderr. | |
47 """ | |
48 for query in query_list: | |
49 # Source error string defined in src/scripts/build_image | |
50 if check_stdout: | |
51 self.assertNotEqual(-1, self.output.find(query)) | |
52 else: | |
53 self.assertNotEqual(-1, self.error.find(query)) | |
54 | |
55 def _RunBuildImageCmd(self, cmd, assert_success=True): | |
56 """Run build_image with flags. | |
57 | |
58 Args: | |
59 cmd: a string. | |
60 assert_success: a boolean. True == check child process return code is 0. | |
61 False otherwise. | |
62 """ | |
63 Info ('About to run command: %s' % cmd) | |
64 cmd_result = RunCommand( | |
65 cmd, error_ok=True, exit_code=True, redirect_stdout=True, | |
66 redirect_stderr=True, shell=True) | |
67 self.output = cmd_result.output | |
68 self.error = cmd_result.error | |
69 Info ('output =\n%r' % self.output) | |
70 Info ('error =\n%r' % self.error) | |
71 | |
72 message = 'cmd should have failed! error:\n%s' % self.error | |
73 if assert_success: | |
74 self.assertEqual(0, cmd_result.returncode) | |
75 else: | |
76 self.assertNotEqual(0, cmd_result.returncode, message) | |
77 | |
78 def _VerifyOutputImagesExist(self, image_dir, image_list): | |
79 """Verify output images exist in image_dir. | |
80 | |
81 Args: | |
82 image_dir: a string, absolute path to output directory with images. | |
83 image_list: a list of strings, names of output images. | |
84 """ | |
85 for i in image_list: | |
86 image_path = os.path.join(image_dir, i) | |
87 self.assertTrue(os.path.exists(image_path)) | |
88 | |
89 def testWithoutBoardExit(self): | |
90 """Fail when no --board is specified.""" | |
91 self._RunBuildImageCmd('./build_image --board=""', assert_success=False) | |
92 self._CheckStringPresent(['ERROR', '--board is required']) | |
93 | |
94 def testIncompatibleInstallFlags(self): | |
95 """Fail when both --factory_install and --dev_install are set.""" | |
96 cmd = './build_image --board=x86-generic --factory_install --dev_install' | |
97 self._RunBuildImageCmd(cmd, assert_success=False) | |
98 self._CheckStringPresent(['ERROR', 'Incompatible flags']) | |
99 | |
100 def testIncompatibleRootfsFlags(self): | |
101 """Fail when rootfs partition is not large enough.""" | |
102 cmd = ('./build_image --board=x86-generic --rootfs_size=100' | |
103 ' --rootfs_hash_pad=10 --rootfs_partition_size=20') | |
104 self._RunBuildImageCmd(cmd, assert_success=False) | |
105 self._CheckStringPresent(['ERROR', 'bigger than partition']) | |
106 | |
107 def _BuildImageForBoard(self, board, image_list): | |
108 """Build image for specific board type. | |
109 | |
110 Args: | |
111 board: a string. | |
112 image_list: a list of strings, names of output images. | |
113 """ | |
114 cmd = './build_image --board=%s' % board | |
115 Info ('If all goes well, it takes ~5 min. to build an image...') | |
116 self._RunBuildImageCmd(cmd) | |
117 self._CheckStringPresent(['Image created in', 'copy to USB keyfob'], | |
118 check_stdout=True) | |
119 chromeos_version_str = GetChromeosVersion(self.output) | |
120 image_dir = GetOutputImageDir(board, chromeos_version_str) | |
121 self._VerifyOutputImagesExist(image_dir, image_list) | |
122 | |
123 def testBuildX86Generic(self): | |
124 """Verify we can build an x86-generic image.""" | |
125 self._BuildImageForBoard( | |
126 'x86-generic', ['chromiumos_image.bin', 'chromiumos_base_image.bin']) | |
127 | |
128 | |
129 if __name__ == '__main__': | |
130 unittest.main() | |
OLD | NEW |