From 8ab0989f8f0e1b25142d2bff28818f2de613a780 Mon Sep 17 00:00:00 2001 From: Ryo Kawaguchi Date: Mon, 22 Apr 2024 21:10:57 +0900 Subject: [PATCH] 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 --- .github/workflows/docker-tests.yml | 2 +- docker-tests/build-docker-images.sh | 2 +- docker-tests/run-docker-tests.sh | 249 +++++++++++++++------------- docker-tests/start-client.sh | 7 +- 4 files changed, 138 insertions(+), 122 deletions(-) diff --git a/.github/workflows/docker-tests.yml b/.github/workflows/docker-tests.yml index 5bb06efd..8055df4c 100644 --- a/.github/workflows/docker-tests.yml +++ b/.github/workflows/docker-tests.yml @@ -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 diff --git a/docker-tests/build-docker-images.sh b/docker-tests/build-docker-images.sh index e6f1889e..18b0884c 100755 --- a/docker-tests/build-docker-images.sh +++ b/docker-tests/build-docker-images.sh @@ -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" . diff --git a/docker-tests/run-docker-tests.sh b/docker-tests/run-docker-tests.sh index 23b62653..1eb4167f 100755 --- a/docker-tests/run-docker-tests.sh +++ b/docker-tests/run-docker-tests.sh @@ -2,37 +2,54 @@ 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" - ;; - userspace) - INNERNET_ARGS="-vvv --backend userspace" - ;; - *) - echo "invalid backend (must be kernel or userspace)" - exit 1 - esac -else +help() { cat >&2 <<-_EOF - Usage: "${0##*/}" - - BACKEND: "kernel" or "userspace" +Usage: "${0##*/}" [options...] () + --userspace Use userspace wireguard instead of kernel one + --verbose Print verbose innernet logs _EOF - exit -fi +} + +TEST_FILTER=() +while [[ $# -gt 0 ]]; do + case $1 in + --userspace) + INNERNET_ARGS="$INNERNET_ARGS --backend userspace" + shift + ;; + --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 + ;; + *) + TEST_FILTER+=("$1") + shift + ;; + esac +done cmd() { - echo "[#] $*" >&2 - "$@" + echo "[#] $*" >&2 + "$@" } info() { - echo -e "\033[0;34m- $@\033[0m" 1>&2 + echo -e "\033[0;34m- $@\033[0m" 1>&2 } tmp_dir=$(mktemp -d -t innernet-tests-XXXXXXXXXX) @@ -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,99 +129,87 @@ 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) -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 - -info "Creating short-lived invitation for third peer." -cmd docker exec "$PEER1_CONTAINER" innernet \ - add-peer evilcorp \ - --name "peer3" \ - --cidr "robots" \ - --admin false \ - --ip "10.66.2.100" \ - --save-config "/app/peer3.toml" \ - --invite-expires "1s" \ - --yes - -info "waiting 15 seconds to see if the server clears out the IP address." -sleep 11 - -info "Re-requesting invite after expiration with the same parameters." -cmd docker exec "$PEER1_CONTAINER" innernet \ - add-peer evilcorp \ - --name "peer3" \ - --cidr "robots" \ - --admin false \ - --ip "10.66.2.100" \ - --save-config "/app/peer3_2.toml" \ - --invite-expires "30m" \ - --yes - +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 -info "Creating invitation for fourth and fifth peer from first peer." -cmd docker exec "$PEER1_CONTAINER" innernet \ - add-peer evilcorp \ - --name "peer4" \ - --cidr "robots" \ - --admin false \ - --auto-ip \ - --save-config "/app/peer4.toml" \ - --invite-expires "30s" \ - --yes -cmd docker cp "$PEER1_CONTAINER:/app/peer4.toml" "$tmp_dir" -cmd docker exec "$PEER1_CONTAINER" innernet \ - add-peer evilcorp \ - --name "peer5" \ - --cidr "robots" \ - --admin false \ - --auto-ip \ - --save-config "/app/peer5.toml" \ - --invite-expires "30s" \ - --yes -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) -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) -cmd docker cp "$tmp_dir/peer5.toml" "$PEER5_CONTAINER:/app/invite.toml" +test_short_lived_invitation() { + info "Creating short-lived invitation for third peer." + cmd docker exec "$PEER1_CONTAINER" innernet \ + add-peer evilcorp \ + --name "peer3" \ + --cidr "robots" \ + --admin false \ + --ip "10.66.2.100" \ + --save-config "/app/peer3.toml" \ + --invite-expires "1s" \ + --yes + + info "waiting 15 seconds to see if the server clears out the IP address." + sleep 11 + + info "Re-requesting invite after expiration with the same parameters." + cmd docker exec "$PEER1_CONTAINER" innernet \ + add-peer evilcorp \ + --name "peer3" \ + --cidr "robots" \ + --admin false \ + --ip "10.66.2.100" \ + --save-config "/app/peer3_2.toml" \ + --invite-expires "30m" \ + --yes +} -cmd docker start -a "$PEER4_CONTAINER" | sed -e 's/^/\x1B[0;92mpeer 4\x1B[0m: /' & -info "peer4 started as $PEER4_CONTAINER" -cmd docker start -a "$PEER5_CONTAINER" | sed -e 's/^/\x1B[0;94mpeer 5\x1B[0m: /' & -info "peer5 started as $PEER5_CONTAINER" +test_simultaneous_redemption() { + info "Creating invitation for fourth and fifth peer from first peer." + cmd docker exec "$PEER1_CONTAINER" innernet \ + add-peer evilcorp \ + --name "peer4" \ + --cidr "robots" \ + --admin false \ + --auto-ip \ + --save-config "/app/peer4.toml" \ + --invite-expires "30s" \ + --yes + cmd docker cp "$PEER1_CONTAINER:/app/peer4.toml" "$tmp_dir" + cmd docker exec "$PEER1_CONTAINER" innernet \ + add-peer evilcorp \ + --name "peer5" \ + --cidr "robots" \ + --admin false \ + --auto-ip \ + --save-config "/app/peer5.toml" \ + --invite-expires "30s" \ + --yes + cmd docker cp "$PEER1_CONTAINER:/app/peer5.toml" "$tmp_dir" + + info "Starting fourth and fifth peer and redeeming simultaneously." + PEER4_CONTAINER=$(create_peer_docker 172.18.1.4) + cmd docker cp "$tmp_dir/peer4.toml" "$PEER4_CONTAINER:/app/invite.toml" + 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: /' & + info "peer4 started as $PEER4_CONTAINER" + cmd docker start -a "$PEER5_CONTAINER" | sed -e 's/^/\x1B[0;94mpeer 5\x1B[0m: /' & + 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 +} -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." diff --git a/docker-tests/start-client.sh b/docker-tests/start-client.sh index 263421d7..9e98907e 100755 --- a/docker-tests/start-client.sh +++ b/docker-tests/start-client.sh @@ -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 - innernet $INNERNET_ARGS up --no-write-hosts "$INTERFACE" + 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