Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 1 | #! /bin/bash |
| 2 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | # Test for warn_collector. Run the warn collector in the background, emulate |
| 7 | # the kernel by appending lines to the log file "messages", and observe the log |
| 8 | # of the (fake) crash reporter each time is run by the warn collector daemon. |
| 9 | |
| 10 | set -e |
| 11 | |
Mike Frysinger | 44054a0 | 2013-05-22 17:50:39 -0400 | [diff] [blame^] | 12 | fail() { |
| 13 | printf '[ FAIL ] %b\n' "$*" |
| 14 | exit 1 |
| 15 | } |
Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 16 | |
Mike Frysinger | 44054a0 | 2013-05-22 17:50:39 -0400 | [diff] [blame^] | 17 | if [[ -z ${SYSROOT} ]]; then |
| 18 | fail "SYSROOT must be set for this test to work" |
| 19 | fi |
| 20 | : ${OUT:=${PWD}} |
| 21 | cd "${OUT}" |
| 22 | PATH=${OUT}:${PATH} |
| 23 | TESTLOG="${OUT}/warn-test-log" |
| 24 | |
| 25 | echo "Testing: $(which warn_collector)" |
Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 26 | |
| 27 | cleanup() { |
| 28 | # Kill daemon (if started) on exit |
| 29 | kill % |
| 30 | } |
| 31 | |
| 32 | check_log() { |
Mike Frysinger | 44054a0 | 2013-05-22 17:50:39 -0400 | [diff] [blame^] | 33 | local n_expected=$1 |
| 34 | if [[ ! -f ${TESTLOG} ]]; then |
| 35 | fail "${TESTLOG} was not created" |
Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 36 | fi |
Mike Frysinger | 44054a0 | 2013-05-22 17:50:39 -0400 | [diff] [blame^] | 37 | if [[ $(wc -l < "${TESTLOG}") -ne ${n_expected} ]]; then |
| 38 | fail "expected ${n_expected} lines in ${TESTLOG}, found this instead: |
| 39 | $(<"${TESTLOG}")" |
Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 40 | fi |
Mike Frysinger | 44054a0 | 2013-05-22 17:50:39 -0400 | [diff] [blame^] | 41 | if egrep -qv '^[0-9a-f]{8}$' "${TESTLOG}"; then |
| 42 | fail "found bad lines in ${TESTLOG}: |
| 43 | $(<"${TESTLOG}")" |
Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 44 | fi |
| 45 | } |
| 46 | |
Mike Frysinger | 44054a0 | 2013-05-22 17:50:39 -0400 | [diff] [blame^] | 47 | rm -f "${TESTLOG}" |
| 48 | cp "${SRC}/warn_collector_test_reporter.sh" . |
| 49 | cp "${SRC}/TEST_WARNING" . |
Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 50 | cp TEST_WARNING messages |
| 51 | |
| 52 | # Start the collector daemon. With the --test option, the daemon reads input |
| 53 | # from ./messages, writes the warning into ./warning, and invokes |
| 54 | # ./warn_collector_test_reporter.sh to report the warning. |
| 55 | warn_collector --test & |
Mike Frysinger | 44054a0 | 2013-05-22 17:50:39 -0400 | [diff] [blame^] | 56 | trap cleanup EXIT |
Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 57 | |
| 58 | # After a while, check that the first warning has been collected. |
| 59 | sleep 1 |
| 60 | check_log 1 |
| 61 | |
| 62 | # Add the same warning to messages, verify that it is NOT collected |
| 63 | cat TEST_WARNING >> messages |
| 64 | sleep 1 |
| 65 | check_log 1 |
| 66 | |
| 67 | # Add a slightly different warning to messages, check that it is collected. |
| 68 | sed s/intel_dp.c/intel_xx.c/ < TEST_WARNING >> messages |
| 69 | sleep 1 |
| 70 | check_log 2 |
| 71 | |
| 72 | # Emulate log rotation, add a warning, and check. |
| 73 | mv messages messages.1 |
| 74 | sed s/intel_dp.c/intel_xy.c/ < TEST_WARNING > messages |
| 75 | sleep 2 |
| 76 | check_log 3 |
| 77 | |
| 78 | # Success! |
| 79 | exit 0 |