From b9907fa7aeca6b1d33e194495c178b61025cb22f Mon Sep 17 00:00:00 2001 From: Tobias Waldekranz Date: Fri, 10 May 2024 08:54:56 +0000 Subject: [PATCH] test: Add `nsenter` helper script to enter Infamy namespaces When a test starts an isolated MACVLAN namespace, it is often useful to interactively join that namespace to investigate issues. To do this, we need to find the "sleeper" process backing the namespace, and `nsenter` into that process's network namespace. Add a script that automates this process. To start a shell in the netns of the MACVLAN stacked on top of d1b, start a shell in the test environment: infix/test $ ./shell 13:37:00 infamy0:test # ./nsenter d1b 13:37:01 infamy0(netns:d1b):test # --- test/.env | 5 +++++ test/env | 2 +- test/nsenter | 24 ++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100755 test/nsenter diff --git a/test/.env b/test/.env index 4318443d..2c0fbef5 100644 --- a/test/.env +++ b/test/.env @@ -9,6 +9,11 @@ logdir=$(readlink -f "$testdir/.log") envdir="$HOME/.infix-test-venv" qeneth="$testdir/qeneth/qeneth" +build_ps1() +{ + echo "\e[1m\$(date \"+%H:%M:%S\") \h$1\e[0m:\W # " +} + # # Figure out available container runner # diff --git a/test/env b/test/env index 06268372..48cae343 100755 --- a/test/env +++ b/test/env @@ -160,7 +160,7 @@ if [ "$containerize" ]; then --env PYTHONHASHSEED=${PYTHONHASHSEED:-$(shuf -i 0-$(((1 << 32) - 1)) -n 1)} \ --env VIRTUAL_ENV_DISABLE_PROMPT=yes \ --env PROMPT_DIRTRIM="$ixdir" \ - --env PS1='\e[1m$(date "+%H:%M:%S") \h\e[0m:\W # ' \ + --env PS1="$(build_ps1)" \ --expose 9001-9010 --publish-all \ --hostname "$(name)" \ --name "$(name)" \ diff --git a/test/nsenter b/test/nsenter new file mode 100755 index 00000000..6557a5c5 --- /dev/null +++ b/test/nsenter @@ -0,0 +1,24 @@ +#!/bin/sh + +set -e + +testdir=$(dirname "$(readlink -f "$0")") +. "$testdir/.env" + +lower=$1 +shift +[ $# -gt 0 ] || set sh + +for pid in $(pgrep -fx "sleep infinity"); do + ifidxs=$(nsenter -t $pid -U -n ip -j link show | \ + jq '.[] | select(has("link_netnsid")) | .link_index') + for ifidx in $ifidxs; do + ifname=$(ip -j link show | jq -r ".[] | select(.ifindex == $ifidx) | .ifname") + if [ "$ifname" = "$lower" ]; then + exec nsenter -t $pid -U -n env PS1="$(build_ps1 "(netns:$ifname)")" "$@" + fi + done +done + +echo Found no namespace with interface stacked on $lower >&2 +exit 1