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 27errors = [ 28 '''main.c: In function 'main_loop': 29main.c:260:6: warning: unused variable 'joe' [-Wunused-variable] 30''', 31 '''main.c: In function 'main_loop2': 32main.c:295:2: error: 'fred' undeclared (first use in this function) 33main.c:295:2: note: each undeclared identifier is reported only once for each function it appears in 34make[1]: *** [main.o] Error 1 35make: *** [common/libcommon.o] Error 2 36Make failed 37''', 38 '''main.c: In function 'main_loop3': 39main.c:280:6: warning: unused variable 'mary' [-Wunused-variable] 40''', 41 '''powerpc-linux-ld: warning: dot moved backwards before `.bss' 42powerpc-linux-ld: warning: dot moved backwards before `.bss' 43powerpc-linux-ld: u-boot: section .text lma 0xfffc0000 overlaps previous sections 44powerpc-linux-ld: u-boot: section .rodata lma 0xfffef3ec overlaps previous sections 45powerpc-linux-ld: u-boot: section .reloc lma 0xffffa400 overlaps previous sections 46powerpc-linux-ld: u-boot: section .data lma 0xffffcd38 overlaps previous sections 47powerpc-linux-ld: u-boot: section .u_boot_cmd lma 0xffffeb40 overlaps previous sections 48powerpc-linux-ld: u-boot: section .bootpg lma 0xfffff198 overlaps previous sections 49''' 50] 51 52 53# hash, subject, return code, list of errors/warnings 54commits = [ 55 ['1234', 'upstream/master, ok', 0, []], 56 ['5678', 'Second commit, a warning', 0, errors[0:1]], 57 ['9012', 'Third commit, error', 1, errors[0:2]], 58 ['3456', 'Fourth commit, warning', 0, [errors[0], errors[2]]], 59 ['7890', 'Fifth commit, link errors', 1, [errors[0], errors[3]]], 60 ['abcd', 'Sixth commit, fixes all errors', 0, []] 61] 62 63boards = [ 64 ['Active', 'arm', 'armv7', '', 'Tester', 'ARM Board 1', 'board0', ''], 65 ['Active', 'arm', 'armv7', '', 'Tester', 'ARM Board 2', 'board1', ''], 66 ['Active', 'powerpc', 'powerpc', '', 'Tester', 'PowerPC board 1', 'board2', ''], 67 ['Active', 'powerpc', 'mpc5xx', '', 'Tester', 'PowerPC board 2', 'board3', ''], 68 ['Active', 'sandbox', 'sandbox', '', 'Tester', 'Sandbox board', 'board4', ''], 69] 70 71class Options: 72 """Class that holds build options""" 73 pass 74 75class TestBuild(unittest.TestCase): 76 """Test buildman 77 78 TODO: Write tests for the rest of the functionality 79 """ 80 def setUp(self): 81 # Set up commits to build 82 self.commits = [] 83 sequence = 0 84 for commit_info in commits: 85 comm = commit.Commit(commit_info[0]) 86 comm.subject = commit_info[1] 87 comm.return_code = commit_info[2] 88 comm.error_list = commit_info[3] 89 comm.sequence = sequence 90 sequence += 1 91 self.commits.append(comm) 92 93 # Set up boards to build 94 self.boards = board.Boards() 95 for brd in boards: 96 self.boards.AddBoard(board.Board(*brd)) 97 self.boards.SelectBoards([]) 98 99 # Set up the toolchains 100 bsettings.Setup() 101 self.toolchains = toolchain.Toolchains() 102 self.toolchains.Add('arm-linux-gcc', test=False) 103 self.toolchains.Add('sparc-linux-gcc', test=False) 104 self.toolchains.Add('powerpc-linux-gcc', test=False) 105 self.toolchains.Add('gcc', test=False) 106 107 # Avoid sending any output 108 terminal.SetPrintTestMode() 109 self._col = terminal.Color() 110 111 def Make(self, commit, brd, stage, *args, **kwargs): 112 result = command.CommandResult() 113 boardnum = int(brd.target[-1]) 114 result.return_code = 0 115 result.stderr = '' 116 result.stdout = ('This is the test output for board %s, commit %s' % 117 (brd.target, commit.hash)) 118 if boardnum >= 1 and boardnum >= commit.sequence: 119 result.return_code = commit.return_code 120 result.stderr = ''.join(commit.error_list) 121 if stage == 'build': 122 target_dir = None 123 for arg in args: 124 if arg.startswith('O='): 125 target_dir = arg[2:] 126 127 if not os.path.isdir(target_dir): 128 os.mkdir(target_dir) 129 130 result.combined = result.stdout + result.stderr 131 return result 132 133 def assertSummary(self, text, arch, plus, boards, ok=False): 134 col = self._col 135 expected_colour = col.GREEN if ok else col.RED 136 expect = '%10s: ' % arch 137 # TODO(sjg@chromium.org): If plus is '', we shouldn't need this 138 expect += col.Color(expected_colour, plus) 139 expect += ' ' 140 for board in boards: 141 expect += col.Color(expected_colour, ' %s' % board) 142 self.assertEqual(text, expect) 143 144 def testOutput(self): 145 """Test basic builder operation and output 146 147 This does a line-by-line verification of the summary output. 148 """ 149 output_dir = tempfile.mkdtemp() 150 if not os.path.isdir(output_dir): 151 os.mkdir(output_dir) 152 build = builder.Builder(self.toolchains, output_dir, None, 1, 2, 153 checkout=False, show_unknown=False) 154 build.do_make = self.Make 155 board_selected = self.boards.GetSelectedDict() 156 157 build.BuildBoards(self.commits, board_selected, keep_outputs=False, 158 verbose=False) 159 lines = terminal.GetPrintTestLines() 160 count = 0 161 for line in lines: 162 if line.text.strip(): 163 count += 1 164 165 # We should get one starting message, then an update for every commit 166 # built. 167 self.assertEqual(count, len(commits) * len(boards) + 1) 168 build.SetDisplayOptions(show_errors=True); 169 build.ShowSummary(self.commits, board_selected) 170 lines = terminal.GetPrintTestLines() 171 self.assertEqual(lines[0].text, '01: %s' % commits[0][1]) 172 self.assertEqual(lines[1].text, '02: %s' % commits[1][1]) 173 174 # We expect all archs to fail 175 col = terminal.Color() 176 self.assertSummary(lines[2].text, 'sandbox', '+', ['board4']) 177 self.assertSummary(lines[3].text, 'arm', '+', ['board1']) 178 self.assertSummary(lines[4].text, 'powerpc', '+', ['board2', 'board3']) 179 180 # Now we should have the compiler warning 181 self.assertEqual(lines[5].text, 'w+%s' % 182 errors[0].rstrip().replace('\n', '\nw+')) 183 self.assertEqual(lines[5].colour, col.MAGENTA) 184 185 self.assertEqual(lines[6].text, '03: %s' % commits[2][1]) 186 self.assertSummary(lines[7].text, 'sandbox', '+', ['board4']) 187 self.assertSummary(lines[8].text, 'arm', '', ['board1'], ok=True) 188 self.assertSummary(lines[9].text, 'powerpc', '+', ['board2', 'board3']) 189 190 # Compiler error 191 self.assertEqual(lines[10].text, '+%s' % 192 errors[1].rstrip().replace('\n', '\n+')) 193 194 self.assertEqual(lines[11].text, '04: %s' % commits[3][1]) 195 self.assertSummary(lines[12].text, 'sandbox', '', ['board4'], ok=True) 196 self.assertSummary(lines[13].text, 'powerpc', '', ['board2', 'board3'], 197 ok=True) 198 199 # Compile error fixed 200 self.assertEqual(lines[14].text, '-%s' % 201 errors[1].rstrip().replace('\n', '\n-')) 202 self.assertEqual(lines[14].colour, col.GREEN) 203 204 self.assertEqual(lines[15].text, 'w+%s' % 205 errors[2].rstrip().replace('\n', '\nw+')) 206 self.assertEqual(lines[15].colour, col.MAGENTA) 207 208 self.assertEqual(lines[16].text, '05: %s' % commits[4][1]) 209 self.assertSummary(lines[17].text, 'sandbox', '+', ['board4']) 210 self.assertSummary(lines[18].text, 'powerpc', '', ['board3'], ok=True) 211 212 # The second line of errors[3] is a duplicate, so buildman will drop it 213 expect = errors[3].rstrip().split('\n') 214 expect = [expect[0]] + expect[2:] 215 self.assertEqual(lines[19].text, '+%s' % 216 '\n'.join(expect).replace('\n', '\n+')) 217 218 self.assertEqual(lines[20].text, 'w-%s' % 219 errors[2].rstrip().replace('\n', '\nw-')) 220 221 self.assertEqual(lines[21].text, '06: %s' % commits[5][1]) 222 self.assertSummary(lines[22].text, 'sandbox', '', ['board4'], ok=True) 223 224 # The second line of errors[3] is a duplicate, so buildman will drop it 225 expect = errors[3].rstrip().split('\n') 226 expect = [expect[0]] + expect[2:] 227 self.assertEqual(lines[23].text, '-%s' % 228 '\n'.join(expect).replace('\n', '\n-')) 229 230 self.assertEqual(lines[24].text, 'w-%s' % 231 errors[0].rstrip().replace('\n', '\nw-')) 232 233 self.assertEqual(len(lines), 25) 234 235 def _testGit(self): 236 """Test basic builder operation by building a branch""" 237 base_dir = tempfile.mkdtemp() 238 if not os.path.isdir(base_dir): 239 os.mkdir(base_dir) 240 options = Options() 241 options.git = os.getcwd() 242 options.summary = False 243 options.jobs = None 244 options.dry_run = False 245 #options.git = os.path.join(base_dir, 'repo') 246 options.branch = 'test-buildman' 247 options.force_build = False 248 options.list_tool_chains = False 249 options.count = -1 250 options.git_dir = None 251 options.threads = None 252 options.show_unknown = False 253 options.quick = False 254 options.show_errors = False 255 options.keep_outputs = False 256 args = ['tegra20'] 257 control.DoBuildman(options, args) 258 259 def testBoardSingle(self): 260 """Test single board selection""" 261 self.assertEqual(self.boards.SelectBoards(['sandbox']), 262 {'all': 1, 'sandbox': 1}) 263 264 def testBoardArch(self): 265 """Test single board selection""" 266 self.assertEqual(self.boards.SelectBoards(['arm']), 267 {'all': 2, 'arm': 2}) 268 269 def testBoardArchSingle(self): 270 """Test single board selection""" 271 self.assertEqual(self.boards.SelectBoards(['arm sandbox']), 272 {'all': 3, 'arm': 2, 'sandbox' : 1}) 273 274 def testBoardArchSingleMultiWord(self): 275 """Test single board selection""" 276 self.assertEqual(self.boards.SelectBoards(['arm', 'sandbox']), 277 {'all': 3, 'arm': 2, 'sandbox' : 1}) 278 279 def testBoardSingleAnd(self): 280 """Test single board selection""" 281 self.assertEqual(self.boards.SelectBoards(['Tester & arm']), 282 {'all': 2, 'Tester&arm': 2}) 283 284 def testBoardTwoAnd(self): 285 """Test single board selection""" 286 self.assertEqual(self.boards.SelectBoards(['Tester', '&', 'arm', 287 'Tester' '&', 'powerpc', 288 'sandbox']), 289 {'all': 5, 'Tester&powerpc': 2, 'Tester&arm': 2, 290 'sandbox' : 1}) 291 292 def testBoardAll(self): 293 """Test single board selection""" 294 self.assertEqual(self.boards.SelectBoards([]), {'all': 5}) 295 296 def testBoardRegularExpression(self): 297 """Test single board selection""" 298 self.assertEqual(self.boards.SelectBoards(['T.*r&^Po']), 299 {'T.*r&^Po': 2, 'all': 2}) 300 301 def testBoardDuplicate(self): 302 """Test single board selection""" 303 self.assertEqual(self.boards.SelectBoards(['sandbox sandbox', 304 'sandbox']), 305 {'all': 1, 'sandbox': 1}) 306 307if __name__ == "__main__": 308 unittest.main() 309