summaryrefslogtreecommitdiffstats
path: root/src/collectors/log2journal/tests.sh
blob: 4024388669176941d0a9280f5c5113591b501586 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env bash

if [ -f "${PWD}/log2journal" ]; then
  log2journal_bin="${PWD}/log2journal"
else
  log2journal_bin="$(which log2journal)"
fi

[ -z "${log2journal_bin}" ] && echo >&2 "Cannot find log2journal binary" && exit 1
echo >&2 "Using: ${log2journal_bin}"

script_dir=$(dirname "$(readlink -f "$0")")
tests="${script_dir}/tests.d"

if [ ! -d "${tests}" ]; then
  echo >&2 "tests directory '${tests}' is not found."
  exit 1
fi

# Create a random directory name in /tmp
tmp=$(mktemp -d /tmp/script_temp.XXXXXXXXXX)

# Function to clean up the temporary directory on exit
cleanup() {
  echo "Cleaning up..."
  rm -rf "$tmp"
}

# Register the cleanup function to run on script exit
trap cleanup EXIT

# Change to the temporary directory
cd "$tmp" || exit 1

# -----------------------------------------------------------------------------

test_log2journal_config() {
  local in="${1}"
  local out="${2}"
  shift 2

  [ -f output ] && rm output

  printf >&2 "running: "
  printf >&2 "%q " "${log2journal_bin}" "${@}"
  printf >&2 "\n"

  "${log2journal_bin}" <"${in}" "${@}" >output 2>&1
  ret=$?

  [ $ret -ne 0 ] && echo >&2 "${log2journal_bin} exited with code: $ret" && cat output && exit 1

  diff --ignore-all-space "${out}" output
  [ $? -ne -0 ] && echo >&2 "${log2journal_bin} output does not match!" && exit 1

  echo >&2 "OK"
  echo >&2

  return 0
}

# test yaml parsing
echo >&2
echo >&2 "Testing full yaml config parsing..."
test_log2journal_config /dev/null "${tests}/full.output" -f "${tests}/full.yaml" --show-config || exit 1

echo >&2 "Testing command line parsing..."
test_log2journal_config /dev/null "${tests}/full.output" --show-config      \
  --prefix=NGINX_                                                           \
  --filename-key NGINX_LOG_FILENAME                                         \
  --inject SYSLOG_IDENTIFIER=nginx-log                                      \
  --inject=SYSLOG_IDENTIFIER2=nginx-log2                                    \
  --inject 'PRIORITY=${NGINX_STATUS}'                                       \
  --inject='NGINX_STATUS_FAMILY=${NGINX_STATUS}${NGINX_METHOD}'             \
  --rewrite 'PRIORITY=//${NGINX_STATUS}/inject,dont-stop'                   \
  --rewrite "PRIORITY=/^[123]/6"                                            \
  --rewrite='PRIORITY=|^4|5'                                                \
  '--rewrite=PRIORITY=-^5-3'                                                \
  --rewrite "PRIORITY=;.*;4"                                                \
  --rewrite 'NGINX_STATUS_FAMILY=|^(?<first_digit>[1-5])|${first_digit}xx'  \
  --rewrite 'NGINX_STATUS_FAMILY=|.*|UNKNOWN'                               \
  --rename TEST1=TEST2                                                      \
  --rename=TEST3=TEST4                                                      \
  --unmatched-key MESSAGE                                                   \
  --inject-unmatched PRIORITY=1                                             \
  --inject-unmatched=PRIORITY2=2                                            \
  --include=".*"                                                            \
  --exclude ".*HELLO.*WORLD.*"                                              \
  '(?x)                                   # Enable PCRE2 extended mode
   ^
   (?<NGINX_REMOTE_ADDR>[^ ]+) \s - \s    # NGINX_REMOTE_ADDR
   (?<NGINX_REMOTE_USER>[^ ]+) \s         # NGINX_REMOTE_USER
   \[
     (?<NGINX_TIME_LOCAL>[^\]]+)          # NGINX_TIME_LOCAL
   \]
   \s+ "
   (?<MESSAGE>
     (?<NGINX_METHOD>[A-Z]+) \s+          # NGINX_METHOD
     (?<NGINX_URL>[^ ]+) \s+
     HTTP/(?<NGINX_HTTP_VERSION>[^"]+)
   )
   " \s+
   (?<NGINX_STATUS>\d+) \s+               # NGINX_STATUS
   (?<NGINX_BODY_BYTES_SENT>\d+) \s+      # NGINX_BODY_BYTES_SENT
   "(?<NGINX_HTTP_REFERER>[^"]*)" \s+     # NGINX_HTTP_REFERER
   "(?<NGINX_HTTP_USER_AGENT>[^"]*)"      # NGINX_HTTP_USER_AGENT' \
    || exit 1

# -----------------------------------------------------------------------------

test_log2journal() {
  local n="${1}"
  local in="${2}"
  local out="${3}"
  shift 3

  printf >&2 "running test No ${n}: "
  printf >&2 "%q " "${log2journal_bin}" "${@}"
  printf >&2 "\n"
  echo >&2 "using as input  : ${in}"
  echo >&2 "expecting output: ${out}"

  [ -f output ] && rm output

  "${log2journal_bin}" <"${in}" "${@}" >output 2>&1
  ret=$?

  [ $ret -ne 0 ] && echo >&2 "${log2journal_bin} exited with code: $ret" && cat output && exit 1

  diff "${out}" output
  [ $? -ne -0 ] && echo >&2 "${log2journal_bin} output does not match! - here is what we got:" && cat output && exit 1

  echo >&2 "OK"
  echo >&2

  return 0
}

echo >&2
echo >&2 "Testing parsing and output..."

test_log2journal 1 "${tests}/json.log" "${tests}/json.output" json
test_log2journal 2 "${tests}/json.log" "${tests}/json-include.output" json --include "OBJECT"
test_log2journal 3 "${tests}/json.log" "${tests}/json-exclude.output" json --exclude "ARRAY[^2]"
test_log2journal 4 "${tests}/nginx-json.log" "${tests}/nginx-json.output" -f "${script_dir}/log2journal.d/nginx-json.yaml"
test_log2journal 5 "${tests}/nginx-combined.log" "${tests}/nginx-combined.output" -f "${script_dir}/log2journal.d/nginx-combined.yaml"
test_log2journal 6 "${tests}/logfmt.log" "${tests}/logfmt.output" -f "${tests}/logfmt.yaml"
test_log2journal 7 "${tests}/logfmt.log" "${tests}/default.output" -f "${script_dir}/log2journal.d/default.yaml"