test_log.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright (c) 2016, Google Inc.
  2. #
  3. # SPDX-License-Identifier: GPL-2.0+
  4. #
  5. # U-Boot Verified Boot Test
  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. import pytest
  11. LOGL_FIRST, LOGL_WARNING, LOGL_INFO = (0, 4, 6)
  12. @pytest.mark.buildconfigspec('log')
  13. def test_log(u_boot_console):
  14. """Test that U-Boot logging works correctly."""
  15. def check_log_entries(lines, mask, max_level=LOGL_INFO):
  16. """Check that the expected log records appear in the output
  17. Args:
  18. lines: iterator containing lines to check
  19. mask: bit mask to select which lines to check for:
  20. bit 0: standard log line
  21. bit 1: _log line
  22. max_level: maximum log level to expect in the output
  23. """
  24. for i in range(max_level):
  25. if mask & 1:
  26. assert 'log %d' % i == lines.next()
  27. if mask & 3:
  28. assert '_log %d' % i == lines.next()
  29. def run_test(testnum):
  30. """Run a particular test number (the 'log test' command)
  31. Args:
  32. testnum: Test number to run
  33. Returns:
  34. iterator containing the lines output from the command
  35. """
  36. with cons.log.section('basic'):
  37. output = u_boot_console.run_command('log test %d' % testnum)
  38. split = output.replace('\r', '').splitlines()
  39. lines = iter(split)
  40. assert 'test %d' % testnum == lines.next()
  41. return lines
  42. def test0():
  43. lines = run_test(0)
  44. check_log_entries(lines, 3)
  45. def test1():
  46. lines = run_test(1)
  47. check_log_entries(lines, 3)
  48. def test2():
  49. lines = run_test(2)
  50. def test3():
  51. lines = run_test(3)
  52. check_log_entries(lines, 2)
  53. def test4():
  54. lines = run_test(4)
  55. assert next(lines, None) == None
  56. def test5():
  57. lines = run_test(5)
  58. check_log_entries(lines, 2)
  59. def test6():
  60. lines = run_test(6)
  61. check_log_entries(lines, 3)
  62. def test7():
  63. lines = run_test(7)
  64. check_log_entries(lines, 3, LOGL_WARNING)
  65. def test8():
  66. lines = run_test(8)
  67. check_log_entries(lines, 3)
  68. def test9():
  69. lines = run_test(9)
  70. check_log_entries(lines, 3)
  71. # TODO(sjg@chromium.org): Consider structuring this as separate tests
  72. cons = u_boot_console
  73. test0()
  74. test1()
  75. test2()
  76. test3()
  77. test4()
  78. test5()
  79. test6()
  80. test7()
  81. test8()
  82. test9()