forked from agolo/logstash-test-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.sh
executable file
·136 lines (118 loc) · 2.63 KB
/
test.sh
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
#!/usr/bin/env bash
# set -x
set -euo pipefail
# COLORS
YELLOW='\033[0;33m'
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
TEST_RESULT_FILE="test_output.log"
TEST_PARENT_DIRECTORY=$1
if [ ! -z "${2:-}" ]; then
TEST_LOGSTASH_IMAGE=$2
else
TEST_LOGSTASH_IMAGE="docker.elastic.co/logstash/logstash:5.5.1"
fi
declare -A benchmarks
spinner() {
spin='-\|/'
pid="$1"
i=0
while kill -0 "$pid" 2>/dev/null
do
i=$(( (i+1) %4 ))
printf "\r%s" "${spin:$i:1}"
sleep .1
done
}
log() {
CASE=$1
TEST_NAME=$2
TIME=""
if [[ $CASE == "pass" ]]
then
SYMBOL="✔"
COLOR=$GREEN
CONTENT="passed"
elif [[ $CASE == "time" ]]
then
SYMBOL="⏰"
COLOR=$YELLOW
CONTENT="time spent"
TIME=${benchmarks[$TEST_NAME]}
else
SYMBOL="𝗫"
COLOR=$RED
CONTENT="failed"
fi
echo -e "${COLOR}${SYMBOL} ${CONTENT}${NC} - ${TEST_NAME} ${TIME}"
}
logstashTest() {
TEST_DIRECTORY="$1"
echo '' > $TEST_RESULT_FILE
chmod 777 $TEST_RESULT_FILE
START=$(date +%s)
docker run \
--rm \
-i \
-v "$PWD/config/logstash.yml":/usr/share/logstash/config/logstash.yml \
-v "$PWD/$TEST_DIRECTORY/logstash.conf":/usr/share/logstash/pipeline/logstash.conf \
-v "$PWD/logstash-common.conf":/usr/share/logstash/pipeline/logstash-common.conf \
-v "$PWD/$TEST_RESULT_FILE":/output.log \
"$TEST_LOGSTASH_IMAGE" < "$TEST_DIRECTORY/input.log" 2>/dev/null &
# SPINNER
test_pid=$!
spinner $test_pid # Process Id of the previous running command
wait $test_pid
test_status=$?
END=$(date +%s)
DIFF=$(( $END - $START ))
benchmarks[$TEST_DIRECTORY]="${DIFF}s"
# here we ignore comparison of timestamp fields. You can ignore any other fields you need to
[ "$test_status" = "0" ] && ./log-diff.js -i '@timestamp,timestamp' -c "$TEST_DIRECTORY/output.log,$TEST_RESULT_FILE"
}
# TODO: Check if docker is running
# if ! docker info 2&>1 > /dev/null
# then
# log fail "docker might not be running"
# false
# fi
# RUN TESTS
EXIT_CODE=0
TESTS_DIRECTORIES="$TEST_PARENT_DIRECTORY/*"
declare -A results
for d in $TESTS_DIRECTORIES ; do
echo "Testing $d"
logstashTest "$d"
results[$d]="$?"
if [[ ${results[$d]} == 0 ]]
then
log time "$d"
log pass "$d"
else
log time "$d"
log fail "$d"
EXIT_CODE=1
fi
done
# PRINT RESULTS
echo ""
echo "TEST SUMMARY"
echo "------------"
for K in "${!results[@]}"
do
if [[ ${results[$K]} == 0 ]]
then
log pass "$K ${benchmarks[$K]}"
else
log fail "$K ${benchmarks[$K]}"
EXIT_CODE=1
fi
done
if [[ $EXIT_CODE == 0 ]]
then
log pass "All tests"
else
log fail "Some tests"
fi
exit $EXIT_CODE