test_log.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. with cons.log.section('basic'):
  36. output = u_boot_console.run_command('log test %d' % testnum)
  37. split = output.replace('\r', '').splitlines()
  38. lines = iter(split)
  39. assert 'test %d' % testnum == next(lines)
  40. return lines
  41. def test0():
  42. lines = run_test(0)
  43. check_log_entries(lines, 3)
  44. def test1():
  45. lines = run_test(1)
  46. check_log_entries(lines, 3)
  47. def test2():
  48. lines = run_test(2)
  49. def test3():
  50. lines = run_test(3)
  51. check_log_entries(lines, 2)
  52. def test4():
  53. lines = run_test(4)
  54. assert next(lines, None) == None
  55. def test5():
  56. lines = run_test(5)
  57. check_log_entries(lines, 2)
  58. def test6():
  59. lines = run_test(6)
  60. check_log_entries(lines, 3)
  61. def test7():
  62. lines = run_test(7)
  63. check_log_entries(lines, 3, LOGL_WARNING)
  64. def test8():
  65. lines = run_test(8)
  66. check_log_entries(lines, 3)
  67. def test9():
  68. lines = run_test(9)
  69. check_log_entries(lines, 3)
  70. def test10():
  71. lines = run_test(10)
  72. for i in range(7):
  73. assert 'log_test() level %d' % i == next(lines)
  74. # TODO(sjg@chromium.org): Consider structuring this as separate tests
  75. cons = u_boot_console
  76. test0()
  77. test1()
  78. test2()
  79. test3()
  80. test4()
  81. test5()
  82. test6()
  83. test7()
  84. test8()
  85. test9()
  86. test10()
  87. @pytest.mark.buildconfigspec('cmd_log')
  88. def test_log_format(u_boot_console):
  89. """Test the 'log format' and 'log rec' commands"""
  90. def run_with_format(fmt, expected_output):
  91. """Set up the log format and then write a log record
  92. Args:
  93. fmt: Format to use for 'log format'
  94. expected_output: Expected output from the 'log rec' command
  95. """
  96. output = cons.run_command('log format %s' % fmt)
  97. assert output == ''
  98. output = cons.run_command('log rec arch notice file.c 123 func msg')
  99. assert output == expected_output
  100. cons = u_boot_console
  101. with cons.log.section('format'):
  102. run_with_format('all', 'NOTICE.arch,file.c:123-func() msg')
  103. output = cons.run_command('log format')
  104. assert output == 'Log format: clFLfm'
  105. run_with_format('fm', 'func() msg')
  106. run_with_format('clfm', 'NOTICE.arch,func() msg')
  107. run_with_format('FLfm', 'file.c:123-func() msg')
  108. run_with_format('lm', 'NOTICE. msg')
  109. run_with_format('m', 'msg')