Skip to content

Commit

Permalink
Use elevated privileges only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
erthalion committed Dec 16, 2024
1 parent ada3cdd commit 640a3c0
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions tests/workers/network/smoke.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ set -eou pipefail

stop() { echo "$*" 1>&2 ; exit 1; }

with_sudo() {
if [ -z "${CI_RUN}"]; then
command sudo "$@"
else
command "$@"
fi
}

export CI_RUN=${CI:-""}

if [ -z "${CI_RUN}"]; then
which sudo &>/dev/null || stop "Don't have sudo"
fi

which bpftrace &>/dev/null || stop "Don't have bpftrace"
which bpftool &>/dev/null || stop "Don't have bpftool"
which berserker &>/dev/null || stop "Don't have berserker"
Expand All @@ -13,15 +27,21 @@ if [ ! -d "tests/workers/network/" ]; then
echo "Can't find test directory. Smoke tests have to be run from the project root directory"
fi

if [ -z "${CI_RUN}" ]; then
# Needs elevated privileges to run bpftrace, bpftool and client berserker.
# Ask for it and cache the credentials.
sudo -v
fi

echo "Cleanup..."
rm -f /tmp/server.log
rm -f /tmp/client.log
rm -f /tmp/tcpaccept.log
with_sudo rm -f /tmp/client.log
with_sudo rm -f /tmp/tcpaccept.log
# in case if it's still running from a previous run
pkill berserker || true
with_sudo pkill berserker || true

# make berserkers verbose
#export RUST_LOG=trace
export RUST_LOG=trace

# start the server before bpftrace, to skip first accept
echo "Starting the server..."
Expand All @@ -35,12 +55,13 @@ do
done

echo "Starting bpftrace..."
bpftrace tests/workers/network/sys_accept.bt &> /tmp/tcpaccept.log &

with_sudo bpftrace tests/workers/network/sys_accept.bt &> /tmp/tcpaccept.log &

# let bpftrace attach probes
attempts=0

while ! bpftool prog | grep -q bpftrace ;
while ! with_sudo bpftool prog | grep -q bpftrace ;
do
if [[ "$attempts" -gt 20 ]]; then
echo "Can't find bpftool after ${attempts} attempts."
Expand All @@ -53,14 +74,15 @@ do
done

echo "Starting the client..."
berserker tests/workers/network/workload.client.toml &> /tmp/client.log &
with_sudo -E PATH=$PATH bash -c \
'berserker tests/workers/network/workload.client.toml &> /tmp/client.log &'

# let it do some work
sleep 5;

echo "Stopping..."
pkill berserker || true
pkill bpftrace || true
with_sudo pkill berserker || true
with_sudo pkill bpftrace || true

echo "Verifying the results..."
ENDPOINTS=$(cat /tmp/tcpaccept.log | grep hit | wc -l || echo "")
Expand All @@ -69,8 +91,8 @@ if (( $ENDPOINTS > 0 )); then
echo "PASS (${ENDPOINTS} seen connections)"

rm -f /tmp/server.log
rm -f /tmp/client.log
rm -f /tmp/tcpaccept.log
with_sudo rm -f /tmp/client.log
with_sudo rm -f /tmp/tcpaccept.log

exit 0;
else
Expand Down

0 comments on commit 640a3c0

Please sign in to comment.