blob: bfad309303078cd88f4560b23dced77df1ca1b01 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glassc05694f2013-04-03 11:07:16 +00002# Copyright (c) 2012 The Chromium OS Authors.
3#
Simon Glassc05694f2013-04-03 11:07:16 +00004
Simon Glassc229d322024-06-23 11:55:15 -06005from filelock import FileLock
Simon Glassc05694f2013-04-03 11:07:16 +00006import os
7import shutil
8import sys
9import tempfile
10import time
11import unittest
Simon Glassc229d322024-06-23 11:55:15 -060012from unittest.mock import patch
Simon Glassc05694f2013-04-03 11:07:16 +000013
Simon Glassf0d9c102020-04-17 18:09:02 -060014from buildman import board
Simon Glass20751d62022-07-11 19:04:03 -060015from buildman import boards
Simon Glassf0d9c102020-04-17 18:09:02 -060016from buildman import bsettings
17from buildman import builder
Simon Glass22901f92022-01-22 05:07:31 -070018from buildman import cfgutil
Simon Glassf0d9c102020-04-17 18:09:02 -060019from buildman import control
20from buildman import toolchain
Simon Glassa997ea52020-04-17 18:09:04 -060021from patman import commit
Simon Glass131444f2023-02-23 18:18:04 -070022from u_boot_pylib import command
23from u_boot_pylib import terminal
24from u_boot_pylib import test_util
25from u_boot_pylib import tools
Simon Glassc05694f2013-04-03 11:07:16 +000026
Simon Glass2bfc6972017-11-12 21:52:14 -070027use_network = True
28
Simon Glass4bea81e2014-12-01 17:34:04 -070029settings_data = '''
30# Buildman settings file
31
32[toolchain]
33main: /usr/sbin
34
35[toolchain-alias]
36x86: i386 x86_64
37'''
38
Simon Glassf4ebfba2020-04-09 15:08:53 -060039migration = '''===================== WARNING ======================
40This board does not use CONFIG_DM. CONFIG_DM will be
41compulsory starting with the v2020.01 release.
42Failure to update may result in board removal.
Johannes Krottmayerce53c8b2022-03-01 04:49:51 +010043See doc/develop/driver-model/migration.rst for more info.
Simon Glassf4ebfba2020-04-09 15:08:53 -060044====================================================
45'''
46
Simon Glassc05694f2013-04-03 11:07:16 +000047errors = [
48 '''main.c: In function 'main_loop':
49main.c:260:6: warning: unused variable 'joe' [-Wunused-variable]
50''',
Simon Glass7fb8aa22014-09-05 19:00:08 -060051 '''main.c: In function 'main_loop2':
Simon Glassc05694f2013-04-03 11:07:16 +000052main.c:295:2: error: 'fred' undeclared (first use in this function)
53main.c:295:2: note: each undeclared identifier is reported only once for each function it appears in
54make[1]: *** [main.o] Error 1
55make: *** [common/libcommon.o] Error 2
56Make failed
57''',
Simon Glass0db94432018-11-06 16:02:11 -070058 '''arch/arm/dts/socfpga_arria10_socdk_sdmmc.dtb: Warning \
59(avoid_unnecessary_addr_size): /clocks: unnecessary #address-cells/#size-cells \
60without "ranges" or child "reg" property
Simon Glassc05694f2013-04-03 11:07:16 +000061''',
62 '''powerpc-linux-ld: warning: dot moved backwards before `.bss'
63powerpc-linux-ld: warning: dot moved backwards before `.bss'
64powerpc-linux-ld: u-boot: section .text lma 0xfffc0000 overlaps previous sections
65powerpc-linux-ld: u-boot: section .rodata lma 0xfffef3ec overlaps previous sections
66powerpc-linux-ld: u-boot: section .reloc lma 0xffffa400 overlaps previous sections
67powerpc-linux-ld: u-boot: section .data lma 0xffffcd38 overlaps previous sections
68powerpc-linux-ld: u-boot: section .u_boot_cmd lma 0xffffeb40 overlaps previous sections
69powerpc-linux-ld: u-boot: section .bootpg lma 0xfffff198 overlaps previous sections
Simon Glassa8b7b1b2014-09-05 19:00:21 -060070''',
71 '''In file included from %(basedir)sarch/sandbox/cpu/cpu.c:9:0:
72%(basedir)sarch/sandbox/include/asm/state.h:44:0: warning: "xxxx" redefined [enabled by default]
73%(basedir)sarch/sandbox/include/asm/state.h:43:0: note: this is the location of the previous definition
74%(basedir)sarch/sandbox/cpu/cpu.c: In function 'do_reset':
75%(basedir)sarch/sandbox/cpu/cpu.c:27:1: error: unknown type name 'blah'
76%(basedir)sarch/sandbox/cpu/cpu.c:28:12: error: expected declaration specifiers or '...' before numeric constant
77make[2]: *** [arch/sandbox/cpu/cpu.o] Error 1
78make[1]: *** [arch/sandbox/cpu] Error 2
79make[1]: *** Waiting for unfinished jobs....
80In file included from %(basedir)scommon/board_f.c:55:0:
81%(basedir)sarch/sandbox/include/asm/state.h:44:0: warning: "xxxx" redefined [enabled by default]
82%(basedir)sarch/sandbox/include/asm/state.h:43:0: note: this is the location of the previous definition
83make: *** [sub-make] Error 2
Simon Glassc05694f2013-04-03 11:07:16 +000084'''
85]
86
87
88# hash, subject, return code, list of errors/warnings
89commits = [
Simon Glassf4ebfba2020-04-09 15:08:53 -060090 ['1234', 'upstream/master, migration warning', 0, []],
Simon Glassc05694f2013-04-03 11:07:16 +000091 ['5678', 'Second commit, a warning', 0, errors[0:1]],
92 ['9012', 'Third commit, error', 1, errors[0:2]],
93 ['3456', 'Fourth commit, warning', 0, [errors[0], errors[2]]],
94 ['7890', 'Fifth commit, link errors', 1, [errors[0], errors[3]]],
Simon Glassa8b7b1b2014-09-05 19:00:21 -060095 ['abcd', 'Sixth commit, fixes all errors', 0, []],
Simon Glassf4ebfba2020-04-09 15:08:53 -060096 ['ef01', 'Seventh commit, fix migration, check directory suppression', 1,
97 [errors[4]]],
Simon Glassc05694f2013-04-03 11:07:16 +000098]
99
Simon Glassb0786df2022-07-11 19:03:59 -0600100BOARDS = [
Simon Glass584cf862013-09-23 17:35:16 -0600101 ['Active', 'arm', 'armv7', '', 'Tester', 'ARM Board 1', 'board0', ''],
102 ['Active', 'arm', 'armv7', '', 'Tester', 'ARM Board 2', 'board1', ''],
103 ['Active', 'powerpc', 'powerpc', '', 'Tester', 'PowerPC board 1', 'board2', ''],
Simon Glass5e0f06d2017-11-12 21:52:15 -0700104 ['Active', 'powerpc', 'mpc83xx', '', 'Tester', 'PowerPC board 2', 'board3', ''],
Simon Glass584cf862013-09-23 17:35:16 -0600105 ['Active', 'sandbox', 'sandbox', '', 'Tester', 'Sandbox board', 'board4', ''],
Simon Glassc05694f2013-04-03 11:07:16 +0000106]
107
Simon Glass2e1646c2014-12-01 17:33:51 -0700108BASE_DIR = 'base'
109
Simon Glass071a1782018-11-06 16:02:13 -0700110OUTCOME_OK, OUTCOME_WARN, OUTCOME_ERR = range(3)
111
Simon Glassc05694f2013-04-03 11:07:16 +0000112class Options:
113 """Class that holds build options"""
114 pass
115
116class TestBuild(unittest.TestCase):
117 """Test buildman
118
119 TODO: Write tests for the rest of the functionality
120 """
121 def setUp(self):
122 # Set up commits to build
123 self.commits = []
124 sequence = 0
125 for commit_info in commits:
126 comm = commit.Commit(commit_info[0])
127 comm.subject = commit_info[1]
128 comm.return_code = commit_info[2]
129 comm.error_list = commit_info[3]
Simon Glassf4ebfba2020-04-09 15:08:53 -0600130 if sequence < 6:
131 comm.error_list += [migration]
Simon Glassc05694f2013-04-03 11:07:16 +0000132 comm.sequence = sequence
133 sequence += 1
134 self.commits.append(comm)
135
136 # Set up boards to build
Simon Glass20751d62022-07-11 19:04:03 -0600137 self.brds = boards.Boards()
Simon Glassb0786df2022-07-11 19:03:59 -0600138 for brd in BOARDS:
Simon Glass127a2392022-07-11 19:04:02 -0600139 self.brds.add_board(board.Board(*brd))
140 self.brds.select_boards([])
Simon Glassc05694f2013-04-03 11:07:16 +0000141
Simon Glass4bea81e2014-12-01 17:34:04 -0700142 # Add some test settings
Simon Glass06b83a52023-07-19 17:49:05 -0600143 bsettings.setup(None)
144 bsettings.add_file(settings_data)
Simon Glass4bea81e2014-12-01 17:34:04 -0700145
Simon Glassc05694f2013-04-03 11:07:16 +0000146 # Set up the toolchains
Simon Glassc05694f2013-04-03 11:07:16 +0000147 self.toolchains = toolchain.Toolchains()
148 self.toolchains.Add('arm-linux-gcc', test=False)
149 self.toolchains.Add('sparc-linux-gcc', test=False)
150 self.toolchains.Add('powerpc-linux-gcc', test=False)
151 self.toolchains.Add('gcc', test=False)
152
Simon Glass7fb8aa22014-09-05 19:00:08 -0600153 # Avoid sending any output
Simon Glass02811582022-01-29 14:14:18 -0700154 terminal.set_print_test_mode()
Simon Glass7fb8aa22014-09-05 19:00:08 -0600155 self._col = terminal.Color()
156
Simon Glass09bbfcd2020-04-09 15:08:31 -0600157 self.base_dir = tempfile.mkdtemp()
158 if not os.path.isdir(self.base_dir):
159 os.mkdir(self.base_dir)
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600160
Simon Glassc229d322024-06-23 11:55:15 -0600161 self.cur_time = 0
162 self.valid_pids = []
163 self.finish_time = None
164 self.finish_pid = None
165
Simon Glass09bbfcd2020-04-09 15:08:31 -0600166 def tearDown(self):
167 shutil.rmtree(self.base_dir)
168
169 def Make(self, commit, brd, stage, *args, **kwargs):
Simon Glassc05694f2013-04-03 11:07:16 +0000170 result = command.CommandResult()
171 boardnum = int(brd.target[-1])
172 result.return_code = 0
173 result.stderr = ''
174 result.stdout = ('This is the test output for board %s, commit %s' %
175 (brd.target, commit.hash))
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600176 if ((boardnum >= 1 and boardnum >= commit.sequence) or
177 boardnum == 4 and commit.sequence == 6):
Simon Glassc05694f2013-04-03 11:07:16 +0000178 result.return_code = commit.return_code
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600179 result.stderr = (''.join(commit.error_list)
Simon Glass09bbfcd2020-04-09 15:08:31 -0600180 % {'basedir' : self.base_dir + '/.bm-work/00/'})
Simon Glassf4ebfba2020-04-09 15:08:53 -0600181 elif commit.sequence < 6:
182 result.stderr = migration
Simon Glassc05694f2013-04-03 11:07:16 +0000183
184 result.combined = result.stdout + result.stderr
185 return result
186
Simon Glassb0786df2022-07-11 19:03:59 -0600187 def assertSummary(self, text, arch, plus, brds, outcome=OUTCOME_ERR):
Simon Glass7fb8aa22014-09-05 19:00:08 -0600188 col = self._col
Simon Glass071a1782018-11-06 16:02:13 -0700189 expected_colour = (col.GREEN if outcome == OUTCOME_OK else
190 col.YELLOW if outcome == OUTCOME_WARN else col.RED)
Simon Glass7fb8aa22014-09-05 19:00:08 -0600191 expect = '%10s: ' % arch
192 # TODO(sjg@chromium.org): If plus is '', we shouldn't need this
Simon Glassf45d3742022-01-29 14:14:17 -0700193 expect += ' ' + col.build(expected_colour, plus)
Simon Glass7fb8aa22014-09-05 19:00:08 -0600194 expect += ' '
Simon Glassb0786df2022-07-11 19:03:59 -0600195 for brd in brds:
Simon Glass8132f982022-07-11 19:03:57 -0600196 expect += col.build(expected_colour, ' %s' % brd)
Simon Glass7fb8aa22014-09-05 19:00:08 -0600197 self.assertEqual(text, expect)
198
Simon Glassc635d892021-01-30 22:17:46 -0700199 def _SetupTest(self, echo_lines=False, threads=1, **kwdisplay_args):
Simon Glass724c1752020-04-09 15:08:32 -0600200 """Set up the test by running a build and summary
Simon Glass7fb8aa22014-09-05 19:00:08 -0600201
Simon Glass724c1752020-04-09 15:08:32 -0600202 Args:
203 echo_lines: True to echo lines to the terminal to aid test
204 development
Heinrich Schuchardt37153112024-04-19 13:37:46 +0200205 kwdisplay_args: Dict of arguments to pass to
Simon Glass724c1752020-04-09 15:08:32 -0600206 Builder.SetDisplayOptions()
207
208 Returns:
209 Iterator containing the output lines, each a PrintLine() object
Simon Glass7fb8aa22014-09-05 19:00:08 -0600210 """
Simon Glassc635d892021-01-30 22:17:46 -0700211 build = builder.Builder(self.toolchains, self.base_dir, None, threads,
212 2, checkout=False, show_unknown=False)
Simon Glassc05694f2013-04-03 11:07:16 +0000213 build.do_make = self.Make
Simon Glass127a2392022-07-11 19:04:02 -0600214 board_selected = self.brds.get_selected_dict()
Simon Glassc05694f2013-04-03 11:07:16 +0000215
Simon Glass071a1782018-11-06 16:02:13 -0700216 # Build the boards for the pre-defined commits and warnings/errors
217 # associated with each. This calls our Make() to inject the fake output.
Simon Glassbc74d942023-07-19 17:49:06 -0600218 build.build_boards(self.commits, board_selected, keep_outputs=False,
219 verbose=False)
Simon Glass02811582022-01-29 14:14:18 -0700220 lines = terminal.get_print_test_lines()
Simon Glass7fb8aa22014-09-05 19:00:08 -0600221 count = 0
222 for line in lines:
223 if line.text.strip():
224 count += 1
225
Simon Glass726ae812020-04-09 15:08:47 -0600226 # We should get two starting messages, an update for every commit built
227 # and a summary message
Simon Glassb0786df2022-07-11 19:03:59 -0600228 self.assertEqual(count, len(commits) * len(BOARDS) + 3)
Simon Glassbc74d942023-07-19 17:49:06 -0600229 build.set_display_options(**kwdisplay_args);
230 build.show_summary(self.commits, board_selected)
Simon Glass724c1752020-04-09 15:08:32 -0600231 if echo_lines:
Simon Glass02811582022-01-29 14:14:18 -0700232 terminal.echo_print_test_lines()
233 return iter(terminal.get_print_test_lines())
Simon Glass071a1782018-11-06 16:02:13 -0700234
Simon Glassf4ebfba2020-04-09 15:08:53 -0600235 def _CheckOutput(self, lines, list_error_boards=False,
236 filter_dtb_warnings=False,
237 filter_migration_warnings=False):
Simon Glass724c1752020-04-09 15:08:32 -0600238 """Check for expected output from the build summary
239
240 Args:
241 lines: Iterator containing the lines returned from the summary
Simon Glassdacbe1e2020-04-09 15:08:34 -0600242 list_error_boards: Adjust the check for output produced with the
243 --list-error-boards flag
Simon Glass9ea93812020-04-09 15:08:52 -0600244 filter_dtb_warnings: Adjust the check for output produced with the
245 --filter-dtb-warnings flag
Simon Glass724c1752020-04-09 15:08:32 -0600246 """
Simon Glassb0786df2022-07-11 19:03:59 -0600247 def add_line_prefix(prefix, brds, error_str, colour):
Simon Glass6643eb42020-04-09 15:08:33 -0600248 """Add a prefix to each line of a string
249
250 The training \n in error_str is removed before processing
251
252 Args:
253 prefix: String prefix to add
254 error_str: Error string containing the lines
Simon Glassea49f9b2020-04-09 15:08:37 -0600255 colour: Expected colour for the line. Note that the board list,
256 if present, always appears in magenta
Simon Glass6643eb42020-04-09 15:08:33 -0600257
258 Returns:
259 New string where each line has the prefix added
260 """
261 lines = error_str.strip().splitlines()
Simon Glassea49f9b2020-04-09 15:08:37 -0600262 new_lines = []
263 for line in lines:
Simon Glassb0786df2022-07-11 19:03:59 -0600264 if brds:
Simon Glassf45d3742022-01-29 14:14:17 -0700265 expect = self._col.build(colour, prefix + '(')
Simon Glassb0786df2022-07-11 19:03:59 -0600266 expect += self._col.build(self._col.MAGENTA, brds,
Simon Glassea49f9b2020-04-09 15:08:37 -0600267 bright=False)
Simon Glassf45d3742022-01-29 14:14:17 -0700268 expect += self._col.build(colour, ') %s' % line)
Simon Glassea49f9b2020-04-09 15:08:37 -0600269 else:
Simon Glassf45d3742022-01-29 14:14:17 -0700270 expect = self._col.build(colour, prefix + line)
Simon Glassea49f9b2020-04-09 15:08:37 -0600271 new_lines.append(expect)
Simon Glass6643eb42020-04-09 15:08:33 -0600272 return '\n'.join(new_lines)
273
Simon Glassf4ebfba2020-04-09 15:08:53 -0600274 col = terminal.Color()
275 boards01234 = ('board0 board1 board2 board3 board4'
276 if list_error_boards else '')
Simon Glass070589b2020-04-09 15:08:38 -0600277 boards1234 = 'board1 board2 board3 board4' if list_error_boards else ''
278 boards234 = 'board2 board3 board4' if list_error_boards else ''
279 boards34 = 'board3 board4' if list_error_boards else ''
Simon Glassdacbe1e2020-04-09 15:08:34 -0600280 boards4 = 'board4' if list_error_boards else ''
281
Simon Glassf4ebfba2020-04-09 15:08:53 -0600282 # Upstream commit: migration warnings only
Simon Glass575ce512020-04-09 15:08:30 -0600283 self.assertEqual(next(lines).text, '01: %s' % commits[0][1])
Simon Glass071a1782018-11-06 16:02:13 -0700284
Simon Glassf4ebfba2020-04-09 15:08:53 -0600285 if not filter_migration_warnings:
286 self.assertSummary(next(lines).text, 'arm', 'w+',
287 ['board0', 'board1'], outcome=OUTCOME_WARN)
288 self.assertSummary(next(lines).text, 'powerpc', 'w+',
289 ['board2', 'board3'], outcome=OUTCOME_WARN)
290 self.assertSummary(next(lines).text, 'sandbox', 'w+', ['board4'],
291 outcome=OUTCOME_WARN)
292
293 self.assertEqual(next(lines).text,
294 add_line_prefix('+', boards01234, migration, col.RED))
295
Simon Glass071a1782018-11-06 16:02:13 -0700296 # Second commit: all archs should fail with warnings
Simon Glass575ce512020-04-09 15:08:30 -0600297 self.assertEqual(next(lines).text, '02: %s' % commits[1][1])
Simon Glass7fb8aa22014-09-05 19:00:08 -0600298
Simon Glassf4ebfba2020-04-09 15:08:53 -0600299 if filter_migration_warnings:
300 self.assertSummary(next(lines).text, 'arm', 'w+',
301 ['board1'], outcome=OUTCOME_WARN)
302 self.assertSummary(next(lines).text, 'powerpc', 'w+',
303 ['board2', 'board3'], outcome=OUTCOME_WARN)
304 self.assertSummary(next(lines).text, 'sandbox', 'w+', ['board4'],
305 outcome=OUTCOME_WARN)
Simon Glass7fb8aa22014-09-05 19:00:08 -0600306
Simon Glass071a1782018-11-06 16:02:13 -0700307 # Second commit: The warnings should be listed
Simon Glassea49f9b2020-04-09 15:08:37 -0600308 self.assertEqual(next(lines).text,
309 add_line_prefix('w+', boards1234, errors[0], col.YELLOW))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600310
Simon Glass071a1782018-11-06 16:02:13 -0700311 # Third commit: Still fails
Simon Glass575ce512020-04-09 15:08:30 -0600312 self.assertEqual(next(lines).text, '03: %s' % commits[2][1])
Simon Glassf4ebfba2020-04-09 15:08:53 -0600313 if filter_migration_warnings:
314 self.assertSummary(next(lines).text, 'arm', '',
315 ['board1'], outcome=OUTCOME_OK)
Simon Glass575ce512020-04-09 15:08:30 -0600316 self.assertSummary(next(lines).text, 'powerpc', '+',
317 ['board2', 'board3'])
318 self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
Simon Glass7fb8aa22014-09-05 19:00:08 -0600319
Simon Glass071a1782018-11-06 16:02:13 -0700320 # Expect a compiler error
Simon Glassea49f9b2020-04-09 15:08:37 -0600321 self.assertEqual(next(lines).text,
322 add_line_prefix('+', boards234, errors[1], col.RED))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600323
Simon Glass071a1782018-11-06 16:02:13 -0700324 # Fourth commit: Compile errors are fixed, just have warning for board3
Simon Glass575ce512020-04-09 15:08:30 -0600325 self.assertEqual(next(lines).text, '04: %s' % commits[3][1])
Simon Glassf4ebfba2020-04-09 15:08:53 -0600326 if filter_migration_warnings:
327 expect = '%10s: ' % 'powerpc'
Simon Glassf45d3742022-01-29 14:14:17 -0700328 expect += ' ' + col.build(col.GREEN, '')
Simon Glassf4ebfba2020-04-09 15:08:53 -0600329 expect += ' '
Simon Glassf45d3742022-01-29 14:14:17 -0700330 expect += col.build(col.GREEN, ' %s' % 'board2')
331 expect += ' ' + col.build(col.YELLOW, 'w+')
Simon Glassf4ebfba2020-04-09 15:08:53 -0600332 expect += ' '
Simon Glassf45d3742022-01-29 14:14:17 -0700333 expect += col.build(col.YELLOW, ' %s' % 'board3')
Simon Glassf4ebfba2020-04-09 15:08:53 -0600334 self.assertEqual(next(lines).text, expect)
335 else:
336 self.assertSummary(next(lines).text, 'powerpc', 'w+',
337 ['board2', 'board3'], outcome=OUTCOME_WARN)
Simon Glass575ce512020-04-09 15:08:30 -0600338 self.assertSummary(next(lines).text, 'sandbox', 'w+', ['board4'],
Simon Glassf4ebfba2020-04-09 15:08:53 -0600339 outcome=OUTCOME_WARN)
Simon Glass7fb8aa22014-09-05 19:00:08 -0600340
341 # Compile error fixed
Simon Glassea49f9b2020-04-09 15:08:37 -0600342 self.assertEqual(next(lines).text,
343 add_line_prefix('-', boards234, errors[1], col.GREEN))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600344
Simon Glass9ea93812020-04-09 15:08:52 -0600345 if not filter_dtb_warnings:
346 self.assertEqual(
347 next(lines).text,
348 add_line_prefix('w+', boards34, errors[2], col.YELLOW))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600349
Simon Glass071a1782018-11-06 16:02:13 -0700350 # Fifth commit
Simon Glass575ce512020-04-09 15:08:30 -0600351 self.assertEqual(next(lines).text, '05: %s' % commits[4][1])
Simon Glassf4ebfba2020-04-09 15:08:53 -0600352 if filter_migration_warnings:
353 self.assertSummary(next(lines).text, 'powerpc', '', ['board3'],
354 outcome=OUTCOME_OK)
Simon Glass575ce512020-04-09 15:08:30 -0600355 self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
Simon Glass7fb8aa22014-09-05 19:00:08 -0600356
357 # The second line of errors[3] is a duplicate, so buildman will drop it
358 expect = errors[3].rstrip().split('\n')
359 expect = [expect[0]] + expect[2:]
Simon Glass6643eb42020-04-09 15:08:33 -0600360 expect = '\n'.join(expect)
Simon Glassea49f9b2020-04-09 15:08:37 -0600361 self.assertEqual(next(lines).text,
362 add_line_prefix('+', boards4, expect, col.RED))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600363
Simon Glass9ea93812020-04-09 15:08:52 -0600364 if not filter_dtb_warnings:
365 self.assertEqual(
366 next(lines).text,
367 add_line_prefix('w-', boards34, errors[2], col.CYAN))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600368
Simon Glass071a1782018-11-06 16:02:13 -0700369 # Sixth commit
Simon Glass575ce512020-04-09 15:08:30 -0600370 self.assertEqual(next(lines).text, '06: %s' % commits[5][1])
Simon Glassf4ebfba2020-04-09 15:08:53 -0600371 if filter_migration_warnings:
372 self.assertSummary(next(lines).text, 'sandbox', '', ['board4'],
373 outcome=OUTCOME_OK)
374 else:
375 self.assertSummary(next(lines).text, 'sandbox', 'w+', ['board4'],
376 outcome=OUTCOME_WARN)
Simon Glass7fb8aa22014-09-05 19:00:08 -0600377
378 # The second line of errors[3] is a duplicate, so buildman will drop it
379 expect = errors[3].rstrip().split('\n')
380 expect = [expect[0]] + expect[2:]
Simon Glass6643eb42020-04-09 15:08:33 -0600381 expect = '\n'.join(expect)
Simon Glassea49f9b2020-04-09 15:08:37 -0600382 self.assertEqual(next(lines).text,
383 add_line_prefix('-', boards4, expect, col.GREEN))
384 self.assertEqual(next(lines).text,
385 add_line_prefix('w-', boards4, errors[0], col.CYAN))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600386
Simon Glass071a1782018-11-06 16:02:13 -0700387 # Seventh commit
Simon Glass575ce512020-04-09 15:08:30 -0600388 self.assertEqual(next(lines).text, '07: %s' % commits[6][1])
Simon Glassf4ebfba2020-04-09 15:08:53 -0600389 if filter_migration_warnings:
390 self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
391 else:
392 self.assertSummary(next(lines).text, 'arm', '', ['board0', 'board1'],
393 outcome=OUTCOME_OK)
394 self.assertSummary(next(lines).text, 'powerpc', '',
395 ['board2', 'board3'], outcome=OUTCOME_OK)
396 self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600397
398 # Pick out the correct error lines
399 expect_str = errors[4].rstrip().replace('%(basedir)s', '').split('\n')
400 expect = expect_str[3:8] + [expect_str[-1]]
Simon Glass6643eb42020-04-09 15:08:33 -0600401 expect = '\n'.join(expect)
Simon Glassf4ebfba2020-04-09 15:08:53 -0600402 if not filter_migration_warnings:
403 self.assertEqual(
404 next(lines).text,
405 add_line_prefix('-', boards01234, migration, col.GREEN))
406
Simon Glassea49f9b2020-04-09 15:08:37 -0600407 self.assertEqual(next(lines).text,
408 add_line_prefix('+', boards4, expect, col.RED))
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600409
410 # Now the warnings lines
411 expect = [expect_str[0]] + expect_str[10:12] + [expect_str[9]]
Simon Glass6643eb42020-04-09 15:08:33 -0600412 expect = '\n'.join(expect)
Simon Glassea49f9b2020-04-09 15:08:37 -0600413 self.assertEqual(next(lines).text,
414 add_line_prefix('w+', boards4, expect, col.YELLOW))
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600415
Simon Glass724c1752020-04-09 15:08:32 -0600416 def testOutput(self):
417 """Test basic builder operation and output
418
419 This does a line-by-line verification of the summary output.
420 """
421 lines = self._SetupTest(show_errors=True)
Simon Glass9ea93812020-04-09 15:08:52 -0600422 self._CheckOutput(lines, list_error_boards=False,
423 filter_dtb_warnings=False)
Simon Glassdacbe1e2020-04-09 15:08:34 -0600424
425 def testErrorBoards(self):
426 """Test output with --list-error-boards
427
428 This does a line-by-line verification of the summary output.
429 """
430 lines = self._SetupTest(show_errors=True, list_error_boards=True)
Simon Glassf4ebfba2020-04-09 15:08:53 -0600431 self._CheckOutput(lines, list_error_boards=True)
Simon Glass9ea93812020-04-09 15:08:52 -0600432
433 def testFilterDtb(self):
434 """Test output with --filter-dtb-warnings
435
436 This does a line-by-line verification of the summary output.
437 """
438 lines = self._SetupTest(show_errors=True, filter_dtb_warnings=True)
Simon Glassf4ebfba2020-04-09 15:08:53 -0600439 self._CheckOutput(lines, filter_dtb_warnings=True)
440
441 def testFilterMigration(self):
442 """Test output with --filter-migration-warnings
443
444 This does a line-by-line verification of the summary output.
445 """
446 lines = self._SetupTest(show_errors=True,
447 filter_migration_warnings=True)
448 self._CheckOutput(lines, filter_migration_warnings=True)
Simon Glass724c1752020-04-09 15:08:32 -0600449
Simon Glassc635d892021-01-30 22:17:46 -0700450 def testSingleThread(self):
451 """Test operation without threading"""
452 lines = self._SetupTest(show_errors=True, threads=0)
453 self._CheckOutput(lines, list_error_boards=False,
454 filter_dtb_warnings=False)
455
Simon Glassc05694f2013-04-03 11:07:16 +0000456 def _testGit(self):
457 """Test basic builder operation by building a branch"""
Simon Glassc05694f2013-04-03 11:07:16 +0000458 options = Options()
459 options.git = os.getcwd()
460 options.summary = False
461 options.jobs = None
462 options.dry_run = False
Simon Glass09bbfcd2020-04-09 15:08:31 -0600463 #options.git = os.path.join(self.base_dir, 'repo')
Simon Glassc05694f2013-04-03 11:07:16 +0000464 options.branch = 'test-buildman'
465 options.force_build = False
466 options.list_tool_chains = False
467 options.count = -1
468 options.git_dir = None
469 options.threads = None
470 options.show_unknown = False
471 options.quick = False
472 options.show_errors = False
473 options.keep_outputs = False
474 args = ['tegra20']
Simon Glassc1e1e1d2023-07-19 17:48:30 -0600475 control.do_buildman(options, args)
Simon Glassc05694f2013-04-03 11:07:16 +0000476
Simon Glassaa40f9a2014-08-09 15:33:08 -0600477 def testBoardSingle(self):
478 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600479 self.assertEqual(self.brds.select_boards(['sandbox']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600480 ({'all': ['board4'], 'sandbox': ['board4']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600481
482 def testBoardArch(self):
483 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600484 self.assertEqual(self.brds.select_boards(['arm']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600485 ({'all': ['board0', 'board1'],
486 'arm': ['board0', 'board1']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600487
488 def testBoardArchSingle(self):
489 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600490 self.assertEqual(self.brds.select_boards(['arm sandbox']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600491 ({'sandbox': ['board4'],
Simon Glass5e0f06d2017-11-12 21:52:15 -0700492 'all': ['board0', 'board1', 'board4'],
Simon Glassd9eb9f02018-06-11 23:26:46 -0600493 'arm': ['board0', 'board1']}, []))
Simon Glass5e0f06d2017-11-12 21:52:15 -0700494
Simon Glassaa40f9a2014-08-09 15:33:08 -0600495
496 def testBoardArchSingleMultiWord(self):
497 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600498 self.assertEqual(self.brds.select_boards(['arm', 'sandbox']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600499 ({'sandbox': ['board4'],
500 'all': ['board0', 'board1', 'board4'],
501 'arm': ['board0', 'board1']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600502
503 def testBoardSingleAnd(self):
504 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600505 self.assertEqual(self.brds.select_boards(['Tester & arm']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600506 ({'Tester&arm': ['board0', 'board1'],
507 'all': ['board0', 'board1']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600508
509 def testBoardTwoAnd(self):
510 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600511 self.assertEqual(self.brds.select_boards(['Tester', '&', 'arm',
Simon Glassaa40f9a2014-08-09 15:33:08 -0600512 'Tester' '&', 'powerpc',
513 'sandbox']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600514 ({'sandbox': ['board4'],
Simon Glass5e0f06d2017-11-12 21:52:15 -0700515 'all': ['board0', 'board1', 'board2', 'board3',
516 'board4'],
517 'Tester&powerpc': ['board2', 'board3'],
Simon Glassd9eb9f02018-06-11 23:26:46 -0600518 'Tester&arm': ['board0', 'board1']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600519
520 def testBoardAll(self):
521 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600522 self.assertEqual(self.brds.select_boards([]),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600523 ({'all': ['board0', 'board1', 'board2', 'board3',
524 'board4']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600525
526 def testBoardRegularExpression(self):
527 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600528 self.assertEqual(self.brds.select_boards(['T.*r&^Po']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600529 ({'all': ['board2', 'board3'],
530 'T.*r&^Po': ['board2', 'board3']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600531
532 def testBoardDuplicate(self):
533 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600534 self.assertEqual(self.brds.select_boards(['sandbox sandbox',
Simon Glassaa40f9a2014-08-09 15:33:08 -0600535 'sandbox']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600536 ({'all': ['board4'], 'sandbox': ['board4']}, []))
Simon Glass2e1646c2014-12-01 17:33:51 -0700537 def CheckDirs(self, build, dirname):
Simon Glass4cb54682023-07-19 17:49:10 -0600538 self.assertEqual('base%s' % dirname, build.get_output_dir(1))
Simon Glass2e1646c2014-12-01 17:33:51 -0700539 self.assertEqual('base%s/fred' % dirname,
Simon Glassbc74d942023-07-19 17:49:06 -0600540 build.get_build_dir(1, 'fred'))
Simon Glass2e1646c2014-12-01 17:33:51 -0700541 self.assertEqual('base%s/fred/done' % dirname,
Simon Glassbc74d942023-07-19 17:49:06 -0600542 build.get_done_file(1, 'fred'))
Simon Glass2e1646c2014-12-01 17:33:51 -0700543 self.assertEqual('base%s/fred/u-boot.sizes' % dirname,
Simon Glassbc74d942023-07-19 17:49:06 -0600544 build.get_func_sizes_file(1, 'fred', 'u-boot'))
Simon Glass2e1646c2014-12-01 17:33:51 -0700545 self.assertEqual('base%s/fred/u-boot.objdump' % dirname,
Simon Glassbc74d942023-07-19 17:49:06 -0600546 build.get_objdump_file(1, 'fred', 'u-boot'))
Simon Glass2e1646c2014-12-01 17:33:51 -0700547 self.assertEqual('base%s/fred/err' % dirname,
Simon Glassbc74d942023-07-19 17:49:06 -0600548 build.get_err_file(1, 'fred'))
Simon Glass2e1646c2014-12-01 17:33:51 -0700549
550 def testOutputDir(self):
551 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
552 checkout=False, show_unknown=False)
553 build.commits = self.commits
554 build.commit_count = len(self.commits)
555 subject = self.commits[1].subject.translate(builder.trans_valid_chars)
Simon Glass36971712020-07-19 12:28:11 -0600556 dirname ='/%02d_g%s_%s' % (2, commits[1][0], subject[:20])
Simon Glass2e1646c2014-12-01 17:33:51 -0700557 self.CheckDirs(build, dirname)
558
559 def testOutputDirCurrent(self):
560 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
561 checkout=False, show_unknown=False)
562 build.commits = None
563 build.commit_count = 0
564 self.CheckDirs(build, '/current')
Simon Glassaa40f9a2014-08-09 15:33:08 -0600565
Simon Glasse87bde12014-12-01 17:33:55 -0700566 def testOutputDirNoSubdirs(self):
567 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
568 checkout=False, show_unknown=False,
569 no_subdirs=True)
570 build.commits = None
571 build.commit_count = 0
572 self.CheckDirs(build, '')
573
Simon Glassc1528c12014-12-01 17:34:05 -0700574 def testToolchainAliases(self):
575 self.assertTrue(self.toolchains.Select('arm') != None)
576 with self.assertRaises(ValueError):
577 self.toolchains.Select('no-arch')
578 with self.assertRaises(ValueError):
579 self.toolchains.Select('x86')
580
581 self.toolchains = toolchain.Toolchains()
582 self.toolchains.Add('x86_64-linux-gcc', test=False)
583 self.assertTrue(self.toolchains.Select('x86') != None)
584
585 self.toolchains = toolchain.Toolchains()
586 self.toolchains.Add('i386-linux-gcc', test=False)
587 self.assertTrue(self.toolchains.Select('x86') != None)
588
Simon Glass7e803e12014-12-01 17:34:06 -0700589 def testToolchainDownload(self):
590 """Test that we can download toolchains"""
Simon Glass2bfc6972017-11-12 21:52:14 -0700591 if use_network:
Simon Glass04150022018-10-01 21:12:43 -0600592 with test_util.capture_sys_output() as (stdout, stderr):
593 url = self.toolchains.LocateArchUrl('arm')
Brandon Maiera657bc62024-06-04 16:16:05 +0000594 self.assertRegex(url, 'https://www.kernel.org/pub/tools/'
Simon Glass72e358c2018-10-01 21:12:35 -0600595 'crosstool/files/bin/x86_64/.*/'
Simon Glassdd65c5f2020-04-17 17:51:30 -0600596 'x86_64-gcc-.*-nolibc[-_]arm-.*linux-gnueabi.tar.xz')
Simon Glass7e803e12014-12-01 17:34:06 -0700597
Simon Glass48ac42e2019-12-05 15:59:14 -0700598 def testGetEnvArgs(self):
599 """Test the GetEnvArgs() function"""
600 tc = self.toolchains.Select('arm')
601 self.assertEqual('arm-linux-',
602 tc.GetEnvArgs(toolchain.VAR_CROSS_COMPILE))
603 self.assertEqual('', tc.GetEnvArgs(toolchain.VAR_PATH))
604 self.assertEqual('arm',
605 tc.GetEnvArgs(toolchain.VAR_ARCH))
606 self.assertEqual('', tc.GetEnvArgs(toolchain.VAR_MAKE_ARGS))
607
608 self.toolchains.Add('/path/to/x86_64-linux-gcc', test=False)
609 tc = self.toolchains.Select('x86')
610 self.assertEqual('/path/to',
611 tc.GetEnvArgs(toolchain.VAR_PATH))
612 tc.override_toolchain = 'clang'
613 self.assertEqual('HOSTCC=clang CC=clang',
614 tc.GetEnvArgs(toolchain.VAR_MAKE_ARGS))
615
Simon Glass5dc1ca72020-03-18 09:42:45 -0600616 def testPrepareOutputSpace(self):
617 def _Touch(fname):
Simon Glass80025522022-01-29 14:14:04 -0700618 tools.write_file(os.path.join(base_dir, fname), b'')
Simon Glass5dc1ca72020-03-18 09:42:45 -0600619
620 base_dir = tempfile.mkdtemp()
621
622 # Add various files that we want removed and left alone
Ovidiu Panaitee8e9cb2020-05-15 09:30:12 +0300623 to_remove = ['01_g0982734987_title', '102_g92bf_title',
624 '01_g2938abd8_title']
625 to_leave = ['something_else', '01-something.patch', '01_another']
Simon Glass5dc1ca72020-03-18 09:42:45 -0600626 for name in to_remove + to_leave:
627 _Touch(name)
628
629 build = builder.Builder(self.toolchains, base_dir, None, 1, 2)
630 build.commits = self.commits
631 build.commit_count = len(commits)
Simon Glassbc74d942023-07-19 17:49:06 -0600632 result = set(build._get_output_space_removals())
Simon Glass5dc1ca72020-03-18 09:42:45 -0600633 expected = set([os.path.join(base_dir, f) for f in to_remove])
634 self.assertEqual(expected, result)
Simon Glass7e803e12014-12-01 17:34:06 -0700635
Simon Glass22901f92022-01-22 05:07:31 -0700636 def test_adjust_cfg_nop(self):
637 """check various adjustments of config that are nops"""
638 # enable an enabled CONFIG
639 self.assertEqual(
640 'CONFIG_FRED=y',
641 cfgutil.adjust_cfg_line('CONFIG_FRED=y', {'FRED':'FRED'})[0])
642
643 # disable a disabled CONFIG
644 self.assertEqual(
645 '# CONFIG_FRED is not set',
646 cfgutil.adjust_cfg_line(
647 '# CONFIG_FRED is not set', {'FRED':'~FRED'})[0])
648
649 # use the adjust_cfg_lines() function
650 self.assertEqual(
651 ['CONFIG_FRED=y'],
652 cfgutil.adjust_cfg_lines(['CONFIG_FRED=y'], {'FRED':'FRED'}))
653 self.assertEqual(
654 ['# CONFIG_FRED is not set'],
655 cfgutil.adjust_cfg_lines(['CONFIG_FRED=y'], {'FRED':'~FRED'}))
656
657 # handling an empty line
658 self.assertEqual('#', cfgutil.adjust_cfg_line('#', {'FRED':'~FRED'})[0])
659
660 def test_adjust_cfg(self):
661 """check various adjustments of config"""
662 # disable a CONFIG
663 self.assertEqual(
664 '# CONFIG_FRED is not set',
665 cfgutil.adjust_cfg_line('CONFIG_FRED=1' , {'FRED':'~FRED'})[0])
666
667 # enable a disabled CONFIG
668 self.assertEqual(
669 'CONFIG_FRED=y',
670 cfgutil.adjust_cfg_line(
671 '# CONFIG_FRED is not set', {'FRED':'FRED'})[0])
672
673 # enable a CONFIG that doesn't exist
674 self.assertEqual(
675 ['CONFIG_FRED=y'],
676 cfgutil.adjust_cfg_lines([], {'FRED':'FRED'}))
677
678 # disable a CONFIG that doesn't exist
679 self.assertEqual(
680 ['# CONFIG_FRED is not set'],
681 cfgutil.adjust_cfg_lines([], {'FRED':'~FRED'}))
682
683 # disable a value CONFIG
684 self.assertEqual(
685 '# CONFIG_FRED is not set',
686 cfgutil.adjust_cfg_line('CONFIG_FRED="fred"' , {'FRED':'~FRED'})[0])
687
688 # setting a value CONFIG
689 self.assertEqual(
690 'CONFIG_FRED="fred"',
691 cfgutil.adjust_cfg_line('# CONFIG_FRED is not set' ,
692 {'FRED':'FRED="fred"'})[0])
693
694 # changing a value CONFIG
695 self.assertEqual(
696 'CONFIG_FRED="fred"',
697 cfgutil.adjust_cfg_line('CONFIG_FRED="ernie"' ,
698 {'FRED':'FRED="fred"'})[0])
699
700 # setting a value for a CONFIG that doesn't exist
701 self.assertEqual(
702 ['CONFIG_FRED="fred"'],
703 cfgutil.adjust_cfg_lines([], {'FRED':'FRED="fred"'}))
704
705 def test_convert_adjust_cfg_list(self):
706 """Check conversion of the list of changes into a dict"""
707 self.assertEqual({}, cfgutil.convert_list_to_dict(None))
708
709 expect = {
710 'FRED':'FRED',
711 'MARY':'~MARY',
712 'JOHN':'JOHN=0x123',
713 'ALICE':'ALICE="alice"',
714 'AMY':'AMY',
715 'ABE':'~ABE',
716 'MARK':'MARK=0x456',
717 'ANNA':'ANNA="anna"',
718 }
719 actual = cfgutil.convert_list_to_dict(
720 ['FRED', '~MARY', 'JOHN=0x123', 'ALICE="alice"',
721 'CONFIG_AMY', '~CONFIG_ABE', 'CONFIG_MARK=0x456',
722 'CONFIG_ANNA="anna"'])
723 self.assertEqual(expect, actual)
724
725 def test_check_cfg_file(self):
726 """Test check_cfg_file detects conflicts as expected"""
727 # Check failure to disable CONFIG
728 result = cfgutil.check_cfg_lines(['CONFIG_FRED=1'], {'FRED':'~FRED'})
729 self.assertEqual([['~FRED', 'CONFIG_FRED=1']], result)
730
731 result = cfgutil.check_cfg_lines(
732 ['CONFIG_FRED=1', 'CONFIG_MARY="mary"'], {'FRED':'~FRED'})
733 self.assertEqual([['~FRED', 'CONFIG_FRED=1']], result)
734
735 result = cfgutil.check_cfg_lines(
736 ['CONFIG_FRED=1', 'CONFIG_MARY="mary"'], {'MARY':'~MARY'})
737 self.assertEqual([['~MARY', 'CONFIG_MARY="mary"']], result)
738
739 # Check failure to enable CONFIG
740 result = cfgutil.check_cfg_lines(
741 ['# CONFIG_FRED is not set'], {'FRED':'FRED'})
742 self.assertEqual([['FRED', '# CONFIG_FRED is not set']], result)
743
744 # Check failure to set CONFIG value
745 result = cfgutil.check_cfg_lines(
746 ['# CONFIG_FRED is not set', 'CONFIG_MARY="not"'],
747 {'MARY':'MARY="mary"', 'FRED':'FRED'})
748 self.assertEqual([
749 ['FRED', '# CONFIG_FRED is not set'],
750 ['MARY="mary"', 'CONFIG_MARY="not"']], result)
751
752 # Check failure to add CONFIG value
753 result = cfgutil.check_cfg_lines([], {'MARY':'MARY="mary"'})
754 self.assertEqual([
755 ['MARY="mary"', 'Missing expected line: CONFIG_MARY="mary"']], result)
756
Simon Glassc229d322024-06-23 11:55:15 -0600757 def get_procs(self):
758 running_fname = os.path.join(self.base_dir, control.RUNNING_FNAME)
759 items = tools.read_file(running_fname, binary=False).split()
760 return [int(x) for x in items]
761
762 def get_time(self):
763 return self.cur_time
764
765 def inc_time(self, amount):
766 self.cur_time += amount
767
768 # Handle a process exiting
769 if self.finish_time == self.cur_time:
770 self.valid_pids = [pid for pid in self.valid_pids
771 if pid != self.finish_pid]
772
773 def kill(self, pid, signal):
774 if pid not in self.valid_pids:
775 raise OSError('Invalid PID')
776
777 def test_process_limit(self):
778 """Test wait_for_process_limit() function"""
779 tmpdir = self.base_dir
780
781 with (patch('time.time', side_effect=self.get_time),
782 patch('time.sleep', side_effect=self.inc_time),
783 patch('os.kill', side_effect=self.kill)):
784 # Grab the process. Since there is no other profcess, this should
785 # immediately succeed
786 control.wait_for_process_limit(1, tmpdir=tmpdir, pid=1)
787 lines = terminal.get_print_test_lines()
788 self.assertEqual(0, self.cur_time)
789 self.assertEqual('Waiting for other buildman processes...',
790 lines[0].text)
791 self.assertEqual(self._col.RED, lines[0].colour)
792 self.assertEqual(False, lines[0].newline)
793 self.assertEqual(True, lines[0].bright)
794
795 self.assertEqual('done...', lines[1].text)
796 self.assertEqual(None, lines[1].colour)
797 self.assertEqual(False, lines[1].newline)
798 self.assertEqual(True, lines[1].bright)
799
800 self.assertEqual('starting build', lines[2].text)
801 self.assertEqual([1], control.read_procs(tmpdir))
802 self.assertEqual(None, lines[2].colour)
803 self.assertEqual(False, lines[2].newline)
804 self.assertEqual(True, lines[2].bright)
805
806 # Try again, with a different PID...this should eventually timeout
807 # and start the build anyway
808 self.cur_time = 0
809 self.valid_pids = [1]
810 control.wait_for_process_limit(1, tmpdir=tmpdir, pid=2)
811 lines = terminal.get_print_test_lines()
812 self.assertEqual('Waiting for other buildman processes...',
813 lines[0].text)
814 self.assertEqual('timeout...', lines[1].text)
815 self.assertEqual(None, lines[1].colour)
816 self.assertEqual(False, lines[1].newline)
817 self.assertEqual(True, lines[1].bright)
818 self.assertEqual('starting build', lines[2].text)
819 self.assertEqual([1, 2], control.read_procs(tmpdir))
820 self.assertEqual(control.RUN_WAIT_S, self.cur_time)
821
822 # Check lock-busting
823 self.cur_time = 0
824 self.valid_pids = [1, 2]
825 lock_fname = os.path.join(tmpdir, control.LOCK_FNAME)
826 lock = FileLock(lock_fname)
827 lock.acquire(timeout=1)
828 control.wait_for_process_limit(1, tmpdir=tmpdir, pid=3)
829 lines = terminal.get_print_test_lines()
830 self.assertEqual('Waiting for other buildman processes...',
831 lines[0].text)
832 self.assertEqual('failed to get lock: busting...', lines[1].text)
833 self.assertEqual(None, lines[1].colour)
834 self.assertEqual(False, lines[1].newline)
835 self.assertEqual(True, lines[1].bright)
836 self.assertEqual('timeout...', lines[2].text)
837 self.assertEqual('starting build', lines[3].text)
838 self.assertEqual([1, 2, 3], control.read_procs(tmpdir))
839 self.assertEqual(control.RUN_WAIT_S, self.cur_time)
840 lock.release()
841
842 # Check handling of dead processes. Here we have PID 2 as a running
843 # process, even though the PID file contains 1, 2 and 3. So we can
844 # add one more PID, to make 2 and 4
845 self.cur_time = 0
846 self.valid_pids = [2]
847 control.wait_for_process_limit(2, tmpdir=tmpdir, pid=4)
848 lines = terminal.get_print_test_lines()
849 self.assertEqual('Waiting for other buildman processes...',
850 lines[0].text)
851 self.assertEqual('done...', lines[1].text)
852 self.assertEqual('starting build', lines[2].text)
853 self.assertEqual([2, 4], control.read_procs(tmpdir))
854 self.assertEqual(0, self.cur_time)
855
856 # Try again, with PID 2 quitting at time 50. This allows the new
857 # build to start
858 self.cur_time = 0
859 self.valid_pids = [2, 4]
860 self.finish_pid = 2
861 self.finish_time = 50
862 control.wait_for_process_limit(2, tmpdir=tmpdir, pid=5)
863 lines = terminal.get_print_test_lines()
864 self.assertEqual('Waiting for other buildman processes...',
865 lines[0].text)
866 self.assertEqual('done...', lines[1].text)
867 self.assertEqual('starting build', lines[2].text)
868 self.assertEqual([4, 5], control.read_procs(tmpdir))
869 self.assertEqual(self.finish_time, self.cur_time)
870
Simon Glass22901f92022-01-22 05:07:31 -0700871
Simon Glassc05694f2013-04-03 11:07:16 +0000872if __name__ == "__main__":
873 unittest.main()