Skip to content

Commit cb56c6a

Browse files
committed
kerneltest: improve error handling and stderr output
1 parent db8d7f5 commit cb56c6a

File tree

1 file changed

+94
-49
lines changed

1 file changed

+94
-49
lines changed

tests/kerneltest.sh

Lines changed: 94 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
# This test is executed by github workflows inside the action runners
55
#
66

7+
TRACEE_STARTUP_TIMEOUT=30
8+
SCRIPT_TMP_DIR=/tmp
9+
TRACEE_TMP_DIR=/tmp/tracee
10+
711
info() {
812
echo -n "INFO: "
913
echo $@
@@ -15,11 +19,13 @@ error_exit() {
1519
exit 1
1620
}
1721

18-
if [[ $UID -ne 0 ]]; then
22+
if [[ $UID -ne 0 ]]
23+
then
1924
error_exit "need root privileges for docker caps config"
2025
fi
2126

22-
if [[ ! -d ./signatures ]]; then
27+
if [[ ! -d ./signatures ]]
28+
then
2329
error_exit "need to be in tracee root directory"
2430
fi
2531

@@ -29,86 +35,115 @@ ISNONCORE=${ISNONCORE:=0}
2935
DONTSLEEP=${DONTSLEEP:=1}
3036

3137
# randomize start point (for parallel runners)
32-
if [[ $DONTSLEEP -ne 1 ]]; then
38+
if [[ $DONTSLEEP -ne 1 ]]
39+
then
3340
rand=$(( $RANDOM % 10 ))
3441
info "sleeping for $rand seconds"
3542
sleep $rand
3643
fi
3744

3845
# startup needs
39-
rm -rf /tmp/tracee/* || error_exit "could not delete /tmp/tracee"
46+
rm -rf $TRACEE_TMP_DIR/* || error_exit "could not delete $TRACEE_TMP_DIR"
4047
git config --global --add safe.directory "*"
4148

42-
info "=== ENVIRONMENT ==="
49+
info
50+
info "= ENVIRONMENT ================================================="
51+
info
4352
info "KERNEL: $(uname -r)"
4453
info "NON CO-RE: $ISNONCORE"
4554
info "CLANG: $(clang --version)"
4655
info "GO: $(go version)"
47-
info "==================="
48-
info "PULLING aquasec/tracee-tester:latest"
56+
info
57+
info "= PULLING CONTAINER IMAGE ====================================="
58+
info
4959
docker image pull aquasec/tracee-tester:latest
50-
info "==================="
51-
info "COMPILING TRACEE"
52-
make clean
60+
info
61+
info "= COMPILING TRACEE ============================================"
62+
info
63+
# make clean # if you want to be extra cautious
5364
set -e
5465
make -j$(nproc) all
5566
set +e
56-
if [[ ! -x ./dist/tracee-ebpf || ! -x ./dist/tracee-rules ]]; then
67+
if [[ ! -x ./dist/tracee-ebpf || ! -x ./dist/tracee-rules ]]
68+
then
5769
error_exit "could not find tracee executables"
5870
fi
59-
if [[ $ISNONCORE -eq 1 ]]; then
71+
if [[ $ISNONCORE -eq 1 ]]
72+
then
6073
info "STATE: Compiling non CO-RE eBPF object"
6174
make clean-bpf-nocore
6275
set -e
6376
make install-bpf-nocore
6477
set +e
65-
export TRACEE_BPF_FILE=$(ls -1tr /tmp/tracee/*tracee.bpf*.o | head -n1)
78+
export TRACEE_BPF_FILE=$(ls -1tr $TRACEE_TMP_DIR/*tracee.bpf*.o | head -n1)
6679
fi
67-
info "==================="
6880

6981
# if any test has failed
7082
anyerror=""
7183

7284
# run tests
7385
for TEST in $TESTS; do
7486

75-
info ""
76-
info "=== TESTING: $TEST ==="
77-
info ""
87+
info
88+
info "= TEST: $TEST ================================================="
89+
info
7890

79-
# file containing tracee-event output (to check for detection)
80-
rm -f /tmp/build-$$
91+
rm -f $SCRIPT_TMP_DIR/build-$$
92+
rm -f $SCRIPT_TMP_DIR/ebpf-$$
8193

8294
events=$(./dist/tracee-rules --rules $TEST --list-events)
8395

8496
./dist/tracee-ebpf \
85-
--cache cache-type=mem \
86-
--cache mem-cache-size=512 \
87-
-o format:gob \
88-
-o option:parse-arguments \
89-
-o option:detect-syscall \
90-
-trace container \
91-
-trace event=$events \
92-
| \
97+
--install-path $TRACEE_TMP_DIR \
98+
--cache cache-type=mem \
99+
--cache mem-cache-size=512 \
100+
--output format:gob \
101+
--output option:parse-arguments \
102+
--output option:detect-syscall \
103+
--trace container=new \
104+
--trace event=$events \
105+
2>$SCRIPT_TMP_DIR/ebpf-$$ \
106+
| \
93107
./dist/tracee-rules \
94-
--input-tracee=file:stdin \
95-
--input-tracee format:gob \
96-
--rules $TEST | tee /tmp/build-$$ &
108+
--input-tracee=file:stdin \
109+
--input-tracee format:gob \
110+
--rules $TEST 2>&1 \
111+
| \
112+
tee $SCRIPT_TMP_DIR/build-$$ 2>&1 &
97113

98114
# wait tracee-ebpf to be started (30 sec most)
99115
times=0
116+
timedout=0
100117
while true; do
101118
times=$(($times + 1))
102119
sleep 1
103-
if [[ -f /tmp/tracee/out/tracee.pid ]]; then
104-
info "tracee is up"
120+
if [[ -f $TRACEE_TMP_DIR/out/tracee.pid ]]
121+
then
122+
info
123+
info "UP AND RUNNING"
124+
info
105125
break
106126
fi
107-
if [[ $times -gt 30 ]]; then
108-
error_exit "time out waiting for tracee initialization"
127+
128+
if [[ $times -gt $TRACEE_STARTUP_TIMEOUT ]]
129+
then
130+
timedout=1
131+
break
109132
fi
110133
done
111134

135+
# tracee-ebpf could not start for some reason, check stderr
136+
if [[ $timedout -eq 1 ]]
137+
then
138+
info
139+
info "$TEST: FAILED. ERRORS:"
140+
info
141+
cat $SCRIPT_TMP_DIR/ebpf-$$
142+
143+
anyerror="${anyerror}$TEST,"
144+
continue
145+
fi
146+
112147
# special capabilities needed for some tests
113148
case $TEST in
114149
TRC-2 | TRC-3)
@@ -121,10 +156,11 @@ for TEST in $TESTS; do
121156
;;
122157
esac
123158

124-
# run tracee-tester (triggering the signature) many times
125-
for i in 1 2 3; do
126-
docker run $docker_extra_arg --rm aquasec/tracee-tester $TEST > /dev/null 2>&1
127-
done
159+
# give some time for tracee to settle
160+
sleep 5
161+
162+
# run tracee-tester (triggering the signature)
163+
docker run $docker_extra_arg --rm aquasec/tracee-tester $TEST > /dev/null 2>&1
128164

129165
# so event can be processed and detected
130166
sleep 5
@@ -133,32 +169,41 @@ for TEST in $TESTS; do
133169

134170
success=1
135171
found=0
136-
cat /tmp/build-$$ | grep "Signature ID: $test_name" -B2 | head -3 | grep -q "\*\*\* Detection" && found=1
137-
echo ""
138-
if [[ $found -eq 1 ]]; then
139-
echo "TEST $TEST: SUCCESS"
172+
cat $SCRIPT_TMP_DIR/build-$$ | grep "Signature ID: $test_name" -B2 | head -3 | grep -q "\*\*\* Detection" && found=1
173+
info
174+
if [[ $found -eq 1 ]]
175+
then
176+
info "$TEST: SUCCESS"
140177
else
141-
echo "TEST $TEST: FAILED"
142178
anyerror="${anyerror}$TEST,"
179+
info "$TEST: FAILED, stderr from tracee-ebpf:"
180+
cat $SCRIPT_TMP_DIR/ebpf-$$
181+
info
143182
fi
144-
echo ""
183+
info
184+
185+
rm -f $SCRIPT_TMP_DIR/build-$$
186+
rm -f $SCRIPT_TMP_DIR/ebpf-$$
187+
188+
# make sure we exit both to start them again
145189

146190
kill -19 $(pidof tracee-rules)
147191
kill -19 $(pidof tracee-ebpf)
148192

149193
kill -9 $(pidof tracee-rules)
150194
kill -9 $(pidof tracee-ebpf)
151195

152-
# give a little break
196+
# give a little break for OS noise to reduce
153197
sleep 5
154198
done
155199

156-
info ""
157-
if [[ $anyerror != "" ]]; then
158-
info "TESTS HAVE FAILED: ${anyerror::-1}"
200+
info
201+
if [[ $anyerror != "" ]]
202+
then
203+
info "ALL TESTS: FAILED: ${anyerror::-1}"
159204
exit 1
160205
fi
161206

162-
info "SUCCESS"
207+
info "ALL TESTS: SUCCESS"
163208

164209
exit 0

0 commit comments

Comments
 (0)