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