blob: 15801f6097f4b6e4534e40194f4dd808b168df48 [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
Jerome Forissieraa276952024-09-11 11:58:14 +020039settings_data_wrapper = '''
40# Buildman settings file
41
42[toolchain]
43main: /usr/sbin
44
45[toolchain-wrapper]
46wrapper = ccache
47'''
48
Simon Glassf4ebfba2020-04-09 15:08:53 -060049migration = '''===================== WARNING ======================
50This board does not use CONFIG_DM. CONFIG_DM will be
51compulsory starting with the v2020.01 release.
52Failure to update may result in board removal.
Johannes Krottmayerce53c8b2022-03-01 04:49:51 +010053See doc/develop/driver-model/migration.rst for more info.
Simon Glassf4ebfba2020-04-09 15:08:53 -060054====================================================
55'''
56
Simon Glassc05694f2013-04-03 11:07:16 +000057errors = [
58 '''main.c: In function 'main_loop':
59main.c:260:6: warning: unused variable 'joe' [-Wunused-variable]
60''',
Simon Glass7fb8aa22014-09-05 19:00:08 -060061 '''main.c: In function 'main_loop2':
Simon Glassc05694f2013-04-03 11:07:16 +000062main.c:295:2: error: 'fred' undeclared (first use in this function)
63main.c:295:2: note: each undeclared identifier is reported only once for each function it appears in
64make[1]: *** [main.o] Error 1
65make: *** [common/libcommon.o] Error 2
66Make failed
67''',
Simon Glass0db94432018-11-06 16:02:11 -070068 '''arch/arm/dts/socfpga_arria10_socdk_sdmmc.dtb: Warning \
69(avoid_unnecessary_addr_size): /clocks: unnecessary #address-cells/#size-cells \
70without "ranges" or child "reg" property
Simon Glassc05694f2013-04-03 11:07:16 +000071''',
72 '''powerpc-linux-ld: warning: dot moved backwards before `.bss'
73powerpc-linux-ld: warning: dot moved backwards before `.bss'
74powerpc-linux-ld: u-boot: section .text lma 0xfffc0000 overlaps previous sections
75powerpc-linux-ld: u-boot: section .rodata lma 0xfffef3ec overlaps previous sections
76powerpc-linux-ld: u-boot: section .reloc lma 0xffffa400 overlaps previous sections
77powerpc-linux-ld: u-boot: section .data lma 0xffffcd38 overlaps previous sections
78powerpc-linux-ld: u-boot: section .u_boot_cmd lma 0xffffeb40 overlaps previous sections
79powerpc-linux-ld: u-boot: section .bootpg lma 0xfffff198 overlaps previous sections
Simon Glassa8b7b1b2014-09-05 19:00:21 -060080''',
81 '''In file included from %(basedir)sarch/sandbox/cpu/cpu.c:9:0:
82%(basedir)sarch/sandbox/include/asm/state.h:44:0: warning: "xxxx" redefined [enabled by default]
83%(basedir)sarch/sandbox/include/asm/state.h:43:0: note: this is the location of the previous definition
84%(basedir)sarch/sandbox/cpu/cpu.c: In function 'do_reset':
85%(basedir)sarch/sandbox/cpu/cpu.c:27:1: error: unknown type name 'blah'
86%(basedir)sarch/sandbox/cpu/cpu.c:28:12: error: expected declaration specifiers or '...' before numeric constant
87make[2]: *** [arch/sandbox/cpu/cpu.o] Error 1
88make[1]: *** [arch/sandbox/cpu] Error 2
89make[1]: *** Waiting for unfinished jobs....
90In file included from %(basedir)scommon/board_f.c:55:0:
91%(basedir)sarch/sandbox/include/asm/state.h:44:0: warning: "xxxx" redefined [enabled by default]
92%(basedir)sarch/sandbox/include/asm/state.h:43:0: note: this is the location of the previous definition
93make: *** [sub-make] Error 2
Simon Glassc05694f2013-04-03 11:07:16 +000094'''
95]
96
97
98# hash, subject, return code, list of errors/warnings
99commits = [
Simon Glassf4ebfba2020-04-09 15:08:53 -0600100 ['1234', 'upstream/master, migration warning', 0, []],
Simon Glassc05694f2013-04-03 11:07:16 +0000101 ['5678', 'Second commit, a warning', 0, errors[0:1]],
102 ['9012', 'Third commit, error', 1, errors[0:2]],
103 ['3456', 'Fourth commit, warning', 0, [errors[0], errors[2]]],
104 ['7890', 'Fifth commit, link errors', 1, [errors[0], errors[3]]],
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600105 ['abcd', 'Sixth commit, fixes all errors', 0, []],
Simon Glassf4ebfba2020-04-09 15:08:53 -0600106 ['ef01', 'Seventh commit, fix migration, check directory suppression', 1,
107 [errors[4]]],
Simon Glassc05694f2013-04-03 11:07:16 +0000108]
109
Simon Glassb0786df2022-07-11 19:03:59 -0600110BOARDS = [
Simon Glass584cf862013-09-23 17:35:16 -0600111 ['Active', 'arm', 'armv7', '', 'Tester', 'ARM Board 1', 'board0', ''],
112 ['Active', 'arm', 'armv7', '', 'Tester', 'ARM Board 2', 'board1', ''],
113 ['Active', 'powerpc', 'powerpc', '', 'Tester', 'PowerPC board 1', 'board2', ''],
Simon Glass5e0f06d2017-11-12 21:52:15 -0700114 ['Active', 'powerpc', 'mpc83xx', '', 'Tester', 'PowerPC board 2', 'board3', ''],
Simon Glass584cf862013-09-23 17:35:16 -0600115 ['Active', 'sandbox', 'sandbox', '', 'Tester', 'Sandbox board', 'board4', ''],
Simon Glassc05694f2013-04-03 11:07:16 +0000116]
117
Simon Glass2e1646c2014-12-01 17:33:51 -0700118BASE_DIR = 'base'
119
Simon Glass071a1782018-11-06 16:02:13 -0700120OUTCOME_OK, OUTCOME_WARN, OUTCOME_ERR = range(3)
121
Simon Glassc05694f2013-04-03 11:07:16 +0000122class Options:
123 """Class that holds build options"""
124 pass
125
126class TestBuild(unittest.TestCase):
127 """Test buildman
128
129 TODO: Write tests for the rest of the functionality
130 """
131 def setUp(self):
132 # Set up commits to build
133 self.commits = []
134 sequence = 0
135 for commit_info in commits:
136 comm = commit.Commit(commit_info[0])
137 comm.subject = commit_info[1]
138 comm.return_code = commit_info[2]
139 comm.error_list = commit_info[3]
Simon Glassf4ebfba2020-04-09 15:08:53 -0600140 if sequence < 6:
141 comm.error_list += [migration]
Simon Glassc05694f2013-04-03 11:07:16 +0000142 comm.sequence = sequence
143 sequence += 1
144 self.commits.append(comm)
145
146 # Set up boards to build
Simon Glass20751d62022-07-11 19:04:03 -0600147 self.brds = boards.Boards()
Simon Glassb0786df2022-07-11 19:03:59 -0600148 for brd in BOARDS:
Simon Glass127a2392022-07-11 19:04:02 -0600149 self.brds.add_board(board.Board(*brd))
150 self.brds.select_boards([])
Simon Glassc05694f2013-04-03 11:07:16 +0000151
Simon Glass4bea81e2014-12-01 17:34:04 -0700152 # Add some test settings
Simon Glass06b83a52023-07-19 17:49:05 -0600153 bsettings.setup(None)
154 bsettings.add_file(settings_data)
Simon Glass4bea81e2014-12-01 17:34:04 -0700155
Simon Glassc05694f2013-04-03 11:07:16 +0000156 # Set up the toolchains
Simon Glassc05694f2013-04-03 11:07:16 +0000157 self.toolchains = toolchain.Toolchains()
158 self.toolchains.Add('arm-linux-gcc', test=False)
159 self.toolchains.Add('sparc-linux-gcc', test=False)
160 self.toolchains.Add('powerpc-linux-gcc', test=False)
Simon Glass038a3dd2024-08-15 13:57:44 -0600161 self.toolchains.Add('/path/to/aarch64-linux-gcc', test=False)
Simon Glassc05694f2013-04-03 11:07:16 +0000162 self.toolchains.Add('gcc', test=False)
163
Simon Glass7fb8aa22014-09-05 19:00:08 -0600164 # Avoid sending any output
Simon Glass02811582022-01-29 14:14:18 -0700165 terminal.set_print_test_mode()
Simon Glass7fb8aa22014-09-05 19:00:08 -0600166 self._col = terminal.Color()
167
Simon Glass09bbfcd2020-04-09 15:08:31 -0600168 self.base_dir = tempfile.mkdtemp()
169 if not os.path.isdir(self.base_dir):
170 os.mkdir(self.base_dir)
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600171
Simon Glassc229d322024-06-23 11:55:15 -0600172 self.cur_time = 0
173 self.valid_pids = []
174 self.finish_time = None
175 self.finish_pid = None
176
Simon Glass09bbfcd2020-04-09 15:08:31 -0600177 def tearDown(self):
178 shutil.rmtree(self.base_dir)
179
180 def Make(self, commit, brd, stage, *args, **kwargs):
Simon Glassc05694f2013-04-03 11:07:16 +0000181 result = command.CommandResult()
182 boardnum = int(brd.target[-1])
183 result.return_code = 0
184 result.stderr = ''
185 result.stdout = ('This is the test output for board %s, commit %s' %
186 (brd.target, commit.hash))
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600187 if ((boardnum >= 1 and boardnum >= commit.sequence) or
188 boardnum == 4 and commit.sequence == 6):
Simon Glassc05694f2013-04-03 11:07:16 +0000189 result.return_code = commit.return_code
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600190 result.stderr = (''.join(commit.error_list)
Simon Glass09bbfcd2020-04-09 15:08:31 -0600191 % {'basedir' : self.base_dir + '/.bm-work/00/'})
Simon Glassf4ebfba2020-04-09 15:08:53 -0600192 elif commit.sequence < 6:
193 result.stderr = migration
Simon Glassc05694f2013-04-03 11:07:16 +0000194
195 result.combined = result.stdout + result.stderr
196 return result
197
Simon Glassb0786df2022-07-11 19:03:59 -0600198 def assertSummary(self, text, arch, plus, brds, outcome=OUTCOME_ERR):
Simon Glass7fb8aa22014-09-05 19:00:08 -0600199 col = self._col
Simon Glass071a1782018-11-06 16:02:13 -0700200 expected_colour = (col.GREEN if outcome == OUTCOME_OK else
201 col.YELLOW if outcome == OUTCOME_WARN else col.RED)
Simon Glass7fb8aa22014-09-05 19:00:08 -0600202 expect = '%10s: ' % arch
203 # TODO(sjg@chromium.org): If plus is '', we shouldn't need this
Simon Glassf45d3742022-01-29 14:14:17 -0700204 expect += ' ' + col.build(expected_colour, plus)
Simon Glass7fb8aa22014-09-05 19:00:08 -0600205 expect += ' '
Simon Glassb0786df2022-07-11 19:03:59 -0600206 for brd in brds:
Simon Glass8132f982022-07-11 19:03:57 -0600207 expect += col.build(expected_colour, ' %s' % brd)
Simon Glass7fb8aa22014-09-05 19:00:08 -0600208 self.assertEqual(text, expect)
209
Simon Glassc635d892021-01-30 22:17:46 -0700210 def _SetupTest(self, echo_lines=False, threads=1, **kwdisplay_args):
Simon Glass724c1752020-04-09 15:08:32 -0600211 """Set up the test by running a build and summary
Simon Glass7fb8aa22014-09-05 19:00:08 -0600212
Simon Glass724c1752020-04-09 15:08:32 -0600213 Args:
214 echo_lines: True to echo lines to the terminal to aid test
215 development
Heinrich Schuchardt37153112024-04-19 13:37:46 +0200216 kwdisplay_args: Dict of arguments to pass to
Simon Glass724c1752020-04-09 15:08:32 -0600217 Builder.SetDisplayOptions()
218
219 Returns:
220 Iterator containing the output lines, each a PrintLine() object
Simon Glass7fb8aa22014-09-05 19:00:08 -0600221 """
Simon Glassc635d892021-01-30 22:17:46 -0700222 build = builder.Builder(self.toolchains, self.base_dir, None, threads,
223 2, checkout=False, show_unknown=False)
Simon Glassc05694f2013-04-03 11:07:16 +0000224 build.do_make = self.Make
Simon Glass127a2392022-07-11 19:04:02 -0600225 board_selected = self.brds.get_selected_dict()
Simon Glassc05694f2013-04-03 11:07:16 +0000226
Simon Glass071a1782018-11-06 16:02:13 -0700227 # Build the boards for the pre-defined commits and warnings/errors
228 # associated with each. This calls our Make() to inject the fake output.
Simon Glassbc74d942023-07-19 17:49:06 -0600229 build.build_boards(self.commits, board_selected, keep_outputs=False,
230 verbose=False)
Simon Glass02811582022-01-29 14:14:18 -0700231 lines = terminal.get_print_test_lines()
Simon Glass7fb8aa22014-09-05 19:00:08 -0600232 count = 0
233 for line in lines:
234 if line.text.strip():
235 count += 1
236
Simon Glass726ae812020-04-09 15:08:47 -0600237 # We should get two starting messages, an update for every commit built
238 # and a summary message
Simon Glassb0786df2022-07-11 19:03:59 -0600239 self.assertEqual(count, len(commits) * len(BOARDS) + 3)
Simon Glassbc74d942023-07-19 17:49:06 -0600240 build.set_display_options(**kwdisplay_args);
241 build.show_summary(self.commits, board_selected)
Simon Glass724c1752020-04-09 15:08:32 -0600242 if echo_lines:
Simon Glass02811582022-01-29 14:14:18 -0700243 terminal.echo_print_test_lines()
244 return iter(terminal.get_print_test_lines())
Simon Glass071a1782018-11-06 16:02:13 -0700245
Simon Glassf4ebfba2020-04-09 15:08:53 -0600246 def _CheckOutput(self, lines, list_error_boards=False,
247 filter_dtb_warnings=False,
248 filter_migration_warnings=False):
Simon Glass724c1752020-04-09 15:08:32 -0600249 """Check for expected output from the build summary
250
251 Args:
252 lines: Iterator containing the lines returned from the summary
Simon Glassdacbe1e2020-04-09 15:08:34 -0600253 list_error_boards: Adjust the check for output produced with the
254 --list-error-boards flag
Simon Glass9ea93812020-04-09 15:08:52 -0600255 filter_dtb_warnings: Adjust the check for output produced with the
256 --filter-dtb-warnings flag
Simon Glass724c1752020-04-09 15:08:32 -0600257 """
Simon Glassb0786df2022-07-11 19:03:59 -0600258 def add_line_prefix(prefix, brds, error_str, colour):
Simon Glass6643eb42020-04-09 15:08:33 -0600259 """Add a prefix to each line of a string
260
261 The training \n in error_str is removed before processing
262
263 Args:
264 prefix: String prefix to add
265 error_str: Error string containing the lines
Simon Glassea49f9b2020-04-09 15:08:37 -0600266 colour: Expected colour for the line. Note that the board list,
267 if present, always appears in magenta
Simon Glass6643eb42020-04-09 15:08:33 -0600268
269 Returns:
270 New string where each line has the prefix added
271 """
272 lines = error_str.strip().splitlines()
Simon Glassea49f9b2020-04-09 15:08:37 -0600273 new_lines = []
274 for line in lines:
Simon Glassb0786df2022-07-11 19:03:59 -0600275 if brds:
Simon Glassf45d3742022-01-29 14:14:17 -0700276 expect = self._col.build(colour, prefix + '(')
Simon Glassb0786df2022-07-11 19:03:59 -0600277 expect += self._col.build(self._col.MAGENTA, brds,
Simon Glassea49f9b2020-04-09 15:08:37 -0600278 bright=False)
Simon Glassf45d3742022-01-29 14:14:17 -0700279 expect += self._col.build(colour, ') %s' % line)
Simon Glassea49f9b2020-04-09 15:08:37 -0600280 else:
Simon Glassf45d3742022-01-29 14:14:17 -0700281 expect = self._col.build(colour, prefix + line)
Simon Glassea49f9b2020-04-09 15:08:37 -0600282 new_lines.append(expect)
Simon Glass6643eb42020-04-09 15:08:33 -0600283 return '\n'.join(new_lines)
284
Simon Glassf4ebfba2020-04-09 15:08:53 -0600285 col = terminal.Color()
286 boards01234 = ('board0 board1 board2 board3 board4'
287 if list_error_boards else '')
Simon Glass070589b2020-04-09 15:08:38 -0600288 boards1234 = 'board1 board2 board3 board4' if list_error_boards else ''
289 boards234 = 'board2 board3 board4' if list_error_boards else ''
290 boards34 = 'board3 board4' if list_error_boards else ''
Simon Glassdacbe1e2020-04-09 15:08:34 -0600291 boards4 = 'board4' if list_error_boards else ''
292
Simon Glassf4ebfba2020-04-09 15:08:53 -0600293 # Upstream commit: migration warnings only
Simon Glass575ce512020-04-09 15:08:30 -0600294 self.assertEqual(next(lines).text, '01: %s' % commits[0][1])
Simon Glass071a1782018-11-06 16:02:13 -0700295
Simon Glassf4ebfba2020-04-09 15:08:53 -0600296 if not filter_migration_warnings:
297 self.assertSummary(next(lines).text, 'arm', 'w+',
298 ['board0', 'board1'], outcome=OUTCOME_WARN)
299 self.assertSummary(next(lines).text, 'powerpc', 'w+',
300 ['board2', 'board3'], outcome=OUTCOME_WARN)
301 self.assertSummary(next(lines).text, 'sandbox', 'w+', ['board4'],
302 outcome=OUTCOME_WARN)
303
304 self.assertEqual(next(lines).text,
305 add_line_prefix('+', boards01234, migration, col.RED))
306
Simon Glass071a1782018-11-06 16:02:13 -0700307 # Second commit: all archs should fail with warnings
Simon Glass575ce512020-04-09 15:08:30 -0600308 self.assertEqual(next(lines).text, '02: %s' % commits[1][1])
Simon Glass7fb8aa22014-09-05 19:00:08 -0600309
Simon Glassf4ebfba2020-04-09 15:08:53 -0600310 if filter_migration_warnings:
311 self.assertSummary(next(lines).text, 'arm', 'w+',
312 ['board1'], outcome=OUTCOME_WARN)
313 self.assertSummary(next(lines).text, 'powerpc', 'w+',
314 ['board2', 'board3'], outcome=OUTCOME_WARN)
315 self.assertSummary(next(lines).text, 'sandbox', 'w+', ['board4'],
316 outcome=OUTCOME_WARN)
Simon Glass7fb8aa22014-09-05 19:00:08 -0600317
Simon Glass071a1782018-11-06 16:02:13 -0700318 # Second commit: The warnings should be listed
Simon Glassea49f9b2020-04-09 15:08:37 -0600319 self.assertEqual(next(lines).text,
320 add_line_prefix('w+', boards1234, errors[0], col.YELLOW))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600321
Simon Glass071a1782018-11-06 16:02:13 -0700322 # Third commit: Still fails
Simon Glass575ce512020-04-09 15:08:30 -0600323 self.assertEqual(next(lines).text, '03: %s' % commits[2][1])
Simon Glassf4ebfba2020-04-09 15:08:53 -0600324 if filter_migration_warnings:
325 self.assertSummary(next(lines).text, 'arm', '',
326 ['board1'], outcome=OUTCOME_OK)
Simon Glass575ce512020-04-09 15:08:30 -0600327 self.assertSummary(next(lines).text, 'powerpc', '+',
328 ['board2', 'board3'])
329 self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
Simon Glass7fb8aa22014-09-05 19:00:08 -0600330
Simon Glass071a1782018-11-06 16:02:13 -0700331 # Expect a compiler error
Simon Glassea49f9b2020-04-09 15:08:37 -0600332 self.assertEqual(next(lines).text,
333 add_line_prefix('+', boards234, errors[1], col.RED))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600334
Simon Glass071a1782018-11-06 16:02:13 -0700335 # Fourth commit: Compile errors are fixed, just have warning for board3
Simon Glass575ce512020-04-09 15:08:30 -0600336 self.assertEqual(next(lines).text, '04: %s' % commits[3][1])
Simon Glassf4ebfba2020-04-09 15:08:53 -0600337 if filter_migration_warnings:
338 expect = '%10s: ' % 'powerpc'
Simon Glassf45d3742022-01-29 14:14:17 -0700339 expect += ' ' + col.build(col.GREEN, '')
Simon Glassf4ebfba2020-04-09 15:08:53 -0600340 expect += ' '
Simon Glassf45d3742022-01-29 14:14:17 -0700341 expect += col.build(col.GREEN, ' %s' % 'board2')
342 expect += ' ' + col.build(col.YELLOW, 'w+')
Simon Glassf4ebfba2020-04-09 15:08:53 -0600343 expect += ' '
Simon Glassf45d3742022-01-29 14:14:17 -0700344 expect += col.build(col.YELLOW, ' %s' % 'board3')
Simon Glassf4ebfba2020-04-09 15:08:53 -0600345 self.assertEqual(next(lines).text, expect)
346 else:
347 self.assertSummary(next(lines).text, 'powerpc', 'w+',
348 ['board2', 'board3'], outcome=OUTCOME_WARN)
Simon Glass575ce512020-04-09 15:08:30 -0600349 self.assertSummary(next(lines).text, 'sandbox', 'w+', ['board4'],
Simon Glassf4ebfba2020-04-09 15:08:53 -0600350 outcome=OUTCOME_WARN)
Simon Glass7fb8aa22014-09-05 19:00:08 -0600351
352 # Compile error fixed
Simon Glassea49f9b2020-04-09 15:08:37 -0600353 self.assertEqual(next(lines).text,
354 add_line_prefix('-', boards234, errors[1], col.GREEN))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600355
Simon Glass9ea93812020-04-09 15:08:52 -0600356 if not filter_dtb_warnings:
357 self.assertEqual(
358 next(lines).text,
359 add_line_prefix('w+', boards34, errors[2], col.YELLOW))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600360
Simon Glass071a1782018-11-06 16:02:13 -0700361 # Fifth commit
Simon Glass575ce512020-04-09 15:08:30 -0600362 self.assertEqual(next(lines).text, '05: %s' % commits[4][1])
Simon Glassf4ebfba2020-04-09 15:08:53 -0600363 if filter_migration_warnings:
364 self.assertSummary(next(lines).text, 'powerpc', '', ['board3'],
365 outcome=OUTCOME_OK)
Simon Glass575ce512020-04-09 15:08:30 -0600366 self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
Simon Glass7fb8aa22014-09-05 19:00:08 -0600367
368 # The second line of errors[3] is a duplicate, so buildman will drop it
369 expect = errors[3].rstrip().split('\n')
370 expect = [expect[0]] + expect[2:]
Simon Glass6643eb42020-04-09 15:08:33 -0600371 expect = '\n'.join(expect)
Simon Glassea49f9b2020-04-09 15:08:37 -0600372 self.assertEqual(next(lines).text,
373 add_line_prefix('+', boards4, expect, col.RED))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600374
Simon Glass9ea93812020-04-09 15:08:52 -0600375 if not filter_dtb_warnings:
376 self.assertEqual(
377 next(lines).text,
378 add_line_prefix('w-', boards34, errors[2], col.CYAN))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600379
Simon Glass071a1782018-11-06 16:02:13 -0700380 # Sixth commit
Simon Glass575ce512020-04-09 15:08:30 -0600381 self.assertEqual(next(lines).text, '06: %s' % commits[5][1])
Simon Glassf4ebfba2020-04-09 15:08:53 -0600382 if filter_migration_warnings:
383 self.assertSummary(next(lines).text, 'sandbox', '', ['board4'],
384 outcome=OUTCOME_OK)
385 else:
386 self.assertSummary(next(lines).text, 'sandbox', 'w+', ['board4'],
387 outcome=OUTCOME_WARN)
Simon Glass7fb8aa22014-09-05 19:00:08 -0600388
389 # The second line of errors[3] is a duplicate, so buildman will drop it
390 expect = errors[3].rstrip().split('\n')
391 expect = [expect[0]] + expect[2:]
Simon Glass6643eb42020-04-09 15:08:33 -0600392 expect = '\n'.join(expect)
Simon Glassea49f9b2020-04-09 15:08:37 -0600393 self.assertEqual(next(lines).text,
394 add_line_prefix('-', boards4, expect, col.GREEN))
395 self.assertEqual(next(lines).text,
396 add_line_prefix('w-', boards4, errors[0], col.CYAN))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600397
Simon Glass071a1782018-11-06 16:02:13 -0700398 # Seventh commit
Simon Glass575ce512020-04-09 15:08:30 -0600399 self.assertEqual(next(lines).text, '07: %s' % commits[6][1])
Simon Glassf4ebfba2020-04-09 15:08:53 -0600400 if filter_migration_warnings:
401 self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
402 else:
403 self.assertSummary(next(lines).text, 'arm', '', ['board0', 'board1'],
404 outcome=OUTCOME_OK)
405 self.assertSummary(next(lines).text, 'powerpc', '',
406 ['board2', 'board3'], outcome=OUTCOME_OK)
407 self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600408
409 # Pick out the correct error lines
410 expect_str = errors[4].rstrip().replace('%(basedir)s', '').split('\n')
411 expect = expect_str[3:8] + [expect_str[-1]]
Simon Glass6643eb42020-04-09 15:08:33 -0600412 expect = '\n'.join(expect)
Simon Glassf4ebfba2020-04-09 15:08:53 -0600413 if not filter_migration_warnings:
414 self.assertEqual(
415 next(lines).text,
416 add_line_prefix('-', boards01234, migration, col.GREEN))
417
Simon Glassea49f9b2020-04-09 15:08:37 -0600418 self.assertEqual(next(lines).text,
419 add_line_prefix('+', boards4, expect, col.RED))
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600420
421 # Now the warnings lines
422 expect = [expect_str[0]] + expect_str[10:12] + [expect_str[9]]
Simon Glass6643eb42020-04-09 15:08:33 -0600423 expect = '\n'.join(expect)
Simon Glassea49f9b2020-04-09 15:08:37 -0600424 self.assertEqual(next(lines).text,
425 add_line_prefix('w+', boards4, expect, col.YELLOW))
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600426
Simon Glass724c1752020-04-09 15:08:32 -0600427 def testOutput(self):
428 """Test basic builder operation and output
429
430 This does a line-by-line verification of the summary output.
431 """
432 lines = self._SetupTest(show_errors=True)
Simon Glass9ea93812020-04-09 15:08:52 -0600433 self._CheckOutput(lines, list_error_boards=False,
434 filter_dtb_warnings=False)
Simon Glassdacbe1e2020-04-09 15:08:34 -0600435
436 def testErrorBoards(self):
437 """Test output with --list-error-boards
438
439 This does a line-by-line verification of the summary output.
440 """
441 lines = self._SetupTest(show_errors=True, list_error_boards=True)
Simon Glassf4ebfba2020-04-09 15:08:53 -0600442 self._CheckOutput(lines, list_error_boards=True)
Simon Glass9ea93812020-04-09 15:08:52 -0600443
444 def testFilterDtb(self):
445 """Test output with --filter-dtb-warnings
446
447 This does a line-by-line verification of the summary output.
448 """
449 lines = self._SetupTest(show_errors=True, filter_dtb_warnings=True)
Simon Glassf4ebfba2020-04-09 15:08:53 -0600450 self._CheckOutput(lines, filter_dtb_warnings=True)
451
452 def testFilterMigration(self):
453 """Test output with --filter-migration-warnings
454
455 This does a line-by-line verification of the summary output.
456 """
457 lines = self._SetupTest(show_errors=True,
458 filter_migration_warnings=True)
459 self._CheckOutput(lines, filter_migration_warnings=True)
Simon Glass724c1752020-04-09 15:08:32 -0600460
Simon Glassc635d892021-01-30 22:17:46 -0700461 def testSingleThread(self):
462 """Test operation without threading"""
463 lines = self._SetupTest(show_errors=True, threads=0)
464 self._CheckOutput(lines, list_error_boards=False,
465 filter_dtb_warnings=False)
466
Simon Glassc05694f2013-04-03 11:07:16 +0000467 def _testGit(self):
468 """Test basic builder operation by building a branch"""
Simon Glassc05694f2013-04-03 11:07:16 +0000469 options = Options()
470 options.git = os.getcwd()
471 options.summary = False
472 options.jobs = None
473 options.dry_run = False
Simon Glass09bbfcd2020-04-09 15:08:31 -0600474 #options.git = os.path.join(self.base_dir, 'repo')
Simon Glassc05694f2013-04-03 11:07:16 +0000475 options.branch = 'test-buildman'
476 options.force_build = False
477 options.list_tool_chains = False
478 options.count = -1
479 options.git_dir = None
480 options.threads = None
481 options.show_unknown = False
482 options.quick = False
483 options.show_errors = False
484 options.keep_outputs = False
485 args = ['tegra20']
Simon Glassc1e1e1d2023-07-19 17:48:30 -0600486 control.do_buildman(options, args)
Simon Glassc05694f2013-04-03 11:07:16 +0000487
Simon Glassaa40f9a2014-08-09 15:33:08 -0600488 def testBoardSingle(self):
489 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600490 self.assertEqual(self.brds.select_boards(['sandbox']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600491 ({'all': ['board4'], 'sandbox': ['board4']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600492
493 def testBoardArch(self):
494 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600495 self.assertEqual(self.brds.select_boards(['arm']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600496 ({'all': ['board0', 'board1'],
497 'arm': ['board0', 'board1']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600498
499 def testBoardArchSingle(self):
500 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600501 self.assertEqual(self.brds.select_boards(['arm sandbox']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600502 ({'sandbox': ['board4'],
Simon Glass5e0f06d2017-11-12 21:52:15 -0700503 'all': ['board0', 'board1', 'board4'],
Simon Glassd9eb9f02018-06-11 23:26:46 -0600504 'arm': ['board0', 'board1']}, []))
Simon Glass5e0f06d2017-11-12 21:52:15 -0700505
Simon Glassaa40f9a2014-08-09 15:33:08 -0600506
507 def testBoardArchSingleMultiWord(self):
508 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600509 self.assertEqual(self.brds.select_boards(['arm', 'sandbox']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600510 ({'sandbox': ['board4'],
511 'all': ['board0', 'board1', 'board4'],
512 'arm': ['board0', 'board1']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600513
514 def testBoardSingleAnd(self):
515 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600516 self.assertEqual(self.brds.select_boards(['Tester & arm']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600517 ({'Tester&arm': ['board0', 'board1'],
518 'all': ['board0', 'board1']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600519
520 def testBoardTwoAnd(self):
521 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600522 self.assertEqual(self.brds.select_boards(['Tester', '&', 'arm',
Simon Glassaa40f9a2014-08-09 15:33:08 -0600523 'Tester' '&', 'powerpc',
524 'sandbox']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600525 ({'sandbox': ['board4'],
Simon Glass5e0f06d2017-11-12 21:52:15 -0700526 'all': ['board0', 'board1', 'board2', 'board3',
527 'board4'],
528 'Tester&powerpc': ['board2', 'board3'],
Simon Glassd9eb9f02018-06-11 23:26:46 -0600529 'Tester&arm': ['board0', 'board1']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600530
531 def testBoardAll(self):
532 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600533 self.assertEqual(self.brds.select_boards([]),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600534 ({'all': ['board0', 'board1', 'board2', 'board3',
535 'board4']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600536
537 def testBoardRegularExpression(self):
538 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600539 self.assertEqual(self.brds.select_boards(['T.*r&^Po']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600540 ({'all': ['board2', 'board3'],
541 'T.*r&^Po': ['board2', 'board3']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600542
543 def testBoardDuplicate(self):
544 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600545 self.assertEqual(self.brds.select_boards(['sandbox sandbox',
Simon Glassaa40f9a2014-08-09 15:33:08 -0600546 'sandbox']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600547 ({'all': ['board4'], 'sandbox': ['board4']}, []))
Simon Glass2e1646c2014-12-01 17:33:51 -0700548 def CheckDirs(self, build, dirname):
Simon Glass4cb54682023-07-19 17:49:10 -0600549 self.assertEqual('base%s' % dirname, build.get_output_dir(1))
Simon Glass2e1646c2014-12-01 17:33:51 -0700550 self.assertEqual('base%s/fred' % dirname,
Simon Glassbc74d942023-07-19 17:49:06 -0600551 build.get_build_dir(1, 'fred'))
Simon Glass2e1646c2014-12-01 17:33:51 -0700552 self.assertEqual('base%s/fred/done' % dirname,
Simon Glassbc74d942023-07-19 17:49:06 -0600553 build.get_done_file(1, 'fred'))
Simon Glass2e1646c2014-12-01 17:33:51 -0700554 self.assertEqual('base%s/fred/u-boot.sizes' % dirname,
Simon Glassbc74d942023-07-19 17:49:06 -0600555 build.get_func_sizes_file(1, 'fred', 'u-boot'))
Simon Glass2e1646c2014-12-01 17:33:51 -0700556 self.assertEqual('base%s/fred/u-boot.objdump' % dirname,
Simon Glassbc74d942023-07-19 17:49:06 -0600557 build.get_objdump_file(1, 'fred', 'u-boot'))
Simon Glass2e1646c2014-12-01 17:33:51 -0700558 self.assertEqual('base%s/fred/err' % dirname,
Simon Glassbc74d942023-07-19 17:49:06 -0600559 build.get_err_file(1, 'fred'))
Simon Glass2e1646c2014-12-01 17:33:51 -0700560
561 def testOutputDir(self):
562 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
563 checkout=False, show_unknown=False)
564 build.commits = self.commits
565 build.commit_count = len(self.commits)
566 subject = self.commits[1].subject.translate(builder.trans_valid_chars)
Simon Glass36971712020-07-19 12:28:11 -0600567 dirname ='/%02d_g%s_%s' % (2, commits[1][0], subject[:20])
Simon Glass2e1646c2014-12-01 17:33:51 -0700568 self.CheckDirs(build, dirname)
569
570 def testOutputDirCurrent(self):
571 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
572 checkout=False, show_unknown=False)
573 build.commits = None
574 build.commit_count = 0
575 self.CheckDirs(build, '/current')
Simon Glassaa40f9a2014-08-09 15:33:08 -0600576
Simon Glasse87bde12014-12-01 17:33:55 -0700577 def testOutputDirNoSubdirs(self):
578 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
579 checkout=False, show_unknown=False,
580 no_subdirs=True)
581 build.commits = None
582 build.commit_count = 0
583 self.CheckDirs(build, '')
584
Simon Glassc1528c12014-12-01 17:34:05 -0700585 def testToolchainAliases(self):
586 self.assertTrue(self.toolchains.Select('arm') != None)
587 with self.assertRaises(ValueError):
588 self.toolchains.Select('no-arch')
589 with self.assertRaises(ValueError):
590 self.toolchains.Select('x86')
591
592 self.toolchains = toolchain.Toolchains()
593 self.toolchains.Add('x86_64-linux-gcc', test=False)
594 self.assertTrue(self.toolchains.Select('x86') != None)
595
596 self.toolchains = toolchain.Toolchains()
597 self.toolchains.Add('i386-linux-gcc', test=False)
598 self.assertTrue(self.toolchains.Select('x86') != None)
599
Simon Glass7e803e12014-12-01 17:34:06 -0700600 def testToolchainDownload(self):
601 """Test that we can download toolchains"""
Simon Glass2bfc6972017-11-12 21:52:14 -0700602 if use_network:
Simon Glass04150022018-10-01 21:12:43 -0600603 with test_util.capture_sys_output() as (stdout, stderr):
604 url = self.toolchains.LocateArchUrl('arm')
Brandon Maiera657bc62024-06-04 16:16:05 +0000605 self.assertRegex(url, 'https://www.kernel.org/pub/tools/'
Simon Glass72e358c2018-10-01 21:12:35 -0600606 'crosstool/files/bin/x86_64/.*/'
Simon Glassdd65c5f2020-04-17 17:51:30 -0600607 'x86_64-gcc-.*-nolibc[-_]arm-.*linux-gnueabi.tar.xz')
Simon Glass7e803e12014-12-01 17:34:06 -0700608
Simon Glass48ac42e2019-12-05 15:59:14 -0700609 def testGetEnvArgs(self):
610 """Test the GetEnvArgs() function"""
611 tc = self.toolchains.Select('arm')
612 self.assertEqual('arm-linux-',
613 tc.GetEnvArgs(toolchain.VAR_CROSS_COMPILE))
614 self.assertEqual('', tc.GetEnvArgs(toolchain.VAR_PATH))
615 self.assertEqual('arm',
616 tc.GetEnvArgs(toolchain.VAR_ARCH))
617 self.assertEqual('', tc.GetEnvArgs(toolchain.VAR_MAKE_ARGS))
618
Jerome Forissieraa276952024-09-11 11:58:14 +0200619 tc = self.toolchains.Select('sandbox')
620 self.assertEqual('', tc.GetEnvArgs(toolchain.VAR_CROSS_COMPILE))
621
Simon Glass48ac42e2019-12-05 15:59:14 -0700622 self.toolchains.Add('/path/to/x86_64-linux-gcc', test=False)
623 tc = self.toolchains.Select('x86')
624 self.assertEqual('/path/to',
625 tc.GetEnvArgs(toolchain.VAR_PATH))
626 tc.override_toolchain = 'clang'
627 self.assertEqual('HOSTCC=clang CC=clang',
628 tc.GetEnvArgs(toolchain.VAR_MAKE_ARGS))
629
Jerome Forissieraa276952024-09-11 11:58:14 +0200630 # Test config with ccache wrapper
631 bsettings.setup(None)
632 bsettings.add_file(settings_data_wrapper)
633
634 tc = self.toolchains.Select('arm')
635 self.assertEqual('ccache arm-linux-',
636 tc.GetEnvArgs(toolchain.VAR_CROSS_COMPILE))
637
638 tc = self.toolchains.Select('sandbox')
639 self.assertEqual('', tc.GetEnvArgs(toolchain.VAR_CROSS_COMPILE))
640
641 def testMakeEnvironment(self):
642 """Test the MakeEnvironment function"""
643 tc = self.toolchains.Select('arm')
644 env = tc.MakeEnvironment(False)
645 self.assertEqual(env[b'CROSS_COMPILE'], b'arm-linux-')
646
647 tc = self.toolchains.Select('sandbox')
648 env = tc.MakeEnvironment(False)
649 self.assertTrue(b'CROSS_COMPILE' not in env)
650
651 # Test config with ccache wrapper
652 bsettings.setup(None)
653 bsettings.add_file(settings_data_wrapper)
654
655 tc = self.toolchains.Select('arm')
656 env = tc.MakeEnvironment(False)
657 self.assertEqual(env[b'CROSS_COMPILE'], b'ccache arm-linux-')
658
659 tc = self.toolchains.Select('sandbox')
660 env = tc.MakeEnvironment(False)
661 self.assertTrue(b'CROSS_COMPILE' not in env)
662
Simon Glass5dc1ca72020-03-18 09:42:45 -0600663 def testPrepareOutputSpace(self):
664 def _Touch(fname):
Simon Glass80025522022-01-29 14:14:04 -0700665 tools.write_file(os.path.join(base_dir, fname), b'')
Simon Glass5dc1ca72020-03-18 09:42:45 -0600666
667 base_dir = tempfile.mkdtemp()
668
669 # Add various files that we want removed and left alone
Ovidiu Panaitee8e9cb2020-05-15 09:30:12 +0300670 to_remove = ['01_g0982734987_title', '102_g92bf_title',
671 '01_g2938abd8_title']
672 to_leave = ['something_else', '01-something.patch', '01_another']
Simon Glass5dc1ca72020-03-18 09:42:45 -0600673 for name in to_remove + to_leave:
674 _Touch(name)
675
676 build = builder.Builder(self.toolchains, base_dir, None, 1, 2)
677 build.commits = self.commits
678 build.commit_count = len(commits)
Simon Glassbc74d942023-07-19 17:49:06 -0600679 result = set(build._get_output_space_removals())
Simon Glass5dc1ca72020-03-18 09:42:45 -0600680 expected = set([os.path.join(base_dir, f) for f in to_remove])
681 self.assertEqual(expected, result)
Simon Glass7e803e12014-12-01 17:34:06 -0700682
Simon Glass22901f92022-01-22 05:07:31 -0700683 def test_adjust_cfg_nop(self):
684 """check various adjustments of config that are nops"""
685 # enable an enabled CONFIG
686 self.assertEqual(
687 'CONFIG_FRED=y',
688 cfgutil.adjust_cfg_line('CONFIG_FRED=y', {'FRED':'FRED'})[0])
689
690 # disable a disabled CONFIG
691 self.assertEqual(
692 '# CONFIG_FRED is not set',
693 cfgutil.adjust_cfg_line(
694 '# CONFIG_FRED is not set', {'FRED':'~FRED'})[0])
695
696 # use the adjust_cfg_lines() function
697 self.assertEqual(
698 ['CONFIG_FRED=y'],
699 cfgutil.adjust_cfg_lines(['CONFIG_FRED=y'], {'FRED':'FRED'}))
700 self.assertEqual(
701 ['# CONFIG_FRED is not set'],
702 cfgutil.adjust_cfg_lines(['CONFIG_FRED=y'], {'FRED':'~FRED'}))
703
704 # handling an empty line
705 self.assertEqual('#', cfgutil.adjust_cfg_line('#', {'FRED':'~FRED'})[0])
706
707 def test_adjust_cfg(self):
708 """check various adjustments of config"""
709 # disable a CONFIG
710 self.assertEqual(
711 '# CONFIG_FRED is not set',
712 cfgutil.adjust_cfg_line('CONFIG_FRED=1' , {'FRED':'~FRED'})[0])
713
714 # enable a disabled CONFIG
715 self.assertEqual(
716 'CONFIG_FRED=y',
717 cfgutil.adjust_cfg_line(
718 '# CONFIG_FRED is not set', {'FRED':'FRED'})[0])
719
720 # enable a CONFIG that doesn't exist
721 self.assertEqual(
722 ['CONFIG_FRED=y'],
723 cfgutil.adjust_cfg_lines([], {'FRED':'FRED'}))
724
725 # disable a CONFIG that doesn't exist
726 self.assertEqual(
727 ['# CONFIG_FRED is not set'],
728 cfgutil.adjust_cfg_lines([], {'FRED':'~FRED'}))
729
730 # disable a value CONFIG
731 self.assertEqual(
732 '# CONFIG_FRED is not set',
733 cfgutil.adjust_cfg_line('CONFIG_FRED="fred"' , {'FRED':'~FRED'})[0])
734
735 # setting a value CONFIG
736 self.assertEqual(
737 'CONFIG_FRED="fred"',
738 cfgutil.adjust_cfg_line('# CONFIG_FRED is not set' ,
739 {'FRED':'FRED="fred"'})[0])
740
741 # changing a value CONFIG
742 self.assertEqual(
743 'CONFIG_FRED="fred"',
744 cfgutil.adjust_cfg_line('CONFIG_FRED="ernie"' ,
745 {'FRED':'FRED="fred"'})[0])
746
747 # setting a value for a CONFIG that doesn't exist
748 self.assertEqual(
749 ['CONFIG_FRED="fred"'],
750 cfgutil.adjust_cfg_lines([], {'FRED':'FRED="fred"'}))
751
752 def test_convert_adjust_cfg_list(self):
753 """Check conversion of the list of changes into a dict"""
754 self.assertEqual({}, cfgutil.convert_list_to_dict(None))
755
756 expect = {
757 'FRED':'FRED',
758 'MARY':'~MARY',
759 'JOHN':'JOHN=0x123',
760 'ALICE':'ALICE="alice"',
761 'AMY':'AMY',
762 'ABE':'~ABE',
763 'MARK':'MARK=0x456',
764 'ANNA':'ANNA="anna"',
765 }
766 actual = cfgutil.convert_list_to_dict(
767 ['FRED', '~MARY', 'JOHN=0x123', 'ALICE="alice"',
768 'CONFIG_AMY', '~CONFIG_ABE', 'CONFIG_MARK=0x456',
769 'CONFIG_ANNA="anna"'])
770 self.assertEqual(expect, actual)
771
772 def test_check_cfg_file(self):
773 """Test check_cfg_file detects conflicts as expected"""
774 # Check failure to disable CONFIG
775 result = cfgutil.check_cfg_lines(['CONFIG_FRED=1'], {'FRED':'~FRED'})
776 self.assertEqual([['~FRED', 'CONFIG_FRED=1']], result)
777
778 result = cfgutil.check_cfg_lines(
779 ['CONFIG_FRED=1', 'CONFIG_MARY="mary"'], {'FRED':'~FRED'})
780 self.assertEqual([['~FRED', 'CONFIG_FRED=1']], result)
781
782 result = cfgutil.check_cfg_lines(
783 ['CONFIG_FRED=1', 'CONFIG_MARY="mary"'], {'MARY':'~MARY'})
784 self.assertEqual([['~MARY', 'CONFIG_MARY="mary"']], result)
785
786 # Check failure to enable CONFIG
787 result = cfgutil.check_cfg_lines(
788 ['# CONFIG_FRED is not set'], {'FRED':'FRED'})
789 self.assertEqual([['FRED', '# CONFIG_FRED is not set']], result)
790
791 # Check failure to set CONFIG value
792 result = cfgutil.check_cfg_lines(
793 ['# CONFIG_FRED is not set', 'CONFIG_MARY="not"'],
794 {'MARY':'MARY="mary"', 'FRED':'FRED'})
795 self.assertEqual([
796 ['FRED', '# CONFIG_FRED is not set'],
797 ['MARY="mary"', 'CONFIG_MARY="not"']], result)
798
799 # Check failure to add CONFIG value
800 result = cfgutil.check_cfg_lines([], {'MARY':'MARY="mary"'})
801 self.assertEqual([
802 ['MARY="mary"', 'Missing expected line: CONFIG_MARY="mary"']], result)
803
Simon Glassc229d322024-06-23 11:55:15 -0600804 def get_procs(self):
805 running_fname = os.path.join(self.base_dir, control.RUNNING_FNAME)
806 items = tools.read_file(running_fname, binary=False).split()
807 return [int(x) for x in items]
808
809 def get_time(self):
810 return self.cur_time
811
812 def inc_time(self, amount):
813 self.cur_time += amount
814
815 # Handle a process exiting
816 if self.finish_time == self.cur_time:
817 self.valid_pids = [pid for pid in self.valid_pids
818 if pid != self.finish_pid]
819
820 def kill(self, pid, signal):
821 if pid not in self.valid_pids:
822 raise OSError('Invalid PID')
823
824 def test_process_limit(self):
825 """Test wait_for_process_limit() function"""
826 tmpdir = self.base_dir
827
828 with (patch('time.time', side_effect=self.get_time),
Simon Glassf4f0acc2024-08-22 07:57:46 -0600829 patch('time.monotonic', side_effect=self.get_time),
Simon Glassc229d322024-06-23 11:55:15 -0600830 patch('time.sleep', side_effect=self.inc_time),
831 patch('os.kill', side_effect=self.kill)):
832 # Grab the process. Since there is no other profcess, this should
833 # immediately succeed
834 control.wait_for_process_limit(1, tmpdir=tmpdir, pid=1)
835 lines = terminal.get_print_test_lines()
836 self.assertEqual(0, self.cur_time)
837 self.assertEqual('Waiting for other buildman processes...',
838 lines[0].text)
839 self.assertEqual(self._col.RED, lines[0].colour)
840 self.assertEqual(False, lines[0].newline)
841 self.assertEqual(True, lines[0].bright)
842
843 self.assertEqual('done...', lines[1].text)
844 self.assertEqual(None, lines[1].colour)
845 self.assertEqual(False, lines[1].newline)
846 self.assertEqual(True, lines[1].bright)
847
848 self.assertEqual('starting build', lines[2].text)
849 self.assertEqual([1], control.read_procs(tmpdir))
850 self.assertEqual(None, lines[2].colour)
851 self.assertEqual(False, lines[2].newline)
852 self.assertEqual(True, lines[2].bright)
853
854 # Try again, with a different PID...this should eventually timeout
855 # and start the build anyway
856 self.cur_time = 0
857 self.valid_pids = [1]
858 control.wait_for_process_limit(1, tmpdir=tmpdir, pid=2)
859 lines = terminal.get_print_test_lines()
860 self.assertEqual('Waiting for other buildman processes...',
861 lines[0].text)
862 self.assertEqual('timeout...', lines[1].text)
863 self.assertEqual(None, lines[1].colour)
864 self.assertEqual(False, lines[1].newline)
865 self.assertEqual(True, lines[1].bright)
866 self.assertEqual('starting build', lines[2].text)
867 self.assertEqual([1, 2], control.read_procs(tmpdir))
868 self.assertEqual(control.RUN_WAIT_S, self.cur_time)
869
870 # Check lock-busting
871 self.cur_time = 0
872 self.valid_pids = [1, 2]
873 lock_fname = os.path.join(tmpdir, control.LOCK_FNAME)
874 lock = FileLock(lock_fname)
875 lock.acquire(timeout=1)
876 control.wait_for_process_limit(1, tmpdir=tmpdir, pid=3)
877 lines = terminal.get_print_test_lines()
878 self.assertEqual('Waiting for other buildman processes...',
879 lines[0].text)
880 self.assertEqual('failed to get lock: busting...', lines[1].text)
881 self.assertEqual(None, lines[1].colour)
882 self.assertEqual(False, lines[1].newline)
883 self.assertEqual(True, lines[1].bright)
884 self.assertEqual('timeout...', lines[2].text)
885 self.assertEqual('starting build', lines[3].text)
886 self.assertEqual([1, 2, 3], control.read_procs(tmpdir))
887 self.assertEqual(control.RUN_WAIT_S, self.cur_time)
888 lock.release()
889
890 # Check handling of dead processes. Here we have PID 2 as a running
891 # process, even though the PID file contains 1, 2 and 3. So we can
892 # add one more PID, to make 2 and 4
893 self.cur_time = 0
894 self.valid_pids = [2]
895 control.wait_for_process_limit(2, tmpdir=tmpdir, pid=4)
896 lines = terminal.get_print_test_lines()
897 self.assertEqual('Waiting for other buildman processes...',
898 lines[0].text)
899 self.assertEqual('done...', lines[1].text)
900 self.assertEqual('starting build', lines[2].text)
901 self.assertEqual([2, 4], control.read_procs(tmpdir))
902 self.assertEqual(0, self.cur_time)
903
904 # Try again, with PID 2 quitting at time 50. This allows the new
905 # build to start
906 self.cur_time = 0
907 self.valid_pids = [2, 4]
908 self.finish_pid = 2
909 self.finish_time = 50
910 control.wait_for_process_limit(2, tmpdir=tmpdir, pid=5)
911 lines = terminal.get_print_test_lines()
912 self.assertEqual('Waiting for other buildman processes...',
913 lines[0].text)
914 self.assertEqual('done...', lines[1].text)
915 self.assertEqual('starting build', lines[2].text)
916 self.assertEqual([4, 5], control.read_procs(tmpdir))
917 self.assertEqual(self.finish_time, self.cur_time)
918
Simon Glass038a3dd2024-08-15 13:57:44 -0600919 def call_make_environment(self, tchn, full_path, in_env=None):
920 """Call Toolchain.MakeEnvironment() and process the result
921
922 Args:
923 tchn (Toolchain): Toolchain to use
924 full_path (bool): True to return the full path in CROSS_COMPILE
925 rather than adding it to the PATH variable
926 in_env (dict): Input environment to use, None to use current env
927
928 Returns:
929 tuple:
930 dict: Changes that MakeEnvironment has made to the environment
931 key: Environment variable that was changed
932 value: New value (for PATH this only includes components
933 which were added)
934 str: Full value of the new PATH variable
935 """
936 env = tchn.MakeEnvironment(full_path, env=in_env)
937
938 # Get the original environment
939 orig_env = dict(os.environb if in_env is None else in_env)
940 orig_path = orig_env[b'PATH'].split(b':')
941
942 # Find new variables
943 diff = dict((k, env[k]) for k in env if orig_env.get(k) != env[k])
944
945 # Find new / different path components
946 diff_path = None
947 new_path = None
948 if b'PATH' in diff:
949 new_path = diff[b'PATH'].split(b':')
950 diff_paths = [p for p in new_path if p not in orig_path]
951 diff_path = b':'.join(p for p in new_path if p not in orig_path)
952 if diff_path:
953 diff[b'PATH'] = diff_path
954 else:
955 del diff[b'PATH']
956 return diff, new_path
957
958 def test_toolchain_env(self):
959 """Test PATH and other environment settings for toolchains"""
960 # Use a toolchain which has a path, so that full_path makes a difference
961 tchn = self.toolchains.Select('aarch64')
962
963 # Normal cases
964 diff = self.call_make_environment(tchn, full_path=False)[0]
965 self.assertEqual(
966 {b'CROSS_COMPILE': b'aarch64-linux-', b'LC_ALL': b'C',
967 b'PATH': b'/path/to'}, diff)
968
969 diff = self.call_make_environment(tchn, full_path=True)[0]
970 self.assertEqual(
971 {b'CROSS_COMPILE': b'/path/to/aarch64-linux-', b'LC_ALL': b'C'},
972 diff)
973
974 # When overriding the toolchain, only LC_ALL should be set
975 tchn.override_toolchain = True
976 diff = self.call_make_environment(tchn, full_path=True)[0]
977 self.assertEqual({b'LC_ALL': b'C'}, diff)
978
979 # Test that virtualenv is handled correctly
980 tchn.override_toolchain = False
981 sys.prefix = '/some/venv'
982 env = dict(os.environb)
983 env[b'PATH'] = b'/some/venv/bin:other/things'
984 tchn.path = '/my/path'
985 diff, diff_path = self.call_make_environment(tchn, False, env)
986
987 self.assertIn(b'PATH', diff)
988 self.assertEqual([b'/some/venv/bin', b'/my/path', b'other/things'],
989 diff_path)
990 self.assertEqual(
991 {b'CROSS_COMPILE': b'aarch64-linux-', b'LC_ALL': b'C',
992 b'PATH': b'/my/path'}, diff)
993
994 # Handle a toolchain wrapper
995 tchn.path = ''
996 bsettings.add_section('toolchain-wrapper')
997 bsettings.set_item('toolchain-wrapper', 'my-wrapper', 'fred')
998 diff = self.call_make_environment(tchn, full_path=True)[0]
999 self.assertEqual(
1000 {b'CROSS_COMPILE': b'fred aarch64-linux-', b'LC_ALL': b'C'}, diff)
1001
Simon Glass600ede92024-08-15 13:57:45 -06001002 def test_skip_dtc(self):
1003 """Test skipping building the dtc tool"""
1004 old_path = os.getenv('PATH')
1005 try:
1006 os.environ['PATH'] = self.base_dir
1007
1008 # Check a missing tool
1009 with self.assertRaises(ValueError) as exc:
1010 builder.Builder(self.toolchains, self.base_dir, None, 0, 2,
1011 dtc_skip=True)
1012 self.assertIn('Cannot find dtc', str(exc.exception))
1013
1014 # Create a fake tool to use
1015 dtc = os.path.join(self.base_dir, 'dtc')
1016 tools.write_file(dtc, b'xx')
1017 os.chmod(dtc, 0o777)
1018
1019 build = builder.Builder(self.toolchains, self.base_dir, None, 0, 2,
1020 dtc_skip=True)
1021 toolchain = self.toolchains.Select('arm')
1022 env = build.make_environment(toolchain)
1023 self.assertIn(b'DTC', env)
1024
1025 # Try the normal case, i.e. not skipping the dtc build
1026 build = builder.Builder(self.toolchains, self.base_dir, None, 0, 2)
1027 toolchain = self.toolchains.Select('arm')
1028 env = build.make_environment(toolchain)
1029 self.assertNotIn(b'DTC', env)
1030 finally:
1031 os.environ['PATH'] = old_path
1032
Simon Glass22901f92022-01-22 05:07:31 -07001033
Simon Glassc05694f2013-04-03 11:07:16 +00001034if __name__ == "__main__":
1035 unittest.main()