test_log.py 3.8 KB

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