blob: 8c0c7fa98d9863b6142a0b20ff6be3795035526f [file] [log] [blame]
Tan Gao6f9f71a2010-09-10 16:31:26 -07001#!/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
Tan Gao2990a4d2010-09-22 09:34:27 -07007"""Tests for build_image shell script.
Tan Gao6f9f71a2010-09-10 16:31:26 -07008
9Note:
Tan Gao2990a4d2010-09-22 09:34:27 -070010 This script must be run from INSIDE chroot.
Tan Gao6f9f71a2010-09-10 16:31:26 -070011
12Sample usage:
Tan Gao2990a4d2010-09-22 09:34:27 -070013 # (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
Tan Gao6f9f71a2010-09-10 16:31:26 -070019
20 # run a specific test
Tan Gao2990a4d2010-09-22 09:34:27 -070021 python chromite/tests/build_image_test.py BuildImageTest.testWithoutBoardExit
Tan Gao6f9f71a2010-09-10 16:31:26 -070022"""
23
Tan Gao6f9f71a2010-09-10 16:31:26 -070024import os
25import re
26import sys
27import unittest
28sys.path.append(os.path.join(os.path.dirname(__file__), '../lib'))
Tan Gao2990a4d2010-09-22 09:34:27 -070029from cros_build_lib import (RunCommand, IsInsideChroot, GetChromeosVersion,
30 GetOutputImageDir)
Tan Gao6f9f71a2010-09-10 16:31:26 -070031
32
33class 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 """
Tan Gao2990a4d2010-09-22 09:34:27 -070063 Info ('About to run command: %s' % cmd)
Tan Gao6f9f71a2010-09-10 16:31:26 -070064 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
Tan Gao2990a4d2010-09-22 09:34:27 -070069 Info ('output =\n%r' % self.output)
70 Info ('error =\n%r' % self.error)
Tan Gao6f9f71a2010-09-10 16:31:26 -070071
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
Tan Gao2990a4d2010-09-22 09:34:27 -0700115 Info ('If all goes well, it takes ~5 min. to build an image...')
Tan Gao6f9f71a2010-09-10 16:31:26 -0700116 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
129if __name__ == '__main__':
130 unittest.main()