Skip to content

Commit eddb23c

Browse files
authored
Merge pull request #204 from whyscream/test-pipeline
test script updates
2 parents b63258c + a0c98f7 commit eddb23c

File tree

4 files changed

+98
-5
lines changed

4 files changed

+98
-5
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@ Tests
2424

2525
In the `test/` directory, there is a test suite that tries to make sure that no previously supported log line will break because of changing common patterns and such. It also returns results a lot faster than doing `sudo service logstash restart` :-).
2626

27-
The test suite needs the patterns provided by Logstash, you can easily pull these from github by running `git submodule update --init`. To run the test suite, you need a recent version of `ruby` (`2.6` or newer should work), and the `jls-grok` and `minitest` gems. Then simply execute `ruby test/test.rb`. NOTE: The whole test process can now be executed inside a docker container, simply by running the `runtests.sh` script.
27+
The test suite needs the patterns provided by Logstash, you can easily pull these from github by running `git submodule update --init`. To run the test suite, you need a recent version of `ruby` (`2.6` or newer should work), and the `jls-grok` and `minitest` gems. Then simply execute `ruby test/test.rb`. NOTE: The whole test process can now be executed inside a docker container, simply by running the `test_grok_patterns.sh` script.
2828

2929
Adding new test cases can easily be done by creating new yaml files in the test directory. Each file specifies a grok pattern to validate, a sample log line, and a list of expected results.
3030

3131
Also, the example Logstash config file adds some informative tags that aid in finding grok failures and unparsed lines. If you're not interested in those, you can remove all occurrences of `add_tag` and `tag_on_failure` from the config file.
3232

33+
Additional test scripts are available for local tests (using docker containers):
34+
- `test_grok_patterns.sh`: runs the test suite for the grok patterns in `postfix.grok`
35+
- `test_logstash_config.sh`: validates the logstash config in `50-filter-postfix.conf`
36+
- `test_pipeline.sh`: validates that the logstash config can be used in a simple logstash pipeline, and ensures that this results in parsed messages
37+
3338
Contributing
3439
------------
3540

test_config_syntax.sh

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
#!/bin/sh
22

3+
#
4+
# This script is used to test the config syntax of the 50-filter-postfix.conf file.
5+
#
6+
# The configuration file is validated using the logstash --config.test_and_exit command in a docker container.
7+
#
8+
39
set -eux
410

511
docker run --rm -it \
6-
--volume $(pwd)/postfix.grok:/etc/logstash/patterns.d/postfix.grok \
7-
--volume $(pwd)/50-filter-postfix.conf:/usr/share/logstash/pipeline/50-filter-postfix.conf \
8-
logstash:8.12.0 \
12+
--volume "$(pwd)"/postfix.grok:/etc/logstash/patterns.d/postfix.grok \
13+
--volume "$(pwd)"/50-filter-postfix.conf:/usr/share/logstash/pipeline/50-filter-postfix.conf \
14+
logstash:8.14.1 \
915
logstash --config.test_and_exit -f /usr/share/logstash/pipeline/50-filter-postfix.conf

test_grok_patterns.sh

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#!/bin/sh
22

3+
#
4+
# This script is used to test the grok patterns in the postfix.grok file.
5+
#
6+
# The patterns are tested by running the test suite (in test/test.rb and test/*.yaml)
7+
# against the patterns in the postfix.grok file in a docker container.
8+
#
39
set -eux
410

511
DOCKERIMAGE="postfix-grok-patterns-runtests"
@@ -12,4 +18,4 @@ FROM ruby:slim
1218
RUN gem install jls-grok minitest
1319
EOF
1420

15-
docker run --volume $(pwd):"${VOLUMEPATH}" --workdir ${VOLUMEPATH} ${DOCKERIMAGE} sh -c "ruby test/test.rb"
21+
docker run --volume "$(pwd)":"${VOLUMEPATH}" --workdir ${VOLUMEPATH} ${DOCKERIMAGE} sh -c "ruby test/test.rb"

test_pipeline.sh

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/sh
2+
3+
#
4+
# This script is used to test the logstash pipeline configuration.
5+
#
6+
# It sets up a logstash pipeline with the postfix configuration,
7+
# sends a test logline through the pipeline and checks the results.
8+
#
9+
10+
set -eux
11+
12+
INPUT=$(mktemp tmp.logstash.in.XXXXX)
13+
OUTPUT=$(mktemp tmp.logstash.out.XXXXX)
14+
PIPELINE=$(mktemp tmp.logstash.pipeline.XXXXX)
15+
16+
echo Preparing input data
17+
echo "postfix/smtp[123]: 7EE668039: to=<[email protected]>, relay=127.0.0.1[127.0.0.1]:2525, delay=3.6, delays=0.2/0.02/0.04/3.3, dsn=2.0.0, status=sent (250 2.0.0 Ok: queued as 153053D)" > "$INPUT"
18+
19+
echo Preparing pipeline config
20+
cat > "$PIPELINE" << EOF
21+
input {
22+
file {
23+
path => "/tmp/logstash.in"
24+
start_position => beginning
25+
}
26+
}
27+
filter {
28+
dissect {
29+
mapping => {
30+
"message" => "%{program}[%{pid}]: %{message}"
31+
}
32+
}
33+
}
34+
EOF
35+
36+
cat 50-filter-postfix.conf >> "$PIPELINE"
37+
38+
cat >> "$PIPELINE" << EOF
39+
output {
40+
file {
41+
path => "/tmp/logstash.out"
42+
}
43+
}
44+
EOF
45+
46+
echo Starting logstash docker container
47+
CONTAINER_ID=$(docker run --rm --detach \
48+
--volume ./"${INPUT}":/tmp/logstash.in \
49+
--volume ./"${OUTPUT}":/tmp/logstash.out \
50+
--volume ./postfix.grok:/etc/logstash/patterns.d/postfix.grok \
51+
--volume ./"${PIPELINE}":/usr/share/logstash/pipeline/pipeline.conf \
52+
logstash:8.12.0 \
53+
logstash -f /usr/share/logstash/pipeline/pipeline.conf)
54+
55+
printf "Waiting for output from logstash "
56+
until test -s "$OUTPUT"; do
57+
printf "."
58+
sleep 2
59+
done
60+
echo
61+
62+
docker stop --time 1 "$CONTAINER_ID" > /dev/null
63+
64+
if test "$(jq .tags[0] "$OUTPUT")" = '"_grok_postfix_success"'; then
65+
echo Grok processing successful!
66+
jq . "$OUTPUT"
67+
else
68+
echo "Grok processing failed :<"
69+
jq . "$OUTPUT"
70+
exit 1
71+
fi
72+
73+
echo Cleaning up
74+
rm -f "$INPUT" "$OUTPUT" "$PIPELINE"
75+
76+
echo Done

0 commit comments

Comments
 (0)