xref: /openbmc/u-boot/tools/buildman/test.py (revision b5493d17)
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   '''In file included from %(basedir)sarch/sandbox/cpu/cpu.c:9:0:
51%(basedir)sarch/sandbox/include/asm/state.h:44:0: warning: "xxxx" redefined [enabled by default]
52%(basedir)sarch/sandbox/include/asm/state.h:43:0: note: this is the location of the previous definition
53%(basedir)sarch/sandbox/cpu/cpu.c: In function 'do_reset':
54%(basedir)sarch/sandbox/cpu/cpu.c:27:1: error: unknown type name 'blah'
55%(basedir)sarch/sandbox/cpu/cpu.c:28:12: error: expected declaration specifiers or '...' before numeric constant
56make[2]: *** [arch/sandbox/cpu/cpu.o] Error 1
57make[1]: *** [arch/sandbox/cpu] Error 2
58make[1]: *** Waiting for unfinished jobs....
59In file included from %(basedir)scommon/board_f.c:55:0:
60%(basedir)sarch/sandbox/include/asm/state.h:44:0: warning: "xxxx" redefined [enabled by default]
61%(basedir)sarch/sandbox/include/asm/state.h:43:0: note: this is the location of the previous definition
62make: *** [sub-make] Error 2
63'''
64]
65
66
67# hash, subject, return code, list of errors/warnings
68commits = [
69    ['1234', 'upstream/master, ok', 0, []],
70    ['5678', 'Second commit, a warning', 0, errors[0:1]],
71    ['9012', 'Third commit, error', 1, errors[0:2]],
72    ['3456', 'Fourth commit, warning', 0, [errors[0], errors[2]]],
73    ['7890', 'Fifth commit, link errors', 1, [errors[0], errors[3]]],
74    ['abcd', 'Sixth commit, fixes all errors', 0, []],
75    ['ef01', 'Seventh commit, check directory suppression', 1, [errors[4]]],
76]
77
78boards = [
79    ['Active', 'arm', 'armv7', '', 'Tester', 'ARM Board 1', 'board0',  ''],
80    ['Active', 'arm', 'armv7', '', 'Tester', 'ARM Board 2', 'board1', ''],
81    ['Active', 'powerpc', 'powerpc', '', 'Tester', 'PowerPC board 1', 'board2', ''],
82    ['Active', 'powerpc', 'mpc5xx', '', 'Tester', 'PowerPC board 2', 'board3', ''],
83    ['Active', 'sandbox', 'sandbox', '', 'Tester', 'Sandbox board', 'board4', ''],
84]
85
86class Options:
87    """Class that holds build options"""
88    pass
89
90class TestBuild(unittest.TestCase):
91    """Test buildman
92
93    TODO: Write tests for the rest of the functionality
94    """
95    def setUp(self):
96        # Set up commits to build
97        self.commits = []
98        sequence = 0
99        for commit_info in commits:
100            comm = commit.Commit(commit_info[0])
101            comm.subject = commit_info[1]
102            comm.return_code = commit_info[2]
103            comm.error_list = commit_info[3]
104            comm.sequence = sequence
105            sequence += 1
106            self.commits.append(comm)
107
108        # Set up boards to build
109        self.boards = board.Boards()
110        for brd in boards:
111            self.boards.AddBoard(board.Board(*brd))
112        self.boards.SelectBoards([])
113
114        # Set up the toolchains
115        bsettings.Setup()
116        self.toolchains = toolchain.Toolchains()
117        self.toolchains.Add('arm-linux-gcc', test=False)
118        self.toolchains.Add('sparc-linux-gcc', test=False)
119        self.toolchains.Add('powerpc-linux-gcc', test=False)
120        self.toolchains.Add('gcc', test=False)
121
122        # Avoid sending any output
123        terminal.SetPrintTestMode()
124        self._col = terminal.Color()
125
126    def Make(self, commit, brd, stage, *args, **kwargs):
127        global base_dir
128
129        result = command.CommandResult()
130        boardnum = int(brd.target[-1])
131        result.return_code = 0
132        result.stderr = ''
133        result.stdout = ('This is the test output for board %s, commit %s' %
134                (brd.target, commit.hash))
135        if ((boardnum >= 1 and boardnum >= commit.sequence) or
136                boardnum == 4 and commit.sequence == 6):
137            result.return_code = commit.return_code
138            result.stderr = (''.join(commit.error_list)
139                % {'basedir' : base_dir + '/.bm-work/00/'})
140        if stage == 'build':
141            target_dir = None
142            for arg in args:
143                if arg.startswith('O='):
144                    target_dir = arg[2:]
145
146            if not os.path.isdir(target_dir):
147                os.mkdir(target_dir)
148
149        result.combined = result.stdout + result.stderr
150        return result
151
152    def assertSummary(self, text, arch, plus, boards, ok=False):
153        col = self._col
154        expected_colour = col.GREEN if ok else col.RED
155        expect = '%10s: ' % arch
156        # TODO(sjg@chromium.org): If plus is '', we shouldn't need this
157        expect += col.Color(expected_colour, plus)
158        expect += '  '
159        for board in boards:
160            expect += col.Color(expected_colour, ' %s' % board)
161        self.assertEqual(text, expect)
162
163    def testOutput(self):
164        """Test basic builder operation and output
165
166        This does a line-by-line verification of the summary output.
167        """
168        global base_dir
169
170        base_dir = tempfile.mkdtemp()
171        if not os.path.isdir(base_dir):
172            os.mkdir(base_dir)
173        build = builder.Builder(self.toolchains, base_dir, None, 1, 2,
174                                checkout=False, show_unknown=False)
175        build.do_make = self.Make
176        board_selected = self.boards.GetSelectedDict()
177
178        build.BuildBoards(self.commits, board_selected, keep_outputs=False,
179                          verbose=False)
180        lines = terminal.GetPrintTestLines()
181        count = 0
182        for line in lines:
183            if line.text.strip():
184                count += 1
185
186        # We should get one starting message, then an update for every commit
187        # built.
188        self.assertEqual(count, len(commits) * len(boards) + 1)
189        build.SetDisplayOptions(show_errors=True);
190        build.ShowSummary(self.commits, board_selected)
191        #terminal.EchoPrintTestLines()
192        lines = terminal.GetPrintTestLines()
193        self.assertEqual(lines[0].text, '01: %s' % commits[0][1])
194        self.assertEqual(lines[1].text, '02: %s' % commits[1][1])
195
196        # We expect all archs to fail
197        col = terminal.Color()
198        self.assertSummary(lines[2].text, 'sandbox', '+', ['board4'])
199        self.assertSummary(lines[3].text, 'arm', '+', ['board1'])
200        self.assertSummary(lines[4].text, 'powerpc', '+', ['board2', 'board3'])
201
202        # Now we should have the compiler warning
203        self.assertEqual(lines[5].text, 'w+%s' %
204                errors[0].rstrip().replace('\n', '\nw+'))
205        self.assertEqual(lines[5].colour, col.MAGENTA)
206
207        self.assertEqual(lines[6].text, '03: %s' % commits[2][1])
208        self.assertSummary(lines[7].text, 'sandbox', '+', ['board4'])
209        self.assertSummary(lines[8].text, 'arm', '', ['board1'], ok=True)
210        self.assertSummary(lines[9].text, 'powerpc', '+', ['board2', 'board3'])
211
212        # Compiler error
213        self.assertEqual(lines[10].text, '+%s' %
214                errors[1].rstrip().replace('\n', '\n+'))
215
216        self.assertEqual(lines[11].text, '04: %s' % commits[3][1])
217        self.assertSummary(lines[12].text, 'sandbox', '', ['board4'], ok=True)
218        self.assertSummary(lines[13].text, 'powerpc', '', ['board2', 'board3'],
219                ok=True)
220
221        # Compile error fixed
222        self.assertEqual(lines[14].text, '-%s' %
223                errors[1].rstrip().replace('\n', '\n-'))
224        self.assertEqual(lines[14].colour, col.GREEN)
225
226        self.assertEqual(lines[15].text, 'w+%s' %
227                errors[2].rstrip().replace('\n', '\nw+'))
228        self.assertEqual(lines[15].colour, col.MAGENTA)
229
230        self.assertEqual(lines[16].text, '05: %s' % commits[4][1])
231        self.assertSummary(lines[17].text, 'sandbox', '+', ['board4'])
232        self.assertSummary(lines[18].text, 'powerpc', '', ['board3'], ok=True)
233
234        # The second line of errors[3] is a duplicate, so buildman will drop it
235        expect = errors[3].rstrip().split('\n')
236        expect = [expect[0]] + expect[2:]
237        self.assertEqual(lines[19].text, '+%s' %
238                '\n'.join(expect).replace('\n', '\n+'))
239
240        self.assertEqual(lines[20].text, 'w-%s' %
241                errors[2].rstrip().replace('\n', '\nw-'))
242
243        self.assertEqual(lines[21].text, '06: %s' % commits[5][1])
244        self.assertSummary(lines[22].text, 'sandbox', '', ['board4'], ok=True)
245
246        # The second line of errors[3] is a duplicate, so buildman will drop it
247        expect = errors[3].rstrip().split('\n')
248        expect = [expect[0]] + expect[2:]
249        self.assertEqual(lines[23].text, '-%s' %
250                '\n'.join(expect).replace('\n', '\n-'))
251
252        self.assertEqual(lines[24].text, 'w-%s' %
253                errors[0].rstrip().replace('\n', '\nw-'))
254
255        self.assertEqual(lines[25].text, '07: %s' % commits[6][1])
256        self.assertSummary(lines[26].text, 'sandbox', '+', ['board4'])
257
258        # Pick out the correct error lines
259        expect_str = errors[4].rstrip().replace('%(basedir)s', '').split('\n')
260        expect = expect_str[3:8] + [expect_str[-1]]
261        self.assertEqual(lines[27].text, '+%s' %
262                '\n'.join(expect).replace('\n', '\n+'))
263
264        # Now the warnings lines
265        expect = [expect_str[0]] + expect_str[10:12] + [expect_str[9]]
266        self.assertEqual(lines[28].text, 'w+%s' %
267                '\n'.join(expect).replace('\n', '\nw+'))
268
269        self.assertEqual(len(lines), 29)
270        shutil.rmtree(base_dir)
271
272    def _testGit(self):
273        """Test basic builder operation by building a branch"""
274        base_dir = tempfile.mkdtemp()
275        if not os.path.isdir(base_dir):
276            os.mkdir(base_dir)
277        options = Options()
278        options.git = os.getcwd()
279        options.summary = False
280        options.jobs = None
281        options.dry_run = False
282        #options.git = os.path.join(base_dir, 'repo')
283        options.branch = 'test-buildman'
284        options.force_build = False
285        options.list_tool_chains = False
286        options.count = -1
287        options.git_dir = None
288        options.threads = None
289        options.show_unknown = False
290        options.quick = False
291        options.show_errors = False
292        options.keep_outputs = False
293        args = ['tegra20']
294        control.DoBuildman(options, args)
295        shutil.rmtree(base_dir)
296
297    def testBoardSingle(self):
298        """Test single board selection"""
299        self.assertEqual(self.boards.SelectBoards(['sandbox']),
300                         {'all': 1, 'sandbox': 1})
301
302    def testBoardArch(self):
303        """Test single board selection"""
304        self.assertEqual(self.boards.SelectBoards(['arm']),
305                         {'all': 2, 'arm': 2})
306
307    def testBoardArchSingle(self):
308        """Test single board selection"""
309        self.assertEqual(self.boards.SelectBoards(['arm sandbox']),
310                         {'all': 3, 'arm': 2, 'sandbox' : 1})
311
312    def testBoardArchSingleMultiWord(self):
313        """Test single board selection"""
314        self.assertEqual(self.boards.SelectBoards(['arm', 'sandbox']),
315                         {'all': 3, 'arm': 2, 'sandbox' : 1})
316
317    def testBoardSingleAnd(self):
318        """Test single board selection"""
319        self.assertEqual(self.boards.SelectBoards(['Tester & arm']),
320                         {'all': 2, 'Tester&arm': 2})
321
322    def testBoardTwoAnd(self):
323        """Test single board selection"""
324        self.assertEqual(self.boards.SelectBoards(['Tester', '&', 'arm',
325                                                   'Tester' '&', 'powerpc',
326                                                   'sandbox']),
327                         {'all': 5, 'Tester&powerpc': 2, 'Tester&arm': 2,
328                          'sandbox' : 1})
329
330    def testBoardAll(self):
331        """Test single board selection"""
332        self.assertEqual(self.boards.SelectBoards([]), {'all': 5})
333
334    def testBoardRegularExpression(self):
335        """Test single board selection"""
336        self.assertEqual(self.boards.SelectBoards(['T.*r&^Po']),
337                         {'T.*r&^Po': 2, 'all': 2})
338
339    def testBoardDuplicate(self):
340        """Test single board selection"""
341        self.assertEqual(self.boards.SelectBoards(['sandbox sandbox',
342                                                   'sandbox']),
343                         {'all': 1, 'sandbox': 1})
344
345if __name__ == "__main__":
346    unittest.main()
347