Skip to content

Commit

Permalink
refactor(docker): moved connectivity helpers for docker compose scena…
Browse files Browse the repository at this point in the history
…rios to bash helper lib
  • Loading branch information
gh0st42 committed May 6, 2024
1 parent a322308 commit aa03c7b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
42 changes: 42 additions & 0 deletions tests/docker/scenarios/libnetemhelper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
DOCKERCMD=docker
# use podman if available
if command -v podman &> /dev/null; then
DOCKERCMD=podman
fi

set_on_all_interfaces() {
for n in $NODES; do
IFACES=$($DOCKERCMD exec $n cat /proc/net/dev | awk '{print $1}' | grep -E '^eth[0-9]+' | cut -d ':' -f1)
CMDS=""
for i in $IFACES; do
CMDS="$CMDS echo \"[$1] Setting loss on $n ($i) to $2\" && tc qdisc $1 dev $i root netem loss $2 && "
done
$DOCKERCMD exec $n bash -c "${CMDS} true"
done
}

set_on_all_interfaces add 0%


set_loss () {
echo "Setting loss on $1 to $2"
$DOCKERCMD exec $1 tc qdisc change dev eth0 root netem loss $2
}

loss_interval() {
IFACE=eth0
if [ -n "$4" ]; then
IFACE=$4
fi
echo "Setting loss on $1 ($IFACE) to $2 for $3s"
$DOCKERCMD exec $1 tc qdisc change dev $IFACE root netem loss $2
sleep $3
$DOCKERCMD exec $1 tc qdisc change dev $IFACE root netem loss 0%
}

cleanup () {
echo "Cleaning up..."
set_on_all_interfaces del 0%
}

trap cleanup EXIT
31 changes: 5 additions & 26 deletions tests/docker/scenarios/line-3n-loss.sh
Original file line number Diff line number Diff line change
@@ -1,33 +1,12 @@
#!/bin/bash

DOCKERCMD=docker
# use podman if available
if command -v podman &> /dev/null; then
DOCKERCMD=podman
fi
$DOCKERCMD exec line-3n-n1-1 tc qdisc add dev eth0 root netem loss 0%
$DOCKERCMD exec line-3n-n2-1 tc qdisc add dev eth0 root netem loss 0%
$DOCKERCMD exec line-3n-n3-1 tc qdisc add dev eth0 root netem loss 0%
NODES="line-3n-n1-1 line-3n-n2-1 line-3n-n3-1"

cleanup () {
echo "Cleaning up..."
$DOCKERCMD exec line-3n-n1-1 tc qdisc del dev eth0 root netem loss 0%
$DOCKERCMD exec line-3n-n2-1 tc qdisc del dev eth0 root netem loss 0%
$DOCKERCMD exec line-3n-n3-1 tc qdisc del dev eth0 root netem loss 0%
}

trap cleanup EXIT
# Remember: define list of NODES before loading the helper functions
. $(dirname $0)/libnetemhelper.sh


while true; do
echo "Setting loss on n3 to 100% for 30s"
$DOCKERCMD exec line-3n-n3-1 tc qdisc change dev eth0 root netem loss 100%
sleep 30
echo " n3 back to 0% loss."
$DOCKERCMD exec line-3n-n3-1 tc qdisc change dev eth0 root netem loss 0%
echo "Setting loss on n1 to 100% for 30s"
$DOCKERCMD exec line-3n-n1-1 tc qdisc change dev eth0 root netem loss 100%
sleep 30
echo " n1 back to 0% loss."
$DOCKERCMD exec line-3n-n1-1 tc qdisc change dev eth0 root netem loss 0%
loss_interval line-3n-n1-1 100% 30
loss_interval line-3n-n3-1 100% 30
done

0 comments on commit aa03c7b

Please sign in to comment.