Make docker-tests less verbose and easier to add new tests (#311)

* Make docker-tests less verbose and easier to add new tests

* Fix positional argument parsing

* Suppress more logs unless --verbose is specified
pull/310/head
Ryo Kawaguchi 2024-04-22 21:10:57 +09:00 committed by GitHub
parent 15602388f6
commit 8ab0989f8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 133 additions and 117 deletions

View File

@ -17,5 +17,5 @@ jobs:
- name: Build the Docker images
run: docker-tests/build-docker-images.sh
- name: Run the Docker tests
run: docker-tests/run-docker-tests.sh userspace
run: docker-tests/run-docker-tests.sh --userspace --verbose

View File

@ -1,7 +1,7 @@
#!/usr/bin/env bash
set -ex
SELF_DIR="$(dirname "$0")"
SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd "$SELF_DIR/.."
docker build -t innernet -f "$SELF_DIR/Dockerfile.innernet" .

View File

@ -2,29 +2,46 @@
set -e
shopt -s nocasematch
SELF_DIR="$(dirname "$0")"
SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd "$SELF_DIR/.."
if [[ $# -eq 1 ]]; then
case "$1" in
kernel)
INNERNET_ARGS="-vvv"
help() {
cat >&2 <<-_EOF
Usage: "${0##*/}" [options...] (<test>)
--userspace Use userspace wireguard instead of kernel one
--verbose Print verbose innernet logs
_EOF
}
TEST_FILTER=()
while [[ $# -gt 0 ]]; do
case $1 in
--userspace)
INNERNET_ARGS="$INNERNET_ARGS --backend userspace"
shift
;;
userspace)
INNERNET_ARGS="-vvv --backend userspace"
--verbose)
INNERNET_ARGS="$INNERNET_ARGS -vvv"
CLIENT_ARGS="$CLIENT_ARGS --verbose"
SERVER_RUST_LOG="debug"
CLIENT_RUST_LOG="trace"
shift
;;
--help)
help
exit
;;
-*)
echo "Invalid option."
help
exit 1
;;
*)
echo "invalid backend (must be kernel or userspace)"
exit 1
TEST_FILTER+=("$1")
shift
;;
esac
else
cat >&2 <<-_EOF
Usage: "${0##*/}" <BACKEND>
BACKEND: "kernel" or "userspace"
_EOF
exit
fi
done
cmd() {
echo "[#] $*" >&2
@ -53,7 +70,7 @@ SERVER_CONTAINER=$(cmd docker create -it --rm \
--network "$NETWORK" \
--ip 172.18.1.1 \
--volume /dev/net/tun:/dev/net/tun \
--env RUST_LOG=debug \
--env RUST_LOG="$SERVER_RUST_LOG" \
--env INNERNET_ARGS="$INNERNET_ARGS" \
--cap-add NET_ADMIN \
innernet)
@ -63,17 +80,23 @@ info "server started as $SERVER_CONTAINER"
info "Waiting for server to initialize."
cmd sleep 5
create_peer_docker() {
local IP=$1
cmd docker create --rm -it \
--network "$NETWORK" \
--ip $IP \
--volume /dev/net/tun:/dev/net/tun \
--cap-add NET_ADMIN \
--env RUST_LOG="$CLIENT_RUST_LOG" \
--env INTERFACE=evilcorp \
--env INNERNET_ARGS="$INNERNET_ARGS" \
--env CLIENT_ARGS="$CLIENT_ARGS" \
innernet /app/start-client.sh
}
info "Starting first peer."
cmd docker cp "$SERVER_CONTAINER:/app/peer1.toml" "$tmp_dir"
PEER1_CONTAINER=$(cmd docker create --rm -it \
--network "$NETWORK" \
--ip 172.18.1.2 \
--volume /dev/net/tun:/dev/net/tun \
--env INTERFACE=evilcorp \
--env RUST_LOG=trace \
--env INNERNET_ARGS="$INNERNET_ARGS" \
--cap-add NET_ADMIN \
innernet /app/start-client.sh)
PEER1_CONTAINER=$(create_peer_docker 172.18.1.2)
info "peer1 started as $PEER1_CONTAINER"
cmd docker cp "$tmp_dir/peer1.toml" "$PEER1_CONTAINER:/app/invite.toml"
cmd docker start -a "$PEER1_CONTAINER" | sed -e 's/^/\x1B[0;96mpeer 1\x1B[0m: /' &
@ -106,19 +129,13 @@ cmd docker exec "$PEER1_CONTAINER" innernet \
cmd docker cp "$PEER1_CONTAINER:/app/peer2.toml" "$tmp_dir"
info "Starting second peer."
PEER2_CONTAINER=$(docker create --rm -it \
--network "$NETWORK" \
--ip 172.18.1.3 \
--volume /dev/net/tun:/dev/net/tun \
--cap-add NET_ADMIN \
--env INTERFACE=evilcorp \
--env INNERNET_ARGS="$INNERNET_ARGS" \
innernet /app/start-client.sh)
PEER2_CONTAINER=$(create_peer_docker 172.18.1.3)
info "peer2 started as $PEER2_CONTAINER"
cmd docker cp "$tmp_dir/peer2.toml" "$PEER2_CONTAINER:/app/invite.toml"
cmd docker start -a "$PEER2_CONTAINER" | sed -e 's/^/\x1B[0;93mpeer 2\x1B[0m: /' &
sleep 10
test_short_lived_invitation() {
info "Creating short-lived invitation for third peer."
cmd docker exec "$PEER1_CONTAINER" innernet \
add-peer evilcorp \
@ -143,12 +160,9 @@ cmd docker exec "$PEER1_CONTAINER" innernet \
--save-config "/app/peer3_2.toml" \
--invite-expires "30m" \
--yes
}
info "peer2 started as $PEER2_CONTAINER"
cmd docker cp "$tmp_dir/peer2.toml" "$PEER2_CONTAINER:/app/invite.toml"
cmd docker start -a "$PEER2_CONTAINER" | sed -e 's/^/\x1B[0;93mpeer 2\x1B[0m: /' &
sleep 10
test_simultaneous_redemption() {
info "Creating invitation for fourth and fifth peer from first peer."
cmd docker exec "$PEER1_CONTAINER" innernet \
add-peer evilcorp \
@ -172,23 +186,9 @@ cmd docker exec "$PEER1_CONTAINER" innernet \
cmd docker cp "$PEER1_CONTAINER:/app/peer5.toml" "$tmp_dir"
info "Starting fourth and fifth peer and redeeming simultaneously."
PEER4_CONTAINER=$(docker create --rm -it \
--network "$NETWORK" \
--ip 172.18.1.4 \
--volume /dev/net/tun:/dev/net/tun \
--cap-add NET_ADMIN \
--env INTERFACE=evilcorp \
--env INNERNET_ARGS="$INNERNET_ARGS" \
innernet /app/start-client.sh)
PEER4_CONTAINER=$(create_peer_docker 172.18.1.4)
cmd docker cp "$tmp_dir/peer4.toml" "$PEER4_CONTAINER:/app/invite.toml"
PEER5_CONTAINER=$(docker create --rm -it \
--network "$NETWORK" \
--ip 172.18.1.5 \
--volume /dev/net/tun:/dev/net/tun \
--cap-add NET_ADMIN \
--env INTERFACE=evilcorp \
--env INNERNET_ARGS="$INNERNET_ARGS" \
innernet /app/start-client.sh)
PEER5_CONTAINER=$(create_peer_docker 172.18.1.5)
cmd docker cp "$tmp_dir/peer5.toml" "$PEER5_CONTAINER:/app/invite.toml"
cmd docker start -a "$PEER4_CONTAINER" | sed -e 's/^/\x1B[0;92mpeer 4\x1B[0m: /' &
@ -199,6 +199,17 @@ info "peer5 started as $PEER5_CONTAINER"
info "Checking connectivity betweeen peers."
cmd docker exec "$PEER2_CONTAINER" ping -c3 10.66.0.1
cmd docker exec "$PEER2_CONTAINER" ping -c3 10.66.1.1
}
# Run tests (functions prefixed with test_) in alphabetical order.
# Optional filter provided by positional arguments is applied.
for func in $(declare -F | awk '{print $3}'); do
if [[ "$func" =~ ^test_ ]]; then
if [ ${#TEST_FILTER[@]} -eq 0 ] || [[ "${TEST_FILTER[*]}" =~ "$func" ]]; then
$func
fi
fi
done
echo
info "test succeeded."
info "Test succeeded."

View File

@ -2,6 +2,7 @@
set -e
INTERFACE="${INTERFACE:-innernet}"
innernet $INNERNET_ARGS install \
--name "$INTERFACE" \
--delete-invite \
@ -9,6 +10,10 @@ innernet $INNERNET_ARGS install \
/app/invite.toml
while true; do
if [[ $CLIENT_ARGS =~ --verbose ]]; then
innernet $INNERNET_ARGS up --no-write-hosts "$INTERFACE"
else
innernet $INNERNET_ARGS up --no-write-hosts "$INTERFACE" > /dev/null
fi
sleep 1
done