Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use elevated privileges only when needed #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading