xref: /openbmc/u-boot/test/py/tests/test_log.py (revision aa5e3e22)
1# Copyright (c) 2016, Google Inc.
2#
3# SPDX-License-Identifier:      GPL-2.0+
4#
5# U-Boot Verified Boot Test
6
7"""
8This tests U-Boot logging. It uses the 'log test' command with various options
9and checks that the output is correct.
10"""
11
12import pytest
13
14LOGL_FIRST, LOGL_WARNING, LOGL_INFO = (0, 4, 6)
15
16@pytest.mark.buildconfigspec('log')
17def test_log(u_boot_console):
18    """Test that U-Boot logging works correctly."""
19    def check_log_entries(lines, mask, max_level=LOGL_INFO):
20        """Check that the expected log records appear in the output
21
22        Args:
23            lines: iterator containing lines to check
24            mask: bit mask to select which lines to check for:
25                bit 0: standard log line
26                bit 1: _log line
27            max_level: maximum log level to expect in the output
28        """
29        for i in range(max_level):
30            if mask & 1:
31                assert 'log_run() log %d' % i == lines.next()
32            if mask & 3:
33                assert 'func() _log %d' % i == lines.next()
34
35    def run_test(testnum):
36        """Run a particular test number (the 'log test' command)
37
38        Args:
39            testnum: Test number to run
40        Returns:
41            iterator containing the lines output from the command
42        """
43        with cons.log.section('basic'):
44           output = u_boot_console.run_command('log test %d' % testnum)
45        split = output.replace('\r', '').splitlines()
46        lines = iter(split)
47        assert 'test %d' % testnum == lines.next()
48        return lines
49
50    def test0():
51        lines = run_test(0)
52        check_log_entries(lines, 3)
53
54    def test1():
55        lines = run_test(1)
56        check_log_entries(lines, 3)
57
58    def test2():
59        lines = run_test(2)
60
61    def test3():
62        lines = run_test(3)
63        check_log_entries(lines, 2)
64
65    def test4():
66        lines = run_test(4)
67        assert next(lines, None) == None
68
69    def test5():
70        lines = run_test(5)
71        check_log_entries(lines, 2)
72
73    def test6():
74        lines = run_test(6)
75        check_log_entries(lines, 3)
76
77    def test7():
78        lines = run_test(7)
79        check_log_entries(lines, 3, LOGL_WARNING)
80
81    def test8():
82        lines = run_test(8)
83        check_log_entries(lines, 3)
84
85    def test9():
86        lines = run_test(9)
87        check_log_entries(lines, 3)
88
89    # TODO(sjg@chromium.org): Consider structuring this as separate tests
90    cons = u_boot_console
91    test0()
92    test1()
93    test2()
94    test3()
95    test4()
96    test5()
97    test6()
98    test7()
99    test8()
100    test9()
101
102@pytest.mark.buildconfigspec('log')
103def test_log_format(u_boot_console):
104    """Test the 'log format' and 'log rec' commands"""
105    def run_with_format(fmt, expected_output):
106        """Set up the log format and then write a log record
107
108        Args:
109            fmt: Format to use for 'log format'
110            expected_output: Expected output from the 'log rec' command
111        """
112        output = cons.run_command('log format %s' % fmt)
113        assert output == ''
114        output = cons.run_command('log rec arch notice file.c 123 func msg')
115        assert output == expected_output
116
117    cons = u_boot_console
118    with cons.log.section('format'):
119        run_with_format('all', 'NOTICE.arch,file.c:123-func() msg')
120        output = cons.run_command('log format')
121        assert output == 'Log format: clFLfm'
122
123        run_with_format('fm', 'func() msg')
124        run_with_format('clfm', 'NOTICE.arch,func() msg')
125        run_with_format('FLfm', 'file.c:123-func() msg')
126        run_with_format('lm', 'NOTICE. msg')
127        run_with_format('m', 'msg')
128