blob: e81400f37259aa26c90556cc84e24bb5af26b888 [file] [log] [blame]
Simon Glassfc3fe1c2013-04-03 11:07:16 +00001#
2# Copyright (c) 2012 The Chromium OS Authors.
3#
Wolfgang Denk1a459662013-07-08 09:37:19 +02004# SPDX-License-Identifier: GPL-2.0+
Simon Glassfc3fe1c2013-04-03 11:07:16 +00005#
6
7import os
8import shutil
9import sys
10import tempfile
11import time
12import unittest
13
14# Bring in the patman libraries
15our_path = os.path.dirname(os.path.realpath(__file__))
16sys.path.append(os.path.join(our_path, '../patman'))
17
18import board
19import bsettings
20import builder
21import control
22import command
23import commit
Simon Glass6208fce2014-09-05 19:00:08 -060024import terminal
Simon Glassfc3fe1c2013-04-03 11:07:16 +000025import toolchain
26
Simon Glasscb39a102017-11-12 21:52:14 -070027use_network = True
28
Simon Glasscc935292014-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 Glassfc3fe1c2013-04-03 11:07:16 +000039errors = [
40 '''main.c: In function 'main_loop':
41main.c:260:6: warning: unused variable 'joe' [-Wunused-variable]
42''',
Simon Glass6208fce2014-09-05 19:00:08 -060043 '''main.c: In function 'main_loop2':
Simon Glassfc3fe1c2013-04-03 11:07:16 +000044main.c:295:2: error: 'fred' undeclared (first use in this function)
45main.c:295:2: note: each undeclared identifier is reported only once for each function it appears in
46make[1]: *** [main.o] Error 1
47make: *** [common/libcommon.o] Error 2
48Make failed
49''',
Simon Glass6208fce2014-09-05 19:00:08 -060050 '''main.c: In function 'main_loop3':
Simon Glassfc3fe1c2013-04-03 11:07:16 +000051main.c:280:6: warning: unused variable 'mary' [-Wunused-variable]
52''',
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 Glasse19d5782013-09-23 17:35:16 -060094 ['Active', 'sandbox', 'sandbox', '', 'Tester', 'Sandbox board', 'board4', ''],
Simon Glassfc3fe1c2013-04-03 11:07:16 +000095]
96
Simon Glass4466c1f2014-12-01 17:33:51 -070097BASE_DIR = 'base'
98
Simon Glassfc3fe1c2013-04-03 11:07:16 +000099class Options:
100 """Class that holds build options"""
101 pass
102
103class TestBuild(unittest.TestCase):
104 """Test buildman
105
106 TODO: Write tests for the rest of the functionality
107 """
108 def setUp(self):
109 # Set up commits to build
110 self.commits = []
111 sequence = 0
112 for commit_info in commits:
113 comm = commit.Commit(commit_info[0])
114 comm.subject = commit_info[1]
115 comm.return_code = commit_info[2]
116 comm.error_list = commit_info[3]
117 comm.sequence = sequence
118 sequence += 1
119 self.commits.append(comm)
120
121 # Set up boards to build
122 self.boards = board.Boards()
123 for brd in boards:
124 self.boards.AddBoard(board.Board(*brd))
125 self.boards.SelectBoards([])
126
Simon Glasscc935292014-12-01 17:34:04 -0700127 # Add some test settings
128 bsettings.Setup(None)
129 bsettings.AddFile(settings_data)
130
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000131 # Set up the toolchains
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000132 self.toolchains = toolchain.Toolchains()
133 self.toolchains.Add('arm-linux-gcc', test=False)
134 self.toolchains.Add('sparc-linux-gcc', test=False)
135 self.toolchains.Add('powerpc-linux-gcc', test=False)
136 self.toolchains.Add('gcc', test=False)
137
Simon Glass6208fce2014-09-05 19:00:08 -0600138 # Avoid sending any output
139 terminal.SetPrintTestMode()
140 self._col = terminal.Color()
141
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000142 def Make(self, commit, brd, stage, *args, **kwargs):
Simon Glass930c8d42014-09-05 19:00:21 -0600143 global base_dir
144
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000145 result = command.CommandResult()
146 boardnum = int(brd.target[-1])
147 result.return_code = 0
148 result.stderr = ''
149 result.stdout = ('This is the test output for board %s, commit %s' %
150 (brd.target, commit.hash))
Simon Glass930c8d42014-09-05 19:00:21 -0600151 if ((boardnum >= 1 and boardnum >= commit.sequence) or
152 boardnum == 4 and commit.sequence == 6):
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000153 result.return_code = commit.return_code
Simon Glass930c8d42014-09-05 19:00:21 -0600154 result.stderr = (''.join(commit.error_list)
155 % {'basedir' : base_dir + '/.bm-work/00/'})
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000156 if stage == 'build':
157 target_dir = None
158 for arg in args:
159 if arg.startswith('O='):
160 target_dir = arg[2:]
161
162 if not os.path.isdir(target_dir):
163 os.mkdir(target_dir)
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000164
165 result.combined = result.stdout + result.stderr
166 return result
167
Simon Glass6208fce2014-09-05 19:00:08 -0600168 def assertSummary(self, text, arch, plus, boards, ok=False):
169 col = self._col
170 expected_colour = col.GREEN if ok else col.RED
171 expect = '%10s: ' % arch
172 # TODO(sjg@chromium.org): If plus is '', we shouldn't need this
Simon Glass63c619e2015-02-05 22:06:11 -0700173 expect += ' ' + col.Color(expected_colour, plus)
Simon Glass6208fce2014-09-05 19:00:08 -0600174 expect += ' '
175 for board in boards:
176 expect += col.Color(expected_colour, ' %s' % board)
177 self.assertEqual(text, expect)
178
179 def testOutput(self):
180 """Test basic builder operation and output
181
182 This does a line-by-line verification of the summary output.
183 """
Simon Glass930c8d42014-09-05 19:00:21 -0600184 global base_dir
185
186 base_dir = tempfile.mkdtemp()
187 if not os.path.isdir(base_dir):
188 os.mkdir(base_dir)
189 build = builder.Builder(self.toolchains, base_dir, None, 1, 2,
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000190 checkout=False, show_unknown=False)
191 build.do_make = self.Make
192 board_selected = self.boards.GetSelectedDict()
193
Simon Glasse5a0e5d2014-08-09 15:33:03 -0600194 build.BuildBoards(self.commits, board_selected, keep_outputs=False,
195 verbose=False)
Simon Glass6208fce2014-09-05 19:00:08 -0600196 lines = terminal.GetPrintTestLines()
197 count = 0
198 for line in lines:
199 if line.text.strip():
200 count += 1
201
Simon Glass745b3952016-09-18 16:48:33 -0600202 # We should get two starting messages, then an update for every commit
Simon Glass6208fce2014-09-05 19:00:08 -0600203 # built.
Simon Glass745b3952016-09-18 16:48:33 -0600204 self.assertEqual(count, len(commits) * len(boards) + 2)
Simon Glassb2ea7ab2014-08-09 15:33:02 -0600205 build.SetDisplayOptions(show_errors=True);
206 build.ShowSummary(self.commits, board_selected)
Simon Glass930c8d42014-09-05 19:00:21 -0600207 #terminal.EchoPrintTestLines()
Simon Glass6208fce2014-09-05 19:00:08 -0600208 lines = terminal.GetPrintTestLines()
209 self.assertEqual(lines[0].text, '01: %s' % commits[0][1])
210 self.assertEqual(lines[1].text, '02: %s' % commits[1][1])
211
212 # We expect all archs to fail
213 col = terminal.Color()
214 self.assertSummary(lines[2].text, 'sandbox', '+', ['board4'])
215 self.assertSummary(lines[3].text, 'arm', '+', ['board1'])
216 self.assertSummary(lines[4].text, 'powerpc', '+', ['board2', 'board3'])
217
218 # Now we should have the compiler warning
219 self.assertEqual(lines[5].text, 'w+%s' %
220 errors[0].rstrip().replace('\n', '\nw+'))
221 self.assertEqual(lines[5].colour, col.MAGENTA)
222
223 self.assertEqual(lines[6].text, '03: %s' % commits[2][1])
224 self.assertSummary(lines[7].text, 'sandbox', '+', ['board4'])
225 self.assertSummary(lines[8].text, 'arm', '', ['board1'], ok=True)
226 self.assertSummary(lines[9].text, 'powerpc', '+', ['board2', 'board3'])
227
228 # Compiler error
229 self.assertEqual(lines[10].text, '+%s' %
230 errors[1].rstrip().replace('\n', '\n+'))
231
232 self.assertEqual(lines[11].text, '04: %s' % commits[3][1])
233 self.assertSummary(lines[12].text, 'sandbox', '', ['board4'], ok=True)
234 self.assertSummary(lines[13].text, 'powerpc', '', ['board2', 'board3'],
235 ok=True)
236
237 # Compile error fixed
238 self.assertEqual(lines[14].text, '-%s' %
239 errors[1].rstrip().replace('\n', '\n-'))
240 self.assertEqual(lines[14].colour, col.GREEN)
241
242 self.assertEqual(lines[15].text, 'w+%s' %
243 errors[2].rstrip().replace('\n', '\nw+'))
244 self.assertEqual(lines[15].colour, col.MAGENTA)
245
246 self.assertEqual(lines[16].text, '05: %s' % commits[4][1])
247 self.assertSummary(lines[17].text, 'sandbox', '+', ['board4'])
248 self.assertSummary(lines[18].text, 'powerpc', '', ['board3'], ok=True)
249
250 # The second line of errors[3] is a duplicate, so buildman will drop it
251 expect = errors[3].rstrip().split('\n')
252 expect = [expect[0]] + expect[2:]
253 self.assertEqual(lines[19].text, '+%s' %
254 '\n'.join(expect).replace('\n', '\n+'))
255
256 self.assertEqual(lines[20].text, 'w-%s' %
257 errors[2].rstrip().replace('\n', '\nw-'))
258
259 self.assertEqual(lines[21].text, '06: %s' % commits[5][1])
260 self.assertSummary(lines[22].text, 'sandbox', '', ['board4'], ok=True)
261
262 # The second line of errors[3] is a duplicate, so buildman will drop it
263 expect = errors[3].rstrip().split('\n')
264 expect = [expect[0]] + expect[2:]
265 self.assertEqual(lines[23].text, '-%s' %
266 '\n'.join(expect).replace('\n', '\n-'))
267
268 self.assertEqual(lines[24].text, 'w-%s' %
269 errors[0].rstrip().replace('\n', '\nw-'))
270
Simon Glass930c8d42014-09-05 19:00:21 -0600271 self.assertEqual(lines[25].text, '07: %s' % commits[6][1])
272 self.assertSummary(lines[26].text, 'sandbox', '+', ['board4'])
273
274 # Pick out the correct error lines
275 expect_str = errors[4].rstrip().replace('%(basedir)s', '').split('\n')
276 expect = expect_str[3:8] + [expect_str[-1]]
277 self.assertEqual(lines[27].text, '+%s' %
278 '\n'.join(expect).replace('\n', '\n+'))
279
280 # Now the warnings lines
281 expect = [expect_str[0]] + expect_str[10:12] + [expect_str[9]]
282 self.assertEqual(lines[28].text, 'w+%s' %
283 '\n'.join(expect).replace('\n', '\nw+'))
284
285 self.assertEqual(len(lines), 29)
286 shutil.rmtree(base_dir)
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000287
288 def _testGit(self):
289 """Test basic builder operation by building a branch"""
290 base_dir = tempfile.mkdtemp()
291 if not os.path.isdir(base_dir):
292 os.mkdir(base_dir)
293 options = Options()
294 options.git = os.getcwd()
295 options.summary = False
296 options.jobs = None
297 options.dry_run = False
298 #options.git = os.path.join(base_dir, 'repo')
299 options.branch = 'test-buildman'
300 options.force_build = False
301 options.list_tool_chains = False
302 options.count = -1
303 options.git_dir = None
304 options.threads = None
305 options.show_unknown = False
306 options.quick = False
307 options.show_errors = False
308 options.keep_outputs = False
309 args = ['tegra20']
310 control.DoBuildman(options, args)
Simon Glass930c8d42014-09-05 19:00:21 -0600311 shutil.rmtree(base_dir)
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000312
Simon Glass6131bea2014-08-09 15:33:08 -0600313 def testBoardSingle(self):
314 """Test single board selection"""
315 self.assertEqual(self.boards.SelectBoards(['sandbox']),
316 {'all': 1, 'sandbox': 1})
317
318 def testBoardArch(self):
319 """Test single board selection"""
320 self.assertEqual(self.boards.SelectBoards(['arm']),
321 {'all': 2, 'arm': 2})
322
323 def testBoardArchSingle(self):
324 """Test single board selection"""
325 self.assertEqual(self.boards.SelectBoards(['arm sandbox']),
326 {'all': 3, 'arm': 2, 'sandbox' : 1})
327
328 def testBoardArchSingleMultiWord(self):
329 """Test single board selection"""
330 self.assertEqual(self.boards.SelectBoards(['arm', 'sandbox']),
331 {'all': 3, 'arm': 2, 'sandbox' : 1})
332
333 def testBoardSingleAnd(self):
334 """Test single board selection"""
335 self.assertEqual(self.boards.SelectBoards(['Tester & arm']),
336 {'all': 2, 'Tester&arm': 2})
337
338 def testBoardTwoAnd(self):
339 """Test single board selection"""
340 self.assertEqual(self.boards.SelectBoards(['Tester', '&', 'arm',
341 'Tester' '&', 'powerpc',
342 'sandbox']),
343 {'all': 5, 'Tester&powerpc': 2, 'Tester&arm': 2,
344 'sandbox' : 1})
345
346 def testBoardAll(self):
347 """Test single board selection"""
348 self.assertEqual(self.boards.SelectBoards([]), {'all': 5})
349
350 def testBoardRegularExpression(self):
351 """Test single board selection"""
352 self.assertEqual(self.boards.SelectBoards(['T.*r&^Po']),
353 {'T.*r&^Po': 2, 'all': 2})
354
355 def testBoardDuplicate(self):
356 """Test single board selection"""
357 self.assertEqual(self.boards.SelectBoards(['sandbox sandbox',
358 'sandbox']),
359 {'all': 1, 'sandbox': 1})
Simon Glass4466c1f2014-12-01 17:33:51 -0700360 def CheckDirs(self, build, dirname):
361 self.assertEqual('base%s' % dirname, build._GetOutputDir(1))
362 self.assertEqual('base%s/fred' % dirname,
363 build.GetBuildDir(1, 'fred'))
364 self.assertEqual('base%s/fred/done' % dirname,
365 build.GetDoneFile(1, 'fred'))
366 self.assertEqual('base%s/fred/u-boot.sizes' % dirname,
367 build.GetFuncSizesFile(1, 'fred', 'u-boot'))
368 self.assertEqual('base%s/fred/u-boot.objdump' % dirname,
369 build.GetObjdumpFile(1, 'fred', 'u-boot'))
370 self.assertEqual('base%s/fred/err' % dirname,
371 build.GetErrFile(1, 'fred'))
372
373 def testOutputDir(self):
374 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
375 checkout=False, show_unknown=False)
376 build.commits = self.commits
377 build.commit_count = len(self.commits)
378 subject = self.commits[1].subject.translate(builder.trans_valid_chars)
379 dirname ='/%02d_of_%02d_g%s_%s' % (2, build.commit_count, commits[1][0],
380 subject[:20])
381 self.CheckDirs(build, dirname)
382
383 def testOutputDirCurrent(self):
384 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
385 checkout=False, show_unknown=False)
386 build.commits = None
387 build.commit_count = 0
388 self.CheckDirs(build, '/current')
Simon Glass6131bea2014-08-09 15:33:08 -0600389
Simon Glass5971ab52014-12-01 17:33:55 -0700390 def testOutputDirNoSubdirs(self):
391 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
392 checkout=False, show_unknown=False,
393 no_subdirs=True)
394 build.commits = None
395 build.commit_count = 0
396 self.CheckDirs(build, '')
397
Simon Glass9b83bfd2014-12-01 17:34:05 -0700398 def testToolchainAliases(self):
399 self.assertTrue(self.toolchains.Select('arm') != None)
400 with self.assertRaises(ValueError):
401 self.toolchains.Select('no-arch')
402 with self.assertRaises(ValueError):
403 self.toolchains.Select('x86')
404
405 self.toolchains = toolchain.Toolchains()
406 self.toolchains.Add('x86_64-linux-gcc', test=False)
407 self.assertTrue(self.toolchains.Select('x86') != None)
408
409 self.toolchains = toolchain.Toolchains()
410 self.toolchains.Add('i386-linux-gcc', test=False)
411 self.assertTrue(self.toolchains.Select('x86') != None)
412
Simon Glass827e37b2014-12-01 17:34:06 -0700413 def testToolchainDownload(self):
414 """Test that we can download toolchains"""
Simon Glasscb39a102017-11-12 21:52:14 -0700415 if use_network:
416 self.assertEqual('https://www.kernel.org/pub/tools/crosstool/files/bin/x86_64/4.9.0/x86_64-gcc-4.9.0-nolibc_arm-unknown-linux-gnueabi.tar.xz',
417 self.toolchains.LocateArchUrl('arm'))
Simon Glass827e37b2014-12-01 17:34:06 -0700418
419
Simon Glassfc3fe1c2013-04-03 11:07:16 +0000420if __name__ == "__main__":
421 unittest.main()