tests.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/env bash
  2. if [ -f "${PWD}/log2journal" ]; then
  3. log2journal_bin="${PWD}/log2journal"
  4. else
  5. log2journal_bin="$(which log2journal)"
  6. fi
  7. [ -z "${log2journal_bin}" ] && echo >&2 "Cannot find log2journal binary" && exit 1
  8. echo >&2 "Using: ${log2journal_bin}"
  9. script_dir=$(dirname "$(readlink -f "$0")")
  10. tests="${script_dir}/tests.d"
  11. if [ ! -d "${tests}" ]; then
  12. echo >&2 "tests directory '${tests}' is not found."
  13. exit 1
  14. fi
  15. # Create a random directory name in /tmp
  16. tmp=$(mktemp -d /tmp/script_temp.XXXXXXXXXX)
  17. # Function to clean up the temporary directory on exit
  18. cleanup() {
  19. echo "Cleaning up..."
  20. rm -rf "$tmp"
  21. }
  22. # Register the cleanup function to run on script exit
  23. trap cleanup EXIT
  24. # Change to the temporary directory
  25. cd "$tmp" || exit 1
  26. # -----------------------------------------------------------------------------
  27. test_log2journal_config() {
  28. local in="${1}"
  29. local out="${2}"
  30. shift 2
  31. [ -f output ] && rm output
  32. printf >&2 "running: "
  33. printf >&2 "%q " "${log2journal_bin}" "${@}"
  34. printf >&2 "\n"
  35. "${log2journal_bin}" <"${in}" "${@}" >output 2>&1
  36. ret=$?
  37. [ $ret -ne 0 ] && echo >&2 "${log2journal_bin} exited with code: $ret" && cat output && exit 1
  38. diff --ignore-all-space "${out}" output
  39. [ $? -ne -0 ] && echo >&2 "${log2journal_bin} output does not match!" && exit 1
  40. echo >&2 "OK"
  41. echo >&2
  42. return 0
  43. }
  44. # test yaml parsing
  45. echo >&2
  46. echo >&2 "Testing full yaml config parsing..."
  47. test_log2journal_config /dev/null "${tests}/full.output" -f "${tests}/full.yaml" --show-config || exit 1
  48. echo >&2 "Testing command line parsing..."
  49. test_log2journal_config /dev/null "${tests}/full.output" --show-config \
  50. --prefix=NGINX_ \
  51. --filename-key NGINX_LOG_FILENAME \
  52. --inject SYSLOG_IDENTIFIER=nginx-log \
  53. --inject=SYSLOG_IDENTIFIER2=nginx-log2 \
  54. --inject 'PRIORITY=${NGINX_STATUS}' \
  55. --inject='NGINX_STATUS_FAMILY=${NGINX_STATUS}${NGINX_METHOD}' \
  56. --rewrite 'PRIORITY=//${NGINX_STATUS}/inject,dont-stop' \
  57. --rewrite "PRIORITY=/^[123]/6" \
  58. --rewrite='PRIORITY=|^4|5' \
  59. '--rewrite=PRIORITY=-^5-3' \
  60. --rewrite "PRIORITY=;.*;4" \
  61. --rewrite 'NGINX_STATUS_FAMILY=|^(?<first_digit>[1-5])|${first_digit}xx' \
  62. --rewrite 'NGINX_STATUS_FAMILY=|.*|UNKNOWN' \
  63. --rename TEST1=TEST2 \
  64. --rename=TEST3=TEST4 \
  65. --unmatched-key MESSAGE \
  66. --inject-unmatched PRIORITY=1 \
  67. --inject-unmatched=PRIORITY2=2 \
  68. --include=".*" \
  69. --exclude ".*HELLO.*WORLD.*" \
  70. '(?x) # Enable PCRE2 extended mode
  71. ^
  72. (?<NGINX_REMOTE_ADDR>[^ ]+) \s - \s # NGINX_REMOTE_ADDR
  73. (?<NGINX_REMOTE_USER>[^ ]+) \s # NGINX_REMOTE_USER
  74. \[
  75. (?<NGINX_TIME_LOCAL>[^\]]+) # NGINX_TIME_LOCAL
  76. \]
  77. \s+ "
  78. (?<MESSAGE>
  79. (?<NGINX_METHOD>[A-Z]+) \s+ # NGINX_METHOD
  80. (?<NGINX_URL>[^ ]+) \s+
  81. HTTP/(?<NGINX_HTTP_VERSION>[^"]+)
  82. )
  83. " \s+
  84. (?<NGINX_STATUS>\d+) \s+ # NGINX_STATUS
  85. (?<NGINX_BODY_BYTES_SENT>\d+) \s+ # NGINX_BODY_BYTES_SENT
  86. "(?<NGINX_HTTP_REFERER>[^"]*)" \s+ # NGINX_HTTP_REFERER
  87. "(?<NGINX_HTTP_USER_AGENT>[^"]*)" # NGINX_HTTP_USER_AGENT' \
  88. || exit 1
  89. # -----------------------------------------------------------------------------
  90. test_log2journal() {
  91. local n="${1}"
  92. local in="${2}"
  93. local out="${3}"
  94. shift 3
  95. printf >&2 "running test No ${n}: "
  96. printf >&2 "%q " "${log2journal_bin}" "${@}"
  97. printf >&2 "\n"
  98. echo >&2 "using as input : ${in}"
  99. echo >&2 "expecting output: ${out}"
  100. [ -f output ] && rm output
  101. "${log2journal_bin}" <"${in}" "${@}" >output 2>&1
  102. ret=$?
  103. [ $ret -ne 0 ] && echo >&2 "${log2journal_bin} exited with code: $ret" && cat output && exit 1
  104. diff "${out}" output
  105. [ $? -ne -0 ] && echo >&2 "${log2journal_bin} output does not match! - here is what we got:" && cat output && exit 1
  106. echo >&2 "OK"
  107. echo >&2
  108. return 0
  109. }
  110. echo >&2
  111. echo >&2 "Testing parsing and output..."
  112. test_log2journal 1 "${tests}/json.log" "${tests}/json.output" json
  113. test_log2journal 2 "${tests}/json.log" "${tests}/json-include.output" json --include "OBJECT"
  114. test_log2journal 3 "${tests}/json.log" "${tests}/json-exclude.output" json --exclude "ARRAY[^2]"
  115. test_log2journal 4 "${tests}/nginx-json.log" "${tests}/nginx-json.output" -f "${script_dir}/log2journal.d/nginx-json.yaml"
  116. test_log2journal 5 "${tests}/nginx-combined.log" "${tests}/nginx-combined.output" -f "${script_dir}/log2journal.d/nginx-combined.yaml"
  117. test_log2journal 6 "${tests}/logfmt.log" "${tests}/logfmt.output" -f "${tests}/logfmt.yaml"
  118. test_log2journal 7 "${tests}/logfmt.log" "${tests}/default.output" -f "${script_dir}/log2journal.d/default.yaml"