On Tue, Dec 05, 2023 at 01:00:29PM +0100, Paolo Abeni wrote: > > +cleanup_ns() > > +{ > > + local ns="" > > + local errexit=0 > > + local ret=0 > > + > > + # disable errexit temporary > > + if [[ $- =~ "e" ]]; then > > + errexit=1 > > + set +e > > + fi > > + > > + for ns in "$@"; do > > + ip netns delete "${ns}" &> /dev/null > > + if ! busywait 2 ip netns list \| grep -vq "^$ns$" &> /dev/null; then > > + echo "Warn: Failed to remove namespace $ns" > > + ret=1 > > + fi > > + done > > + > > + [ $errexit -eq 1 ] && set -e > > + return $ret > > +} > > + > > +# setup netns with given names as prefix. e.g > > +# setup_ns local remote > > +setup_ns() > > +{ > > + local ns="" > > + local ns_name="" > > + local ns_list="" > > + for ns_name in "$@"; do > > + # Some test may setup/remove same netns multi times > > + if unset ${ns_name} 2> /dev/null; then > > + ns="${ns_name,,}-$(mktemp -u XXXXXX)" > > + eval readonly ${ns_name}="$ns" > > + else > > + eval ns='$'${ns_name} > > + cleanup_ns "$ns" > > + > > + fi > > + > > + if ! ip netns add "$ns"; then > > + echo "Failed to create namespace $ns_name" > > + cleanup_ns "$ns_list" > > + return $ksft_skip > > + fi > > + ip -n "$ns" link set lo up > > + ns_list="$ns_list $ns" > > Side note for a possible follow-up: if you maintain $ns_list as global > variable, and remove from such list the ns deleted by cleanup_ns, you > could remove the cleanup trap from the individual test with something > alike: > > final_cleanup_ns() > { > cleanup_ns $ns_list > } > > trap final_cleanup_ns EXIT > > No respin needed for the above, could be a follow-up if agreed upon. Hi Paolo, I did similar in the first version. But Petr said[1] we should let the client do cleanup specifically. I agree that we should let client script keep this in mind. On the other hand, maybe we can add this final cleanup and let client call it directly. What do you think? [1] https://lore.kernel.org/netdev/878r6nf9x5.fsf@xxxxxxxxxx/ Thanks Hangbin