blob: 5c82c5079389be88e78c07701383c8244cbf0875 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glassfc3fe1c2013-04-03 11:07:16 +00002# Copyright (c) 2012 The Chromium OS Authors.
3#
Simon Glassfc3fe1c2013-04-03 11:07:16 +00004
5import os
6import shutil
7import sys
8import tempfile
9import time
10import unittest
11
12# Bring in the patman libraries
13our_path = os.path.dirname(os.path.realpath(__file__))
14sys.path.append(os.path.join(our_path, '../patman'))
15
16import board
17import bsettings
18import builder
19import control
20import command
21import commit
Simon Glass6208fce2014-09-05 19:00:08 -060022import terminal
Simon Glass4b4bc062018-10-01 21:12:43 -060023import test_util
Simon Glassfc3fe1c2013-04-03 11:07:16 +000024import toolchain
25
Simon Glasscb39a102017-11-12 21:52:14 -070026use_network = True
27
Simon Glasscc935292014-12-01 17:34:04 -070028settings_data = '''
29# Buildman settings file
30
31[toolchain]
32main: /usr/sbin
33
34[toolchain-alias]
35x86: i386 x86_64
36'''
37
Simon Glassfc3fe1c2013-04-03 11:07:16 +000038errors = [
39 '''main.c: In function 'main_loop':
40main.c:260:6: warning: unused variable 'joe' [-Wunused-variable]
41''',
Simon Glass6208fce2014-09-05 19:00:08 -060042 '''main.c: In function 'main_loop2':
Simon Glassfc3fe1c2013-04-03 11:07:16 +000043main.c:295:2: error: 'fred' undeclared (first use in this function)
44main.c:295:2: note: each undeclared identifier is reported only once for each function it appears in
45make[1]: *** [main.o] Error 1
46make: *** [common/libcommon.o] Error 2
47Make failed
48''',
Simon Glass2d483332018-11-06 16:02:11 -070049 '''arch/arm/dts/socfpga_arria10_socdk_sdmmc.dtb: Warning \
50(avoid_unnecessary_addr_size): /clocks: unnecessary #address-cells/#size-cells \
51without "ranges" or child "reg" property
Simon Glassfc3fe1c2013-04-03 11:07:16 +000052''',
53 '''powerpc-linux-ld: warning: dot moved backwards before `.bss'
54powerpc-linux-ld: warning: dot moved backwards before `.bss'
55powerpc-linux-ld: u-boot: section .text lma 0xfffc0000 overlaps previous sections
56powerpc-linux-ld: u-boot: section .rodata lma 0xfffef3ec overlaps previous sections
57powerpc-linux-ld: u-boot: section .reloc lma 0xffffa400 overlaps previous sections
58powerpc-linux-ld: u-boot: section .data lma 0xffffcd38 overlaps previous sections
59powerpc-linux-ld: u-boot: section .u_boot_cmd lma 0xffffeb40 overlaps previous sections
60powerpc-linux-ld: u-boot: section .bootpg lma 0xfffff198 overlaps previous sections
Simon Glass930c8d42014-09-05 19:00:21 -060061''',
62 '''In file included from %(basedir)sarch/sandbox/cpu/cpu.c:9:0:
63%(basedir)sarch/sandbox/include/asm/state.h:44:0: warning: "xxxx" redefined [enabled by default]
64%(basedir)sarch/sandbox/include/asm/state.h:43:0: note: this is the location of the previous definition
65%(basedir)sarch/sandbox/cpu/cpu.c: In function 'do_reset':
66%(basedir)sarch/sandbox/cpu/cpu.c:27:1: error: unknown type name 'blah'
67%(basedir)sarch/sandbox/cpu/cpu.c:28:12: error: expected declaration specifiers or '...' before numeric constant
68make[2]: *** [arch/sandbox/cpu/cpu.o] Error 1
69make[1]: *** [arch/sandbox/cpu] Error 2
70make[1]: *** Waiting for unfinished jobs....
71In file included from %(basedir)scommon/board_f.c:55: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
74make: *** [sub-make] Error 2
Simon Glassfc3fe1c2013-04-03 11:07:16 +000075'''
76]
77
78
79# hash, subject, return code, list of errors/warnings
80commits = [
81 ['1234', 'upstream/master, ok', 0, []],
82 ['5678', 'Second commit, a warning', 0, errors[0:1]],
83 ['9012', 'Third commit, error', 1, errors[0:2]],
84 ['3456', 'Fourth commit, warning', 0, [errors[0], errors[2]]],
85 ['7890', 'Fifth commit, link errors', 1, [errors[0], errors[3]]],
Simon Glass930c8d42014-09-05 19:00:21 -060086 ['abcd', 'Sixth commit, fixes all errors', 0, []],
87 ['ef01', 'Seventh commit, check directory suppression', 1, [errors[4]]],
Simon Glassfc3fe1c2013-04-03 11:07:16 +000088]
89
90boards = [
Simon Glasse19d5782013-09-23 17:35:16 -060091 ['Active', 'arm', 'armv7', '', 'Tester', 'ARM Board 1', 'board0', ''],
92 ['Active', 'arm', 'armv7', '', 'Tester', 'ARM Board 2', 'board1', ''],
93 ['Active', 'powerpc', 'powerpc', '', 'Tester', 'PowerPC board 1', 'board2', ''],
Simon Glass251f5862017-11-12 21:52:15 -070094 ['Active', 'powerpc', 'mpc83xx', '', 'Tester', 'PowerPC board 2', 'board3', ''],
Simon Glasse19d5782013-09-23 17:35:16 -060095 ['Active', 'sandbox', 'sandbox', '', 'Tester', 'Sandbox board', 'board4', ''],
Simon Glassfc3fe1c2013-04-03 11:07:16 +000096]
97
Simon Glass4466c1f2014-12-01 17:33:51 -070098BASE_DIR = 'base'
99
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000100class Options:
101 """Class that holds build options"""
102 pass
103
104class TestBuild(unittest.TestCase):
105 """Test buildman
106
107 TODO: Write tests for the rest of the functionality
108 """
109 def setUp(self):
110 # Set up commits to build
111 self.commits = []
112 sequence = 0
113 for commit_info in commits:
114 comm = commit.Commit(commit_info[0])
115 comm.subject = commit_info[1]
116 comm.return_code = commit_info[2]
117 comm.error_list = commit_info[3]
118 comm.sequence = sequence
119 sequence += 1
120 self.commits.append(comm)
121
122 # Set up boards to build
123 self.boards = board.Boards()
124 for brd in boards:
125 self.boards.AddBoard(board.Board(*brd))
126 self.boards.SelectBoards([])
127
Simon Glasscc935292014-12-01 17:34:04 -0700128 # Add some test settings
129 bsettings.Setup(None)
130 bsettings.AddFile(settings_data)
131
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000132 # Set up the toolchains
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000133 self.toolchains = toolchain.Toolchains()
134 self.toolchains.Add('arm-linux-gcc', test=False)
135 self.toolchains.Add('sparc-linux-gcc', test=False)
136 self.toolchains.Add('powerpc-linux-gcc', test=False)
137 self.toolchains.Add('gcc', test=False)
138
Simon Glass6208fce2014-09-05 19:00:08 -0600139 # Avoid sending any output
140 terminal.SetPrintTestMode()
141 self._col = terminal.Color()
142
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000143 def Make(self, commit, brd, stage, *args, **kwargs):
Simon Glass930c8d42014-09-05 19:00:21 -0600144 global base_dir
145
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000146 result = command.CommandResult()
147 boardnum = int(brd.target[-1])
148 result.return_code = 0
149 result.stderr = ''
150 result.stdout = ('This is the test output for board %s, commit %s' %
151 (brd.target, commit.hash))
Simon Glass930c8d42014-09-05 19:00:21 -0600152 if ((boardnum >= 1 and boardnum >= commit.sequence) or
153 boardnum == 4 and commit.sequence == 6):
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000154 result.return_code = commit.return_code
Simon Glass930c8d42014-09-05 19:00:21 -0600155 result.stderr = (''.join(commit.error_list)
156 % {'basedir' : base_dir + '/.bm-work/00/'})
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000157 if stage == 'build':
158 target_dir = None
159 for arg in args:
160 if arg.startswith('O='):
161 target_dir = arg[2:]
162
163 if not os.path.isdir(target_dir):
164 os.mkdir(target_dir)
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000165
166 result.combined = result.stdout + result.stderr
167 return result
168
Simon Glass6208fce2014-09-05 19:00:08 -0600169 def assertSummary(self, text, arch, plus, boards, ok=False):
170 col = self._col
171 expected_colour = col.GREEN if ok else col.RED
172 expect = '%10s: ' % arch
173 # TODO(sjg@chromium.org): If plus is '', we shouldn't need this
Simon Glass63c619e2015-02-05 22:06:11 -0700174 expect += ' ' + col.Color(expected_colour, plus)
Simon Glass6208fce2014-09-05 19:00:08 -0600175 expect += ' '
176 for board in boards:
177 expect += col.Color(expected_colour, ' %s' % board)
178 self.assertEqual(text, expect)
179
180 def testOutput(self):
181 """Test basic builder operation and output
182
183 This does a line-by-line verification of the summary output.
184 """
Simon Glass930c8d42014-09-05 19:00:21 -0600185 global base_dir
186
187 base_dir = tempfile.mkdtemp()
188 if not os.path.isdir(base_dir):
189 os.mkdir(base_dir)
190 build = builder.Builder(self.toolchains, base_dir, None, 1, 2,
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000191 checkout=False, show_unknown=False)
192 build.do_make = self.Make
193 board_selected = self.boards.GetSelectedDict()
194
Simon Glasse5a0e5d2014-08-09 15:33:03 -0600195 build.BuildBoards(self.commits, board_selected, keep_outputs=False,
196 verbose=False)
Simon Glass6208fce2014-09-05 19:00:08 -0600197 lines = terminal.GetPrintTestLines()
198 count = 0
199 for line in lines:
200 if line.text.strip():
201 count += 1
202
Simon Glass745b3952016-09-18 16:48:33 -0600203 # We should get two starting messages, then an update for every commit
Simon Glass6208fce2014-09-05 19:00:08 -0600204 # built.
Simon Glass745b3952016-09-18 16:48:33 -0600205 self.assertEqual(count, len(commits) * len(boards) + 2)
Simon Glassb2ea7ab2014-08-09 15:33:02 -0600206 build.SetDisplayOptions(show_errors=True);
207 build.ShowSummary(self.commits, board_selected)
Simon Glass930c8d42014-09-05 19:00:21 -0600208 #terminal.EchoPrintTestLines()
Simon Glass6208fce2014-09-05 19:00:08 -0600209 lines = terminal.GetPrintTestLines()
210 self.assertEqual(lines[0].text, '01: %s' % commits[0][1])
211 self.assertEqual(lines[1].text, '02: %s' % commits[1][1])
212
213 # We expect all archs to fail
214 col = terminal.Color()
215 self.assertSummary(lines[2].text, 'sandbox', '+', ['board4'])
216 self.assertSummary(lines[3].text, 'arm', '+', ['board1'])
217 self.assertSummary(lines[4].text, 'powerpc', '+', ['board2', 'board3'])
218
219 # Now we should have the compiler warning
220 self.assertEqual(lines[5].text, 'w+%s' %
221 errors[0].rstrip().replace('\n', '\nw+'))
222 self.assertEqual(lines[5].colour, col.MAGENTA)
223
224 self.assertEqual(lines[6].text, '03: %s' % commits[2][1])
225 self.assertSummary(lines[7].text, 'sandbox', '+', ['board4'])
226 self.assertSummary(lines[8].text, 'arm', '', ['board1'], ok=True)
227 self.assertSummary(lines[9].text, 'powerpc', '+', ['board2', 'board3'])
228
229 # Compiler error
230 self.assertEqual(lines[10].text, '+%s' %
231 errors[1].rstrip().replace('\n', '\n+'))
232
233 self.assertEqual(lines[11].text, '04: %s' % commits[3][1])
234 self.assertSummary(lines[12].text, 'sandbox', '', ['board4'], ok=True)
235 self.assertSummary(lines[13].text, 'powerpc', '', ['board2', 'board3'],
236 ok=True)
237
238 # Compile error fixed
239 self.assertEqual(lines[14].text, '-%s' %
240 errors[1].rstrip().replace('\n', '\n-'))
241 self.assertEqual(lines[14].colour, col.GREEN)
242
243 self.assertEqual(lines[15].text, 'w+%s' %
244 errors[2].rstrip().replace('\n', '\nw+'))
245 self.assertEqual(lines[15].colour, col.MAGENTA)
246
247 self.assertEqual(lines[16].text, '05: %s' % commits[4][1])
248 self.assertSummary(lines[17].text, 'sandbox', '+', ['board4'])
249 self.assertSummary(lines[18].text, 'powerpc', '', ['board3'], ok=True)
250
251 # The second line of errors[3] is a duplicate, so buildman will drop it
252 expect = errors[3].rstrip().split('\n')
253 expect = [expect[0]] + expect[2:]
254 self.assertEqual(lines[19].text, '+%s' %
255 '\n'.join(expect).replace('\n', '\n+'))
256
257 self.assertEqual(lines[20].text, 'w-%s' %
258 errors[2].rstrip().replace('\n', '\nw-'))
259
260 self.assertEqual(lines[21].text, '06: %s' % commits[5][1])
261 self.assertSummary(lines[22].text, 'sandbox', '', ['board4'], ok=True)
262
263 # The second line of errors[3] is a duplicate, so buildman will drop it
264 expect = errors[3].rstrip().split('\n')
265 expect = [expect[0]] + expect[2:]
266 self.assertEqual(lines[23].text, '-%s' %
267 '\n'.join(expect).replace('\n', '\n-'))
268
269 self.assertEqual(lines[24].text, 'w-%s' %
270 errors[0].rstrip().replace('\n', '\nw-'))
271
Simon Glass930c8d42014-09-05 19:00:21 -0600272 self.assertEqual(lines[25].text, '07: %s' % commits[6][1])
273 self.assertSummary(lines[26].text, 'sandbox', '+', ['board4'])
274
275 # Pick out the correct error lines
276 expect_str = errors[4].rstrip().replace('%(basedir)s', '').split('\n')
277 expect = expect_str[3:8] + [expect_str[-1]]
278 self.assertEqual(lines[27].text, '+%s' %
279 '\n'.join(expect).replace('\n', '\n+'))
280
281 # Now the warnings lines
282 expect = [expect_str[0]] + expect_str[10:12] + [expect_str[9]]
283 self.assertEqual(lines[28].text, 'w+%s' %
284 '\n'.join(expect).replace('\n', '\nw+'))
285
286 self.assertEqual(len(lines), 29)
287 shutil.rmtree(base_dir)
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000288
289 def _testGit(self):
290 """Test basic builder operation by building a branch"""
291 base_dir = tempfile.mkdtemp()
292 if not os.path.isdir(base_dir):
293 os.mkdir(base_dir)
294 options = Options()
295 options.git = os.getcwd()
296 options.summary = False
297 options.jobs = None
298 options.dry_run = False
299 #options.git = os.path.join(base_dir, 'repo')
300 options.branch = 'test-buildman'
301 options.force_build = False
302 options.list_tool_chains = False
303 options.count = -1
304 options.git_dir = None
305 options.threads = None
306 options.show_unknown = False
307 options.quick = False
308 options.show_errors = False
309 options.keep_outputs = False
310 args = ['tegra20']
311 control.DoBuildman(options, args)
Simon Glass930c8d42014-09-05 19:00:21 -0600312 shutil.rmtree(base_dir)
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000313
Simon Glass6131bea2014-08-09 15:33:08 -0600314 def testBoardSingle(self):
315 """Test single board selection"""
316 self.assertEqual(self.boards.SelectBoards(['sandbox']),
Simon Glass06890362018-06-11 23:26:46 -0600317 ({'all': ['board4'], 'sandbox': ['board4']}, []))
Simon Glass6131bea2014-08-09 15:33:08 -0600318
319 def testBoardArch(self):
320 """Test single board selection"""
321 self.assertEqual(self.boards.SelectBoards(['arm']),
Simon Glass06890362018-06-11 23:26:46 -0600322 ({'all': ['board0', 'board1'],
323 'arm': ['board0', 'board1']}, []))
Simon Glass6131bea2014-08-09 15:33:08 -0600324
325 def testBoardArchSingle(self):
326 """Test single board selection"""
327 self.assertEqual(self.boards.SelectBoards(['arm sandbox']),
Simon Glass06890362018-06-11 23:26:46 -0600328 ({'sandbox': ['board4'],
Simon Glass251f5862017-11-12 21:52:15 -0700329 'all': ['board0', 'board1', 'board4'],
Simon Glass06890362018-06-11 23:26:46 -0600330 'arm': ['board0', 'board1']}, []))
Simon Glass251f5862017-11-12 21:52:15 -0700331
Simon Glass6131bea2014-08-09 15:33:08 -0600332
333 def testBoardArchSingleMultiWord(self):
334 """Test single board selection"""
335 self.assertEqual(self.boards.SelectBoards(['arm', 'sandbox']),
Simon Glass06890362018-06-11 23:26:46 -0600336 ({'sandbox': ['board4'],
337 'all': ['board0', 'board1', 'board4'],
338 'arm': ['board0', 'board1']}, []))
Simon Glass6131bea2014-08-09 15:33:08 -0600339
340 def testBoardSingleAnd(self):
341 """Test single board selection"""
342 self.assertEqual(self.boards.SelectBoards(['Tester & arm']),
Simon Glass06890362018-06-11 23:26:46 -0600343 ({'Tester&arm': ['board0', 'board1'],
344 'all': ['board0', 'board1']}, []))
Simon Glass6131bea2014-08-09 15:33:08 -0600345
346 def testBoardTwoAnd(self):
347 """Test single board selection"""
348 self.assertEqual(self.boards.SelectBoards(['Tester', '&', 'arm',
349 'Tester' '&', 'powerpc',
350 'sandbox']),
Simon Glass06890362018-06-11 23:26:46 -0600351 ({'sandbox': ['board4'],
Simon Glass251f5862017-11-12 21:52:15 -0700352 'all': ['board0', 'board1', 'board2', 'board3',
353 'board4'],
354 'Tester&powerpc': ['board2', 'board3'],
Simon Glass06890362018-06-11 23:26:46 -0600355 'Tester&arm': ['board0', 'board1']}, []))
Simon Glass6131bea2014-08-09 15:33:08 -0600356
357 def testBoardAll(self):
358 """Test single board selection"""
Simon Glass251f5862017-11-12 21:52:15 -0700359 self.assertEqual(self.boards.SelectBoards([]),
Simon Glass06890362018-06-11 23:26:46 -0600360 ({'all': ['board0', 'board1', 'board2', 'board3',
361 'board4']}, []))
Simon Glass6131bea2014-08-09 15:33:08 -0600362
363 def testBoardRegularExpression(self):
364 """Test single board selection"""
365 self.assertEqual(self.boards.SelectBoards(['T.*r&^Po']),
Simon Glass06890362018-06-11 23:26:46 -0600366 ({'all': ['board2', 'board3'],
367 'T.*r&^Po': ['board2', 'board3']}, []))
Simon Glass6131bea2014-08-09 15:33:08 -0600368
369 def testBoardDuplicate(self):
370 """Test single board selection"""
371 self.assertEqual(self.boards.SelectBoards(['sandbox sandbox',
372 'sandbox']),
Simon Glass06890362018-06-11 23:26:46 -0600373 ({'all': ['board4'], 'sandbox': ['board4']}, []))
Simon Glass4466c1f2014-12-01 17:33:51 -0700374 def CheckDirs(self, build, dirname):
375 self.assertEqual('base%s' % dirname, build._GetOutputDir(1))
376 self.assertEqual('base%s/fred' % dirname,
377 build.GetBuildDir(1, 'fred'))
378 self.assertEqual('base%s/fred/done' % dirname,
379 build.GetDoneFile(1, 'fred'))
380 self.assertEqual('base%s/fred/u-boot.sizes' % dirname,
381 build.GetFuncSizesFile(1, 'fred', 'u-boot'))
382 self.assertEqual('base%s/fred/u-boot.objdump' % dirname,
383 build.GetObjdumpFile(1, 'fred', 'u-boot'))
384 self.assertEqual('base%s/fred/err' % dirname,
385 build.GetErrFile(1, 'fred'))
386
387 def testOutputDir(self):
388 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
389 checkout=False, show_unknown=False)
390 build.commits = self.commits
391 build.commit_count = len(self.commits)
392 subject = self.commits[1].subject.translate(builder.trans_valid_chars)
393 dirname ='/%02d_of_%02d_g%s_%s' % (2, build.commit_count, commits[1][0],
394 subject[:20])
395 self.CheckDirs(build, dirname)
396
397 def testOutputDirCurrent(self):
398 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
399 checkout=False, show_unknown=False)
400 build.commits = None
401 build.commit_count = 0
402 self.CheckDirs(build, '/current')
Simon Glass6131bea2014-08-09 15:33:08 -0600403
Simon Glass5971ab52014-12-01 17:33:55 -0700404 def testOutputDirNoSubdirs(self):
405 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
406 checkout=False, show_unknown=False,
407 no_subdirs=True)
408 build.commits = None
409 build.commit_count = 0
410 self.CheckDirs(build, '')
411
Simon Glass9b83bfd2014-12-01 17:34:05 -0700412 def testToolchainAliases(self):
413 self.assertTrue(self.toolchains.Select('arm') != None)
414 with self.assertRaises(ValueError):
415 self.toolchains.Select('no-arch')
416 with self.assertRaises(ValueError):
417 self.toolchains.Select('x86')
418
419 self.toolchains = toolchain.Toolchains()
420 self.toolchains.Add('x86_64-linux-gcc', test=False)
421 self.assertTrue(self.toolchains.Select('x86') != None)
422
423 self.toolchains = toolchain.Toolchains()
424 self.toolchains.Add('i386-linux-gcc', test=False)
425 self.assertTrue(self.toolchains.Select('x86') != None)
426
Simon Glass827e37b2014-12-01 17:34:06 -0700427 def testToolchainDownload(self):
428 """Test that we can download toolchains"""
Simon Glasscb39a102017-11-12 21:52:14 -0700429 if use_network:
Simon Glass4b4bc062018-10-01 21:12:43 -0600430 with test_util.capture_sys_output() as (stdout, stderr):
431 url = self.toolchains.LocateArchUrl('arm')
Simon Glassda753e32018-10-01 21:12:35 -0600432 self.assertRegexpMatches(url, 'https://www.kernel.org/pub/tools/'
433 'crosstool/files/bin/x86_64/.*/'
434 'x86_64-gcc-.*-nolibc_arm-.*linux-gnueabi.tar.xz')
Simon Glass827e37b2014-12-01 17:34:06 -0700435
436
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000437if __name__ == "__main__":
438 unittest.main()