1# 2# Copyright (c) 2012 The Chromium OS Authors. 3# 4# SPDX-License-Identifier: GPL-2.0+ 5# 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 24import terminal 25import toolchain 26 27use_network = True 28 29settings_data = ''' 30# Buildman settings file 31 32[toolchain] 33main: /usr/sbin 34 35[toolchain-alias] 36x86: i386 x86_64 37''' 38 39errors = [ 40 '''main.c: In function 'main_loop': 41main.c:260:6: warning: unused variable 'joe' [-Wunused-variable] 42''', 43 '''main.c: In function 'main_loop2': 44main.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''', 50 '''main.c: In function 'main_loop3': 51main.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 61''', 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 75''' 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]]], 86 ['abcd', 'Sixth commit, fixes all errors', 0, []], 87 ['ef01', 'Seventh commit, check directory suppression', 1, [errors[4]]], 88] 89 90boards = [ 91 ['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', ''], 94 ['Active', 'powerpc', 'mpc83xx', '', 'Tester', 'PowerPC board 2', 'board3', ''], 95 ['Active', 'sandbox', 'sandbox', '', 'Tester', 'Sandbox board', 'board4', ''], 96] 97 98BASE_DIR = 'base' 99 100class 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 128 # Add some test settings 129 bsettings.Setup(None) 130 bsettings.AddFile(settings_data) 131 132 # Set up the toolchains 133 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 139 # Avoid sending any output 140 terminal.SetPrintTestMode() 141 self._col = terminal.Color() 142 143 def Make(self, commit, brd, stage, *args, **kwargs): 144 global base_dir 145 146 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)) 152 if ((boardnum >= 1 and boardnum >= commit.sequence) or 153 boardnum == 4 and commit.sequence == 6): 154 result.return_code = commit.return_code 155 result.stderr = (''.join(commit.error_list) 156 % {'basedir' : base_dir + '/.bm-work/00/'}) 157 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) 165 166 result.combined = result.stdout + result.stderr 167 return result 168 169 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 174 expect += ' ' + col.Color(expected_colour, plus) 175 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 """ 185 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, 191 checkout=False, show_unknown=False) 192 build.do_make = self.Make 193 board_selected = self.boards.GetSelectedDict() 194 195 build.BuildBoards(self.commits, board_selected, keep_outputs=False, 196 verbose=False) 197 lines = terminal.GetPrintTestLines() 198 count = 0 199 for line in lines: 200 if line.text.strip(): 201 count += 1 202 203 # We should get two starting messages, then an update for every commit 204 # built. 205 self.assertEqual(count, len(commits) * len(boards) + 2) 206 build.SetDisplayOptions(show_errors=True); 207 build.ShowSummary(self.commits, board_selected) 208 #terminal.EchoPrintTestLines() 209 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 272 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) 288 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) 312 shutil.rmtree(base_dir) 313 314 def testBoardSingle(self): 315 """Test single board selection""" 316 self.assertEqual(self.boards.SelectBoards(['sandbox']), 317 {'all': ['board4'], 'sandbox': ['board4']}) 318 319 def testBoardArch(self): 320 """Test single board selection""" 321 self.assertEqual(self.boards.SelectBoards(['arm']), 322 {'all': ['board0', 'board1'], 323 'arm': ['board0', 'board1']}) 324 325 def testBoardArchSingle(self): 326 """Test single board selection""" 327 self.assertEqual(self.boards.SelectBoards(['arm sandbox']), 328 {'sandbox': ['board4'], 329 'all': ['board0', 'board1', 'board4'], 330 'arm': ['board0', 'board1']}) 331 332 333 def testBoardArchSingleMultiWord(self): 334 """Test single board selection""" 335 self.assertEqual(self.boards.SelectBoards(['arm', 'sandbox']), 336 {'sandbox': ['board4'], 'all': ['board0', 'board1', 'board4'], 'arm': ['board0', 'board1']}) 337 338 def testBoardSingleAnd(self): 339 """Test single board selection""" 340 self.assertEqual(self.boards.SelectBoards(['Tester & arm']), 341 {'Tester&arm': ['board0', 'board1'], 'all': ['board0', 'board1']}) 342 343 def testBoardTwoAnd(self): 344 """Test single board selection""" 345 self.assertEqual(self.boards.SelectBoards(['Tester', '&', 'arm', 346 'Tester' '&', 'powerpc', 347 'sandbox']), 348 {'sandbox': ['board4'], 349 'all': ['board0', 'board1', 'board2', 'board3', 350 'board4'], 351 'Tester&powerpc': ['board2', 'board3'], 352 'Tester&arm': ['board0', 'board1']}) 353 354 def testBoardAll(self): 355 """Test single board selection""" 356 self.assertEqual(self.boards.SelectBoards([]), 357 {'all': ['board0', 'board1', 'board2', 'board3', 358 'board4']}) 359 360 def testBoardRegularExpression(self): 361 """Test single board selection""" 362 self.assertEqual(self.boards.SelectBoards(['T.*r&^Po']), 363 {'all': ['board2', 'board3'], 364 'T.*r&^Po': ['board2', 'board3']}) 365 366 def testBoardDuplicate(self): 367 """Test single board selection""" 368 self.assertEqual(self.boards.SelectBoards(['sandbox sandbox', 369 'sandbox']), 370 {'all': ['board4'], 'sandbox': ['board4']}) 371 def CheckDirs(self, build, dirname): 372 self.assertEqual('base%s' % dirname, build._GetOutputDir(1)) 373 self.assertEqual('base%s/fred' % dirname, 374 build.GetBuildDir(1, 'fred')) 375 self.assertEqual('base%s/fred/done' % dirname, 376 build.GetDoneFile(1, 'fred')) 377 self.assertEqual('base%s/fred/u-boot.sizes' % dirname, 378 build.GetFuncSizesFile(1, 'fred', 'u-boot')) 379 self.assertEqual('base%s/fred/u-boot.objdump' % dirname, 380 build.GetObjdumpFile(1, 'fred', 'u-boot')) 381 self.assertEqual('base%s/fred/err' % dirname, 382 build.GetErrFile(1, 'fred')) 383 384 def testOutputDir(self): 385 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2, 386 checkout=False, show_unknown=False) 387 build.commits = self.commits 388 build.commit_count = len(self.commits) 389 subject = self.commits[1].subject.translate(builder.trans_valid_chars) 390 dirname ='/%02d_of_%02d_g%s_%s' % (2, build.commit_count, commits[1][0], 391 subject[:20]) 392 self.CheckDirs(build, dirname) 393 394 def testOutputDirCurrent(self): 395 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2, 396 checkout=False, show_unknown=False) 397 build.commits = None 398 build.commit_count = 0 399 self.CheckDirs(build, '/current') 400 401 def testOutputDirNoSubdirs(self): 402 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2, 403 checkout=False, show_unknown=False, 404 no_subdirs=True) 405 build.commits = None 406 build.commit_count = 0 407 self.CheckDirs(build, '') 408 409 def testToolchainAliases(self): 410 self.assertTrue(self.toolchains.Select('arm') != None) 411 with self.assertRaises(ValueError): 412 self.toolchains.Select('no-arch') 413 with self.assertRaises(ValueError): 414 self.toolchains.Select('x86') 415 416 self.toolchains = toolchain.Toolchains() 417 self.toolchains.Add('x86_64-linux-gcc', test=False) 418 self.assertTrue(self.toolchains.Select('x86') != None) 419 420 self.toolchains = toolchain.Toolchains() 421 self.toolchains.Add('i386-linux-gcc', test=False) 422 self.assertTrue(self.toolchains.Select('x86') != None) 423 424 def testToolchainDownload(self): 425 """Test that we can download toolchains""" 426 if use_network: 427 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', 428 self.toolchains.LocateArchUrl('arm')) 429 430 431if __name__ == "__main__": 432 unittest.main() 433