1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2012 The Chromium OS Authors.
12 # Bring in the patman libraries
13 our_path = os.path.dirname(os.path.realpath(__file__))
14 sys.path.append(os.path.join(our_path, '../patman'))
30 # Buildman settings file
40 '''main.c: In function 'main_loop':
41 main.c:260:6: warning: unused variable 'joe' [-Wunused-variable]
43 '''main.c: In function 'main_loop2':
44 main.c:295:2: error: 'fred' undeclared (first use in this function)
45 main.c:295:2: note: each undeclared identifier is reported only once for each function it appears in
46 make[1]: *** [main.o] Error 1
47 make: *** [common/libcommon.o] Error 2
50 '''arch/arm/dts/socfpga_arria10_socdk_sdmmc.dtb: Warning \
51 (avoid_unnecessary_addr_size): /clocks: unnecessary #address-cells/#size-cells \
52 without "ranges" or child "reg" property
54 '''powerpc-linux-ld: warning: dot moved backwards before `.bss'
55 powerpc-linux-ld: warning: dot moved backwards before `.bss'
56 powerpc-linux-ld: u-boot: section .text lma 0xfffc0000 overlaps previous sections
57 powerpc-linux-ld: u-boot: section .rodata lma 0xfffef3ec overlaps previous sections
58 powerpc-linux-ld: u-boot: section .reloc lma 0xffffa400 overlaps previous sections
59 powerpc-linux-ld: u-boot: section .data lma 0xffffcd38 overlaps previous sections
60 powerpc-linux-ld: u-boot: section .u_boot_cmd lma 0xffffeb40 overlaps previous sections
61 powerpc-linux-ld: u-boot: section .bootpg lma 0xfffff198 overlaps previous sections
63 '''In file included from %(basedir)sarch/sandbox/cpu/cpu.c:9:0:
64 %(basedir)sarch/sandbox/include/asm/state.h:44:0: warning: "xxxx" redefined [enabled by default]
65 %(basedir)sarch/sandbox/include/asm/state.h:43:0: note: this is the location of the previous definition
66 %(basedir)sarch/sandbox/cpu/cpu.c: In function 'do_reset':
67 %(basedir)sarch/sandbox/cpu/cpu.c:27:1: error: unknown type name 'blah'
68 %(basedir)sarch/sandbox/cpu/cpu.c:28:12: error: expected declaration specifiers or '...' before numeric constant
69 make[2]: *** [arch/sandbox/cpu/cpu.o] Error 1
70 make[1]: *** [arch/sandbox/cpu] Error 2
71 make[1]: *** Waiting for unfinished jobs....
72 In file included from %(basedir)scommon/board_f.c:55:0:
73 %(basedir)sarch/sandbox/include/asm/state.h:44:0: warning: "xxxx" redefined [enabled by default]
74 %(basedir)sarch/sandbox/include/asm/state.h:43:0: note: this is the location of the previous definition
75 make: *** [sub-make] Error 2
80 # hash, subject, return code, list of errors/warnings
82 ['1234', 'upstream/master, ok', 0, []],
83 ['5678', 'Second commit, a warning', 0, errors[0:1]],
84 ['9012', 'Third commit, error', 1, errors[0:2]],
85 ['3456', 'Fourth commit, warning', 0, [errors[0], errors[2]]],
86 ['7890', 'Fifth commit, link errors', 1, [errors[0], errors[3]]],
87 ['abcd', 'Sixth commit, fixes all errors', 0, []],
88 ['ef01', 'Seventh commit, check directory suppression', 1, [errors[4]]],
92 ['Active', 'arm', 'armv7', '', 'Tester', 'ARM Board 1', 'board0', ''],
93 ['Active', 'arm', 'armv7', '', 'Tester', 'ARM Board 2', 'board1', ''],
94 ['Active', 'powerpc', 'powerpc', '', 'Tester', 'PowerPC board 1', 'board2', ''],
95 ['Active', 'powerpc', 'mpc83xx', '', 'Tester', 'PowerPC board 2', 'board3', ''],
96 ['Active', 'sandbox', 'sandbox', '', 'Tester', 'Sandbox board', 'board4', ''],
101 OUTCOME_OK, OUTCOME_WARN, OUTCOME_ERR = range(3)
104 """Class that holds build options"""
107 class TestBuild(unittest.TestCase):
110 TODO: Write tests for the rest of the functionality
113 # Set up commits to build
116 for commit_info in commits:
117 comm = commit.Commit(commit_info[0])
118 comm.subject = commit_info[1]
119 comm.return_code = commit_info[2]
120 comm.error_list = commit_info[3]
121 comm.sequence = sequence
123 self.commits.append(comm)
125 # Set up boards to build
126 self.boards = board.Boards()
128 self.boards.AddBoard(board.Board(*brd))
129 self.boards.SelectBoards([])
131 # Add some test settings
132 bsettings.Setup(None)
133 bsettings.AddFile(settings_data)
135 # Set up the toolchains
136 self.toolchains = toolchain.Toolchains()
137 self.toolchains.Add('arm-linux-gcc', test=False)
138 self.toolchains.Add('sparc-linux-gcc', test=False)
139 self.toolchains.Add('powerpc-linux-gcc', test=False)
140 self.toolchains.Add('gcc', test=False)
142 # Avoid sending any output
143 terminal.SetPrintTestMode()
144 self._col = terminal.Color()
146 self.base_dir = tempfile.mkdtemp()
147 if not os.path.isdir(self.base_dir):
148 os.mkdir(self.base_dir)
151 shutil.rmtree(self.base_dir)
153 def Make(self, commit, brd, stage, *args, **kwargs):
154 result = command.CommandResult()
155 boardnum = int(brd.target[-1])
156 result.return_code = 0
158 result.stdout = ('This is the test output for board %s, commit %s' %
159 (brd.target, commit.hash))
160 if ((boardnum >= 1 and boardnum >= commit.sequence) or
161 boardnum == 4 and commit.sequence == 6):
162 result.return_code = commit.return_code
163 result.stderr = (''.join(commit.error_list)
164 % {'basedir' : self.base_dir + '/.bm-work/00/'})
166 result.combined = result.stdout + result.stderr
169 def assertSummary(self, text, arch, plus, boards, outcome=OUTCOME_ERR):
171 expected_colour = (col.GREEN if outcome == OUTCOME_OK else
172 col.YELLOW if outcome == OUTCOME_WARN else col.RED)
173 expect = '%10s: ' % arch
174 # TODO(sjg@chromium.org): If plus is '', we shouldn't need this
175 expect += ' ' + col.Color(expected_colour, plus)
178 expect += col.Color(expected_colour, ' %s' % board)
179 self.assertEqual(text, expect)
181 def testOutput(self):
182 """Test basic builder operation and output
184 This does a line-by-line verification of the summary output.
186 build = builder.Builder(self.toolchains, self.base_dir, None, 1, 2,
187 checkout=False, show_unknown=False)
188 build.do_make = self.Make
189 board_selected = self.boards.GetSelectedDict()
191 # Build the boards for the pre-defined commits and warnings/errors
192 # associated with each. This calls our Make() to inject the fake output.
193 build.BuildBoards(self.commits, board_selected, keep_outputs=False,
195 lines = terminal.GetPrintTestLines()
198 if line.text.strip():
201 # We should get two starting messages, then an update for every commit
203 self.assertEqual(count, len(commits) * len(boards) + 2)
204 build.SetDisplayOptions(show_errors=True);
205 build.ShowSummary(self.commits, board_selected)
206 #terminal.EchoPrintTestLines()
207 lines = iter(terminal.GetPrintTestLines())
209 # Upstream commit: no errors
210 self.assertEqual(next(lines).text, '01: %s' % commits[0][1])
212 # Second commit: all archs should fail with warnings
213 self.assertEqual(next(lines).text, '02: %s' % commits[1][1])
215 col = terminal.Color()
216 self.assertSummary(next(lines).text, 'arm', 'w+', ['board1'],
217 outcome=OUTCOME_WARN)
218 self.assertSummary(next(lines).text, 'powerpc', 'w+',
219 ['board2', 'board3'], outcome=OUTCOME_WARN)
220 self.assertSummary(next(lines).text, 'sandbox', 'w+', ['board4'],
221 outcome=OUTCOME_WARN)
223 # Second commit: The warnings should be listed
225 self.assertEqual(line.text, 'w+%s' %
226 errors[0].rstrip().replace('\n', '\nw+'))
227 self.assertEqual(line.colour, col.MAGENTA)
229 # Third commit: Still fails
230 self.assertEqual(next(lines).text, '03: %s' % commits[2][1])
231 self.assertSummary(next(lines).text, 'arm', '', ['board1'],
233 self.assertSummary(next(lines).text, 'powerpc', '+',
234 ['board2', 'board3'])
235 self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
237 # Expect a compiler error
239 self.assertEqual(line.text, '+%s' %
240 errors[1].rstrip().replace('\n', '\n+'))
241 self.assertEqual(line.colour, col.RED)
243 # Fourth commit: Compile errors are fixed, just have warning for board3
244 self.assertEqual(next(lines).text, '04: %s' % commits[3][1])
245 expect = '%10s: ' % 'powerpc'
246 expect += ' ' + col.Color(col.GREEN, '')
248 expect += col.Color(col.GREEN, ' %s' % 'board2')
249 expect += ' ' + col.Color(col.YELLOW, 'w+')
251 expect += col.Color(col.YELLOW, ' %s' % 'board3')
252 self.assertEqual(next(lines).text, expect)
253 self.assertSummary(next(lines).text, 'sandbox', 'w+', ['board4'],
254 outcome=OUTCOME_WARN)
256 # Compile error fixed
258 self.assertEqual(line.text, '-%s' %
259 errors[1].rstrip().replace('\n', '\n-'))
260 self.assertEqual(line.colour, col.GREEN)
263 self.assertEqual(line.text, 'w+%s' %
264 errors[2].rstrip().replace('\n', '\nw+'))
265 self.assertEqual(line.colour, col.MAGENTA)
268 self.assertEqual(next(lines).text, '05: %s' % commits[4][1])
269 self.assertSummary(next(lines).text, 'powerpc', '', ['board3'],
271 self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
273 # The second line of errors[3] is a duplicate, so buildman will drop it
274 expect = errors[3].rstrip().split('\n')
275 expect = [expect[0]] + expect[2:]
277 self.assertEqual(line.text, '+%s' %
278 '\n'.join(expect).replace('\n', '\n+'))
279 self.assertEqual(line.colour, col.RED)
282 self.assertEqual(line.text, 'w-%s' %
283 errors[2].rstrip().replace('\n', '\nw-'))
284 self.assertEqual(line.colour, col.CYAN)
287 self.assertEqual(next(lines).text, '06: %s' % commits[5][1])
288 self.assertSummary(next(lines).text, 'sandbox', '', ['board4'],
291 # The second line of errors[3] is a duplicate, so buildman will drop it
292 expect = errors[3].rstrip().split('\n')
293 expect = [expect[0]] + expect[2:]
295 self.assertEqual(line.text, '-%s' %
296 '\n'.join(expect).replace('\n', '\n-'))
297 self.assertEqual(line.colour, col.GREEN)
300 self.assertEqual(line.text, 'w-%s' %
301 errors[0].rstrip().replace('\n', '\nw-'))
302 self.assertEqual(line.colour, col.CYAN)
305 self.assertEqual(next(lines).text, '07: %s' % commits[6][1])
306 self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
308 # Pick out the correct error lines
309 expect_str = errors[4].rstrip().replace('%(basedir)s', '').split('\n')
310 expect = expect_str[3:8] + [expect_str[-1]]
312 self.assertEqual(line.text, '+%s' %
313 '\n'.join(expect).replace('\n', '\n+'))
314 self.assertEqual(line.colour, col.RED)
316 # Now the warnings lines
317 expect = [expect_str[0]] + expect_str[10:12] + [expect_str[9]]
319 self.assertEqual(line.text, 'w+%s' %
320 '\n'.join(expect).replace('\n', '\nw+'))
321 self.assertEqual(line.colour, col.MAGENTA)
324 """Test basic builder operation by building a branch"""
326 options.git = os.getcwd()
327 options.summary = False
329 options.dry_run = False
330 #options.git = os.path.join(self.base_dir, 'repo')
331 options.branch = 'test-buildman'
332 options.force_build = False
333 options.list_tool_chains = False
335 options.git_dir = None
336 options.threads = None
337 options.show_unknown = False
338 options.quick = False
339 options.show_errors = False
340 options.keep_outputs = False
342 control.DoBuildman(options, args)
344 def testBoardSingle(self):
345 """Test single board selection"""
346 self.assertEqual(self.boards.SelectBoards(['sandbox']),
347 ({'all': ['board4'], 'sandbox': ['board4']}, []))
349 def testBoardArch(self):
350 """Test single board selection"""
351 self.assertEqual(self.boards.SelectBoards(['arm']),
352 ({'all': ['board0', 'board1'],
353 'arm': ['board0', 'board1']}, []))
355 def testBoardArchSingle(self):
356 """Test single board selection"""
357 self.assertEqual(self.boards.SelectBoards(['arm sandbox']),
358 ({'sandbox': ['board4'],
359 'all': ['board0', 'board1', 'board4'],
360 'arm': ['board0', 'board1']}, []))
363 def testBoardArchSingleMultiWord(self):
364 """Test single board selection"""
365 self.assertEqual(self.boards.SelectBoards(['arm', 'sandbox']),
366 ({'sandbox': ['board4'],
367 'all': ['board0', 'board1', 'board4'],
368 'arm': ['board0', 'board1']}, []))
370 def testBoardSingleAnd(self):
371 """Test single board selection"""
372 self.assertEqual(self.boards.SelectBoards(['Tester & arm']),
373 ({'Tester&arm': ['board0', 'board1'],
374 'all': ['board0', 'board1']}, []))
376 def testBoardTwoAnd(self):
377 """Test single board selection"""
378 self.assertEqual(self.boards.SelectBoards(['Tester', '&', 'arm',
379 'Tester' '&', 'powerpc',
381 ({'sandbox': ['board4'],
382 'all': ['board0', 'board1', 'board2', 'board3',
384 'Tester&powerpc': ['board2', 'board3'],
385 'Tester&arm': ['board0', 'board1']}, []))
387 def testBoardAll(self):
388 """Test single board selection"""
389 self.assertEqual(self.boards.SelectBoards([]),
390 ({'all': ['board0', 'board1', 'board2', 'board3',
393 def testBoardRegularExpression(self):
394 """Test single board selection"""
395 self.assertEqual(self.boards.SelectBoards(['T.*r&^Po']),
396 ({'all': ['board2', 'board3'],
397 'T.*r&^Po': ['board2', 'board3']}, []))
399 def testBoardDuplicate(self):
400 """Test single board selection"""
401 self.assertEqual(self.boards.SelectBoards(['sandbox sandbox',
403 ({'all': ['board4'], 'sandbox': ['board4']}, []))
404 def CheckDirs(self, build, dirname):
405 self.assertEqual('base%s' % dirname, build._GetOutputDir(1))
406 self.assertEqual('base%s/fred' % dirname,
407 build.GetBuildDir(1, 'fred'))
408 self.assertEqual('base%s/fred/done' % dirname,
409 build.GetDoneFile(1, 'fred'))
410 self.assertEqual('base%s/fred/u-boot.sizes' % dirname,
411 build.GetFuncSizesFile(1, 'fred', 'u-boot'))
412 self.assertEqual('base%s/fred/u-boot.objdump' % dirname,
413 build.GetObjdumpFile(1, 'fred', 'u-boot'))
414 self.assertEqual('base%s/fred/err' % dirname,
415 build.GetErrFile(1, 'fred'))
417 def testOutputDir(self):
418 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
419 checkout=False, show_unknown=False)
420 build.commits = self.commits
421 build.commit_count = len(self.commits)
422 subject = self.commits[1].subject.translate(builder.trans_valid_chars)
423 dirname ='/%02d_of_%02d_g%s_%s' % (2, build.commit_count, commits[1][0],
425 self.CheckDirs(build, dirname)
427 def testOutputDirCurrent(self):
428 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
429 checkout=False, show_unknown=False)
431 build.commit_count = 0
432 self.CheckDirs(build, '/current')
434 def testOutputDirNoSubdirs(self):
435 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
436 checkout=False, show_unknown=False,
439 build.commit_count = 0
440 self.CheckDirs(build, '')
442 def testToolchainAliases(self):
443 self.assertTrue(self.toolchains.Select('arm') != None)
444 with self.assertRaises(ValueError):
445 self.toolchains.Select('no-arch')
446 with self.assertRaises(ValueError):
447 self.toolchains.Select('x86')
449 self.toolchains = toolchain.Toolchains()
450 self.toolchains.Add('x86_64-linux-gcc', test=False)
451 self.assertTrue(self.toolchains.Select('x86') != None)
453 self.toolchains = toolchain.Toolchains()
454 self.toolchains.Add('i386-linux-gcc', test=False)
455 self.assertTrue(self.toolchains.Select('x86') != None)
457 def testToolchainDownload(self):
458 """Test that we can download toolchains"""
460 with test_util.capture_sys_output() as (stdout, stderr):
461 url = self.toolchains.LocateArchUrl('arm')
462 self.assertRegexpMatches(url, 'https://www.kernel.org/pub/tools/'
463 'crosstool/files/bin/x86_64/.*/'
464 'x86_64-gcc-.*-nolibc_arm-.*linux-gnueabi.tar.xz')
466 def testGetEnvArgs(self):
467 """Test the GetEnvArgs() function"""
468 tc = self.toolchains.Select('arm')
469 self.assertEqual('arm-linux-',
470 tc.GetEnvArgs(toolchain.VAR_CROSS_COMPILE))
471 self.assertEqual('', tc.GetEnvArgs(toolchain.VAR_PATH))
472 self.assertEqual('arm',
473 tc.GetEnvArgs(toolchain.VAR_ARCH))
474 self.assertEqual('', tc.GetEnvArgs(toolchain.VAR_MAKE_ARGS))
476 self.toolchains.Add('/path/to/x86_64-linux-gcc', test=False)
477 tc = self.toolchains.Select('x86')
478 self.assertEqual('/path/to',
479 tc.GetEnvArgs(toolchain.VAR_PATH))
480 tc.override_toolchain = 'clang'
481 self.assertEqual('HOSTCC=clang CC=clang',
482 tc.GetEnvArgs(toolchain.VAR_MAKE_ARGS))
484 def testPrepareOutputSpace(self):
486 tools.WriteFile(os.path.join(base_dir, fname), b'')
488 base_dir = tempfile.mkdtemp()
490 # Add various files that we want removed and left alone
491 to_remove = ['01_of_22_g0982734987_title', '102_of_222_g92bf_title',
492 '01_of_22_g2938abd8_title']
493 to_leave = ['something_else', '01-something.patch', '01_of_22_another']
494 for name in to_remove + to_leave:
497 build = builder.Builder(self.toolchains, base_dir, None, 1, 2)
498 build.commits = self.commits
499 build.commit_count = len(commits)
500 result = set(build._GetOutputSpaceRemovals())
501 expected = set([os.path.join(base_dir, f) for f in to_remove])
502 self.assertEqual(expected, result)
504 if __name__ == "__main__":